-
Notifications
You must be signed in to change notification settings - Fork 1
/
fixStromableser.py
58 lines (51 loc) · 1.72 KB
/
fixStromableser.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
from influxdb import InfluxDBClient
from datetime import datetime
import pickle
# InfluxDB parameters
server = 'job4'
port = 8086
database = 'power'
measurement = 'energy'
field = 'watt'
# CLI interface
if __name__ == "__main__":
# algo:
# * read measurement
# * remove rows where values increase when going backwards
# * if rows removed
# * create backup file
# * drop measurement
# * insert remaining rows in new measurement
# request whole measurement
client = InfluxDBClient(host=server, port=port, database=database)
response = client.query(f'select * from {measurement}')
if response.error != None:
print(f'Reading measurements failed: {response.error}\n')
quit()
# create list of new influx values
points = list(response.get_points())
maxValue = points[-1][field]
newPoints = []
for point in reversed(points):
if point[field] <= maxValue:
maxValue = point[field]
newPoint = {
"measurement": measurement,
"tags": {
"meter": point["meter"],
},
"time": point["time"],
"fields": {
field: point[field]
}
}
newPoints.append(newPoint)
# if points removed then write backup and new measurement
removed = len(points) - len(newPoints)
if removed > 0:
dt = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
with open(f'{database}-{measurement}-{dt}.bak', 'wb') as f:
pickle.dump(points, f)
client.query(f'drop measurement {measurement}')
client.write_points(reversed(newPoints))
print(f"removed {removed} measurements")