-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
422 lines (368 loc) · 12.7 KB
/
app.py
File metadata and controls
422 lines (368 loc) · 12.7 KB
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
import dash
import dash_core_components as dcc
import dash_bootstrap_components as dbc
import dash_html_components as html
import plotly.graph_objs as go
from plotly.subplots import make_subplots
from scipy import signal
import pandas as pd
import base64
from dash.dependencies import Input, Output
from utils import round_down_to_odd, moving_average, json_to_df, random_color, sav_filter
# DATA ACQUISITION GOES HERE
from dash.exceptions import PreventUpdate
import query_service
external_stylesheets = [
{
"href": "https://fonts.googleapis.com/css2?"
"family=Lato:wght@400;700&display=swap",
"rel": "stylesheet",
},
dbc.themes.BOOTSTRAP
]
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
server = app.server
app.title = "Cryptomining Statistics"
encoded_logo = base64.b64encode(open("assets/logo.svg", 'rb').read())
user_dropdown = dcc.Dropdown(
id='user_dropdown',
options=query_service.get_users(),
value=None,
className='dropdown'
)
miner_dropdown = dcc.Dropdown(
id='miner_dropdown',
value=None,
className='dropdown'
)
tz_dropdown = dcc.Dropdown(
id='tz_dropdown',
options=[
{
'label': 'Eastern Time',
'value': 'US/Eastern'
},
{
'label': 'Pacific Time',
'value': 'US/Pacific'
},
{
'label': 'Central Time',
'value': 'US/Central'
}
],
value='US/Eastern',
className='dropdown'
)
stats_type_dropdown = dcc.Dropdown(
id='stats_type_dropdown',
options=[
{
'label': 'Temperature',
'value': 'temperature'
},
{
'label': 'Hashrate',
'value': 'hashrate'
},
{
'label': 'Fan Speed',
'value': 'fan_speed'
},
{
'label': 'Power',
'value': 'power'
},
{
'label': 'Core clock',
'value': 'core_clock'
},
{
'label': 'Memory clock',
'value': 'mem_clock'
},
],
value='temperature',
className='dropdown'
)
combined_graph = html.Div(
id='combined_graph',
style={'padding': 10}
)
graphs = html.Div(
id='graphs',
style={'padding': 10}
)
app.layout = html.Div(
children=[
html.Div(
children=[
html.Img(src=f"data:image/svg+xml;base64,{encoded_logo.decode()}", className="header-logo",
style={'padding': 10}),
html.H1(children="HashDash", className="header-title"),
html.P(
children="24/7 statistics of your fully managed cryptominer",
className="header-description",
),
],
className="header",
),
dbc.Row(
[
dbc.Col(html.Div(tz_dropdown), width='auto'),
dcc.Loading(dbc.Col(html.Div(user_dropdown), width='auto'), type='circle', color="#8a51ffff"),
dcc.Loading(dbc.Col(html.Div(miner_dropdown), width='auto'), type='circle', color="#8a51ffff"),
dbc.Col(html.Div(stats_type_dropdown), width='auto'),
],
justify='center'
),
dbc.Row(
dbc.Col([
dcc.Loading([
dbc.Col(dcc.Store(id="miner_shares_data"), width='auto'),
dbc.Col(dcc.Store(id="miner_healths_data"), width='auto'),
],
type="default",
color="#8a51ffff"
)],
className="mt-4"
)
),
dbc.Row(
dbc.Col(
dcc.Loading(id="combined_graph_spinner",
children=[combined_graph],
type="default",
color="#8a51ffff"),
className="mt-4"
),
justify='center'
),
dbc.Row(
dbc.Col(
dcc.Loading(id="graphs_spinner",
children=[graphs],
type="default",
color="#8a51ffff"),
className="mt-4"
),
justify='center'
)
]
)
@app.callback(
Output('miner_dropdown', 'options'),
[Input('user_dropdown', 'value')])
def update_miners_dropdown(user_id):
if not user_id:
raise PreventUpdate
miners = query_service.get_miners(user_id)
return miners
@app.callback(
Output('miner_shares_data', 'data'),
[Input('miner_dropdown', 'value')])
def update_miner_shares(miner_id):
if not miner_id:
raise PreventUpdate
shares_frame = query_service.get_miner_shares(miner_id)
if shares_frame.empty:
return html.Div(
dbc.Alert("No share data to display for the selected timeframe", color='danger')
)
return shares_frame.to_json(orient='records', date_format='iso')
@app.callback(
Output('miner_healths_data', 'data'),
[Input('miner_dropdown', 'value')])
def update_miner_healths(miner_id):
if not miner_id:
raise PreventUpdate
healths_frame = query_service.get_miner_healths(miner_id)
if healths_frame.empty:
return html.Div(
dbc.Alert("No health data to display for the selected timeframe", color='danger')
)
return healths_frame.to_json(orient='records', date_format='iso')
@app.callback(
Output('graphs', 'children'),
[Input('miner_shares_data', 'data'),
Input('miner_healths_data', 'data'),
Input('tz_dropdown', 'value')])
def update_shares_graph(shares_data, healths_data, timezone):
if not shares_data or not healths_data:
raise PreventUpdate
healths_frame = json_to_df(healths_data, timezone)
shares_frame = json_to_df(shares_data, timezone)
# get the total valid shares per time period
valid_sum = shares_frame.drop(columns=['duration', 'gpu_no']) \
.groupby('start')['valid'] \
.sum() \
.reset_index(name='total_valid')
window_length = shares_frame.groupby('start').ngroups
valid = go.Bar(x=valid_sum['start'], y=valid_sum['total_valid'], name='Valid shares',
marker={'color': 'mediumpurple'})
valid_avg_ys = sav_filter(valid_sum['total_valid'], window_length)
valid_smoothed_line = go.Line(x=valid_sum['start'],
y=valid_avg_ys,
name='Avg valid shares',
line=dict(color="57CC99", width=2.5, shape='spline', smoothing=10))
invalid_sum = shares_frame.drop(columns=['duration', 'gpu_no']) \
.groupby('start')['invalid'] \
.sum() \
.reset_index(name='total_invalid')
invalid = go.Bar(x=invalid_sum['start'], y=invalid_sum['total_invalid'], name='Invalid shares',
marker={'color': 'indianred'})
invalid_avg_ys = sav_filter(invalid_sum['total_invalid'], window_length)
invalid_smoothed_line = go.Line(x=invalid_sum['start'],
y=invalid_avg_ys,
name='Avg invalid shares',
line=dict(color="orange", width=2.5, shape='spline', smoothing=10))
# create graphs for each gpu showing their invalid vs valid percent
gpu_graphs = []
merged = pd.merge(shares_frame, healths_frame, on=["start", "gpu_no"])
for gpu_no, data in merged.groupby('gpu_no'):
gpu_graphs.append(make_gpu_shares_graph(gpu_no, data))
return html.Div(
children=[
dcc.Graph(
id="shares",
figure={
'data': [valid, valid_smoothed_line, invalid, invalid_smoothed_line],
'layout':
go.Layout(title='Valid/invalid shares past 12 hours', barmode='stack')
}),
*gpu_graphs
],
className="card"
)
@app.callback(
Output('combined_graph', 'children'),
[Input('miner_healths_data', 'data'),
Input('stats_type_dropdown', 'value'),
Input('tz_dropdown', 'value')])
def update_combined_graph(healths_data, stat, timezone):
if not healths_data:
raise PreventUpdate
healths_frame = json_to_df(healths_data, timezone)
if stat == 'power':
return make_power_graph(healths_frame)
window_length = healths_frame.groupby('start').ngroups + 2
fig = make_subplots(rows=1, cols=1)
for gpu_no, data in healths_frame.groupby('gpu_no'):
# calculate a moving average of the y values to smooth them out
avg_ys = moving_average(data[stat], int(window_length / 10))
fig.add_trace(
go.Scatter(x=data['start'], y=avg_ys,
name=data['gpu_name'].iloc[0],
mode='lines'
),
row=1,
col=1,
)
if stat == 'temperature':
fig.update_yaxes(title=dict(text='Temperature (°C)'), hoverformat='.0f')
elif stat == 'fan_speed':
fig.update_yaxes(title=dict(text='Fan speed (%)'), tickformat='%f')
elif stat == 'hashrate':
fig.update_yaxes(title=dict(text='Hashrate (MH/s)'), tickformat='.2f')
elif stat == 'mem_clock' or stat == 'core_clock':
fig.update_yaxes(title=dict(text='Clock (Mhz)'))
fig.update_xaxes(title=dict(text='Time'))
return dcc.Graph(id=f'combined', figure=fig)
def make_power_graph(data):
fig = make_subplots(rows=1, cols=1)
window_length = max(int(data.groupby('start').ngroups / 100), 1)
total_gpus = data['gpu_no'].max()
for gpu_no, data in data.groupby('gpu_no'):
print(data['power_draw'])
# generate a color for the pair of power use/limit for this gpu
color = random_color()
# calculate a moving average of the y values to smooth them out
avg_power_draw = moving_average(data['power_draw'], window_length)
gpu_name = data['gpu_name'].iloc[0]
fig.add_trace(
go.Scatter(x=data['start'], y=avg_power_draw,
name=f'{gpu_name} Power used',
mode='lines',
legendgroup=f'gpu_{gpu_no}',
line=dict(color=color)
),
row=1,
col=1,
)
fig.add_trace(
go.Scatter(x=data['start'], y=data['power_limit'],
name=f'{gpu_name} Power limit',
mode='lines',
legendgroup=f'gpu_{gpu_no}',
line=dict(dash='dot', color=color),
),
row=1,
col=1,
)
fig.update_yaxes(title=dict(text='Power in watts'), hoverformat='.0f')
fig.update_xaxes(title=dict(text='Time'))
return dcc.Graph(id=f'combined', figure=fig)
def make_gpu_shares_graph(gpu_no, data):
total_shares = data['valid'].sum() + data['invalid'].sum()
valid_pct = data['valid'].sum() / total_shares
invalid_pct = data['invalid'].sum() / total_shares
# Create figure with secondary y-axis
fig = make_subplots(rows=2, cols=1)
# Add traces
fig.add_trace(
go.Bar(x=[valid_pct, invalid_pct], y=['Valid', 'Invalid'], orientation='h', name='Shares distribution'),
row=1,
col=1
)
fig.add_trace(
go.Scatter(x=data['start'], y=data['fan_speed'],
name='Fan speed',
mode='lines'
),
row=2,
col=1,
)
fig.add_trace(
go.Scatter(x=data['start'], y=data['temperature'],
name='Temperature',
mode='lines'
),
row=2,
col=1,
)
fig.add_trace(
go.Scatter(x=data['start'], y=data['hashrate'],
name='Hashrate',
mode='lines'
),
row=2,
col=1,
)
fig.add_trace(
go.Scatter(x=data['start'], y=data['power_draw'],
name='Power draw',
mode='lines',
line=dict(color='MediumVioletRed', dash='dot')
),
row=2,
col=1,
)
fig.add_trace(
go.Scatter(x=data['start'], y=data['power_limit'],
name='Power limit',
mode='lines',
line=dict(color='MediumVioletRed')
),
row=2,
col=1,
)
# Add figure title
fig.update_layout(
title_text=f'Share status for GPU {gpu_no}',
)
# Set x-axis title
fig.update_xaxes(title_text="Time", row=2, col=1)
fig.update_xaxes(row=1, col=1, tickformat='.2%')
return dcc.Graph(id=f'gpu{gpu_no}', figure=fig)
if __name__ == "__main__":
app.run_server(debug=True)