-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata_utils.py
376 lines (319 loc) · 12.7 KB
/
data_utils.py
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import pandas as pd
import streamlit as st
from utils import get_data
def add_date_and_search_where_clause(query, prefix=""):
from_date = st.session_state.get("from_date", None)
to_date = st.session_state.get("to_date", None)
if from_date and to_date:
query += f" WHERE {prefix}datetime BETWEEN '{from_date}' AND '{to_date}'"
if st.session_state.get("conversation_ids", None):
if 'WHERE' in query:
query += " AND"
else:
query += " WHERE"
query += f" {prefix}conversation_id IN ({','.join(st.session_state.conversation_ids)})"
return query
def get_date_range(_session):
query = f"""
SELECT
MIN(datetime) AS min_date,
MAX(datetime) AS max_date
FROM Conversation
"""
df = get_data(query, _session)
return df
# Function to fetch conversation stats
def get_conversation_stats(_session):
query = f"""
SELECT
COUNT(conversation_id) AS count,
AVG(call_score) AS avg_score,
SUM(total_silence_sec) AS total_silence,
SUM(total_talk_time_sec) AS total_talk_time,
SUM(total_overlap_sec) AS total_overlap
FROM Conversation
"""
query = add_date_and_search_where_clause(query)
return get_data(query, _session)
# Function to fetch member stats
def get_member_stats(_session):
query = f"""
SELECT
AVG(pace_wpm) AS avg_pace,
SUM(talk_time_sec) AS total_talk_time,
SUM(listen_time_sec) AS total_listen_time,
SUM(overlap_duration_sec) AS total_overlap
FROM Members m
JOIN Conversation c ON m.conversation_id = c.conversation_id
"""
query = add_date_and_search_where_clause(query, prefix="c.")
return get_data(query, _session)
# Function to fetch sentiment data
def get_sentiment_data(_session):
query = f"""
SELECT
sentiment_label, COUNT(*) AS count, AVG(polarity_score) AS avg_polarity
FROM Sentiment s
JOIN Conversation c ON s.conversation_id = c.conversation_id
GROUP BY sentiment_label
"""
query = add_date_and_search_where_clause(query, prefix="c.")
df = get_data(query, _session)
df['SENTIMENT_LABEL'] = df['SENTIMENT_LABEL'].astype(str)
df['COUNT'] = df['COUNT'].astype(int)
df['AVG_POLARITY'] = df['AVG_POLARITY'].astype(float)
return df
# Function to fetch topics data
def get_topics_data(_session):
query = f"""
SELECT
text AS topic, COUNT(*) AS count
FROM Topics t
JOIN Conversation c ON t.conversation_id = c.conversation_id
GROUP BY text
ORDER BY count DESC
"""
query = add_date_and_search_where_clause(query, prefix="c.")
df = get_data(query, _session)
df['TOPIC'] = df['TOPIC'].astype(str)
df['COUNT'] = df['COUNT'].astype(int)
return df
def get_entities_stats(_session):
query = f"""
SELECT
entity_type, COUNT(*) AS count
FROM Entities e
JOIN Conversation c ON e.conversation_id = c.conversation_id
"""
query = add_date_and_search_where_clause(query, prefix="c.")
query += " GROUP BY entity_type ORDER BY count DESC"
df = get_data(query, _session)
df['ENTITY_TYPE'] = df['ENTITY_TYPE'].astype(str)
df['COUNT'] = df['COUNT'].astype(int)
return df
def get_entities_data(_session):
query = f"""
SELECT
entity_type, entity_subtype, COUNT(*) AS count
FROM Entities e
JOIN Conversation c ON e.conversation_id = c.conversation_id
"""
query = add_date_and_search_where_clause(query, prefix="c.")
query += " GROUP BY entity_type, entity_subtype"
df = get_data(query, _session)
df['ENTITY_TYPE'] = df['ENTITY_TYPE'].astype(str)
df['ENTITY_SUBTYPE'] = df['ENTITY_SUBTYPE'].astype(str)
df['COUNT'] = df['COUNT'].astype(int)
return df
# Function to fetch questions and next steps
def get_questions_data(_session):
query = f"""
SELECT COUNT(question_id) AS total_questions
FROM Questions q
JOIN Conversation c ON q.conversation_id = c.conversation_id
"""
query = add_date_and_search_where_clause(query, prefix="c.")
df = get_data(query, _session)
return df[0]["TOTAL_QUESTIONS"]
def get_next_steps_data(_session):
query = f"""
SELECT COUNT(step_id) AS total_next_steps
FROM NextSteps ns
JOIN Conversation c ON ns.conversation_id = c.conversation_id
"""
query = add_date_and_search_where_clause(query, prefix="c.")
df = get_data(query, _session)
return df[0]["TOTAL_NEXT_STEPS"]
def get_insight_urls(conversation_ids=[], _session=None):
query = f"""
SELECT
conversation_id,
SYMBL_INSIGHTS_URL
FROM conversation_summary_view
"""
if conversation_ids:
query += f" WHERE conversation_id IN ({','.join(conversation_ids)})"
return get_data(query, _session)
def get_call_scores(conversation_ids=[], _session=None):
query = f"""
SELECT
conversation_id,
call_score,
datetime
FROM Conversation
"""
if conversation_ids:
query += f" WHERE conversation_id IN ({','.join(conversation_ids)})"
return get_data(query, _session)
def get_conversation_overview(_session):
query = f"""
SELECT
conversation_id,
conversation_name,
SYMBL_INSIGHTS_URL,
datetime,
account_name,
deal_stage,
sales_rep,
call_score,
sentiment_arr,
overall_sentiment,
number_of_objections,
number_of_next_steps
FROM conversation_summary_view
"""
query = add_date_and_search_where_clause(query)
query += " ORDER BY datetime DESC"
return get_data(query, _session)
def get_sales_reps(_session):
query = f"""
SELECT sr.name as rep_name,
cs.name as CRITERIA_NAME,
AVG(cs.score) as call_score,
ARRAY_AGG(DISTINCT REPLACE(REPLACE(tr.tracker_name, 'Symbl.', ''), '_', ' ')) as trackers
FROM SalesRep sr
JOIN CommunicationHistory ch ON sr.sales_rep_id = ch.sales_rep_id
JOIN Conversation c ON ch.communication_id = c.communication_id
JOIN CallScoreCriteria cs ON c.conversation_id = cs.conversation_id
JOIN Trackers tr ON c.conversation_id = tr.conversation_id
"""
query = add_date_and_search_where_clause(query, prefix="c.")
query += " GROUP BY sr.name, cs.name"
df = get_data(query, _session)
df["CALL_SCORE"] = df["CALL_SCORE"].astype(float)
trackers_df = df.groupby("REP_NAME")["TRACKERS"].first().reset_index()
df_pivot = df.pivot(index="REP_NAME", columns="CRITERIA_NAME", values="CALL_SCORE").reset_index()
df_pivot["AVG_CALL_SCORE"] = df_pivot.drop(columns=["REP_NAME"]).mean(axis=1)
df_final = pd.merge(df_pivot, trackers_df, on="REP_NAME", how="left")
return df_final
def get_sales_rep_call_scores(_session):
query = f"""
SELECT sr.name as rep_name,
cs.name as CRITERIA_NAME,
AVG(cs.score) as call_score,
FROM SalesRep sr
JOIN CommunicationHistory ch ON sr.sales_rep_id = ch.sales_rep_id
JOIN Conversation c ON ch.communication_id = c.communication_id
JOIN CallScoreCriteria cs ON c.conversation_id = cs.conversation_id
"""
query = add_date_and_search_where_clause(query, prefix="c.")
query += " GROUP BY sr.name, cs.name"
df = get_data(query, _session)
df["CALL_SCORE"] = df["CALL_SCORE"].astype(float)
return df
def get_trackers_by_rep(_session):
query = f"""
SELECT sr.name as sales_rep, REPLACE(REPLACE(tr.tracker_name, 'Symbl.', ''), '_', ' ') as tracker_name, COUNT(tr.tracker_id) as tracker_count
FROM Trackers tr
JOIN Conversation c ON tr.conversation_id = c.conversation_id
JOIN CommunicationHistory ch ON c.communication_id = ch.communication_id
JOIN SalesRep sr ON ch.sales_rep_id = sr.sales_rep_id
"""
query = add_date_and_search_where_clause(query, prefix="c.")
query += " GROUP BY sr.name, tracker_name"
return get_data(query, _session)
def get_trackers_by_stage(_session):
query = f"""
SELECT o.stage, REPLACE(REPLACE(tr.tracker_name, 'Symbl.', ''), '_', ' ') as tracker_name, COUNT(tr.tracker_id) as tracker_count
FROM Trackers tr
JOIN Conversation c ON tr.conversation_id = c.conversation_id
JOIN CommunicationHistory ch ON c.communication_id = ch.communication_id
JOIN Opportunity o ON ch.opportunity_id = o.opportunity_id
"""
query = add_date_and_search_where_clause(query, prefix="c.")
query += " GROUP BY o.stage, tracker_name"
return get_data(query, _session)
def get_call_score_criteria_scores(_session):
query = f"""
SELECT csc.name as criteria_name, AVG(csc.score) as avg_score
FROM CallScoreCriteria csc
JOIN Conversation c ON csc.conversation_id = c.conversation_id
JOIN CommunicationHistory ch ON c.communication_id = ch.communication_id
"""
query = add_date_and_search_where_clause(query, prefix="c.")
query += " GROUP BY csc.name"
df = get_data(query, _session)
df['AVG_SCORE'] = df['AVG_SCORE'].astype(float)
return df
def get_account_criteria_scores(_session):
query = f"""
SELECT a.account_name AS ACCOUNT_NAME,
csc.name AS criteria_name,
AVG(csc.score) AS avg_score
FROM Account a
JOIN Opportunity o ON a.account_id = o.account_id
JOIN CommunicationHistory ch ON o.opportunity_id = ch.opportunity_id
JOIN Conversation c ON ch.communication_id = c.communication_id
JOIN CallScoreCriteria csc ON c.conversation_id = csc.conversation_id
"""
query = add_date_and_search_where_clause(query, prefix="c.")
query += " GROUP BY a.account_name, csc.name"
df = get_data(query, _session)
df_pivot = df.pivot(index="ACCOUNT_NAME", columns="CRITERIA_NAME", values="AVG_SCORE").reset_index()
return df_pivot
def get_trackers_by_call_score(_session):
query = f"""
SELECT
REPLACE(REPLACE(tr.tracker_name, 'Symbl.', ''), '_', ' ') AS TRACKER_NAME,
AVG(csc.score) AS AVG_CALL_SCORE,
COUNT(tr.tracker_id) AS TRACKER_COUNT,
AVG(o.opportunity_amount) AS AVG_OPPORTUNITY_AMOUNT
FROM Trackers tr
JOIN CallScoreCriteria csc ON tr.conversation_id = csc.conversation_id
JOIN Conversation c ON tr.conversation_id = c.conversation_id
JOIN CommunicationHistory ch ON c.communication_id = ch.communication_id
JOIN Opportunity o ON ch.opportunity_id = o.opportunity_id
"""
query = add_date_and_search_where_clause(query, prefix="c.")
query += " GROUP BY tr.tracker_name"
return get_data(query, _session)
def get_opportunity_details(_session):
query = f"""
SELECT
a.account_name AS ACCOUNT_NAME,
COUNT(DISTINCT c.conversation_id) AS num_meetings,
o.next_step AS NEXT_STEP,
o.stage AS STAGE,
o.opportunity_amount AS OPPORTUNITY_AMOUNT,
o.probability AS PROBABILITY,
o.target_close_date AS TARGET_CLOSE_DATE,
o.time_since_open AS TIME_SINCE_OPEN,
sr.name AS SALES_REP,
AVG(csc.score) AS avg_call_score,
FROM Opportunity o
JOIN Account a ON a.account_id = o.account_id
JOIN SalesRep sr ON sr.sales_rep_id = o.sales_rep_id
JOIN CommunicationHistory ch ON o.opportunity_id = ch.opportunity_id
JOIN Conversation c ON ch.communication_id = c.communication_id
JOIN CallScoreCriteria csc ON c.conversation_id = csc.conversation_id
"""
query = add_date_and_search_where_clause(query, prefix="c.")
query += " GROUP BY a.account_name, o.next_step, o.stage, o.opportunity_amount, o.probability, o.target_close_date, o.time_since_open, sr.name"
return get_data(query, _session)
def get_conversation_data(conversation_id, _session):
query = f"""
SELECT
c.name AS name,
c.datetime AS datetime,
c.executive_summary AS executive_summary,
c.short_bullet_points AS short_bullet_points,
o.stage AS stage,
a.account_name AS account_name,
FROM Conversation AS c
JOIN CommunicationHistory AS ch ON c.communication_id = ch.communication_id
JOIN Opportunity AS o ON ch.opportunity_id = o.opportunity_id
JOIN Account AS a ON o.account_id = a.account_id
WHERE c.conversation_id = '{conversation_id}'
"""
query = add_date_and_search_where_clause(query, prefix="c.")
return get_data(query, _session)
def get_conversation_objections(conversation_id, _session):
query = f"""
SELECT
objection,
objection_response
FROM Objections
WHERE conversation_id = '{conversation_id}'
"""
query = add_date_and_search_where_clause(query, prefix="c.")
return get_data(query, _session)