-
Notifications
You must be signed in to change notification settings - Fork 1
/
influxdb_logger.py
86 lines (74 loc) · 2.59 KB
/
influxdb_logger.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
75
76
77
78
79
80
81
82
83
84
85
86
import logging
from influxdb_client import (
BucketRetentionRules,
InfluxDBClient,
Point,
WriteOptions
)
logger = logging.getLogger(__name__)
INFLUXDB_HOSTNAME = "http://localhost"
INFLUXDB_PORT = 8086
INFLUXDB_URL = f"{INFLUXDB_HOSTNAME}:{INFLUXDB_PORT}"
# A default auth token
INFLUXDB_TOKEN = "my-super-secret-auth-token"
ORGANIZATION = "myorg"
BUCKET = "exchange"
TIME_FIELD = "time"
RECORD_TYPE_FIELD = "measurement"
FIELDS_TYPE_FIELD = "fields"
LOG_BATCH_SIZE = 1000
class InfluxDBLogger:
def __init__(
self,
bucket_name=BUCKET,
batch_size=LOG_BATCH_SIZE,
data_retention=3600,
):
self.organization = ORGANIZATION
self.client = InfluxDBClient(
url=INFLUXDB_URL, token=INFLUXDB_TOKEN, org=self.organization
)
self.batch_size = batch_size
self.bucket_name = bucket_name
self.write_api = self.client.write_api(
write_options=WriteOptions(batch_size=self.batch_size)
)
self.query_api = self.client.query_api()
self.buckets_api = self.client.buckets_api()
bucket = self.buckets_api.find_bucket_by_name(self.bucket_name)
if bucket is None:
logger.warning(
f"Bucket {self.bucket_name!r} not found. "
f"Creating a bucket {self.bucket_name!r}."
)
retention_rules = None
if data_retention is not None:
retention_rules = BucketRetentionRules(
type="expire", every_seconds=data_retention
)
self.buckets_api.create_bucket(
bucket_name=self.bucket_name,
retention_rules=retention_rules,
org=self.organization,
)
def send_event(self, record_type, message):
point = Point(record_type)
for key, value in message.items():
point = point.field(key, value)
self.write_api.write(bucket=self.bucket_name, record=point)
def get_events(self, record_type):
query = '''
from(bucket: currentBucket)
|> range(start: -5m, stop: now())
|> filter(fn: (r) => r._measurement == recordType)
|> pivot(rowKey:["_time"], columnKey: ["_field"], \
valueColumn: "_value")
'''
params = {"currentBucket": self.bucket_name, "recordType": record_type}
tables = self.query_api.query(query=query, params=params)
if len(tables) > 0:
table, *_ = tables
events = table.records
else:
events = []
return events