Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change/api funcs #118

Merged
merged 15 commits into from
May 24, 2024
151 changes: 111 additions & 40 deletions app/api/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from flask import current_app
from flask import jsonify
from flask import request
from flask import url_for
from flask.views import MethodView

from flask_smorest import abort
Expand All @@ -42,10 +43,30 @@ def inc_by_impact(incidents, impact):
return incident_match


def add_component_to_incident(target_component, incident):
def update_incident_status(incident, text_status, status="SYSTEM"):
update = IncidentStatus(
incident_id=incident.id,
text=text_status,
status=status,
)
current_app.logger.debug(f"UPDATE_STATUS: {text_status}")
db.session.add(update)


def add_component_to_incident(
target_component,
incident,
comp_with_attrs=None
):
current_app.logger.debug(
f"Add {target_component} to the incident: {incident}"
)
update_incident_status(
incident,
(
f"{comp_with_attrs} added to {incident.text}"
)
)
incident.components.append(target_component)
db.session.commit()
return incident
Expand All @@ -64,14 +85,43 @@ def create_new_incident(target_component, impact, text):
return new_incident


def update_incident_status(incident, text_status, status="SYSTEM"):
update = IncidentStatus(
incident_id=incident.id,
text=text_status,
status=status,
)
current_app.logger.debug(f"UPDATE_STATUS: {text_status}")
db.session.add(update)
def handling_statuses(
incident,
comp_with_attrs=None,
dst_incident=None,
new_incident=None,
action=None,
impact=None,
impacts=None
):
if action:
url_s = url_for('web.incident', incident_id=incident.id)
link_s = f"<a href='{url_s}'>{incident.text}</a>"

if action == "move":
if dst_incident:
url_d = url_for('web.incident', incident_id=dst_incident.id)
link_d = f"<a href='{url_d}'>{dst_incident.text}</a>"
update_s = (
f"{comp_with_attrs} moved to {link_d}"
)
update_d = f"{comp_with_attrs} moved from {link_s}"
elif new_incident:
url_d = url_for('web.incident', incident_id=new_incident.id)
link_d = f"<a href='{url_d}'>{new_incident.text}</a>"
update_s = f"{comp_with_attrs} moved to {link_d}"
update_n = f"{comp_with_attrs} moved from {link_s}"
elif action == "change_impact":
update_s = (
f"impact changed from {impacts[incident.impact].key} to "
f"{impacts[impact].key}"
)
if dst_incident:
update_incident_status(dst_incident, update_d)
if new_incident:
update_incident_status(new_incident, update_n)

update_incident_status(incident, update_s)


def handling_incidents(
Expand All @@ -89,19 +139,16 @@ def handling_incidents(
f"moved to: '{dst_incident.text}'"
)
current_app.logger.debug(f"{src_incident.text} CLOSED")
update_incident_status(
src_incident,
(
f"{comp_with_attrs} moved to: '{dst_incident.text}', "
"incident closed by system"
)
)
update_incident_status(
dst_incident,
f"{comp_with_attrs} moved from {src_incident.text}"
)
src_incident.end_date = datetime.utcnow()
dst_incident.components.append(target_component)
handling_statuses(
src_incident,
comp_with_attrs,
dst_incident=dst_incident,
action="move"
)
db.session.commit()
update_incident_status(src_incident, "CLOSED BY SYSTEM")
db.session.commit()
return dst_incident
elif len(src_incident.components) == 1 and not dst_incident:
Expand All @@ -114,12 +161,11 @@ def handling_incidents(
f"changing the impact from: {impacts[src_incident.impact].key}"
f"to {impacts[impact].key}"
)
update_incident_status(
handling_statuses(
src_incident,
(
f"Impact changed: from {impacts[src_incident.impact].key} "
f"to {impacts[impact].key}"
)
action="change_impact",
impact=impact,
impacts=impacts
)
src_incident.impact = impact
db.session.commit()
Expand All @@ -129,16 +175,14 @@ def handling_incidents(
f"{target_component} moved from {src_incident.text} to "
f"{dst_incident.text}"
)
update_incident_status(
src_incident,
f"{comp_with_attrs} moved to {dst_incident.text}"
)
update_incident_status(
dst_incident,
f"{comp_with_attrs} moved from {src_incident.text}"
)
src_incident.components.remove(target_component)
dst_incident.components.append(target_component)
handling_statuses(
src_incident,
comp_with_attrs,
dst_incident=dst_incident,
action="move"
)
db.session.commit()
return dst_incident
elif len(src_incident.components) > 1 and not dst_incident:
Expand All @@ -148,12 +192,16 @@ def handling_incidents(
current_app.logger.debug(
f"{target_component} moved from {src_incident.text} to new one"
)
update_incident_status(
src_incident.components.remove(target_component)
new_incident = create_new_incident(target_component, impact, text)
handling_statuses(
src_incident,
f"{comp_with_attrs} moved to new incident"
comp_with_attrs,
new_incident=new_incident,
action="move",
)
src_incident.components.remove(target_component)
return create_new_incident(target_component, impact, text)
db.session.commit()
return new_incident
else:
current_app.logger.error("Unexpected ERROR")

Expand Down Expand Up @@ -324,7 +372,19 @@ def post(self, data):
"requested impact equal or less - not modifying "
"or opening new incident"
)
return incident
existing_incident = incident
response = {
"message": (
"Incident with this the component "
"already exists"
),
"targetComponent": comp_with_attrs,
"existingIncidentId": existing_incident.id,
"existingIncidentTitle": existing_incident.text,
"details": "Check your request parameters"
}
return jsonify(response), 409

incidents = Incident.get_active()
if not incidents:
current_app.logger.debug("No active incidents - opening new one")
Expand All @@ -337,7 +397,7 @@ def post(self, data):
incident_match = inc_by_impact(incidents, impact)
if incident_match:
return add_component_to_incident(
target_component, incident_match
target_component, incident_match, comp_with_attrs
)
else:
current_app.logger.debug(
Expand Down Expand Up @@ -372,7 +432,18 @@ def post(self, data):
f"Active incident for {target_component} - "
"not modifying or opening new incident"
)
return incident
existing_incident = incident
response = {
"message": (
"Incident with this the component "
"already exists"
),
"targetComponent": comp_with_attrs,
"existingIncidentId": existing_incident.id,
"existingIncidentTitle": existing_incident.text,
"details": "Check your request parameters"
}
return jsonify(response), 409


@bp.route("/v1/incidents", methods=["GET"])
Expand Down
52 changes: 46 additions & 6 deletions app/web/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,29 @@ def new_incident(current_user):
messages_to = []
for comp in incident_components:
if comp in inc.components:
messages_to.append(
f"{comp} moved to {new_incident}"
comp_name = comp.name
comp_attributes = comp.attributes
comp_attributes_str = ", ".join(
[
f"{attr.value}" for attr in comp_attributes
]
)
messages_from.append(
f"{comp} moved from {inc}"
comp_with_attrs = f"{comp_name} ({comp_attributes_str})"
url_s = url_for(
'web.incident',
incident_id=inc.id
)
link_s = f"<a href='{url_s}'>{inc.text}</a>"
url_d = url_for(
'web.incident',
incident_id=new_incident.id
)
link_d = f"<a href='{url_d}'>{new_incident.text}</a>"
update_s = f"{comp_with_attrs} moved to {link_d}"
update_n = f"{comp_with_attrs} moved from {link_s}"
messages_to.append(update_s)
messages_from.append(update_n)

if len(inc.components) > 1:
inc.components.remove(comp)
else:
Expand Down Expand Up @@ -220,13 +237,36 @@ def separate_incident(current_user, incident_id, component_id):
f"{new_incident} opened by {get_user_string(current_user)}"
)

comp_name = component.name
comp_attributes = component.attributes
comp_attributes_str = ", ".join(
[
f"{attr.value}" for attr in comp_attributes
]
)
comp_with_attrs = f"{comp_name} ({comp_attributes_str})"

url_s = url_for(
'web.incident',
incident_id=incident.id
)
link_s = f"<a href='{url_s}'>{incident.text}</a>"
url_d = url_for(
'web.incident',
incident_id=new_incident.id
)
link_d = f"<a href='{url_d}'>{new_incident.text}</a>"

update_s = f"{comp_with_attrs} moved to {link_d}"
update_n = f"{comp_with_attrs} moved from {link_s}"

update_incident(
incident,
f"{component} moved to {new_incident}"
update_s
)
update_incident(
new_incident,
f"{component} moved from {incident}"
update_n
)
db.session.commit()
return redirect("/")
Expand Down
2 changes: 1 addition & 1 deletion app/web/templates/incident.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ <h6>{{ update.status }}</h6>
<small>{{ update.timestamp.strftime('%Y-%m-%d %H:%M') }}&nbsp;UTC</small>
</div>
<div class="col-md-10">
{{ update.text }}
{{ update.text|safe }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing space?

</div>
<hr/>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/web/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ <h6>{{ update.status }}</h6>
<small class="text-secondary">Posted {{ update.timestamp.strftime('%Y-%m-%d %H:%M') }}&nbsp;UTC</small>
</div>
<div class="col-md-10">
{{ update.text }}
{{ update.text | safe }}
</div>
<hr/>
</div>
Expand Down