koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 1 | from urlparse import urlparse |
| 2 | from influxdb import InfluxDBClient |
| 3 | |
| 4 | |
| 5 | def connect(url): |
| 6 | parsed_url = urlparse(url) |
| 7 | user_passwd, host_port = parsed_url.netloc.rsplit("@", 1) |
| 8 | user, passwd = user_passwd.split(":", 1) |
| 9 | host, port = host_port.split(":") |
| 10 | return InfluxDBClient(host, int(port), user, passwd, parsed_url.path[1:]) |
| 11 | |
| 12 | |
| 13 | def add_data(conn, hostname, data): |
| 14 | per_sensor_data = {} |
| 15 | for serie in data: |
| 16 | serie = serie.copy() |
| 17 | gtime = serie.pop('time') |
| 18 | for key, val in serie.items(): |
| 19 | dev, sensor = key.split('.') |
| 20 | data = per_sensor_data.setdefault(sensor, []) |
| 21 | data.append([gtime, hostname, dev, val]) |
| 22 | |
| 23 | infl_data = [] |
| 24 | columns = ['time', 'host', 'device', 'value'] |
| 25 | for sensor_name, points in per_sensor_data.items(): |
| 26 | infl_data.append( |
| 27 | {'columns': columns, |
| 28 | 'name': sensor_name, |
| 29 | 'points': points}) |
| 30 | |
| 31 | conn.write_points(infl_data) |