forked from Ahlzen/TopOSM
-
Notifications
You must be signed in to change notification settings - Fork 1
/
stats.py
52 lines (45 loc) · 1.76 KB
/
stats.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
#!/usr/bin/python
import os.path
import pickle
import filelock
from common import *
from coords import *
from env import *
class StatsManager:
lock = filelock.FileLock('stats.lock')
def __init__(self):
self.influx_client = connect_to_influxdb()
with self.lock:
if not os.path.isfile('stats'):
with open('stats', 'wb') as f:
pickle.dump({}, f)
def recordRender(self, metatile, totalTime, layerTimes):
with self.lock:
influx_frames = [{
'measurement': 'render',
'tags': {
'zoom': str(metatile.z),
#'geohash': getTileGeohash(metatile.z, metatile.x, metatile.y, NTILES[metatile.z]),
},
'fields': {'render_time': totalTime, 'z': metatile.z}
}]
for layer, layer_time in layerTimes.items():
influx_frames.append({
'measurement': 'render_layer',
'tags': {
'zoom': str(metatile.z),
'layer': layer,
},
'fields': {'render_time': layer_time}
})
self.influx_client.write_points(influx_frames)
with open('stats', 'rb') as f:
stats = pickle.load(f)
(c, t) = stats.setdefault(metatile.z, {}).setdefault('total', (0, 0))
stats[metatile.z]['total'] = (c + 1, t + totalTime)
for layer in layerTimes:
(c, t) = stats[metatile.z].setdefault(layer, (0, 0))
stats[metatile.z][layer] = (c + 1, t + layerTimes[layer])
with open('stats', 'wb') as f:
pickle.dump(stats, f)
stats = StatsManager()