-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
274 lines (223 loc) · 8.32 KB
/
app.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
import os
import logging
import dash_bootstrap_components as dbc
from dash import Dash, dcc, html, Input, Output, State, callback
import pandas as pd
import load_data as data_loader
import graph
'''
Requirements
Save the following inside a directory (default = "./confs")
-----------
influxdb.conf
org = <org name>
host = <host url>
database = <bucket name>
language = <for v3, it should be sql>
token = <influxDB token>
sites.csv: site, lon, lat (for all sites)
slice.csv: site, ip_address, node_name
'''
logger = logging.getLogger()
# Setting the threshold of logger to DEBUG
logger.setLevel(logging.DEBUG)
############ Config data paths and InfluxDB Version ##############
# InfluxDB version
influxdb_ver = 'v2' # local ('v2') or cloud ('v3')
# Set configs location
#conf_files = './data_cloud'
conf_files = './data_local'
db_conf_path = os.path.join(conf_files, 'influxdb.conf')
sites_f_path = os.path.join(conf_files, 'sites.csv')
slice_f_path = os.path.join(conf_files, 'slice.csv')
# Create one Dataframe with all the geo-location information
sites_df = data_loader.get_geoloc_df(sites_f_path, slice_f_path)
logger.debug(sites_df)
############ Layout #################
app = Dash(external_stylesheets=[dbc.themes.BOOTSTRAP])
# TODO: Move the style to CSS
controls = dbc.Card(
[
html.Div(
[
dbc.Label("Node 1"),
dbc.Select(
id="src-node",
options=[
{"label": i, "value": i} for i in sites_df['site'].tolist()
],
#value="STAR",
),
],
style={'marginBottom': 2,
'marginTop': 20,
'marginLeft': 5,
'marginRight':5},
),
html.Div(
[
dbc.Label("Node 2"),
dbc.Select(
id="dst-node",
options=[
{"label": i, "value": i} for i in sites_df['site'].tolist()
],
#value="STAR",
),
],
style={'marginBottom': 2,
'marginTop': 20,
'marginLeft': 5,
'marginRight':5}
),
html.Div(
[
dbc.Label("Duration"),
dbc.Select(
id="duration2",
options=[
{"label": i, "value": i} for i in \
['5 minutes', '15 minutes', '30 minutes', \
'1 hour', '3 hours', '6 hours', '12 hours', '24 hours']
],
value="5 minutes",
),
],
style={'marginBottom': 2,
'marginTop': 20,
'marginLeft': 5,
'marginRight':5}
),
html.Div(
[
dbc.Button("Submit", id='submit-button-state',
n_clicks=0, outline=True, color="primary", className="me-md-2"),
dbc.Button("Download CSV", id='btn-csv',
n_clicks=0, outline=True, color="primary", className="me-md-2"),
dcc.Download(id="download-csv"),
],
style={'marginBottom': 25,
'marginTop': 20,
'marginLeft': 10}
),
#html.Div(
# [
# dbc.Button("Download CSV", id='btn-csv',
# n_clicks=0, outline=True, color="primary"),
# dcc.Download(id="download-csv"),
# ],
# style={'marginBottom': 25,
# 'marginTop': 20,
# 'marginLeft': 10}
#),
],
)
app.layout = dbc.Container(
[
html.H2("FABRIC Latency Monitor"),
html.Hr(),
dbc.Row(
[
dbc.Col(dcc.Graph(id='map-fig'), lg=8),
dbc.Col(controls, lg=4),
],
align="center",
),
dbc.Row(dcc.Graph(id='single-latency-fwd')),
dbc.Row(dcc.Graph(id='single-latency-rev')),
],
fluid=True,
)
########## Callbacks #########
@callback(
Output('single-latency-fwd', 'figure'),
Output('single-latency-rev', 'figure'),
Output('map-fig', 'figure'),
Input('submit-button-state', 'n_clicks'),
State('src-node', 'value'),
State('dst-node', 'value'),
State('duration2', 'value'), prevent_initial_call=True)
def update_figure(n, src, dst, duration):
'''
Returns 3 graph figures
'''
#### Line graphs #####
src_ip = sites_df.loc[sites_df['site'].str.contains(src), 'ip_address'].item()
dst_ip = sites_df.loc[sites_df['site'].str.contains(dst), 'ip_address'].item()
# debugging
logger.debug(src_ip, dst_ip)
# Forward graph data
if influxdb_ver == 'v3':
latency_fwd = data_loader.download_influx_data(
conf_path=db_conf_path,
duration=duration,
outfile=None,
src_dst=(src_ip, dst_ip))
elif influxdb_ver == 'v2':
latency_fwd = data_loader.download_influx_data_local(
conf_path=db_conf_path,
duration=duration,
outfile=None,
src_dst=(src_ip, dst_ip))
logger.debug(latency_fwd)
line_fig_fwd = graph.generate_line_graph(sites_df, src, dst, latency_fwd)
# Reverse graph data
if influxdb_ver == 'v3':
latency_rev = data_loader.download_influx_data(
conf_path=db_conf_path,
duration=duration,
outfile=None,
src_dst=(dst_ip, src_ip))
elif influxdb_ver == 'v2':
latency_rev = data_loader.download_influx_data_local(
conf_path=db_conf_path,
duration=duration,
outfile=None,
src_dst=(dst_ip, src_ip))
logger.debug(latency_rev)
line_fig_rev = graph.generate_line_graph(sites_df, dst, src, latency_rev)
##### Map graph ######
map_fig = graph.generate_map(sites_df, src, dst)
return line_fig_fwd, line_fig_rev, map_fig
@callback(
Output("download-csv", "data"),
Input("btn-csv", "n_clicks"),
State('src-node', 'value'),
State('dst-node', 'value'),
State('duration2', 'value'), prevent_initial_call=True)
def download_fwd_data(n_clicks, src, dst, duration):
src_ip = sites_df.loc[sites_df['site'].str.contains(src), 'ip_address'].item()
dst_ip = sites_df.loc[sites_df['site'].str.contains(dst), 'ip_address'].item()
# Forward graph data
if influxdb_ver == 'v3':
latency_fwd = data_loader.download_influx_data(
conf_path=db_conf_path,
duration=duration,
outfile=None,
src_dst=(src_ip, dst_ip))
elif influxdb_ver == 'v2':
latency_fwd = data_loader.download_influx_data_local(
conf_path=db_conf_path,
duration=duration,
outfile=None,
src_dst=(src_ip, dst_ip))
# Reverse graph data
if influxdb_ver == 'v3':
latency_rev = data_loader.download_influx_data(
conf_path=db_conf_path,
duration=duration,
outfile=None,
src_dst=(dst_ip, src_ip))
elif influxdb_ver == 'v2':
latency_rev = data_loader.download_influx_data_local(
conf_path=db_conf_path,
duration=duration,
outfile=None,
src_dst=(dst_ip, src_ip))
latency_df = pd.concat([latency_fwd, latency_rev], ignore_index=True)
logger.debug(latency_df)
return dcc.send_data_frame(latency_df.to_csv, "latency.csv", index=False)
if __name__ == "__main__":
app.run_server(debug=True, use_reloader=False)
# If running on a remote node
#app.run_server(host='0.0.0.0',debug=False, use_reloader=False)