-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_connect.py
53 lines (49 loc) · 1.83 KB
/
db_connect.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
from influxdb import InfluxDBClient
from datetime import timezone, datetime
from random import random
import time, pytz
class InfluxClient(InfluxDBClient):
def __init__(self):
self.host = 'qdot-server.phas.ubc.ca'
self.port = 8086
self.dbname = 'test'
self.timezone = pytz.timezone('America/Vancouver')
InfluxDBClient.__init__(self,
host=self.host, port=self.port, database=self.dbname)
def log_float(self, tags, timestamp, value,
field="val", measurement="experiment"):
''' tags (dict) -> identifying information to log with value
examples == label: bfs_P1
component: lksh370
field (str) -> type of measurement (power, pressure, volts...)
defaults to val
timestamp (datetime.datetime) -> timestamp for measurement
value(float) -> value to log
measurement (str) -> not sure how best to use this.
defaults to 'experiment' '''
localstamp = self.timezone.localize(timestamp)
json_body = [
{
"measurement": measurement,
"time": localstamp,
"tags": tags,
"fields": {
field: value,
},
}
]
self.write_points(json_body)
# def main(n, host='localhost', port=8086, dbname='test'):
# """ open a connection and push random numbers until KeyBoard interrupt """
# client = InfluxClient()
#
# for i in range(n):
# tags = {
# 'label': 'influx_test',
# 'component': 'random_num',
# }
# client.log_float(tags, datetime.now(), random())
# print('wrote: ', i)
# time.sleep(5.0)
#
# return None