diff --git a/my/arbtt.py b/my/arbtt.py index ac1a4e3..b69848e 100644 --- a/my/arbtt.py +++ b/my/arbtt.py @@ -88,6 +88,20 @@ def entries() -> Iterable[Entry]: 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 def stats() -> Stats: return stat(entries) diff --git a/my/core/influxdb.py b/my/core/influxdb.py index 3b2b850..96214c2 100644 --- a/my/core/influxdb.py +++ b/my/core/influxdb.py @@ -1,6 +1,8 @@ ''' 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 .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."? -def magic_fill(it) -> None: - assert callable(it) - name = f'{it.__module__}:{it.__name__}' +def magic_fill(it, *, name: Optional[str]=None) -> None: + if name is None: + 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 more_itertools import first, one - it = it() it, x = tee(it) f = first(x, default=None) if f is None: diff --git a/my/core/types.py b/my/core/types.py index ad42b7f..3bafa3d 100644 --- a/my/core/types.py +++ b/my/core/types.py @@ -66,6 +66,8 @@ def test_freezer() -> None: ### # 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.. # guess need to experiment and see