homecontrol-dash: default to last 7 days of values, some cleanup

This commit is contained in:
Konstantin Koslowski 2020-02-10 15:46:41 +01:00
parent 5587ac45f6
commit bb21cd3b80

View file

@ -6,6 +6,7 @@ import dash_html_components as html
from dash.dependencies import Input, Output
import time
import requests
from math import log, log10
URL_BASE="http://innocence:5000"
@ -26,6 +27,7 @@ def get_values(sensorId, min_ts, max_ts, limit):
try:
url = "%s/sensor/get_values/%s?min_ts=%s&max_ts=%s&limit=%s" % (URL_BASE,
sensorId, min_ts, max_ts, limit)
# print(url)
res = requests.get(url)
ret = res.json()[sensorId]
except Exception as ex:
@ -91,56 +93,79 @@ app.layout = html.Div(children=[
]),
])
@app.callback(
[Output('slider-min', 'min'), Output('slider-min', 'value'),
Output('slider-max', 'min')],
Output('slider-min', 'max')],
[Input('tabs-select-sensor', 'value')])
def update_slider_min(sensorId):
res = get_values(sensorId, 0, int(time.time()), 1)
min_ts = 0
# default last 7 days
cur_ts = round(time.time() - (60*60*24*7))
max_ts = int(time.time())
sensorType = None
if "values" in res:
min_ts = int(res["values"][0]["ts"])
sensorType = res["sensorType"]
return min_ts, min_ts, min_ts
@app.callback(
Output('slider-limit-txt', 'children'),
[Input('slider-limit', 'value')])
def update_slider_limit(value):
return "Limit: %s" % (value if value > 0 else None)
# print("min: [%f [%f] %f]" % (min_ts, min_ts, max_ts))
return min_ts, cur_ts, max_ts
@app.callback(
Output('slider-min-txt', 'children'),
[Input('slider-min', 'value')])
def update_slider_min(value):
def update_slider_min_txt(value):
return "From: %s" % (time.strftime("%Y/%m/%d %H:%M", time.localtime(float(value))) if
value > 0 else 0)
@app.callback(
[Output('slider-max', 'min'), Output('slider-max', 'value'),
Output('slider-max', 'max')],
[Input('tabs-select-sensor', 'value')])
def update_slider_max(sensorId):
res = get_values(sensorId, 0, int(time.time()), 1)
min_ts = 0
max_ts = int(time.time())
sensorType = None
if "values" in res:
min_ts = int(res["values"][0]["ts"])
sensorType = res["sensorType"]
# print("max: [%f [%f] %f]" % (min_ts, max_ts, max_ts))
return min_ts, max_ts, max_ts
@app.callback(
Output('slider-max-txt', 'children'),
[Input('slider-max', 'value')])
def update_slider_max(value):
def update_slider_max_txt(value):
return "To: %s" % (time.strftime("%Y/%m/%d %H:%M", time.localtime(float(value))))
@app.callback(
[Output('graph-sensor-values', 'figure'), Output('slider-min', 'max'),
Output('slider-max', 'max')],
Output('slider-limit-txt', 'children'),
[Input('slider-limit', 'value')])
def update_slider_limit_txt(value):
return "Limit: %s" % (value if value > 0 else None)
@app.callback(
Output('graph-sensor-values', 'figure'),
[Input('tabs-select-sensor', 'value'), Input('slider-min', 'value'),
Input('slider-max', 'value'), Input('slider-limit', 'value')])
def update_graph_sensor_values(sensorId, min_ts, max_ts, limit):
res = get_values(sensorId, min_ts, max_ts, limit)
res = {}
if min_ts > 0:
res = get_values(sensorId, min_ts, max_ts, limit)
if "values" in res:
v = res["values"]
s = res["sensorType"]
x = [time.strftime("%m%d-%H%M", time.localtime(float(v[i]["ts"]))) for i in range(len(v))]
y = [v[i]["value"] for i in range(len(v))]
if s == "luminance":
y = [log10(v[i]["value"]/5+1) for i in range(len(v))]
else:
y = [v[i]["value"] for i in range(len(v))]
else:
s = sensorId
x = [0]
@ -148,7 +173,7 @@ def update_graph_sensor_values(sensorId, min_ts, max_ts, limit):
return {
'data': [ {'x': x, 'y': y, 'mode': 'line', 'name': "%s" % (s)} ],
'layout': { 'title': 'Data for sensor: %s (%d elements)' % (s, len(x)) }
}, time.time(), time.time()
}
if __name__ == '__main__':