-
Notifications
You must be signed in to change notification settings - Fork 0
/
influx_connection.py
74 lines (53 loc) · 2.32 KB
/
influx_connection.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
# Copyright (c) 2016, Jonathan Nutzmann
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
from influxdb import InfluxDBClient
from strict_rfc3339 import rfc3339_to_timestamp
class SkynetInflux:
def __init__(self):
# TODO: make this configurable.
# connect to the influx server
self.client = InfluxDBClient('localhost', 8086, 'root', 'root', 'skynet')
self.client.create_database('skynet', if_not_exists=True)
def get_results_for_plot(self, query):
pts = self.client.query(query)
results = list(pts.get_points())
retval = []
if len(results) > 0:
keys = list(results[0].keys())
keys.remove('time')
f = keys[0]
for r in results:
t = rfc3339_to_timestamp(r['time'])
d = r[f]
retval.append([t,d])
return retval
def get_measurements(self):
series = self.client.get_list_series()
measurements = []
for s in series:
print(s)
for t in s["tags"]:
try:
root = ".".join([t["board"], t["name"], s["name"]])
query = "SELECT * FROM %s WHERE \"board\"='%s' AND \"name\"='%s' LIMIT 1" % (s["name"], t["board"], t["name"])
print(query)
rs = self.client.query(query)
qr = list(rs.get_points())[0]
for k in qr.keys():
if k not in ['time', 'name', 'board', 'rtr', 'origin']:
measurements.append(root + "." + k)
except Exception as e:
print(e)
return sorted(measurements)
def get_points(measurement, start, stop, limit):
board, name, packet, data = measurement.split('.')
q = "SELECT %s FROM %s WHERE board='%s' AND name='%s' LIMIT %i"