Skip to content

Commit

Permalink
Fixes surrounding project based recommendations (#1047)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevgliss authored Apr 19, 2021
1 parent 56f4f04 commit b607d4b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/dispatch/incident/flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def get_incident_participants(incident: Incident, db_session: SessionLocal):
incident.incident_type,
incident.incident_priority,
incident.description,
incident.project,
db_session=db_session,
)

Expand Down
20 changes: 15 additions & 5 deletions src/dispatch/incident/messaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
from dispatch.database.core import SessionLocal, resolve_attr
from dispatch.document import service as document_service
from dispatch.incident.enums import IncidentStatus
from dispatch.project.models import Project
from dispatch.incident.models import Incident, IncidentRead
from dispatch.incident_priority.models import IncidentPriority
from dispatch.incident_type.models import IncidentType

from dispatch.notification import service as notification_service
from dispatch.messaging.strings import (
INCIDENT_CLOSED_INFORMATION_REVIEW_REMINDER_NOTIFICATION,
Expand Down Expand Up @@ -43,15 +47,21 @@


def get_suggested_documents(
db_session, project_id: int, incident_type: str, priority: str, description: str
db_session,
project: Project,
incident_type: IncidentType,
incident_priority: IncidentPriority,
description: str,
) -> list:
"""Get additional incident documents based on priority, type, and description."""
plugin = plugin_service.get_active_instance(
db_session=db_session, project_id=project_id, plugin_type="document-resolver"
db_session=db_session, project_id=project.id, plugin_type="document-resolver"
)
documents = []
if plugin:
documents = plugin.instance.get(incident_type, priority, description, db_session=db_session)
documents = plugin.instance.get(
incident_type, incident_priority, description, project, db_session=db_session
)
return documents


Expand Down Expand Up @@ -191,7 +201,7 @@ def get_suggested_document_items(incident: Incident, db_session: SessionLocal):
"""Create the suggested document item message."""
suggested_documents = get_suggested_documents(
db_session,
incident.project.id,
incident.project,
incident.incident_type,
incident.incident_priority,
incident.description,
Expand Down Expand Up @@ -804,7 +814,7 @@ def send_incident_management_help_tips_message(incident: Incident, db_session: S
notification_text = "Incident Management Help Tips"
message_template = INCIDENT_MANAGEMENT_HELP_TIPS_MESSAGE

plugin = plugin_service.get_active(
plugin = plugin_service.get_active_instance(
db_session=db_session, project_id=incident.project.id, plugin_type="conversation"
)
if not plugin:
Expand Down
21 changes: 16 additions & 5 deletions src/dispatch/plugins/dispatch_core/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from dispatch.config import DISPATCH_UI_URL
from dispatch.incident_priority.models import IncidentPriority
from dispatch.incident_type.models import IncidentType
from dispatch.project.models import Project
from dispatch.individual import service as individual_service
from dispatch.plugins import dispatch_core as dispatch_plugin
from dispatch.incident import service as incident_service
Expand Down Expand Up @@ -186,7 +187,12 @@ class DispatchDocumentResolverPlugin(DocumentResolverPlugin):
author_url = "https://github.com/netflix/dispatch.git"

def get(
self, incident_type: str, incident_priority: str, incident_description: str, db_session=None
self,
incident_type: str,
incident_priority: str,
incident_description: str,
project: Project,
db_session=None,
):
"""Fetches documents from Dispatch."""
route_in = {
Expand All @@ -195,6 +201,7 @@ def get(
"incident_priorities": [incident_priority],
"incident_types": [incident_type],
"terms": [],
"project": project,
},
}

Expand Down Expand Up @@ -234,6 +241,7 @@ def get(
incident_type: IncidentType,
incident_priority: IncidentPriority,
incident_description: str,
project: Project,
db_session=None,
):
"""Fetches participants from Dispatch."""
Expand All @@ -243,6 +251,7 @@ def get(
"incident_priorities": [incident_priority],
"incident_types": [incident_type],
"terms": [],
"project": project,
},
}

Expand All @@ -253,12 +262,14 @@ def get(
individual_contacts = [(x, None) for x in recommendation.individual_contacts]
# we need to resolve our service contacts to individuals
for s in recommendation.service_contacts:
plugin = plugin_service.get_by_slug(db_session=db_session, slug=s.type)
plugin_instance = plugin_service.get_active_instance_by_slug(
db_session=db_session, slug=s.type, project_id=project.id
)

if plugin:
if plugin.enabled:
if plugin_instance:
if plugin_instance.enabled:
log.debug(f"Resolving service contact. ServiceContact: {s}")
individual_email = plugin.instance.get(s.external_id)
individual_email = plugin_instance.instance.get(s.external_id)

individual = individual_service.get_or_create(
db_session=db_session, email=individual_email
Expand Down
2 changes: 2 additions & 0 deletions src/dispatch/route/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
ServiceReadNested,
TeamReadNested,
)
from dispatch.project.models import ProjectRead
from dispatch.document.models import DocumentRead
from dispatch.term.models import TermRead

Expand Down Expand Up @@ -123,6 +124,7 @@ class ContextBase(DispatchBase):
incident_priorities: Optional[List[IncidentPriorityRead]] = []
incident_types: Optional[List[IncidentTypeRead]] = []
terms: Optional[List[TermRead]] = []
project: ProjectRead


class RouteBase(DispatchBase):
Expand Down
15 changes: 8 additions & 7 deletions src/dispatch/route/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ def get_terms(db_session, text: str) -> List[str]:

def get_resources_from_incident_types(db_session, incident_types: List[IncidentTypeBase]) -> set:
"""Get all resources related to a specific incident type."""
incident_type_names = [i.name for i in incident_types]
incident_type_ids = [i.id for i in incident_types]
incident_type_models = (
db_session.query(IncidentType).filter(IncidentType.name.in_(incident_type_names)).all()
db_session.query(IncidentType).filter(IncidentType.id.in_(incident_type_ids)).all()
)

return {
Expand All @@ -42,10 +42,10 @@ def get_resources_from_priorities(
db_session, incident_priorities: List[IncidentPriorityBase]
) -> set:
"""Get all resources related to a specific priority."""
incident_priority_names = [i.name for i in incident_priorities]
incident_priority_ids = [i.id for i in incident_priorities]
incident_priority_models = (
db_session.query(IncidentPriority)
.filter(IncidentPriority.name.in_(incident_priority_names))
.filter(IncidentPriority.id.in_(incident_priority_ids))
.all()
)

Expand All @@ -71,20 +71,21 @@ def get_resources_from_context(db_session, context: ContextBase) -> set:
)

_, term_resources = (
get_resources_from_terms(db_session, terms=context.terms)
get_resources_from_terms(db_session, project_id=context.project_id, terms=context.terms)
if context.terms
else (None, set())
)

return (incident_types_resources & priorities_resources) | term_resources


def get_resources_from_terms(db_session, terms: List[str]):
def get_resources_from_terms(db_session, project_id: int, terms: List[str]):
"""Fetch resources based solely on connected terms with the text."""
# lookup extracted terms
matched_terms = (
db_session.query(Term)
.filter(func.upper(Term.text).in_([func.upper(t) for t in terms]))
.filter(Term.project_id == project_id)
.all()
)

Expand Down Expand Up @@ -159,7 +160,7 @@ def get(*, db_session, route_in: RouteRequest) -> Dict[Any, Any]:
# get terms from text (question, incident description, etc,.)
text_terms = get_terms(db_session, text=route_in.text)
resource_matched_terms, term_resources = get_resources_from_terms(
db_session=db_session, terms=text_terms
db_session=db_session, project_id=route_in.context.project.id, terms=text_terms
)

if route_in.context:
Expand Down

0 comments on commit b607d4b

Please sign in to comment.