-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode-file
394 lines (291 loc) · 12.3 KB
/
Code-file
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 30 14:01:03 2020
@author: Farhan
"""
#importing the libraries
import pandas as pd
import webbrowser
# !pip install dash
import dash
import dash_html_components as html
from dash.dependencies import Input, Output
import dash_core_components as dcc
import plotly.graph_objects as go
import plotly.express as px
# pip install dash-bootstrap-components
import dash_bootstrap_components as dbc
import dash_table as dt
# Global variables
app = dash.Dash(external_stylesheets=[dbc.themes.BOOTSTRAP])
project_name = None
def load_data():
#the variable inside a function is local by default.
call_dataset_name = "Call_data.csv"
service_dataset_name = "Service_data.csv"
device_dataset_name = "Device_data.csv"
# We use a global keyword for a variable which is inside a function so that it can be modified.
global call_data
call_data = pd.read_csv(call_dataset_name)
global service_data
service_data = pd.read_csv(service_dataset_name)
global device_data
device_data = pd.read_csv(device_dataset_name)
# [{'label': '2019-06-20', 'value': '2019-06-20'} ]
global start_date_list
temp_list = sorted(call_data['date'].dropna().unique().tolist())
start_date_list = [{"label": str(i), "value": str(i)} for i in temp_list ]
# [{'label': '2019-06-20', 'value': '2019-06-20'} ]
global end_date_list
end_date_list = [ { 'label' : str(i) , 'value': str(i) } for i in temp_list ]
# [{'label': 'Hourly', 'value': 'Hourly'} ]
temp_list = ["Hourly", "Daywise", "Weekly"]
global report_type_list
report_type_list = [{"label": str(i), "value":str(i)} for i in temp_list ]
def open_browser():
webbrowser.open_new('http://127.0.0.1:8050/')
# Layout of your page
def create_app_ui():
main_layout = html.Div(
children =
[
html.H1('CDR Analysis with Insights', id='Main_title'),
dcc.Tabs(id="Tabs", value="Call Analytics tool",children=[
dcc.Tab(label="Call Analytics tool", id="Call Analytics tool", value="Call Analytics tool",
children=[
dcc.Dropdown(
id='start-date-dropdown',
options=start_date_list,
placeholder = "Select Starting Date here",
value = "2019-06-20"
),
html.Br(),
dcc.Dropdown(
id='end-date-dropdown',
options=end_date_list,
placeholder = "Select Ending Date here",
value = "2019-06-25"
),
html.Br(),
dcc.Dropdown(
id='group-dropdown',
placeholder = "Select group here",
multi = True
),
html.Br(),
dcc.Dropdown(
id='Report-type-dropdown',
options=report_type_list,
placeholder = "Select Report Type",
value = "Hourly"
),
html.Br(),
dcc.Loading(html.Div(id='visualization-object',children='Graph,Card, Table')),
]),
dcc.Tab(label="Device Analytics tool",id="Device Analytics tool",value="Device Analytics tool",
children =[
dcc.Dropdown(id="Device_Analytics_Dropdown" ,
options=start_date_list,
placeholder="Select Date"),
html.Br(),
dcc.Loading(
html.Div(id = 'Device-Piechart',
style={'marginLeft': '16%'})
)
]),
dcc.Tab(label="Service Analytics tool",id="Service Analytics tool",value="Service Analytics tool",
children=[
dcc.Dropdown(id="Service-Analytics-Dropdown",
options=start_date_list,
placeholder="Select date"),
html.Br(),
dcc.Loading(
html.Div(id = 'Service-Piechart', style={'marginLeft': '20%'})
)
])
],style={'padding':40})
])
return main_layout
def create_card(title, content, color):
card = dbc.Card(
dbc.CardBody(
[
html.H4(title),
html.Br(),
html.Br(),
html.H2(content),
html.Br(),
]
),
color=color, inverse=True
)
return(card)
# Callback of your page
@app.callback(
Output('visualization-object', 'children'),
[
Input('start-date-dropdown', 'value'),
Input('end-date-dropdown', 'value'),
Input("group-dropdown", 'value'),
Input('Report-type-dropdown', 'value'),
]
)
def update_app_ui(start_date, end_date, group, report_type):
print("Data Type of start_date value = " , str(type(start_date)))
print("Data of start_date value = " , str(start_date))
print("Data Type of end_date value = " , str(type(end_date)))
print("Data of end_date value = " , str(end_date))
print("Data Type of group value = " , str(type(group)))
print("Data of group value = " , str(group))
print("Data Type of report_type value = " , str(type(report_type)))
print("Data of report_type value = " , str(report_type))
# Filter the data as per the selection of the drop downs
call_analytics_data = call_data[ (call_data["date"]>=start_date) & (call_data["date"]<=end_date) ]
if (group == [] or group is None):
pass
else:
call_analytics_data = call_analytics_data[call_analytics_data["Group"].isin(group)]
graph_data = call_analytics_data
# Group the data based on the drop down
if report_type == "Hourly":
graph_data = graph_data.groupby("hourly_range")["Call_Direction"].value_counts().reset_index(name = "count")
x = "hourly_range"
content = call_analytics_data["hourly_range"].value_counts().idxmax()
title = "Busiest Hour"
elif report_type == "Daywise":
graph_data = graph_data.groupby("date")["Call_Direction"].value_counts().reset_index(name = "count")
x = "date"
content = call_analytics_data["date"].value_counts().idxmax()
title = "Busiest Day"
else:
graph_data = graph_data.groupby("weekly_range")["Call_Direction"].value_counts().reset_index(name = "count")
x = "weekly_range"
content = call_analytics_data["weekly_range"].value_counts().idxmax()
title = "Busiest WeekDay"
# Graph Section
figure = px.area(graph_data,
x = x,
y = "count",
color = "Call_Direction",
hover_data=[ "Call_Direction", "count"],
template = "plotly_dark")
figure.update_traces(mode = "lines+markers")
# Card Section
total_calls = call_analytics_data["Call_Direction"].count()
card_1 = create_card("Total Calls",total_calls, "success")
incoming_calls = call_analytics_data["Call_Direction"][call_analytics_data["Call_Direction"]=="Incoming"].count()
card_2 = create_card("Incoming Calls", incoming_calls, "primary")
outgoing_calls = call_analytics_data["Call_Direction"][call_analytics_data["Call_Direction"]=="Outgoing"].count()
card_3 = create_card("Outgoing Calls", outgoing_calls, "primary")
missed_calls = call_analytics_data["Missed Calls"][call_analytics_data["Missed Calls"] == 3].count()
card_4 = create_card("Missed Calls", missed_calls, "danger")
max_duration = call_analytics_data["duration"].max()
card_5 = create_card("Max Duration", f'{max_duration} min', "dark")
card_6 = create_card(title, content, "primary")
# md = 4 indicating that on a 'medium' sized
graphRow0 = dbc.Row([dbc.Col(id='card1', children=[card_1], md=3), dbc.Col(id='card2', children=[card_2], md=3)])
graphRow1 = dbc.Row([dbc.Col(id='card3', children=[card_3], md=3), dbc.Col(id='card4', children=[card_4], md=3)])
graphRow2 = dbc.Row([dbc.Col(id='card5', children=[card_5], md=3), dbc.Col(id='card6', children=[card_6], md=3)])
cardDiv = html.Div([graphRow0,html.Br(), graphRow1,html.Br(), graphRow2])
# Data Table Section
datatable_data = call_analytics_data.groupby(["Group", "UserID", "UserDeviceType"])["Call_Direction"].value_counts().unstack(fill_value = 0).reset_index()
if call_analytics_data["Missed Calls"][call_analytics_data["Missed Calls"]==19].count()!=0:
datatable_data["Missed Calls"] = call_analytics_data.groupby(["Group", "UserID", "UserDeviceType"])["Missed Calls"].value_counts().unstack()[3]
else:
datatable_data["Missed Calls"] = 0
datatable_data["Total_call_duration"] = call_analytics_data.groupby(["Group", "UserID", "UserDeviceType"])["duration"].sum().tolist()
datatable = dt.DataTable(
id='table',
columns=[{"name": i, "id": i} for i in datatable_data.columns],
data=datatable_data.to_dict('records'),
page_current=2,
page_size=30,
page_action='native',
style_header={'backgroundColor': 'rgb(90, 30, 30)'},
style_cell={
'backgroundColor': 'rgb(50, 50, 50)',
'color': 'white'
}
)
return [
dcc.Graph(figure = figure),
html.Br() ,
cardDiv,
html.Br(),
datatable
]
@app.callback(
Output('Service-Piechart', 'children'),
[
Input('Service-Analytics-Dropdown', 'value')
]
)
#Cmethod to create the service analytics Piechart
def update_service_piechart(date):
print("Service Date : ", str(date))
new_data = service_data.groupby('FeatureEventDate')['FeatureName'].value_counts().reset_index(name="count")
pie_data = new_data[new_data['FeatureEventDate']==date]
labels = [i for i in pie_data['FeatureName']]
values = [int(i) for i in pie_data['count']]
figure = {'data':[go.Pie(labels=labels, values = values)],
'layout':go.Layout(height=850, width = 850, autosize = False)}
return dcc.Graph(figure = figure)
@app.callback(
Output("Device-Piechart" , "children"),
[
Input("Device_Analytics_Dropdown" , "value")
]
)
def update_device_piechart(date):
#Creating the Device_Analytics_tool Piechart
print(f"Device Date : {date}")
dd_data = device_data.replace(to_replace = r'^Polycom(.*$)', value = 'Polycom', regex = True)
dd_data = dd_data.replace(to_replace = r'(.*)Windows(.*$)', value = 'Windows', regex = True)
dd_data = dd_data.replace(to_replace = r'^Yealink(.*$)', value = 'Yealink', regex = True)
dd_data = dd_data.replace(to_replace = r'(.*)Android(.*$)', value = 'Android', regex = True)
dd_data = dd_data.replace(to_replace = r'(.*)iPhone(.*$)', value = 'iPhone', regex = True)
dd_data = dd_data.replace(to_replace = r'(.*)Aastra(.*$)', value = 'Aastra', regex = True)
dd_data = dd_data.replace(to_replace = r'(.*)Mac(.*$)', value = 'Mac', regex = True)
dd_data = dd_data.replace(to_replace = r'^Algo(.*$)', value = 'Algo', regex = True)
dd_data = dd_data.replace(to_replace = r'^Linksys(.*$)', value = 'Linksys', regex = True)
dd_data = dd_data.replace(to_replace = r'^CS2000(.*$)', value = 'CS2000 NGSS', regex = True)
new_data = dd_data.groupby('DeviceEventDate')['UserDeviceType'].value_counts().reset_index(name="count")
pie_data = new_data[new_data['DeviceEventDate'] == date]
labels = [i for i in pie_data['UserDeviceType']]
values = [int(i) for i in pie_data['count']]
figure = {'data' : [go.Pie(labels=labels, values=values, hole =.3)],
'layout' : go.Layout(height=800, width = 800, autosize = False)
}
return dcc.Graph(figure = figure)
@app.callback(
Output("group-dropdown", "options"),
[
Input('start-date-dropdown', 'value'),
Input('end-date-dropdown', 'value')
]
)
def update_groups(start_date, end_date):
reformed_data = call_data[(call_data["date"]>=start_date) & (call_data["date"]<=end_date)]
group_list = reformed_data["Group"].unique().tolist()
group_list = [{"label":m, "value":m} for m in group_list]
return group_list
# Flow of your Project
def main():
load_data()
open_browser()
global app, project_name
project_name = "CDR Analysis with Insights"
app.layout = create_app_ui()
app.title = project_name
app.run_server()
print("End of the script")
app = None
project_name = None
global call_data,service_data,start_date_list,end_date_list,report_type_list
call_data = None
service_data = None
start_date_list = None
end_date_list = None
report_type_list = None
if __name__ == '__main__':
main()