forked from Netflix/dispatch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.py
More file actions
181 lines (144 loc) · 5.97 KB
/
Copy pathservice.py
File metadata and controls
181 lines (144 loc) · 5.97 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import logging
from typing import Any, Dict, List
from sqlalchemy import func
from dispatch.nlp import build_phrase_matcher, build_term_vocab, extract_terms_from_text
from dispatch.incident_priority import service as incident_priority_service
from dispatch.incident_priority.models import IncidentPriority, IncidentPriorityBase
from dispatch.incident_type import service as incident_type_service
from dispatch.incident_type.models import IncidentType, IncidentTypeBase
from dispatch.term.models import Term
from .models import Recommendation, RecommendationAccuracy, RouteRequest, ContextBase
log = logging.getLogger(__name__)
def get_terms(db_session, text: str) -> List[str]:
"""Get terms from request."""
all_terms = db_session.query(Term).filter(Term.discoverable == True).all() # noqa
phrases = build_term_vocab([t.text for t in all_terms])
matcher = build_phrase_matcher("dispatch-terms", phrases)
extracted_terms = extract_terms_from_text(text, matcher)
return extracted_terms
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_models = (
db_session.query(IncidentType).filter(IncidentType.name.in_(incident_type_names)).all()
)
return {
resource
for i in incident_type_models
for resource in i.teams + i.individuals + i.services + i.documents
}
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_models = (
db_session.query(IncidentPriority)
.filter(IncidentPriority.name.in_(incident_priority_names))
.all()
)
return {
resource
for i in incident_priority_models
for resource in i.teams + i.individuals + i.services + i.documents
}
def get_resources_from_context(db_session, context: ContextBase) -> set:
"""Fetch relevent resources based on context only."""
incident_types_resources = (
get_resources_from_incident_types(db_session, incident_types=context.incident_types)
if context.incident_types
else set()
)
priorities_resources = (
get_resources_from_priorities(db_session, incident_priorities=context.incident_priorities)
if context.incident_priorities
else set()
)
_, term_resources = (
get_resources_from_terms(db_session, 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]):
"""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]))
.all()
)
# find resources associated with those terms
resources = {
resource
for t in matched_terms
for resource in t.teams + t.individuals + t.services + t.documents
}
return matched_terms, resources
# TODO contacts could be List[Union(...)]
def create_recommendation(
*, db_session, text=str, context: ContextBase, matched_terms: List[Term], resources: List[Any]
):
"""Create recommendation object for accuracy tracking."""
accuracy = [
RecommendationAccuracy(resource_id=r.id, resource_type=type(r).__name__) for r in resources
]
incident_priorities = []
incident_types = []
if context:
incident_priorities = [
incident_priority_service.get_by_name(db_session=db_session, name=n.name)
for n in context.incident_priorities
]
incident_types = [
incident_type_service.get_by_name(db_session=db_session, name=n.name)
for n in context.incident_types
]
service_contacts = [x for x in resources if type(x).__name__ == "Service"]
individual_contacts = [x for x in resources if type(x).__name__ == "IndividualContact"]
team_contacts = [x for x in resources if type(x).__name__ == "TeamContact"]
documents = [x for x in resources if type(x).__name__ == "Document"]
log.debug(
f"Recommendation: Documents: {documents} Individuals: {individual_contacts} Teams: {team_contacts} Services: {service_contacts}"
)
r = Recommendation(
accuracy=accuracy,
service_contacts=service_contacts,
individual_contacts=individual_contacts,
team_contacts=team_contacts,
documents=documents,
incident_types=incident_types,
incident_priorities=incident_priorities,
matched_terms=matched_terms,
text=text,
)
db_session.add(r)
db_session.commit()
return r
def get(*, db_session, route_in: RouteRequest) -> Dict[Any, Any]:
"""Get routed resources."""
resources = []
matched_terms = []
if route_in.context:
resources.extend(
get_resources_from_context(db_session=db_session, context=route_in.context)
)
if route_in.text:
# 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
)
if route_in.context:
route_in.context.terms.extend(resource_matched_terms)
resources.extend(term_resources)
resources = list(set(resources))
# create a recommandation entry we can use to data mine at a later time
recommendation = create_recommendation(
db_session=db_session,
text=route_in.text,
context=route_in.context,
resources=resources,
matched_terms=matched_terms,
)
return recommendation