-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
1137 lines (989 loc) · 46.2 KB
/
main.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from typing import Tuple, List
from base64 import b64decode
import io
from dash import dash, dcc, html, dash_table, Input, Output, callback
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import dash_daq as daq
class LMT_Statistics:
def __init__(self,
statistic_file: str = "history.csv",
font_size: int = 24,
font_family: str = "IBM Plex Sans",
):
"""
Web application for LMT statistics.
Supports launching a default dash server via LMT_Statistics.run_server() method or returning a dash.html.Div object via LMT_Statistics.init() method.
:param statistic_file: path to csv file
:type statistic_file: str
:param font_size: font size
:type font_size: int
:param font_family: font family
:type font_family: str
"""
self.web_layout = None
self.statistic_file = statistic_file
self.uploaded = False
self.font_size = font_size
self.font_family = font_family
self.data = self.import_data()
self.card_element = self.card()
self.data_table_element = self.data_table()
self.web_layout = None
def import_data(self) -> pd.DataFrame:
"""
Imports data from csv file. Converts timestamps.
:return: DataFrame from csv file
:rtype: pd.DataFrame
"""
if not self.uploaded:
data = pd.read_csv(self.statistic_file)
else:
data = self.data
data['data_collection_time'] = pd.to_datetime(data['data_collection_time'])
return data
@staticmethod
def tick_vals(min_value: float, max_value: float, is_percentage: bool = False) -> tuple[
list[float], list[str]]:
"""
Generates tick values and tick labels based on specified parameters for use with graphs
:param min_value: minimum value
:type min_value: float
:param max_value: maximum value
:type max_value: float
:param is_percentage: if True, generates tick values and tick labels for percentage graphs
:type is_percentage: bool
:return: tick values and tick labels
:rtype: list
"""
if is_percentage:
tick_values = [i / 10 for i in range(11)]
tick_labels = [f"{i * 10}%" for i in range(11)]
else:
if min_value > max_value:
min_value, max_value = max_value, min_value
tick_values = [min_value, max_value]
tick_labels = [f"{min_value:,}", f"{max_value:,}"]
return tick_values, tick_labels
def bar_chart(self, _df: pd.DataFrame, _title: str, logarithmic_y_axis: bool, _x: list, _y: list,
_labels: dict = {}, tick_values: list = [], tick_labels: list = [], traces: list = [],
layout_hovermode: str = "x", layout_hoverlabel: dict = {}, layout_legend: dict = {},
html_id: str = '', _text_auto=False) -> html.Div:
"""
Generates a div with a bar chart based on specified parameters
:param _df: DataFrame
:type _df: pd.DataFrame
:param _title: title of the chart
:type _title: str
:param logarithmic_y_axis: if True, y-axis is logarithmic
:type logarithmic_y_axis: bool
:param _x: list of x values to use
:type _x: list
:param _y: list of y values to use
:type _y: list
:param _labels: dictionary of labels to use
:type _labels: dict
:param tick_values: list of tick values (for making simpler y-axis divisions)
:type tick_values: list
:param tick_labels: list of tick labels
:type tick_labels: list
:param traces: list of traces to use in update_traces() for a graph
:type traces: list
:param layout_hovermode: hovermode for the graph (in update_layout())
:type layout_hovermode: str
:param layout_hoverlabel: hoverlabel for the graph (in update_layout())
:type layout_hoverlabel: dict
:param layout_legend: legend for the graph (in update_layout())
:type layout_legend: dict
:param html_id: id of the div containing the graph (if empty, no id is set)
:type html_id: str
:return: A Dash HTML div containing the bar chart
:rtype: html.Div
"""
graph = dcc.Graph(
figure=(
px.bar(_df, title=_title, x=_x, y=_y, log_y=logarithmic_y_axis, labels=_labels,
text_auto=_text_auto) if len(_x) > 0 and len(_y) > 0 else px.bar(_df, title=_title,
log_y=logarithmic_y_axis,
labels=_labels,
text_auto=_text_auto)
)
.update_yaxes(tickvals=tick_values, ticktext=tick_labels)
.update_traces(traces)
.update_layout(
legend_title_text="",
height=600,
font=dict(family=self.font_family, size=self.font_size, color="black"),
hovermode=layout_hovermode,
hoverlabel=layout_hoverlabel,
legend=layout_legend
)
)
if html_id != '':
graph.__setattr__("id", html_id)
div = html.Div([
graph,
])
return div
def data_table(self) -> html.Div:
"""
Generates a div with a dataTable based on specified parameters
:return: A Dash HTML div containing the dataTable
:rtype: html.Div
"""
div = html.Div([
dash_table.DataTable(
id='datatable-interactivity',
columns=[
{"name": i, "id": i} for i in self.data.columns
],
data=self.data.to_dict('records'),
sort_action="native",
sort_mode="multi",
page_action="native",
page_current=0,
page_size=10,
style_table={'overflowX': 'scroll'},
)])
return div
def bar_chart2(self, _df: pd.DataFrame, _title: str, logarithmic_y_axis: bool, _x: list, _y: list,
_labels: dict = {}, tick_values: list = [], tick_labels: list = [], traces: list = [],
layout_hovermode: str = "x", layout_hoverlabel: dict = {}, layout_legend: dict = {},
html_id: str = '', _text_auto=False):
"""
Generates a div with a bar chart based on specified parameters
:param _df: DataFrame
:type _df: pd.DataFrame
:param _title: title of the chart
:type _title: str
:param logarithmic_y_axis: if True, y-axis is logarithmic
:type logarithmic_y_axis: bool
:param _x: list of x values to use
:type _x: list
:param _y: list of y values to use
:type _y: list
:param _labels: dictionary of labels to use
:type _labels: dict
:param tick_values: list of tick values (for making simpler y-axis divisions)
:type tick_values: list
:param tick_labels: list of tick labels
:type tick_labels: list
:param traces: list of traces to use in update_traces() for a graph
:type traces: list
:param layout_hovermode: hovermode for the graph (in update_layout())
:type layout_hovermode: str
:param layout_hoverlabel: hoverlabel for the graph (in update_layout())
:type layout_hoverlabel: dict
:param layout_legend: legend for the graph (in update_layout())
:type layout_legend: dict
:param html_id: id of the div containing the graph (if empty, no id is set)
:type html_id: str
:return: A Dash HTML div containing the bar chart
:rtype: html.Div
"""
graph = dcc.Graph(
figure=(
px.bar(_df, title=_title, x=_x, y=_y, log_y=logarithmic_y_axis, labels=_labels,
text_auto=_text_auto) if len(_x) > 0 and len(_y) > 0 else px.bar(_df, title=_title,
log_y=logarithmic_y_axis,
labels=_labels,
text_auto=_text_auto)
)
.update_yaxes(tickvals=tick_values, ticktext=tick_labels)
.update_traces(traces)
.update_layout(
legend_title_text="",
height=600,
font=dict(family=self.font_family, size=self.font_size, color="black"),
hovermode=layout_hovermode,
hoverlabel=layout_hoverlabel,
legend=layout_legend
)
)
if html_id != '':
graph.__setattr__("id", html_id)
return dcc.figure(
px.bar(_df, title=_title, x=_x, y=_y, log_y=logarithmic_y_axis, labels=_labels,
text_auto=_text_auto) if len(_x) > 0 and len(_y) > 0 else px.bar(_df, title=_title,
log_y=logarithmic_y_axis,
labels=_labels,
text_auto=_text_auto)
).update_yaxes(tickvals=tick_values, ticktext=tick_labels).update_traces(traces).update_layout(
legend_title_text="",
height=600,
font=dict(family=self.font_family, size=self.font_size, color="black"),
hovermode=layout_hovermode,
hoverlabel=layout_hoverlabel,
legend=layout_legend
)
def card(self) -> html.Div:
"""
Generates a div with a card based on specified parameters
:return: div with a bar chart
:rtype: html.Div
"""
_data = self.calculate_card()
_labels = ["Average number of software instances per endpoint", "Average number of endpoints per customer"]
div = html.Div([
html.Div([html.H3([l], className="card-title"), html.Span([f"{d:.3f}"], className="card-text")]
, className="card-body")
for l, d in zip(_labels, _data)
], className="card-container")
return div
def calculate_card(self):
"""
Calculates the average number of software instances per endpoint and the average number of endpoints per customer.
:return: tuple of average number of software instances per endpoint and average number of endpoints per customer
:rtype: tuple
"""
# only to mitigate the bug with loading new data
software_instances_avg_per_endpoint = self.get_avg_instance_per_endpoints()
endpoints_per_customer = self.get_avg_endpoints_per_customer()
return software_instances_avg_per_endpoint, endpoints_per_customer
def change_cards(self):
"""
Updates the card values.
:return: None
"""
values = self.calculate_card()
self.card_element.children[0].children[1].children = f"{values[0]:.3f}"
self.card_element.children[1].children[1].children = f"{values[1]:.3f}"
def change_datatable(self) -> None:
temp_element = html.Div([
dash_table.DataTable(
id='datatable-interactivity',
columns=[
{"name": i, "id": i} for i in self.data.columns
],
data=self.data.to_dict('records'),
sort_action="native",
sort_mode="multi",
page_action="native",
page_current=0,
page_size=10,
style_table={'overflowX': 'scroll'},
)])
self.data_table_element.children = temp_element.children
@staticmethod
def barchart_endpoints_percentage(_df, period_type):
filtered = _df.copy(deep=True)
# fixes bug with loading new data
filtered['data_collection_time'] = pd.to_datetime(filtered['data_collection_time'])
if period_type == "M":
filtered["year_month"] = filtered["data_collection_time"].dt.to_period("M")
elif period_type == "Q":
filtered["year_month"] = filtered["data_collection_time"].dt.to_period("Q")
grouped = filtered.groupby("year_month")[["endpoints_all", "endpoints_disconnected"]].sum().reset_index()
grouped["year_month"] = grouped["year_month"].dt.to_timestamp()
grouped["disconnected_percent"] = grouped["endpoints_disconnected"] / grouped["endpoints_all"]
grouped["connected_percent"] = 1 - grouped["disconnected_percent"]
# print(grouped.head())
fig = px.bar(grouped, x='year_month', y=['connected_percent', 'disconnected_percent'],
labels={'year_month': 'Date', 'value': 'Endpoints percentage'})
fig.update_yaxes(tickvals=[i / 10 for i in range(11)], ticktext=[f"{i * 10}%" for i in range(11)])
fig.update_traces(
hoverinfo='all',
hovertemplate='<b>%{y:.2%}</b><extra></extra>')
fig.update_layout(
legend_title_text="",
height=600,
font=dict(
family="IBM Plex Sans",
size=24,
color="black"
),
hovermode="x",
hoverlabel=dict(
bgcolor="white",
font_size=24,
font_family="IBM Plex Sans",
font_color="black",
bordercolor="black",
),
legend=dict(
orientation="h",
yanchor="bottom",
y=1.02,
xanchor="right",
x=1,
),
)
fig.update_xaxes(tickvals=grouped['year_month'], ticktext=format_xaxis(period_type, grouped['year_month']))
div = html.Div([
html.H2("Endpoints All", id='endpoints-chart-title'),
dcc.Graph(
id='graph-endpoints-over-time',
figure=fig
)
])
return div
@staticmethod
def barchart_endpoints_value(_df, period_type):
filtered = _df.copy(deep=True)
# fixes bug with loading new data
filtered['data_collection_time'] = pd.to_datetime(filtered['data_collection_time'])
if period_type == "M":
filtered["year_month"] = filtered["data_collection_time"].dt.to_period("M")
elif period_type == "Q":
filtered["year_month"] = filtered["data_collection_time"].dt.to_period("Q")
grouped = filtered.groupby("year_month")[["endpoints_all", "endpoints_disconnected"]].sum().reset_index()
grouped["year_month"] = grouped["year_month"].dt.to_timestamp()
grouped["endpoints_connected"] = grouped["endpoints_all"] - grouped["endpoints_disconnected"]
# print(grouped.head())
fig = px.bar(grouped, x='year_month', y=['endpoints_connected', 'endpoints_disconnected'],
labels={'year_month': 'Date', 'value': 'Endpoints value'}) # , log_y=True)
fig.update_traces(
hoverinfo='all',
hovertemplate='<b>%{y:}</b><extra></extra>')
fig.update_layout(
legend_title_text="",
height=600,
font=dict(
family="IBM Plex Sans",
size=24,
color="black"
),
hovermode="x",
hoverlabel=dict(
bgcolor="white",
font_size=24,
font_family="IBM Plex Sans",
font_color="black",
bordercolor="black",
),
legend=dict(
orientation="h",
yanchor="bottom",
y=1.02,
xanchor="right",
x=1,
),
)
fig.update_xaxes(tickvals=grouped['year_month'], ticktext=format_xaxis(period_type, grouped['year_month']))
div = html.Div([
html.H2("Endpoints All", id='endpoints-chart-title'),
dcc.Graph(
id='graph-endpoints-over-time',
figure=fig
)
])
return div
@staticmethod
def barchart_database_percentage(_df, period_type):
filtered = _df.copy(deep=True)
filtered['data_collection_time'] = pd.to_datetime(filtered['data_collection_time'])
if period_type == "M":
filtered["year_month"] = filtered["data_collection_time"].dt.to_period("M")
elif period_type == "Q":
filtered["year_month"] = filtered["data_collection_time"].dt.to_period("Q")
filtered["year_month"] = filtered["year_month"].dt.to_timestamp()
grouped = filtered.groupby(["year_month", "lmt_database_type"]).size().unstack(fill_value=0)
grouped['total'] = grouped.sum(axis=1)
grouped_percentage = grouped.div(grouped['total'], axis=0) * 1
grouped_percentage.drop(columns=['total'], inplace=True)
# print(grouped.head())
fig = px.bar(grouped_percentage, labels={'year_month': 'Date', 'value': 'Types percentage'})
fig.update_yaxes(tickvals=[i / 10 for i in range(11)], ticktext=[f"{i * 10}%" for i in range(11)])
fig.update_traces(
hoverinfo='all',
hovertemplate='<b>%{y:.2%}</b><extra></extra>')
fig.update_layout(
legend_title_text="",
height=600,
font=dict(
family="IBM Plex Sans",
size=24,
color="black"
),
hovermode="x",
hoverlabel=dict(
bgcolor="white",
font_size=24,
font_family="Arial",
font_color="black",
bordercolor="black",
),
legend=dict(
orientation="h",
yanchor="bottom",
y=1.02,
xanchor="right",
x=1,
),
)
fig.update_xaxes(tickvals=grouped.index, ticktext=format_xaxis(period_type, grouped.index))
div = html.Div([
html.H2("Database types", id='chart-title'),
dcc.Graph(
id='graph-database-type-over-time',
figure=fig
)
])
return div
@staticmethod
def barchart_database_value(_df, period_type):
filtered = _df.copy(deep=True)
filtered['data_collection_time'] = pd.to_datetime(filtered['data_collection_time'])
if period_type == "M":
filtered["year_month"] = filtered["data_collection_time"].dt.to_period("M")
elif period_type == "Q":
filtered["year_month"] = filtered["data_collection_time"].dt.to_period("Q")
filtered["year_month"] = filtered["year_month"].dt.to_timestamp()
grouped = filtered.groupby(["year_month", "lmt_database_type"]).size().unstack(fill_value=0)
# print(grouped.head())
fig = px.bar(grouped, labels={'year_month': 'Date', 'value': 'Types value'})
fig.update_traces(
hoverinfo='all',
hovertemplate='<b>%{y:}</b><extra></extra>')
fig.update_layout(
legend_title_text="",
height=600,
font=dict(
family="IBM Plex Sans",
size=24,
color="black"
),
hovermode="x",
hoverlabel=dict(
bgcolor="white",
font_size=24,
font_family="Arial",
font_color="black",
bordercolor="black",
),
legend=dict(
orientation="h",
yanchor="bottom",
y=1.02,
xanchor="right",
x=1,
),
)
fig.update_xaxes(tickvals=grouped.index, ticktext=format_xaxis(period_type, grouped.index))
div = html.Div([
html.H2("Database types", id='chart-title'),
dcc.Graph(
id='graph-database-type-over-time',
figure=fig
)
])
return div
@staticmethod
def get_os_breakdown(_df: pd.DataFrame) -> tuple:
"""
Returns the total number of endpoints per os.
:param _df: DataFrame containing the data
:type _df: pd.DataFrame
:return: columns of total number of endpoints per os and labels
:rtype: tuple
"""
os_columns = [col for col in _df.columns if col.startswith('endpoints_os_')]
os_totals = _df[os_columns].sum(axis=0).sort_values(ascending=False)
os_labels = [
col.replace('endpoints_os_', '').replace('_', ' ').capitalize().replace('Ibm', 'IBM').replace('Hpux',
'HP-UX').replace(
'sparc', 'Sparc') for col in os_totals.axes[0]]
return os_totals, os_labels
@staticmethod
def get_endpoints_per_os(_df: pd.DataFrame) -> tuple:
"""
Returns the average number of endpoints per os.
:param _df: DataFrame containing the data
:type _df: pd.DataFrame
:return: columns of average number of endpoints per os and labels
:rtype: tuple
"""
os_columns = [col for col in _df.columns if col.startswith('endpoints_os_')]
os_avgs = _df[os_columns].mean(axis=0).round(3).sort_values(ascending=False)
os_labels = [
col.replace('endpoints_os_', '').replace('_', ' ').capitalize().replace('Ibm', 'IBM').replace('Hpux',
'HP-UX').replace(
'sparc', 'Sparc') for col in os_avgs.axes[0]]
return os_avgs, os_labels
def get_avg_instance_per_endpoints(self) -> float:
"""
Returns average number of instances per endpoint.
:param _df: DataFrame containing the data
:type _df: pd.DataFrame
:return: average number of instances per endpoint
:rtype: float
"""
endpoints_all = self.data['endpoints_all'].sum()
instances_all = self.data['instances_all'].sum()
avg = (instances_all / endpoints_all)
return avg
def get_avg_endpoints_per_customer(self) -> float:
"""
Returns average number of endpoints per customer (or data length).
:param _df: DataFrame containing the data
:type _df: pd.DataFrame
:return: average number of endpoints per customer
:rtype: float
"""
endpoints_all = self.data['endpoints_all'].sum()
avg = (endpoints_all / len(self.data))
return avg
@staticmethod
def create_upload() -> html.Div:
"""
Creates a div with an upload component for csv files.
:return: div with an upload component for csv files
:rtype: html.Div
"""
div = html.Div([
dcc.Upload(
id='upload_data',
children=html.Div([
'Drag and Drop or ',
html.A('Select Files')
]),
multiple=False,
style={
'width': '20%',
'height': '60px',
'lineHeight': '60px',
'borderWidth': '1px',
'borderStyle': 'dashed',
'borderRadius': '5px',
'textAlign': 'center',
'margin': '10px auto',
}
),
html.Div(id='output_data_upload')
], id="upload_div")
return div
def make_graphs(self, return_to_self: bool = False) -> html.Div:
"""
Initializes and computes data based on specified csv file. Returns either a dash.html.Div layout or sets up layout for default web server (LMT.web_layout and LMT.run_server()).
Call before LMT_Statistics.run_server() method.
:param return_to_self: if True, returns an empty dash.html.Div and sets LMT_Statistics.web_layout
:type return_to_self: bool
:return: dash.html.Div web layout or empty dash.html.Div
:rtype: dash.html.Div
"""
data = self.import_data()
# Disconnected endpoints over time disconnected_endpoints_over_time =
# self.get_disconnected_endpoints_over_time(data) disconnected_endpoints_over_time_tick_vals,
# disconnected_endpoints_over_time_tick_labels = self.tick_vals(0,100,is_percentage=True)
# # Database types over time database_types_over_time = self.get_database_types_over_time(data)
# database_types_over_time_tick_vals, database_types_over_time_tick_labels = self.tick_vals(0,100,
# is_percentage=True)
# Breakdown of OSes total
os_breakdown, os_labels_breakdown = self.get_os_breakdown(data)
os_endpoint_breakdown_tick_vals, os_endpoint_breakdown_tick_labels = self.tick_vals(
min(os_breakdown[os_breakdown > 0]), max(os_breakdown))
# Average number of endpoints for customer
os_avgs, os_labels_avgs = self.get_endpoints_per_os(data)
endpoint_avg_per_customer_tick_vals, endpoint_avg_per_customer_tick_labels = self.tick_vals(
min(os_avgs[os_avgs > 0]), max(os_avgs))
# Average number of software instances per endpoint
# it doesn't change value as of now after update
software_instances_avg_per_endpoint = self.get_avg_instance_per_endpoints()
# Average number of endpoints per customer
# it doesn't change value as of now after update
endpoints_per_customer = self.get_avg_endpoints_per_customer()
card = self.card()
traces = dict(
hoverinfo='all',
hovertemplate='<b>%{y:.2%}</b><extra></extra>'
)
hoverlabel = dict(
bgcolor="white",
font_size=self.font_size,
font_family=self.font_family,
font_color="black",
bordercolor="black",
)
legend = dict(
orientation="h",
yanchor="bottom",
y=1.02,
xanchor="right",
x=1,
)
# Make dictionary for both os breakdown and endpoints per os average
checkbox_os_breakdown = dict(zip(os_labels_breakdown, os_breakdown.axes[0]))
checkbox_os_avg = dict(zip(os_labels_avgs, os_avgs.axes[0]))
# file uploader element
file_uploader = self.create_upload()
def generate_column_options(df):
available_columns = [{'label': col, 'value': col} for col in df.columns if
col != 'data_collection_time' and col != 'lmt_server_install_time' and col != 'lmt_scanner_version_oldest' and col != 'lmt_server_version' and col != 'lmt_database_version' and col != 'lmt_database_type' and col != 'last_import_status']
return available_columns
layout = html.Div([
html.Div(
className="full-screen",
id="top",
children=[
html.A(html.Button("More", className="btn"), href="#first")
]
),
html.H1(id="first"),
html.Nav(
className="navbar",
children=[
html.Div("LMT Statistics — Dashboard", className="logo", id="logo"),
html.Ul(
className="nav-links",
children=[
html.Li(html.A("Compare Charts", href="#compare-charts")),
html.Li(html.A("Endpoints / Databases", href="#endpoints-chart-title")),
html.Li(html.A("OS Charts", href="#graph-os-endpoint-breakdown-title")),
html.Li(html.A("Upload file", href="#refresh-div")),
],
),
html.Div(
className="burger",
children=[
html.Div(className="line1"),
html.Div(className="line2"),
html.Div(className="line3"),
],
),
],
),
html.Div(id="main-content", children=[
# html.Div(
# className="goto-top",
# children=[
# html.A("^", href="#first")
# ]
# ),
html.Div(id="compare-charts"),
dcc.Dropdown(
id='period-selector',
options=[
{'label': 'Monthly', 'value': 'M'},
{'label': 'Quarterly', 'value': 'Q'}
],
value='M'
),
html.Div(className='two-columns', children=[
html.Div([
dcc.Dropdown(
id='column-dropdown-1',
options=generate_column_options(data),
value=generate_column_options(data)[0]['value'] if generate_column_options(data) else None
),
dcc.Graph(id='graph-1'),
]),
html.Div([
dcc.Dropdown(
id='column-dropdown-2',
options=generate_column_options(data),
value=generate_column_options(data)[0]['value'] if generate_column_options(data) else None
),
dcc.Graph(id='graph-2')
]),
]),
html.Div([
html.Div(id='toggle-output', className='toggle-output'),
daq.ToggleSwitch(
id='toggle-switch',
value=True,
label=["VALUES", "PERCENTAGES"]
)
]),
html.Div(className='two-columns', children=[
html.Div([
html.H2("Breakdown of OS Endpoints", id='graph-os-endpoint-breakdown-title'),
dcc.Checklist(
id="breakdown-checklist",
options=[{'label': key, 'value': value} for key, value in checkbox_os_breakdown.items()],
value=os_breakdown.axes[0],
inline=True,
labelStyle={'font-size': self.font_size, 'font-family': self.font_family, 'margin': '10px'}
),
self.bar_chart(
pd.DataFrame({
'OS': os_labels_breakdown, 'Endpoints': os_breakdown
}),
"", True, 'OS', 'Endpoints',
{'index': 'OS', 'y': 'Endpoints'},
os_endpoint_breakdown_tick_vals, os_endpoint_breakdown_tick_labels,
layout_hoverlabel=dict(
bgcolor="white",
font_size=self.font_size,
font_family=self.font_family,
font_color="black",
bordercolor="black",
),
layout_legend=dict(
orientation="h",
yanchor="bottom",
y=1.02,
xanchor="right",
x=1,
),
_text_auto=False,
html_id='graph-os-endpoint-breakdown'
),
]),
html.Div([
html.H2("Average number of endpoints per OS", id='graph-endpoints-per-os-avg-title'),
dcc.Checklist(
id="average-checklist",
options=[{'label': key, 'value': value} for key, value in checkbox_os_avg.items()],
value=os_avgs.axes[0],
inline=True,
labelStyle={'font-size': self.font_size, 'font-family': self.font_family, 'margin': '10px'}
),
self.bar_chart(
pd.DataFrame({
'OS': os_labels_avgs, 'Endpoints': os_avgs
}),
"", True, 'OS', 'Endpoints',
{'index': 'OS', 'y': 'Endpoints'},
endpoint_avg_per_customer_tick_vals, endpoint_avg_per_customer_tick_labels,
layout_hoverlabel=hoverlabel, layout_legend=legend, _text_auto='.3f',
html_id='graph-endpoints-per-os-avg'
),
]),
]),
self.card_element,
self.data_table_element,
# html.Div(className='all-charts', children=[ html.Div([dcc.Graph(id=f'graph-{i}',
# figure=self.create_all_charts(data, column)) for i, column in enumerate(data.columns) if column !=
# 'data_collection_time']) ])
html.Br(),
html.Div(className='file-uploader', children=[
file_uploader,
html.Button('Load file', id='refresh-button', n_clicks=0,
style={
'width': '20%',
'height': '60px',
'lineHeight': '60px',
'borderWidth': '1px',
'borderStyle': 'dashed',
'borderRadius': '5px',
'textAlign': 'center',
'margin': '10px auto',
}),
html.Div(id='refresh-div')
])
])
], className="dashboard"
)
self.callbacks()
if return_to_self:
self.web_layout = layout
return html.Div()
return layout
def update_graph(self, selected_columns, graph_type: int = 0):
data = self.import_data()
if graph_type == 0:
os_breakdown, _ = self.get_os_breakdown(data)
filtered_data = os_breakdown[selected_columns]
filtered_data_labels = [
col.replace('endpoints_os_', '').replace('_', ' ').capitalize().replace('Ibm', 'IBM').replace('Hpux',
'HP-UX').replace(
'sparc', 'Sparc') for col in filtered_data.axes[0]]
dt = pd.DataFrame({
'OS': filtered_data_labels,
'Endpoints': filtered_data})
tick_values, tick_labels = self.tick_vals(min(os_breakdown[os_breakdown > 0]), max(os_breakdown))
else:
os_breakdown, _ = self.get_endpoints_per_os(data)
filtered_data = os_breakdown[selected_columns]
filtered_data_labels = [
col.replace('endpoints_os_', '').replace('_', ' ').capitalize().replace('Ibm', 'IBM').replace('Hpux',
'HP-UX').replace(
'sparc', 'Sparc') for col in filtered_data.axes[0]]
dt = pd.DataFrame({
'OS': filtered_data_labels,
'Endpoints': filtered_data})
tick_values, tick_labels = self.tick_vals(min(os_breakdown[os_breakdown > 0]), max(os_breakdown))
fig = px.bar(dt, x='OS', y='Endpoints')
fig.update_layout(
hoverlabel=dict(
bgcolor="white",
font_size=self.font_size,
font_family=self.font_family,
font_color="black",
bordercolor="black",
),
legend=dict(
orientation="h",
yanchor="bottom",
y=1.02,
xanchor="right",
x=1,
),
font=dict(
family="IBM Plex Sans",
size=24,
color="black"
),
hovermode='x',
yaxis_type='log',
height=600,
)
fig.update_traces(hoverinfo='y', hovertemplate='<b>%{y:}</b><extra></extra>')
fig.update_yaxes(tickvals=tick_values, ticktext=tick_labels)
return fig
def update_title(self, hdata):
data = self.import_data()
os_breakdown, _ = self.get_os_breakdown(data)
# print(os_breakdown.index)
os_breakdown.index = os_breakdown.index.str.replace('endpoints_os_', '').str.replace('_',
' ').str.capitalize().str.replace(
'Ibm', 'IBM').str.replace('Hpux', 'HP-UX').str.replace('sparc', 'Sparc')
if hdata is not None:
# print(hdata)
percentage = str(round(100 * os_breakdown[hdata['points'][0]['x']] / os_breakdown.sum(), 2)) + '%'
return 'Breakdown of OS Endpoints - ' + hdata['points'][0]['x'].replace('endpoints_os_', '').replace('_',
' ').capitalize().replace(
'Ibm', 'IBM').replace('Hp-ux', 'HP-UX').replace('sparc', 'Sparc') + ": " + percentage
else:
return 'Breakdown of OS Endpoints'
def create_line_chart(self, df, selected_column, period_type):
filtered = df.copy(deep=True)
# fixes bug with loading new data
filtered['data_collection_time'] = pd.to_datetime(filtered['data_collection_time'])
if period_type == "M":
filtered["year_month"] = filtered["data_collection_time"].dt.to_period("M")
elif period_type == "Q":
filtered["year_month"] = filtered["data_collection_time"].dt.to_period("Q")
grouped = filtered.groupby("year_month")[selected_column].sum().reset_index()
grouped["year_month"] = grouped["year_month"].dt.to_timestamp()
fig = go.Figure(data=[go.Scatter(x=grouped['year_month'], y=grouped[selected_column])])
fig.update_traces(
hoverinfo='all',
hovertemplate='<b>%{y:}</b><extra></extra>')
fig.update_layout(
title="Chart - " + selected_column,
xaxis_title="Date",
yaxis_title=selected_column,
legend_title_text="",
height=600,
font=dict(
family="IBM Plex Sans",
size=18,
color="black"
),
hovermode="x",
hoverlabel=dict(
bgcolor="white",
font_size=18,
font_family="IBM Plex Sans",