-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgraph.py
executable file
·352 lines (322 loc) · 11.2 KB
/
graph.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
#!/usr/bin/python3
""" graph.py - display information as a graph
v0.0.5 - 2022-05-23 - nelbren@nelbren.com"""
import os
from pathlib import Path
import datetime
import sqlite3
import dash
import dash_bootstrap_components as dbc
from dash import dcc, html, dash_table
from dash.dependencies import Input, Output
import pandas as pd
HOME = str(Path.home())
PWD = os.path.dirname(os.path.realpath(__file__))
PWD_DIR = os.path.basename(PWD)
BASE = f"{HOME}/.{PWD_DIR}.db"
TS_FMT = "%Y-%m-%d %H:%M:%S"
conn = sqlite3.connect(BASE, check_same_thread=False)
c = conn.cursor()
DATAFRAME = None
EACH_HOURS = 4
def get_new_data():
"""Updates the global variable 'DATAFRAME' with new data"""
# print(datetime.datetime.now(), "get_new_data - begin")
dataframe = pd.read_sql("SELECT * FROM unpaid", conn)
dataframe = dataframe[["source", "currency", "timestamp", "usd", "value"]]
dataframe.head(1)
# print(datetime.datetime.now(), "get_new_data - end")
return dataframe
def get_timestamp():
"""Get timestamp"""
timestamp = f"{datetime.datetime.now()}"
timestamp_obj = datetime.datetime.strptime(timestamp, TS_FMT + ".%f")
timestamp = timestamp_obj.strftime(TS_FMT)
return "🕑" + timestamp
def make_layout():
"""Make Layout"""
title_style = {
"text-align": "center",
"font-weight": "700",
"line-height": "32px",
"font-size": "32px",
}
timestamp_style = {
"font-family": "'Console', sans-serif",
"line-height": "32px",
"font-size": "30px",
"font-weight": "bold",
"text-align": "center",
}
tabs_styles = {"height": "44px", "align-items": "center"}
tab_style = {
"borderBottom": "1px solid #d6d6d6",
"fontWeight": "bold",
"font-size": "24px",
"border-radius": "15px",
"background-color": "#F2F2F2",
"padding": "2px",
}
tab_selected_style = {
"borderTop": "1px solid #d6d6d6",
"borderBottom": "1px solid #d6d6d6",
"backgroundColor": "#119DFF",
"fontWeight": "bold",
"font-size": "24px",
"color": "white",
"padding": "2px",
"border-radius": "15px",
}
return html.Div(
[
html.H1("⛏️ Miner Preview 👀", style=title_style),
html.Div(
[
html.H5(
get_timestamp(),
style=timestamp_style,
id="live-update-text",
),
dcc.Interval(
id="interval-component",
interval=EACH_HOURS * 60 * 60 * 1000,
n_intervals=0,
),
]
), # , style=dict(display='flex') ),
dcc.Tabs(
id="tabs",
value="tab-2",
children=[
dcc.Tab(
label="Table",
value="tab-1",
style=tab_style,
selected_style=tab_selected_style,
),
dcc.Tab(
label="Graph",
value="tab-2",
style=tab_style,
selected_style=tab_selected_style,
),
],
style=tabs_styles,
),
html.Div(id="tabs-content", style={"height": "83vh"}),
],
style={"height": "100vh", "padding": "5px"},
)
app = dash.Dash(
__name__, external_stylesheets=[dbc.themes.BOOTSTRAP]
) # , server=server)
server = app.server
app.config.suppress_callback_exceptions = True
# get_new_data()
app.layout = make_layout
def tabla(dataframe):
"""tabla"""
return html.Div(
dash_table.DataTable(
id="table-sorting-filtering",
columns=[
{"name": i, "id": i, "deletable": True}
for i in dataframe.columns
],
style_table={"overflowX": "scroll"},
style_cell={
"height": "90",
# all three widths are needed
"minWidth": "140px",
"width": "140px",
"maxWidth": "140px",
"whiteSpace": "normal",
},
page_current=0,
page_size=23,
page_action="custom",
filter_action="custom",
filter_query="",
sort_action="custom",
sort_mode="multi",
sort_by=[],
)
)
def graph_all(dataframe):
"""graph_all"""
opts_styles = [
dict(
target="cryptoatcost_btc",
value=dict(marker=dict(color="darkgoldenrod")),
),
dict(
target="ethermine_eth", value=dict(marker=dict(color="darkcyan"))
),
dict(
target="cryptoatcost_usd",
value=dict(marker=dict(color="goldenrod")),
),
dict(target="ethermine_usd", value=dict(marker=dict(color="cyan"))),
]
return html.Div(
[
dcc.Graph(
id="graph_all",
style={"height": "80vh"},
config={"displayModeBar": "hover", "displaylogo": False},
figure={
"data": [
dict(
y=dataframe["usd"],
x=dataframe["timestamp"],
mode="lines+markers",
# opacity = 0.7,
marker={
"size": 8,
"line": {"width": 0.5, "color": "white"},
},
name="USD",
# stackgroup='one',
transforms=[
dict(
type="groupby",
groups=dataframe["source"] + "_usd",
styles=opts_styles,
),
],
),
dict(
y=dataframe["value"],
x=dataframe["timestamp"],
mode="lines+markers",
# opacity = 0.7,
marker={
"size": 8,
"line": {"width": 0.5, "color": "white"},
},
name="VALUE",
# stackgroup='one',
transforms=[
dict(
type="groupby",
groups=dataframe["source"]
+ "_"
+ dataframe["currency"],
styles=opts_styles,
),
],
),
],
"layout": dict(
xaxis={"title": "Timestamp"},
yaxis={"type": "log", "title": "USD"},
margin={"l": 40, "b": 40, "t": 10, "r": 10},
legend={"x": 0.99, "y": 0.01},
# legend={"yanchor": "bottom", "xanchor": "left"},
hovermode="closest",
),
},
)
]
)
@app.callback(
Output("live-update-text", "children"),
[Input("interval-component", "n_intervals")],
)
# pylint: disable=unused-argument
def update_text(n_intervals):
"""Update Text"""
return get_timestamp()
@app.callback(
Output("tabs-content", "children"),
[Input("tabs", "value")],
[Input("interval-component", "n_intervals")],
)
# pylint: disable=unused-argument
def render_content(tab, n_intervals):
"""Render Content"""
dataframe = get_new_data()
if tab == "tab-1":
return tabla(dataframe)
return graph_all(dataframe)
operators = [
["ge ", ">="],
["le ", "<="],
["lt ", "<"],
["gt ", ">"],
["ne ", "!="],
["eq ", "="],
["contains "],
["datestartswith "],
]
def split_filter_part(filter_part):
"""Split Filter Part"""
for operator_type in operators:
for operator in operator_type:
if operator in filter_part:
name_part, value_part = filter_part.split(operator, 1)
name = name_part[
name_part.find("{") + 1 : name_part.rfind("}")
]
value_part = value_part.strip()
value_part0 = value_part[0]
if value_part0 == value_part[-1] and value_part0 in (
"'",
'"',
"`",
):
value = value_part[1:-1].replace(
"\\" + value_part0, value_part0
)
else:
try:
value = float(value_part)
except ValueError:
value = value_part
# word operators need spaces after them in the filter string,
# but we don't want these later
return name, operator_type[0].strip(), value
return [None] * 3
@app.callback(
Output("table-sorting-filtering", "data"),
[
Input("table-sorting-filtering", "page_current"),
Input("table-sorting-filtering", "page_size"),
Input("table-sorting-filtering", "sort_by"),
Input("table-sorting-filtering", "filter_query"),
],
)
def update_table(page_current, page_size, sort_by, filter_query):
"""Update Table"""
dataframe = get_new_data()
filtering_expressions = filter_query.split(" && ")
# dataframe = DATAFRAME
for filter_part in filtering_expressions:
col_name, operator, filter_value = split_filter_part(filter_part)
if operator in ("eq", "ne", "lt", "le", "gt", "ge"):
# these operators match pandas series operator method names
dataframe = dataframe.loc[
getattr(dataframe[col_name], operator)(filter_value)
]
elif operator == "contains":
dataframe = dataframe.loc[
dataframe[col_name].str.contains(filter_value)
]
elif operator == "datestartswith":
# this is a simplification of the front-end filtering logic,
# only works with complete fields in standard format
dataframe = dataframe.loc[
dataframe[col_name].str.startswith(filter_value)
]
if len(sort_by):
dataframe = dataframe.sort_values(
[col["column_id"] for col in sort_by],
ascending=[col["direction"] == "asc" for col in sort_by],
inplace=False,
)
page = page_current
size = page_size
dataframe = dataframe.sort_values(by="timestamp", ascending=False)
return dataframe.iloc[page * size : (page + 1) * size].to_dict("records")
if __name__ == "__main__":
app.run_server(debug=True)