-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.py
89 lines (67 loc) · 2.93 KB
/
data.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
from sqlalchemy import create_engine, text
from datetime import datetime, timedelta
from pandas import DataFrame, read_sql_query, Series
from flask import request
import numpy
DB_CONNECTION_STRING = "postgresql://worker:v2_3z7vi_p2JBRtSyvrpQzEUzgcBUdvb@db.bit.io:5432/imdavidmin/grozd"
engine = create_engine(DB_CONNECTION_STRING, pool_pre_ping=True)
max_records = 10000
def getQuery(args):
dfrom = args.get('from')
dto = args.get('to')
interval = args.get('interval')
timestamp_range = f'''timestamp > \'{datetime.fromtimestamp(int(dfrom)/1000)}\'''' + \
(f'''AND timestamp < \'{datetime.fromtimestamp(int(dto)/1000)}\'''' if dto else '')
with engine.connect() as db:
query = f''' SELECT timestamp, base FROM pricing WHERE {timestamp_range} LIMIT {max_records}'''
df: DataFrame = read_sql_query(text(query), db)
df.set_index(df.columns[0], inplace=True)
rtn_df = df.resample(interval).first().fillna(0).astype(int) if interval else df
rtn_df['ts'] = rtn_df.index.to_series().astype(int).floordiv(1000000).astype(int)
return {
"msg": rtn_df.T.to_dict(orient='split').get('data'),
"code": 200
}
def getDashboardData():
lookback_hours = 6
with engine.connect() as db:
query = f'''
SELECT * FROM pricing WHERE timestamp>'{datetime.now()- timedelta(hours=lookback_hours)}' LIMIT {max_records}
'''
print(f'''Sending query as follows{query}''')
df = read_sql_query(text(query), db)
# Get latest prices into "now"
now = {}
df.sort_values(by='timestamp', ascending=False)
for col in ['base', 'fast', 'instant', 'standard']:
now[col] = int(df.at[0, col])
# Get time series for base
base = df[['base', 'timestamp']].astype({'timestamp': "int64"})
base['timestamp'] = base['timestamp'].floordiv(1000000).astype(int)
return {
"avg": getAvgs(df),
"now": now,
"base": base.transpose().to_dict(orient="split").get('data')
}
# Calculate the average for each hour on each day
def getAvgs(df: DataFrame):
avgs = {"base": [], "fast": [], "instant": [], "standard": []}
def getMean(series: Series):
m = round(series.mean(), 2)
return 0 if numpy.isnan(m) else m
for d in range(6):
base, fast, instant, standard = [], [], [], []
for h in range(23):
filtered = df[
(df['timestamp'].dt.day_of_week == d) &
(df['timestamp'].dt.hour == h)
]
base.append(getMean(filtered['base']))
fast.append(getMean(filtered['fast']))
instant.append(getMean(filtered['instant']))
standard.append(getMean(filtered['standard']))
avgs['base'].append(base)
avgs['fast'].append(fast)
avgs['instant'].append(instant)
avgs['standard'].append(standard)
return avgs