From 21899089a86fb9ca2bfac42622ac0c3777059c0b Mon Sep 17 00:00:00 2001 From: makefu Date: Wed, 28 Mar 2018 09:39:20 +0200 Subject: init default.py, setup.py --- arafetch/ara2influx.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 arafetch/ara2influx.py (limited to 'arafetch/ara2influx.py') diff --git a/arafetch/ara2influx.py b/arafetch/ara2influx.py new file mode 100644 index 0000000..26d7d0a --- /dev/null +++ b/arafetch/ara2influx.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python +""" usage: fetchday [options] DB + +Options: + --db FILE path to db [Default: db.json] + --cantine CANTINE name of the cantine [Default: thales-deutschland] + --host HOST hostname or ip of influx [Default: omo] +""" +import sys +from docopt import docopt +import logging +from influxdb import InfluxDBClient + + +import json +from datetime import datetime +logging.basicConfig(level=logging.DEBUG) +log = logging.getLogger() + + +def db2points(db,cantine): + for day,things in db.items(): + time = datetime.strptime(day,"%Y-%m-%d") + time = time.replace(hour=12) + for thing in things: + ret = { + "time": time.isoformat(), + "measurement":"dish", + "tags": { + "allergies": ",".join( sorted(thing['allergies'].keys()) ), + "additives": ",".join( sorted(thing['additives'].keys()) ), + "title": thing["title"], + "type": thing["type"], + "description": thing["description"], + "cantine": cantine + }, + "fields": { + "price": thing["price"] + } + } + log.debug(ret) + yield ret + +def main(): + args = docopt(__doc__) + dbname = args['DB'] + host = args['--host'] + cantine = args['--cantine'] + client = InfluxDBClient( + host=host, + port=8086, + database='araprice') + with open(dbname,'r+') as f: + db = json.load(f) + l = list(db2points(db,cantine)) + # client.drop_database("araprice") + client.create_database("araprice") + client.write_points(l) + +if __name__ == '__main__': + main() -- cgit v1.2.3