bluemaestro: populate grafana

This commit is contained in:
Dima Gerasimov 2021-02-14 22:57:02 +00:00 committed by karlicoss
parent 1899b006de
commit 6d9bc2964b
2 changed files with 31 additions and 3 deletions

View file

@ -25,12 +25,11 @@ from my.core import get_files
import my.bluemaestro as M import my.bluemaestro as M
from my.config import bluemaestro as BC 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(list(M.measurements())[:10])
print(M.dataframe()) M.fill_influxdb()
ffwf ffwf

View file

@ -191,6 +191,35 @@ def dataframe() -> DataFrameT:
return df.set_index('dt') 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: def check() -> None:
temps = list(measurements()) temps = list(measurements())
latest = temps[:-2] latest = temps[:-2]