From 6d9bc2964b24cfe6187945f4634940673dfe9c27 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Sun, 14 Feb 2021 22:57:02 +0000 Subject: [PATCH] bluemaestro: populate grafana --- misc/repl.py | 5 ++--- my/bluemaestro.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/misc/repl.py b/misc/repl.py index 9f79c90..aa45256 100755 --- a/misc/repl.py +++ b/misc/repl.py @@ -25,12 +25,11 @@ from my.core import get_files import my.bluemaestro as M from my.config import bluemaestro as BC -BC.export_path = get_files(BC.export_path) +# BC.export_path = get_files(BC.export_path)[:40] # print(list(M.measurements())[:10]) -print(M.dataframe()) - +M.fill_influxdb() ffwf diff --git a/my/bluemaestro.py b/my/bluemaestro.py index b2ebc5c..73fd757 100755 --- a/my/bluemaestro.py +++ b/my/bluemaestro.py @@ -191,6 +191,35 @@ def dataframe() -> DataFrameT: return df.set_index('dt') +def fill_influxdb() -> None: + from itertools import islice + from .core.common import asdict + + from influxdb import InfluxDBClient # type: ignore + client = InfluxDBClient() + db = 'db' + mname = __name__.replace('.', '_') + client.delete_series(database=db, measurement=mname) + def dissoc(d, k): + del d[k] + return d # meh + jsons = ({ + 'measurement': mname, + # todo maybe good idea to tags with database file/name? to inspect inconsistencies etc.. + # 'tags': {'activity': e.activity}, + 'time': e.dt.isoformat(), + 'fields': dissoc(asdict(e), 'dt'), + } for e in measurements()) + from more_itertools import chunked + # "The optimal batch size is 5000 lines of line protocol." + # some chunking is def necessary, otherwise it fails + for chunk in chunked(jsons, n=5000): + cl = list(chunk) + logger.debug('writing next chunk %s', cl[-1]) + client.write_points(cl, database=db) + # todo "Specify timestamp precision when writing to InfluxDB."? + + def check() -> None: temps = list(measurements()) latest = temps[:-2]