forked from Netflix/dispatch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflows.py
More file actions
230 lines (190 loc) · 8.11 KB
/
flows.py
File metadata and controls
230 lines (190 loc) · 8.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import logging
from datetime import date
from pydantic.error_wrappers import ErrorWrapper, ValidationError
from dispatch.decorators import background_task
from dispatch.document import service as document_service
from dispatch.document.models import DocumentCreate
from dispatch.enums import DocumentResourceTypes
from dispatch.event import service as event_service
from dispatch.exceptions import InvalidConfigurationError
from dispatch.incident import service as incident_service
from dispatch.participant import service as participant_service
from dispatch.plugin import service as plugin_service
from .enums import ReportTypes
from .messaging import (
send_executive_report_to_notifications_group,
send_tactical_report_to_conversation,
send_tactical_report_to_tactical_group,
)
from .models import ReportCreate, TacticalReportCreate, ExecutiveReportCreate
from .service import create, get_all_by_incident_id_and_type
log = logging.getLogger(__name__)
@background_task
def create_tactical_report(
user_email: str,
incident_id: int,
tactical_report_in: TacticalReportCreate,
organization_slug: str = None,
db_session=None,
):
"""Creates and sends a new tactical report to a conversation."""
conditions = tactical_report_in.conditions
actions = tactical_report_in.actions
needs = tactical_report_in.needs
# we load the incident instance
incident = incident_service.get(db_session=db_session, incident_id=incident_id)
# we create a new tactical report
details = {"conditions": conditions, "actions": actions, "needs": needs}
tactical_report_in = ReportCreate(details=details, type=ReportTypes.tactical_report)
tactical_report = create(db_session=db_session, report_in=tactical_report_in)
# we load the participant
participant = participant_service.get_by_incident_id_and_email(
db_session=db_session, incident_id=incident_id, email=user_email
)
# we save the tactical report
participant.reports.append(tactical_report)
incident.reports.append(tactical_report)
db_session.add(participant)
db_session.add(incident)
db_session.commit()
event_service.log_incident_event(
db_session=db_session,
source="Incident Participant",
description=f"{participant.individual.name} created a new tactical report",
details={"conditions": conditions, "actions": actions, "needs": needs},
incident_id=incident_id,
individual_id=participant.individual.id,
owner=participant.individual.name,
)
# we send the tactical report to the conversation
send_tactical_report_to_conversation(incident_id, conditions, actions, needs, db_session)
# we send the tactical report to the tactical group
send_tactical_report_to_tactical_group(incident_id, tactical_report, db_session)
return tactical_report
@background_task
def create_executive_report(
user_email: str,
incident_id: int,
executive_report_in: ExecutiveReportCreate,
organization_slug: str = None,
db_session=None,
):
"""Creates an executive report."""
current_date = date.today().strftime("%B %d, %Y")
current_status = executive_report_in.current_status
overview = executive_report_in.overview
next_steps = executive_report_in.next_steps
# we load the incident instance
incident = incident_service.get(db_session=db_session, incident_id=incident_id)
if not incident.incident_type.executive_template_document:
raise ValidationError(
[
ErrorWrapper(
InvalidConfigurationError(msg="No executive report template defined."),
loc="executive_template_document",
)
],
model=ExecutiveReportCreate,
)
# we fetch all previous executive reports
executive_reports = get_all_by_incident_id_and_type(
db_session=db_session, incident_id=incident_id, report_type=ReportTypes.executive_report
)
previous_executive_reports = []
for executive_report in executive_reports:
previous_executive_reports.append(
f"{executive_report.document.name} - {executive_report.document.weblink}\n"
)
# we create a new executive report
details = {"current_status": current_status, "overview": overview, "next_steps": next_steps}
executive_report_in = ReportCreate(
details=details,
type=ReportTypes.executive_report,
)
executive_report = create(db_session=db_session, report_in=executive_report_in)
# we load the participant
participant = participant_service.get_by_incident_id_and_email(
db_session=db_session, incident_id=incident_id, email=user_email
)
# we save the executive report
participant.reports.append(executive_report)
incident.reports.append(executive_report)
db_session.add(participant)
db_session.add(incident)
db_session.commit()
event_service.log_incident_event(
db_session=db_session,
source="Incident Participant",
description=f"{participant.individual.name} created a new executive report",
details={"current_status": current_status, "overview": overview, "next_steps": next_steps},
incident_id=incident_id,
individual_id=participant.individual.id,
owner=participant.individual.name,
)
# we create a new document for the executive report
storage_plugin = plugin_service.get_active_instance(
db_session=db_session, project_id=incident.project.id, plugin_type="storage"
)
executive_report_document_name = f"{incident.name} - Executive Report - {current_date}"
executive_report_document = storage_plugin.instance.copy_file(
folder_id=incident.storage.resource_id,
file_id=incident.incident_type.executive_template_document.resource_id,
name=executive_report_document_name,
)
executive_report_document.update(
{
"name": executive_report_document_name,
"description": incident.incident_type.executive_template_document.description,
"resource_type": DocumentResourceTypes.executive,
}
)
storage_plugin.instance.move_file(
new_folder_id=incident.storage.resource_id, file_id=executive_report_document["id"]
)
event_service.log_incident_event(
db_session=db_session,
source=storage_plugin.plugin.title,
description="Executive report document added to storage",
incident_id=incident.id,
)
document_in = DocumentCreate(
name=executive_report_document["name"],
description=executive_report_document["description"],
resource_id=executive_report_document["id"],
resource_type=executive_report_document["resource_type"],
project=incident.project,
weblink=executive_report_document["weblink"],
)
executive_report.document = document_service.create(
db_session=db_session, document_in=document_in
)
incident.documents.append(executive_report.document)
db_session.add(executive_report)
db_session.add(incident)
db_session.commit()
event_service.log_incident_event(
db_session=db_session,
source="Dispatch Core App",
description="Executive report document added to incident",
incident_id=incident.id,
)
# we update the incident update document
document_plugin = plugin_service.get_active_instance(
db_session=db_session, project_id=incident.project.id, plugin_type="document"
)
document_plugin.instance.update(
executive_report_document["id"],
name=incident.name,
title=incident.title,
current_date=current_date,
current_status=current_status,
overview=overview,
next_steps=next_steps,
previous_reports="\n".join(previous_executive_reports),
commander_fullname=incident.commander.individual.name,
commander_team=incident.commander.team,
commander_weblink=incident.commander.individual.weblink,
)
# we send the executive report to the notifications group
send_executive_report_to_notifications_group(incident.id, executive_report, db_session)
return executive_report