diff --git a/src/dispatch/document/service.py b/src/dispatch/document/service.py index 7b5629a04c69..c94a0918b63c 100644 --- a/src/dispatch/document/service.py +++ b/src/dispatch/document/service.py @@ -46,12 +46,12 @@ def get_conversation_reference_document(*, db_session, project_id: int): ).one_or_none() -def get_overdue_evergreen_documents(*, db_session, project_id) -> List[Optional[Document]]: +def get_overdue_evergreen_documents(*, db_session, project_id: int) -> List[Optional[Document]]: """Returns all documents that have not had a recent evergreen notification.""" query = ( db_session.query(Document) - .filter(Document.evergreen == True) # noqa .filter(Document.project_id == project_id) + .filter(Document.evergreen == True) # noqa .filter(Document.overdue == True) # noqa ) return query.all() diff --git a/src/dispatch/evergreen/scheduled.py b/src/dispatch/evergreen/scheduled.py index c935bb3e4c24..148ea2539f90 100644 --- a/src/dispatch/evergreen/scheduled.py +++ b/src/dispatch/evergreen/scheduled.py @@ -6,22 +6,21 @@ """ import logging -from typing import Any -from schedule import every -from datetime import datetime from collections import defaultdict +from datetime import datetime +from schedule import every +from typing import Any from dispatch.database.core import SessionLocal -from dispatch.messaging.strings import EVERGREEN_REMINDER from dispatch.decorators import scheduled_project_task -from dispatch.scheduler import scheduler -from dispatch.config import DISPATCH_UI_URL +from dispatch.document import service as document_service +from dispatch.messaging.strings import EVERGREEN_REMINDER +from dispatch.notification import service as notification_service from dispatch.plugin import service as plugin_service from dispatch.project.models import Project -from dispatch.team import service as team_service -from dispatch.notification import service as notification_service +from dispatch.scheduler import scheduler from dispatch.service import service as service_service -from dispatch.document import service as document_service +from dispatch.team import service as team_service log = logging.getLogger(__name__) @@ -35,27 +34,24 @@ def create_evergreen_reminder( db_session=db_session, plugin_type="email", project_id=project.id ) if not plugin: - log.warning("Evergreen reminder not sent, no email plugin enabled.") + log.warning("Evergreen reminder not sent. No email plugin enabled.") return - notification_template = EVERGREEN_REMINDER - items = [] for resource_type, resources in resource_groups.items(): for resource in resources: - weblink = getattr(resource, "weblink", None) - if not weblink: - weblink = DISPATCH_UI_URL - + weblink = getattr(resource, "weblink", "N/A") items.append( { - "resource_type": resource_type.replace("_", " ").title(), - "name": resource.name, "description": getattr(resource, "description", None), + "name": resource.name, + "project": resource.project.name, + "resource_type": resource_type.replace("_", " ").title(), "weblink": weblink, } ) + notification_template = EVERGREEN_REMINDER notification_type = "evergreen-reminder" name = subject = notification_text = "Evergreen Reminder" success = plugin.instance.send( @@ -68,13 +64,16 @@ def create_evergreen_reminder( items=items, # plugin expect dicts ) - if success: - for item in items: - item.evergreen_last_reminder_at = datetime.utcnow() - - db_session.commit() - else: + if not success: log.error(f"Unable to send evergreen message. Email: {owner_email}") + return + + # we set the evergreen last reminder at time to now + for resource_type, resources in resource_groups.items(): + for resource in resources: + resource.evergreen_last_reminder_at = datetime.utcnow() + + db_session.commit() def group_items_by_owner_and_type(items): @@ -90,16 +89,21 @@ def group_items_by_owner_and_type(items): def create_evergreen_reminders(db_session: SessionLocal, project: Project): """Sends reminders for items that have evergreen enabled.""" items = [] + + # Overdue evergreen documents items += document_service.get_overdue_evergreen_documents( db_session=db_session, project_id=project.id ) + # Overdue evergreen oncall services items += service_service.get_overdue_evergreen_services( db_session=db_session, project_id=project.id ) + # Overdue evergreen teams items += team_service.get_overdue_evergreen_teams(db_session=db_session, project_id=project.id) + # Overdue evergreen notifications items += notification_service.get_overdue_evergreen_notifications( db_session=db_session, project_id=project.id ) diff --git a/src/dispatch/messaging/strings.py b/src/dispatch/messaging/strings.py index 10fe1bf20e8f..652e59097ebd 100644 --- a/src/dispatch/messaging/strings.py +++ b/src/dispatch/messaging/strings.py @@ -36,9 +36,9 @@ class MessageType(DispatchEnum): } EVERGREEN_REMINDER_DESCRIPTION = """ -You are the owner of the following Dispatch resources. +You are the owner of the following resources in Dispatch. This is a reminder that these resources should be kept up to date in order to effectively -respond to incidents. Please review them and update, or clearly mark them as deprecated.""".replace( +respond to incidents. Please review and update them, or mark them as deprecated.""".replace( "\n", " " ).strip() @@ -513,7 +513,8 @@ class MessageType(DispatchEnum): ] EVERGREEN_REMINDER = [ - {"title": "Resource Type", "text": "{{ resource_type }}"}, + {"title": "Project", "text": "{{ project }}"}, + {"title": "Type", "text": "{{ resource_type }}"}, {"title": "Name", "text": "{{ name }}"}, {"title": "Description", "text": "{{ description }}"}, {"title": "Link", "text": "{{ weblink }}"}, diff --git a/src/dispatch/models.py b/src/dispatch/models.py index f95ad6879912..7df544eaa759 100644 --- a/src/dispatch/models.py +++ b/src/dispatch/models.py @@ -96,14 +96,6 @@ def overdue(cls): ) -# @listens_for(EvergreenMixin.evergreen, "set", propagate=True) -def reset_last_reminded(target, value, oldvalue, initiator): - """Reset last reminder at if evergreen goes from disabled -> enabled.""" - if not oldvalue: - target.evergreen_last_reminder_at = datetime.utcnow() - target.evergreen = value - - # Pydantic models... class DispatchBase(BaseModel): class Config: diff --git a/src/dispatch/notification/service.py b/src/dispatch/notification/service.py index 22fab5899400..b5dd8c4bcab4 100644 --- a/src/dispatch/notification/service.py +++ b/src/dispatch/notification/service.py @@ -33,12 +33,14 @@ def get_all_enabled(*, db_session, project_id: int) -> Optional[List[Notificatio ).all() -def get_overdue_evergreen_notifications(*, db_session, project_id) -> List[Optional[Notification]]: +def get_overdue_evergreen_notifications( + *, db_session, project_id: int +) -> List[Optional[Notification]]: """Returns all notifications that have not had a recent evergreen notification.""" query = ( db_session.query(Notification) - .filter(Notification.evergreen == True) # noqa .filter(Notification.project_id == project_id) + .filter(Notification.evergreen == True) # noqa .filter(Notification.overdue == True) # noqa ) return query.all() diff --git a/src/dispatch/service/service.py b/src/dispatch/service/service.py index 8e43fc70c13e..e62f4a86844e 100644 --- a/src/dispatch/service/service.py +++ b/src/dispatch/service/service.py @@ -89,12 +89,12 @@ def get_by_external_id_and_project_id_or_raise( return service -def get_overdue_evergreen_services(*, db_session, project_id) -> List[Optional[Service]]: +def get_overdue_evergreen_services(*, db_session, project_id: int) -> List[Optional[Service]]: """Returns all services that have not had a recent evergreen notification.""" query = ( db_session.query(Service) - .filter(Service.evergreen == True) # noqa .filter(Service.project_id == project_id) + .filter(Service.evergreen == True) # noqa .filter(Service.overdue == True) # noqa ) return query.all() diff --git a/src/dispatch/team/service.py b/src/dispatch/team/service.py index 41d6b1d9441d..90d986f9b469 100644 --- a/src/dispatch/team/service.py +++ b/src/dispatch/team/service.py @@ -35,12 +35,12 @@ def get_or_create(*, db_session, email: str, incident: Incident = None, **kwargs return contact -def get_overdue_evergreen_teams(*, db_session, project_id) -> List[Optional[TeamContact]]: +def get_overdue_evergreen_teams(*, db_session, project_id: int) -> List[Optional[TeamContact]]: """Returns all teams that have not had a recent evergreen notification.""" query = ( db_session.query(TeamContact) - .filter(TeamContact.evergreen == True) # noqa .filter(TeamContact.project_id == project_id) + .filter(TeamContact.evergreen == True) # noqa .filter(TeamContact.overdue == True) # noqa ) return query.all()