122 lines
4.1 KiB
Python
122 lines
4.1 KiB
Python
import sys
|
|
from os.path import expanduser
|
|
sys.path.append(expanduser('~'))
|
|
import time
|
|
import math
|
|
import numpy as np
|
|
from sehistory.seinflux import SEHistory, fmtime
|
|
from influx import RegExp
|
|
|
|
DAY = 24 * 3600
|
|
ETERNITY = 1e10
|
|
|
|
# token = "zqDbTcMv9UizfdTj15Fx_6vBetkM5mXN56EE9CiDaFsh7O2FFWZ2X4VwAAmdyqZr3HbpIr5ixRju07-oQmxpXw=="
|
|
|
|
db = SEHistory(*sys.argv[1:]) # arguments: database, access
|
|
|
|
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>, ] ...)
|
|
exp([start], [end], [stream], ...))
|
|
""")
|
|
|
|
now = int(time.time())
|
|
result = {}
|
|
|
|
|
|
def prt(maxpoints=7, maxcurves=50):
|
|
for i, (key, curve) in enumerate(result.items()):
|
|
if i > maxcurves:
|
|
print('--- ...')
|
|
break
|
|
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 > maxpoints:
|
|
curves = [curve[:maxpoints-3], None, curve[-3:]]
|
|
else:
|
|
curves = [curve]
|
|
for crv in curves:
|
|
if crv is None:
|
|
print('...')
|
|
else:
|
|
for row in crv:
|
|
print(fmtime(row[0]), row[1:])
|
|
|
|
|
|
def qry(*args, **kwds):
|
|
result.clear()
|
|
result.update(db.query(*args, **kwds))
|
|
prt()
|
|
|
|
|
|
def crv(*args, **kwds):
|
|
result.clear()
|
|
res = db.curves(*args, **kwds)
|
|
if isinstance(res, list):
|
|
result[()] = res
|
|
else:
|
|
result.update(res)
|
|
prt()
|
|
|
|
|
|
def exp(*args, **kwds):
|
|
res = db.get_experiments(*args, **kwds)
|
|
for exp, streams in res.items():
|
|
print('---',' '.join(exp))
|
|
for key, periods in streams.items():
|
|
print(' '.join(','.join(k) for k in key))
|
|
for period in periods:
|
|
print(' ', fmtime(period[0]), fmtime(period[1]))
|
|
|
|
|
|
def sry(prectime=False):
|
|
interval = 3600
|
|
res = db.query(-DAY * 365, interval=interval, _field='float',
|
|
device=None, stream=None, _measurement=None)
|
|
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:
|
|
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 (day, device, stream), (start, end, pset) in sorted(by_day.items()):
|
|
prev = prev_data.get((device, stream))
|
|
# 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[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][0] for t in res.values()))
|
|
res = db.query(start, end, device=device, stream=stream, single=1)
|
|
last = math.ceil(max(t[0][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))
|