arbtt: feed data to influxdb

This commit is contained in:
Dima Gerasimov 2021-02-22 22:10:12 +00:00 committed by karlicoss
parent ca4d58e4e7
commit 0585cc4a89
3 changed files with 26 additions and 4 deletions

View file

@ -88,6 +88,20 @@ def entries() -> Iterable[Entry]:
yield Entry(json=json) yield Entry(json=json)
def fill_influxdb() -> None:
from .core.influxdb import magic_fill
from .core.types import Freezer
freezer = Freezer(Entry)
fit = (freezer.freeze(e) for e in entries())
# TODO crap, influxdb doesn't like None https://github.com/influxdata/influxdb/issues/7722
# wonder if can check it statically/warn?
fit = (f for f in fit if f.active is not None)
# todo could tag with computer name or something...
# todo should probably also tag with 'program'?
magic_fill(fit, name=f'{entries.__module__}:{entries.__name__}')
from .core import stat, Stats from .core import stat, Stats
def stats() -> Stats: def stats() -> Stats:
return stat(entries) return stat(entries)

View file

@ -1,6 +1,8 @@
''' '''
TODO doesn't really belong to 'core' morally, but can think of moving out later TODO doesn't really belong to 'core' morally, but can think of moving out later
''' '''
from .common import assert_subpackage; assert_subpackage(__name__)
from typing import Iterable, Any, Optional, Dict from typing import Iterable, Any, Optional, Dict
from .common import LazyLogger, asdict, Json from .common import LazyLogger, asdict, Json
@ -84,13 +86,17 @@ def fill(it: Iterable[Any], *, measurement: str, reset: bool=False, dt_col: str=
# todo "Specify timestamp precision when writing to InfluxDB."? # todo "Specify timestamp precision when writing to InfluxDB."?
def magic_fill(it) -> None: def magic_fill(it, *, name: Optional[str]=None) -> None:
assert callable(it) if name is None:
name = f'{it.__module__}:{it.__name__}' assert callable(it) # generators have no name/module
name = f'{it.__module__}:{it.__name__}'
assert name is not None
if callable(it):
it = it()
from itertools import tee from itertools import tee
from more_itertools import first, one from more_itertools import first, one
it = it()
it, x = tee(it) it, x = tee(it)
f = first(x, default=None) f = first(x, default=None)
if f is None: if f is None:

View file

@ -66,6 +66,8 @@ def test_freezer() -> None:
### ###
# TODO shit. what to do with exceptions? # TODO shit. what to do with exceptions?
# e.g. good testcase is date parsing issue. should def yield Exception in this case
# fundamentally it should just be Exception aware, dunno
# #
# TODO not entirely sure if best to use Frozen as the schema, or actually convert objects.. # TODO not entirely sure if best to use Frozen as the schema, or actually convert objects..
# guess need to experiment and see # guess need to experiment and see