-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
193 lines (173 loc) · 5.55 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
import os
import base64
import pickle
import optparse
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import dash_table
import pandas as pd
import distributed
import dask
import dask.dot
import dask_log_server
parser = optparse.OptionParser()
options, args = parser.parse_args()
log_path = args[0]
external_stylesheets = ["https://codepen.io/chriddyp/pen/bWLwgP.css"]
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
df = pd.read_parquet(os.path.join(log_path, "graphs.parquet"))
df_tasks = pd.read_parquet(os.path.join(log_path, "tasks.parquet"))
options = [
{"label": item, "value": item}
for item in ["worker", "duration", "nbytes", "typename"]
]
drop_cols = ["status", "thread", "type", "datetime", "client_id", "id"]
task_columns = [
{"name": item, "id": item}
for item in [
"action",
"start",
"stop",
"worker",
"status",
"nbytes",
"thread",
"type",
"typename",
"key",
"datetime",
"client_id",
"id",
"duration",
]
if item not in drop_cols
]
graph_cols = df.columns
graph_col_options = [{"label": item, "value": item} for item in graph_cols]
app.layout = html.Div(
[
html.Div(
dcc.Dropdown("graph-columns", options=graph_col_options, multi=True, value=['start', 'stop', 'duration', 'n_workers', 'n_tasks', 'client_id'])
),
html.Div(
[
dash_table.DataTable(
id="graph-table",
columns=[{"name": i, "id": i} for i in df.columns],
data=df.to_dict("records"),
editable=True,
filter_action="native",
sort_action="native",
sort_mode="multi",
column_selectable=False,
row_selectable="single",
row_deletable=False,
selected_columns=[],
selected_rows=[],
page_action="native",
page_current=0,
page_size=10,
),
]
),
html.Div(
[
html.Div(
[
html.Label(
["Node Color", dcc.Dropdown(id="color", options=options,)]
),
],
className="three columns",
),
html.Div(
[
html.Label(
["Node Label", dcc.Dropdown(id="label", options=options,)]
),
],
className="three columns",
),
],
className="row",
),
html.Div([html.Img(id="graph", height=300),], className="row"),
html.Div(
[
dash_table.DataTable(
id="tasks-table",
columns=task_columns,
editable=True,
filter_action="native",
sort_action="native",
sort_mode="multi",
column_selectable=False,
row_selectable=False,
row_deletable=False,
selected_columns=[],
selected_rows=[],
page_action="native",
page_current=0,
page_size=10,
)
],
className="row",
),
]
)
@app.callback(
[
Output(component_id="graph-table", component_property="columns"),
Output(component_id="graph-table", component_property="data"),
],
[
Input(component_id="graph-columns", component_property="value"),
],
)
def update_graph_table(column_names):
return [{"name": i, "id": i} for i in column_names], df[column_names].to_dict("records")
@app.callback(
Output(component_id="graph", component_property="src"),
[
Input(component_id="graph-table", component_property="selected_rows"),
Input(component_id="color", component_property="value"),
Input(component_id="label", component_property="value"),
],
)
def load_graph(selected_rows, color, label):
if not selected_rows:
return ""
if color is None:
color = ""
if label is None:
label = ""
graph_id = df.iloc[selected_rows].index[0]
filename = os.path.join(log_path, "graph_" + graph_id + ".dsk")
with open(filename, "rb") as file:
dsk = distributed.protocol.deserialize(*pickle.load(file))
attributes = dask_log_server._get_dsk_attributes(
dsk, df_tasks, label=label, color=color,
)
try:
data = dask.dot.to_graphviz(
dsk, data_attributes=attributes["data"], function_attributes=attributes["func"]
).pipe(format="png")
except RuntimeError:
data = b""
encoded_image = base64.b64encode(data).decode()
return "data:image/png;base64,{}".format(encoded_image)
@app.callback(
Output(component_id="tasks-table", component_property="data"),
[Input(component_id="graph-table", component_property="selected_rows")],
)
def tasks_of_graph(selected_rows):
if not selected_rows:
return list()
graph_id = df.iloc[selected_rows].index[0]
return (
df_tasks[df_tasks["id"] == graph_id].drop(columns=drop_cols).to_dict("records")
)
if __name__ == "__main__":
app.run_server(debug=True)