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
78 changes: 46 additions & 32 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,16 +85,6 @@ 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_incidents(
target_component,
impact,
Expand All @@ -89,17 +100,17 @@ 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}"
)

url_d = url_for('web.incident', incident_id=dst_incident.id)
url_s = url_for('web.incident', incident_id=src_incident.id)
link_s = f"<a href='{url_d}'>{dst_incident.text}</a>"
link_d = f"<a href='{url_s}'>{src_incident.text}</a>"
update_s = f"{comp_with_attrs} moved to {link_s}, closed by system"
update_d = f"{comp_with_attrs} moved from {link_d}"

update_incident_status(src_incident, update_s)
update_incident_status(dst_incident, update_d)

src_incident.end_date = datetime.utcnow()
dst_incident.components.append(target_component)
db.session.commit()
Expand All @@ -117,7 +128,7 @@ def handling_incidents(
update_incident_status(
src_incident,
(
f"Impact changed: from {impacts[src_incident.impact].key} "
f"impact changed from {impacts[src_incident.impact].key} "
f"to {impacts[impact].key}"
)
)
Expand All @@ -129,14 +140,17 @@ 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}"
)

url_d = url_for('web.incident', incident_id=dst_incident.id)
url_s = url_for('web.incident', incident_id=src_incident.id)
link_s = f"<a href='{url_d}'>{dst_incident.text}</a>"
link_d = f"<a href='{url_s}'>{src_incident.text}</a>"
update_s = f"{comp_with_attrs} moved to {link_s}"
update_d = f"{comp_with_attrs} moved from {link_d}"
Copy link
Contributor

Choose a reason for hiding this comment

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

during testing I don't see the details only the strings moved to and moved from
image


update_incident_status(src_incident, update_s)
update_incident_status(dst_incident, update_d)

src_incident.components.remove(target_component)
dst_incident.components.append(target_component)
db.session.commit()
Expand Down Expand Up @@ -337,7 +351,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
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