summaryrefslogtreecommitdiffstats
path: root/arafetch/ara2influx.py
blob: 574a856f8b383e964d31df4becc5e7e3510a6e64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python
""" usage: ara2influx [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()