Repository navigation
Expand file tree
/
Copy pathflows.py
More file actions
144 lines (125 loc) · 4.56 KB
/
Copy pathflows.py
File metadata and controls
144 lines (125 loc) · 4.56 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
"""
.. module: dispatch.task.flows
:platform: Unix
:copyright: (c) 2019 by Netflix Inc., see AUTHORS for more
:license: Apache, see LICENSE for more details.
.. moduleauthor:: Kevin Glisson <kglisson@netflix.com>
"""
import logging
from collections import defaultdict
from datetime import datetime
from dispatch.database import SessionLocal
from dispatch.messaging.strings import (
INCIDENT_TASK_REMINDER,
INCIDENT_TASK_NEW_NOTIFICATION,
INCIDENT_TASK_RESOLVED_NOTIFICATION,
)
from dispatch.plugin import service as plugin_service
from dispatch.task.models import TaskStatus, TaskCreate, TaskUpdate
from dispatch.task import service as task_service
log = logging.getLogger(__name__)
def group_tasks_by_assignee(tasks):
"""Groups tasks by assignee."""
grouped = defaultdict(lambda: [])
for task in tasks:
for a in task.assignees:
grouped[a.individual.email].append(task)
return grouped
def create_reminder(db_session, assignee_email, tasks, contact_fullname, contact_weblink):
"""Contains the logic for incident task reminders."""
# send email
plugin = plugin_service.get_active(db_session=db_session, plugin_type="email")
if not plugin:
log.warning("Task reminder not sent. No email plugin enabled.")
return
items = []
for t in tasks:
items.append(
{
"name": t.incident.name,
"title": t.incident.title,
"creator": t.creator.individual.name,
"description": t.description,
"priority": t.priority,
"created_at": t.created_at,
"resolve_by": t.resolve_by,
"weblink": t.weblink,
}
)
notification_template = INCIDENT_TASK_REMINDER
notification_type = "incident-task-reminder"
name = subject = notification_text = "Incident Task Reminder"
plugin.instance.send(
assignee_email,
notification_text,
notification_template,
notification_type,
name=name,
subject=subject,
contact_fullname=contact_fullname,
contact_weblink=contact_weblink,
items=items, # plugin expect dicts
)
for task in tasks:
task.last_reminder_at = datetime.utcnow()
db_session.commit()
def send_task_notification(
conversation_id, message_template, assignees, description, weblink, db_session: SessionLocal
):
"""Sends a task notification."""
# we send a notification to the incident conversation
notification_text = "Incident Notification"
notification_type = "incident-notification"
plugin = plugin_service.get_active(db_session=db_session, plugin_type="conversation")
plugin.instance.send(
conversation_id,
notification_text,
message_template,
notification_type,
task_assignees=[x.individual.email for x in assignees],
task_description=description,
task_weblink=weblink,
)
def create_or_update_task(
db_session, incident, task: dict, notify: bool = False, sync_external: bool = True
):
"""Creates a new task in the database or updates an existing one."""
existing_task = task_service.get_by_resource_id(
db_session=db_session, resource_id=task["resource_id"]
)
if existing_task:
# save the status before we attempt to update the record
existing_status = existing_task.status
task = task_service.update(
db_session=db_session,
task=existing_task,
task_in=TaskUpdate(**task),
sync_external=sync_external,
)
if notify:
# determine if task was previously resolved
if task.status == TaskStatus.resolved.value:
if existing_status != TaskStatus.resolved.value:
send_task_notification(
incident.conversation.channel_id,
INCIDENT_TASK_RESOLVED_NOTIFICATION,
task.assignees,
task.description,
task.weblink,
db_session,
)
else:
task = task_service.create(
db_session=db_session,
task_in=TaskCreate(**task, incident=incident),
)
if notify:
send_task_notification(
incident.conversation.channel_id,
INCIDENT_TASK_NEW_NOTIFICATION,
task.assignees,
task.description,
task.weblink,
db_session,
)
db_session.commit()