-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.py
35 lines (28 loc) · 944 Bytes
/
client.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
"""Methods to interact with influxdb and imperva logs"""
from influxdb import InfluxDBClient
from itertools import islice
BATCHLEN = 100
def split_every(n, iterable):
"""
For less amount of requests, groups data points into list with BATCHLEN length
"""
i = iter(iterable)
piece = list(islice(i, n))
while piece:
yield piece
piece = list(islice(i, n))
def create_or_connect():
"""
Ensures database 'imperva' exists and returns client with connection to it
"""
client = InfluxDBClient(host='influx', database='imperva')
if not {'name': 'imperva'} in client.get_list_database():
client.create_database('imperva')
return client
def write_points(points):
"""
Writes points to influxDB as chunks with BATCHLEN length
"""
client = create_or_connect()
results = [client.write_points(batch)for batch in split_every(BATCHLEN, points)]
return results