summaryrefslogtreecommitdiffstats
path: root/arafetch/ara2influx.py
diff options
context:
space:
mode:
Diffstat (limited to 'arafetch/ara2influx.py')
-rw-r--r--arafetch/ara2influx.py61
1 files changed, 61 insertions, 0 deletions
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()