-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmetrics_manager.py
48 lines (39 loc) · 1.15 KB
/
metrics_manager.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
import time
'''
ALL_USERS schema
{ object_id : time }
'''
class MetricsManager:
def __init__(self):
self.ACTIVE_IDs = []
self.START = int(time.time())
self.ALL_USERS = {}
def newLoop(self):
self.ACTIVE_IDs = []
self.START = int(time.time())
def currentTimeFromStart(self):
now = int(time.time())
elapsed = now - self.START
return elapsed
def addTimeFor(self, objectId):
elapsed = self.currentTimeFromStart()
if not objectId in self.ALL_USERS:
self.ALL_USERS[objectId] = elapsed
else:
self.ALL_USERS[objectId] += elapsed
# sendTrackDataToServer(objectId, elapsed)
def timeForId(self, objectId):
return self.ALL_USERS[objectId]
def currentMetrics(self):
times = []
for _, time in self.ALL_USERS.items():
times.append(time)
if len(times) == 0:
times.append(0)
return {
"min": min(times),
"max": max(times),
"avg": sum(times) / len(times),
"total": sum(times),
"count": len(self.ALL_USERS.items())
}