major improvements and rework
- add stream / instrument availability data - events contain event kind for dispatching db methods
This commit is contained in:
89
t.py
89
t.py
@ -1,30 +1,34 @@
|
||||
import time
|
||||
import math
|
||||
import numpy as np
|
||||
from influx import InfluxDBWrapper, NamedTuple, RegExp
|
||||
from influx import InfluxDBWrapper, RegExp
|
||||
|
||||
DAY = 24 * 3600
|
||||
|
||||
token = "zqDbTcMv9UizfdTj15Fx_6vBetkM5mXN56EE9CiDaFsh7O2FFWZ2X4VwAAmdyqZr3HbpIr5ixRju07-oQmxpXw=="
|
||||
# token = "zqDbTcMv9UizfdTj15Fx_6vBetkM5mXN56EE9CiDaFsh7O2FFWZ2X4VwAAmdyqZr3HbpIr5ixRju07-oQmxpXw=="
|
||||
|
||||
db = InfluxDBWrapper('http://pc16392:8086', token, 'linse', 'curve-test')
|
||||
db = InfluxDBWrapper('linse-c')
|
||||
|
||||
print("""
|
||||
qry([start], [stop], [interval=...,] [last=True,] [columns=[...],] [<tag>=<value>, ] ...)
|
||||
crv([start], [stop], [mod.par], ['float'], [interval=...,] [add_prev=False,] [add_end=True,] [<tag>=<value>, ] ...)
|
||||
""")
|
||||
|
||||
offset = (time.time() // 3600) * 3600
|
||||
now = int(time.time())
|
||||
offset = (now // 3600) * 3600
|
||||
result = {}
|
||||
maxcurves = 7
|
||||
maxpoints = 7
|
||||
|
||||
|
||||
def prt():
|
||||
for i, (key, curve) in enumerate(result.items()):
|
||||
if i > 5:
|
||||
if i > maxcurves:
|
||||
print('--- ...')
|
||||
break
|
||||
print('---', key, list(curve[0]._idx_by_name))
|
||||
print('---', key, curve.column_names, [f'{k}={v}' for k, v in curve.tags.items() if k not in curve.key_names])
|
||||
n = len(curve)
|
||||
if n > 7:
|
||||
if n > maxpoints:
|
||||
curves = [curve[:3], None, curve[-3:]]
|
||||
else:
|
||||
curves = [curve]
|
||||
@ -37,40 +41,69 @@ def prt():
|
||||
|
||||
|
||||
def qry(*args, **kwds):
|
||||
global result
|
||||
result = db.query(*args, **kwds)
|
||||
result.clear()
|
||||
result.update(db.query(*args, **kwds))
|
||||
print('PRINT')
|
||||
prt()
|
||||
|
||||
|
||||
def crv(*args, **kwds):
|
||||
global result
|
||||
result = db.curves(*args, **kwds)
|
||||
result.clear()
|
||||
res = db.curves(*args, **kwds)
|
||||
if isinstance(res, list):
|
||||
result[()] = res
|
||||
else:
|
||||
result.update(res)
|
||||
prt()
|
||||
|
||||
|
||||
def sry():
|
||||
global result
|
||||
res = db.query(-DAY * 365, interval=DAY, _field='float',
|
||||
def sry(prectime=False):
|
||||
interval = 3600
|
||||
res = db.query(-DAY * 365, interval=interval, _field='float',
|
||||
device=None, stream=None, _measurement=None)
|
||||
result = {} # dict (device, stream) of list of [start, end, set of params]
|
||||
by_day = {} # dict (device, stream) of list of [start, end, set of params]
|
||||
for key, table in res.items():
|
||||
assert table.key_names == ('device', 'stream', '_measurement')
|
||||
device, stream, param = key
|
||||
for row in table:
|
||||
start = row[0] - 3600
|
||||
result.setdefault((start, device, stream), set()).add(param)
|
||||
tm = time.localtime(row[0] - interval)
|
||||
day = time.mktime(tm[0:3] + (0, 0, 0, 0, 0, -1))
|
||||
key = (day, device, stream)
|
||||
info = by_day.get(key)
|
||||
start = row[0] - interval
|
||||
if info:
|
||||
info[0] = min(start, info[0])
|
||||
info[1] = max(row[0], info[1])
|
||||
else:
|
||||
info = [start, row[0], set()]
|
||||
by_day[key] = info
|
||||
info[2].add(param)
|
||||
prev_data = {}
|
||||
print('---')
|
||||
summary = []
|
||||
for (start, device, stream), pset in sorted(result.items()):
|
||||
for (day, device, stream), (start, end, pset) in sorted(by_day.items()):
|
||||
prev = prev_data.get((device, stream))
|
||||
if prev is None or start > prev[1]:
|
||||
if prev:
|
||||
print('PREV', device, stream, start - prev[1])
|
||||
prev_data[device, stream] = prev = [start, start + 3600, pset]
|
||||
summary.append([start, device, stream, prev])
|
||||
# merge continuous days, considering leap hour
|
||||
if prev is None or day > prev[2] + 25 * 3600:
|
||||
experiment = [end, start, day, device, stream, pset]
|
||||
summary.append(experiment)
|
||||
prev_data[device, stream] = experiment
|
||||
else:
|
||||
prev[1] = start + 3600
|
||||
prev[2].update(pset)
|
||||
for start, device, stream, (_, end, pset) in sorted(summary):
|
||||
st = time.strftime('%Y-%m-%d %H:%M', time.localtime(start))
|
||||
print(st, (end - start) / 3600., device, stream, len(pset))
|
||||
prev[0] = end
|
||||
prev[2] = day
|
||||
prev[-1].update(pset)
|
||||
result.clear()
|
||||
for end, start, _, device, stream, pset in sorted(summary):
|
||||
if prectime:
|
||||
res = db.query(start, end, device=device, stream=stream, single=-1)
|
||||
first = int(min(t[0] for t in res.values()))
|
||||
res = db.query(start, end, device=device, stream=stream, single=1)
|
||||
last = math.ceil(max(t[0] for t in res.values()))
|
||||
tm1 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(first))
|
||||
tm2 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(last))
|
||||
else:
|
||||
first, last = start, end - 1
|
||||
tm1 = time.strftime('%Y-%m-%d %Hh', time.localtime(first))
|
||||
tm2 = time.strftime('%Y-%m-%d %Hh', time.localtime(last))
|
||||
result.setdefault(device, []).append([first, last, device, stream, pset])
|
||||
print(tm1, tm2, device, stream, len(pset))
|
||||
|
Reference in New Issue
Block a user