annotate lib/export_to_influxdb.py @ 337:1756a5519026

move export_to_influxdb up to lib Ignore-this: d43cc183b51bec93b28e6a7ba7bf1d6e
author drewp@bigasterisk.com
date Sat, 03 Mar 2018 18:08:03 -0800
parents service/piNode/export_to_influxdb.py@e7cbf250188a
children bf2174646809
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
293
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
1 import time, logging
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
2 from influxdb import InfluxDBClient
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
3 from rdflib import Namespace
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
4
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
5 log = logging.getLogger()
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
6 ROOM = Namespace('http://projects.bigasterisk.com/room/')
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
7
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
8 class InfluxExporter(object):
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
9 def __init__(self, configGraph, influxHost='bang6'):
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
10 self.graph = configGraph
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
11 self.influx = InfluxDBClient(influxHost, 9060, 'root', 'root', 'main')
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
12 self.lastExport = 0
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
13 self.lastSent = {} # (subj, measurementName, tags) : (time, value)
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
14
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
15 self.measurements = {} # (subj, predicate) : measurement
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
16 for s, m in self.graph.subject_objects(ROOM['influxMeasurement']):
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
17 self.measurements[(s, self.graph.value(m, ROOM['predicate']))] = m
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
18
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
19 def exportToInflux(self, currentStatements):
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
20 graph = self.graph
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
21 now = int(time.time())
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
22
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
23 points = []
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
24 for stmt in currentStatements:
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
25 if (stmt[0], stmt[1]) in self.measurements:
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
26 meas = self.measurements[(stmt[0], stmt[1])]
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
27 measurementName = graph.value(meas, ROOM['measurement'])
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
28 tags = {}
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
29 for t in graph.objects(meas, ROOM['tag']):
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
30 k = graph.value(t, ROOM['key']).toPython()
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
31 tags[k] = graph.value(t, ROOM['value']).toPython()
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
32
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
33 value = self.influxValue(stmt[2])
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
34
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
35 if not self.shouldSendNewPoint(now, stmt[0], measurementName,
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
36 tags, value):
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
37 continue
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
38
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
39 points.append({
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
40 'measurement': measurementName,
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
41 "tags": tags,
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
42 "fields": {"value": value},
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
43 "time": now
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
44 })
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
45 log.debug('send to influx %r', points[-1])
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
46 if points:
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
47 self.influx.write_points(points, time_precision='s')
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
48
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
49 def influxValue(self, rdfValue):
304
e7cbf250188a influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents: 293
diff changeset
50 if rdfValue in [ROOM['motion'], ROOM['pressed']]:
293
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
51 value = 1
304
e7cbf250188a influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents: 293
diff changeset
52 elif rdfValue in [ROOM['noMotion'], ROOM['notPressed']]:
293
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
53 value = 0
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
54 else:
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
55 value = rdfValue.toPython()
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
56 if not isinstance(value, (int, float)):
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
57 raise NotImplementedError('value=%r' % value)
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
58 return value
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
59
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
60 def shouldSendNewPoint(self, now, subj, measurementName, tags, value):
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
61 key = (subj, measurementName, tuple(sorted(tags.items())))
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
62 if key in self.lastSent:
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
63 lastTime, lastValue = self.lastSent[key]
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
64 if lastValue == value and lastTime > now - 3600:
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
65 log.debug('skip influx point %r', key)
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
66 return False
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
67
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
68 self.lastSent[key] = (now, value)
fc0e42933baa save data to influxdb, not graphite
drewp@bigasterisk.com
parents:
diff changeset
69 return True