core: more generic functions to jsonify data, rescuetime: fix influxdb filling

This commit is contained in:
Dima Gerasimov 2021-02-13 19:28:32 +00:00 committed by karlicoss
parent 07f901e1e5
commit 4012f9b7c2
5 changed files with 65 additions and 32 deletions

View file

@ -16,7 +16,7 @@ from .core.error import Res, split_errors
from my.config import rescuetime as config
log = LazyLogger(__package__, level='info')
log = LazyLogger(__name__, level='info')
def inputs() -> Sequence[Path]:
@ -80,25 +80,33 @@ def fake_data(rows: int=1000) -> Iterator[None]:
# todo not sure if I want to keep these here? vvv
# guess should move to core? or to 'ext' module, i.e. interfaces?
# make automatic
def fill_influxdb():
def fill_influxdb() -> None:
from .core.common import asdict
from influxdb import InfluxDBClient # type: ignore
client = InfluxDBClient()
# client.delete_series(database='lastfm', measurement='phone')
db = 'test'
client.drop_database(db)
client.create_database(db)
# todo handle errors
vit = (e for e in entries() if isinstance(e, dal.Entry))
jsons = [{
"measurement": 'phone',
"tags": {},
"time": str(e.dt),
"fields": {"name": e.activity},
} for e in vit]
client.write_points(jsons, database=db) # TODO??
db = 'db'
measurement = __name__.replace('.', '_')
client.delete_series(database=db, measurement=measurement)
# client.drop_database(db)
# todo create if not exists?
# client.create_database(db)
# todo handle errors.. not sure how? maybe add tag for 'error' and fill with emtpy data?
vit = (e for e in entries() if isinstance(e, Entry))
jsons = ({
'measurement': measurement, # hmm, influx doesn't like dots?
# hmm, so tags are autoindexed and might be faster?
# not sure what's the big difference though
# "fields are data and tags are metadata"
'tags': {'activity': e.activity},
'time': e.dt.isoformat(),
'fields': {'duration_s': e.duration_s},
# todo asdict(e),
} for e in vit)
# todo do we need to batch?
client.write_points(jsons, database=db)
# TODO lots of garbage in dir()? maybe need to del the imports...