From f57dd7323197363be02b10c7263df99ce6a2159b Mon Sep 17 00:00:00 2001 From: Kevin Glisson Date: Wed, 20 Sep 2023 10:39:05 -0700 Subject: [PATCH 1/6] Adds case status reminders --- src/dispatch/cli.py | 2 ++ src/dispatch/messaging/strings.py | 49 +++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/dispatch/cli.py b/src/dispatch/cli.py index 95ae7d5a3f96..c8c676be1222 100644 --- a/src/dispatch/cli.py +++ b/src/dispatch/cli.py @@ -640,6 +640,8 @@ def dispatch_scheduler(): ) from .term.scheduled import sync_terms # noqa from .workflow.scheduled import sync_workflows # noqa + from .case.scheduled import case_triage_reminder # noqa + from .case.scheduled import case_close_reminder # noqa @dispatch_scheduler.command("list") diff --git a/src/dispatch/messaging/strings.py b/src/dispatch/messaging/strings.py index 0facbef81ce4..4ed67e78cf42 100644 --- a/src/dispatch/messaging/strings.py +++ b/src/dispatch/messaging/strings.py @@ -5,6 +5,7 @@ from dispatch.messaging.email.filters import env from dispatch.conversation.enums import ConversationButtonActions from dispatch.incident.enums import IncidentStatus +from dispatch.case.enums import CaseStatus from dispatch.enums import Visibility from dispatch import config @@ -34,6 +35,7 @@ class MessageType(DispatchEnum): incident_tactical_report = "incident-tactical-report" incident_task_list = "incident-task-list" incident_task_reminder = "incident-task-reminder" + case_status_reminder = "case-status-reminder" service_feedback = "service-feedback" @@ -43,6 +45,13 @@ class MessageType(DispatchEnum): IncidentStatus.closed: "This no longer requires additional involvement, long term incident action items have been assigned to their respective owners.", } +CASE_STATUS_DESCRIPTIONS = { + CaseStatus.new: "This case is new and needs triaging.", + CaseStatus.triage: "This case is being triaged.", + CaseStatus.escalated: "This case has been escalated.", + CaseStatus.closed: "This case has been closed.", +} + INCIDENT_VISIBILITY_DESCRIPTIONS = { Visibility.open: "We ask that you use your best judgment while sharing details about this incident outside of the dedicated channels of communication. Please reach out to the Incident Commander if you have any questions.", Visibility.restricted: "This incident is restricted to immediate participants of this incident. We ask that you exercise extra caution and discretion while talking about this incident outside of the dedicated channels of communication. Only invite new participants that are strictly necessary. Please reach out to the Incident Commander if you have any questions.", @@ -236,6 +245,16 @@ class MessageType(DispatchEnum): "\n", " " ).strip() +CASE_TRIAGE_REMINDER_DESCRIPTION = """The status of this case hasn't been updated recently. +Please ensure you triage the case based on it's priority.""".replace( + "\n", " " +).strip() + +CASE_CLOSE_REMINDER_DESCRIPTION = """The status of this case hasn't been updated recently. +You can use case 'Resolve' button if it has been resolved and can be closed.""".replace( + "\n", " " +).strip() + INCIDENT_TASK_NEW_DESCRIPTION = """ The following incident task has been created and assigned to you by {{task_creator}}: {{task_description}}""" @@ -390,6 +409,14 @@ class MessageType(DispatchEnum): INCIDENT_TITLE = {"title": "Title", "text": "{{title}}"} +CASE_TITLE = {"title": "Title", "text": "{{title}}"} + +CASE_STATUS = { + "title": "Status - {{status}}", + "status_mapping": CASE_STATUS_DESCRIPTIONS, +} + + if config.DISPATCH_MARKDOWN_IN_INCIDENT_DESC: INCIDENT_DESCRIPTION = {"title": "Description", "text": "{{description | markdown}}"} else: @@ -596,6 +623,28 @@ class MessageType(DispatchEnum): INCIDENT_STATUS, ] + +CASE_CLOSE_REMINDER = [ + { + "title": "{{name}} Case - Close Reminder", + "title_link": "{{ticket_weblink}}", + "text": CASE_CLOSE_REMINDER_DESCRIPTION, + }, + CASE_TITLE, + CASE_STATUS, +] + +CASE_TRIAGE_REMINDER = [ + { + "title": "{{name}} Case - Triage Reminder", + "title_link": "{{ticket_weblink}}", + "text": CASE_TRIAGE_REMINDER_DESCRIPTION, + }, + CASE_TITLE, + CASE_STATUS, +] + + INCIDENT_TASK_REMINDER = [ {"title": "Incident - {{ name }}", "text": "{{ title }}"}, {"title": "Creator", "text": "{{ creator }}"}, From 2ec1401f91246031c445b38b9eb08d8ad105fcbc Mon Sep 17 00:00:00 2001 From: Kevin Glisson Date: Wed, 20 Sep 2023 10:40:09 -0700 Subject: [PATCH 2/6] Adds scheduling --- src/dispatch/case/messaging.py | 87 ++++++++++++++++++++++++++++++++++ src/dispatch/case/scheduled.py | 47 ++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 src/dispatch/case/messaging.py create mode 100644 src/dispatch/case/scheduled.py diff --git a/src/dispatch/case/messaging.py b/src/dispatch/case/messaging.py new file mode 100644 index 000000000000..80d8499af764 --- /dev/null +++ b/src/dispatch/case/messaging.py @@ -0,0 +1,87 @@ +""" +.. module: dispatch.case.messaging + :platform: Unix + :copyright: (c) 2019 by Netflix Inc., see AUTHORS for more + :license: Apache, see LICENSE for more details. +""" +import logging + +from dispatch.database.core import SessionLocal +from dispatch.case.models import Case +from dispatch.messaging.strings import ( + CASE_CLOSE_REMINDER, + CASE_TRIAGE_REMINDER, + MessageType, +) +from dispatch.plugin import service as plugin_service + + +log = logging.getLogger(__name__) + + +def send_case_close_reminder(case: Case, db_session: SessionLocal): + """ + Sends a direct message to the assignee reminding them to close the case if possible. + """ + message_text = "Case Close Reminder" + message_template = CASE_CLOSE_REMINDER + + plugin = plugin_service.get_active_instance( + db_session=db_session, project_id=case.project.id, plugin_type="conversation" + ) + if not plugin: + log.warning("Case close reminder message not sent. No conversation plugin enabled.") + return + + items = [ + { + "name": case.name, + "ticket_weblink": case.ticket.weblink, + "title": case.title, + "status": case.status, + } + ] + + plugin.instance.send_direct( + case.assignee.individual.email, + message_text, + message_template, + MessageType.case_status_reminder, + items=items, + ) + + log.debug(f"Case close reminder sent to {case.assignee.individual.email}.") + + +def send_case_triage_reminder(case: Case, db_session: SessionLocal): + """ + Sends a direct message to the assignee reminding them to triage the case if possible. + """ + message_text = "Case Triage Reminder" + message_template = CASE_TRIAGE_REMINDER + + plugin = plugin_service.get_active_instance( + db_session=db_session, project_id=case.project.id, plugin_type="conversation" + ) + if not plugin: + log.warning("Case triage reminder message not sent. No conversation plugin enabled.") + return + + items = [ + { + "name": case.name, + "ticket_weblink": case.ticket.weblink, + "title": case.title, + "status": case.status, + } + ] + + plugin.instance.send_direct( + case.assignee.individual.email, + message_text, + message_template, + MessageType.case_status_reminder, + items=items, + ) + + log.debug(f"Case close reminder sent to {case.assignee.individual.email}.") diff --git a/src/dispatch/case/scheduled.py b/src/dispatch/case/scheduled.py new file mode 100644 index 000000000000..fa386bcf085e --- /dev/null +++ b/src/dispatch/case/scheduled.py @@ -0,0 +1,47 @@ +from datetime import datetime, date +from schedule import every + +from dispatch.database.core import SessionLocal +from dispatch.decorators import scheduled_project_task, timer +from dispatch.project.models import Project +from dispatch.scheduler import scheduler + +from .enums import CaseStatus +from .messaging import send_case_close_reminder, send_case_triage_reminder +from .service import ( + get_all_by_status, +) + + +@scheduler.add(every(1).day.at("18:00"), name="case-close-reminder") +@timer +@scheduled_project_task +def case_close_reminder(db_session: SessionLocal, project: Project): + """Sends a reminder to the case assignee to close out their case.""" + cases = get_all_by_status( + db_session=db_session, project_id=project.id, status=CaseStatus.triage + ) + + for case in cases: + span = datetime.utcnow() - case.triage_at + q, r = divmod(span.days, 7) + if q >= 1 and date.today().isoweekday() == 1: + # we only send the reminder for cases that have been triaging + # longer than a week and only on Mondays + send_case_close_reminder(case, db_session) + + +@scheduler.add(every(1).day.at("18:00"), name="case-triage-reminder") +@timer +@scheduled_project_task +def case_triage_reminder(db_session: SessionLocal, project: Project): + """Sends a reminder to the case assignee to triage their case.""" + cases = get_all_by_status(db_session=db_session, project_id=project.id, status=CaseStatus.new) + + # if we want more specific SLA reminders, we would need to add additional data model + for case in cases: + span = datetime.utcnow() - case.created + q, r = divmod(span.days, 1) + if q >= 1: + # we only send one reminder per case per day + send_case_triage_reminder(case, db_session) From e47302abf040b348559709dcf7327dc05d6794a4 Mon Sep 17 00:00:00 2001 From: kevgliss Date: Wed, 20 Sep 2023 16:17:51 -0700 Subject: [PATCH 3/6] Update src/dispatch/case/messaging.py Co-authored-by: Marc Vilanova <39573146+mvilanova@users.noreply.github.com> --- src/dispatch/case/messaging.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dispatch/case/messaging.py b/src/dispatch/case/messaging.py index 80d8499af764..9cb7780f6e1d 100644 --- a/src/dispatch/case/messaging.py +++ b/src/dispatch/case/messaging.py @@ -84,4 +84,4 @@ def send_case_triage_reminder(case: Case, db_session: SessionLocal): items=items, ) - log.debug(f"Case close reminder sent to {case.assignee.individual.email}.") + log.debug(f"Case triage reminder sent to {case.assignee.individual.email}.") From 00fb8e615761d04a23942b9b570a4d095e3ae050 Mon Sep 17 00:00:00 2001 From: kevgliss Date: Wed, 20 Sep 2023 16:17:58 -0700 Subject: [PATCH 4/6] Update src/dispatch/messaging/strings.py Co-authored-by: Marc Vilanova <39573146+mvilanova@users.noreply.github.com> --- src/dispatch/messaging/strings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dispatch/messaging/strings.py b/src/dispatch/messaging/strings.py index 4ed67e78cf42..c354522535b3 100644 --- a/src/dispatch/messaging/strings.py +++ b/src/dispatch/messaging/strings.py @@ -246,7 +246,7 @@ class MessageType(DispatchEnum): ).strip() CASE_TRIAGE_REMINDER_DESCRIPTION = """The status of this case hasn't been updated recently. -Please ensure you triage the case based on it's priority.""".replace( +Please ensure you triage the case based on its priority.""".replace( "\n", " " ).strip() From 957199918fa2164ebc3c9f027677e5a300d1388b Mon Sep 17 00:00:00 2001 From: kevgliss Date: Wed, 20 Sep 2023 16:18:05 -0700 Subject: [PATCH 5/6] Update src/dispatch/cli.py Co-authored-by: Marc Vilanova <39573146+mvilanova@users.noreply.github.com> --- src/dispatch/cli.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/dispatch/cli.py b/src/dispatch/cli.py index c8c676be1222..689e3ec9c66b 100644 --- a/src/dispatch/cli.py +++ b/src/dispatch/cli.py @@ -640,8 +640,7 @@ def dispatch_scheduler(): ) from .term.scheduled import sync_terms # noqa from .workflow.scheduled import sync_workflows # noqa - from .case.scheduled import case_triage_reminder # noqa - from .case.scheduled import case_close_reminder # noqa + from .case.scheduled import case_triage_reminder, case_close_reminder # noqa @dispatch_scheduler.command("list") From cf90d1b1ea9632bb94019fec336ef09b35e13738 Mon Sep 17 00:00:00 2001 From: kevgliss Date: Wed, 20 Sep 2023 16:18:16 -0700 Subject: [PATCH 6/6] Update src/dispatch/messaging/strings.py Co-authored-by: David Whittaker <84562015+whitdog47@users.noreply.github.com> --- src/dispatch/messaging/strings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dispatch/messaging/strings.py b/src/dispatch/messaging/strings.py index c354522535b3..877dac1ce69b 100644 --- a/src/dispatch/messaging/strings.py +++ b/src/dispatch/messaging/strings.py @@ -251,7 +251,7 @@ class MessageType(DispatchEnum): ).strip() CASE_CLOSE_REMINDER_DESCRIPTION = """The status of this case hasn't been updated recently. -You can use case 'Resolve' button if it has been resolved and can be closed.""".replace( +You can use the case 'Resolve' button if it has been resolved and can be closed.""".replace( "\n", " " ).strip()