1
Fork 0
mirror of https://github.com/pds-nest/nest.git synced 2024-11-22 13:04:19 +00:00

Add alert PUT

This commit is contained in:
Lorenzo Balugani 2021-05-10 11:00:21 +02:00
parent 2824ac71e2
commit 0b9b5e301a
2 changed files with 93 additions and 6 deletions

View file

@ -15,8 +15,8 @@ class BoolOperation(ext.Model):
isRoot = ext.Column(ext.Boolean, default=False, nullable=False) isRoot = ext.Column(ext.Boolean, default=False, nullable=False)
# Foreign Keys # Foreign Keys
condition_id = ext.Column(ext.Integer, ext.ForeignKey("condition.id")) condition_id = ext.Column(ext.Integer, ext.ForeignKey("condition.id"))
node_1_id = ext.Column(ext.Integer, ext.ForeignKey("bool_operation.id")) node_1_id = ext.Column(ext.Integer, ext.ForeignKey("bool_operation.id", ondelete="SET NULL"))
node_2_id = ext.Column(ext.Integer, ext.ForeignKey("bool_operation.id")) node_2_id = ext.Column(ext.Integer, ext.ForeignKey("bool_operation.id", ondelete="SET NULL"))
alert_id = ext.Column(ext.Integer, ext.ForeignKey("alert.id")) alert_id = ext.Column(ext.Integer, ext.ForeignKey("alert.id"))
# Relationships # Relationships
condition = ext.relationship("Condition", back_populates="operations") condition = ext.relationship("Condition", back_populates="operations")
@ -35,3 +35,11 @@ class BoolOperation(ext.Model):
"node_1": self.node_1.to_json() if self.node_1 else None, "node_1": self.node_1.to_json() if self.node_1 else None,
"node_2": self.node_2.to_json() if self.node_2 else None "node_2": self.node_2.to_json() if self.node_2 else None
} }
def get_chain_ids(self, l):
if self.id in l:
# Loop detected!
return -1
l.append(self.id)
self.get_chain_ids(self.node_1, l)
self.get_chain_ids(self.node_2, l)

View file

@ -75,21 +75,21 @@ def page_alert(aid):
tags: tags:
- alert-related - alert-related
patch: patch:
summary: Updates an alert. summary: Updates an alert and the boolops structure.
security: security:
- jwt: [] - jwt: []
requestBody: requestBody:
required: true required: true
content: content:
application/json: application/json:
schema: CreateAlert schema: Alert
parameters: parameters:
- in: path - in: path
schema: AlertParameterSchema schema: AlertParameterSchema
responses: responses:
'200': '200':
description: The repository has been updated successfully. description: The alert has been updated successfully.
content: content:
application/json: application/json:
schema: Alert schema: Alert
@ -138,5 +138,84 @@ def page_alert(aid):
return json_error("Something went wrong while deleting alert."), 500 return json_error("Something went wrong while deleting alert."), 500
return json_success("Deletion completed."), 200 return json_success("Deletion completed."), 200
elif request.method == "PUT": elif request.method == "PUT":
pass if not json_request_authorizer(request.json, alert):
return json_error("Missing one or more parameters in repository json."), 400
alert.limit = request.json['limit']
alert.name = request.json['name']
alert.window_size = request.json['window_size']
root_id = alert.to_json()['root_operation']['id']
root = BoolOperation.filter_by(id=root_id).first()
if not root:
return json_error("Could not find original root element."), 404
# No longer used chain element deletion
l = []
bool_list = root.get_chains_ids(l)
for element in alert.operations:
if element.id not in bool_list:
if element.id == root.id:
root = None
ext.session.delete(element)
ext.session.commit()
if request.json['root-operation'].get('id'): # If an alternative root is already present.
new_root_test = BoolOperation.filter_by(id=request.json['root_operation']['id']).first()
if new_root_test and new_root_test != root:
new_root_test.is_root = True
ext.session.commit()
else: # If the alternative root needs to be brand-new.
condition_id = None
if request.json['root_operation'].get('condition'): # Is the new alternative connected to a condition?
if not Condition.query.filter_by(id=request.json['root_operation']['condition']['id'],
repository_id=alert.repository_id).first():
return json_error("One of the provided IDs is incorrect."), 404
condition_id = request.json['root_operation']['condition']['id']
if (type_ := request.json['root-operation']['operation']) is not None:
try:
type_ = OperationType(type_)
except KeyError:
return json_error("Unknown `operation` specified."), 400
root = BoolOperation(operation=type_, is_root=True, alert_id=aid)
ext.session.add(root)
ext.session.commit()
root = BoolOperation.filter_by(id=request.json['root_operation']['id']).first()
try:
recursion(root, ext, request.json['root_operation'])
except FileNotFoundError:
return json_error("One of the provided IDs is incorrect"), 404
except KeyError:
return json_error("Unknown field specified."), 400
def create_node(node, ext, json, id):
# Check if the node already exists
id_1 = json[f'node_{id}']['id']
if id_1:
node_1 = BoolOperation.query.filter_by(id=id_1).first()
if not node_1:
raise FileNotFoundError
else:
# The node is new.
condition_id = None
if json[f'node_{id}'].get('condition'):
condition = Condition.query.filter_by(
id=json[f'node_{id}']['condition']['id']).filter(
Condition.repository_id == node.alert.repository_id).first()
if not condition:
raise FileNotFoundError
condition_id = condition.id
if (type_ := json[f'node_{id}']['operation']) is not None:
type_ = OperationType(type_)
else:
raise FileNotFoundError
# Create new node
node_1 = BoolOperation(operation=type_, is_root=False, condition_id=condition_id, alert_id=node.alert_id)
ext.session.add(node_1)
ext.session.commit()
# Recursion goes brr
recursion(node_1, ext, json['node_1'])
def recursion(node, ext, json):
if json.get('node_1'): # Create node 1
create_node(node, ext, json, 1)
if json.get('node_2'): # Create node 2
create_node(node, ext, json, 2)