Skip to content

Commit

Permalink
Fixes and improves evergreen reminders and notifications text (#2303)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvilanova authored Jun 29, 2022
1 parent c2bdf82 commit 17b130d
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 43 deletions.
4 changes: 2 additions & 2 deletions src/dispatch/document/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
52 changes: 28 additions & 24 deletions src/dispatch/evergreen/scheduled.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand All @@ -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(
Expand All @@ -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):
Expand All @@ -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
)
Expand Down
7 changes: 4 additions & 3 deletions src/dispatch/messaging/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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 }}"},
Expand Down
8 changes: 0 additions & 8 deletions src/dispatch/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 4 additions & 2 deletions src/dispatch/notification/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions src/dispatch/service/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions src/dispatch/team/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 17b130d

Please sign in to comment.