forked from zhiwehu/Python-programming-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_monitor.py
More file actions
82 lines (71 loc) · 2.21 KB
/
simple_monitor.py
File metadata and controls
82 lines (71 loc) · 2.21 KB
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
'''
Class Stats:
def __init__(self, time_window):
# Implement. time_window is in seconds. This is the number of seconds we care about
def update(self, metric):
# Implement. You'll do some housekeeping here.
def stats(self):
# Return the average metric value in the last time_window seconds.
Example:
time_window=3
t=0, update(3)
t=0.5, update(5)
t=1, update(7)
t=2, stats() => 5
t=3, update(9)
t=5, stats() => 9
.
.
.
.
t=10, stats() => 0 / None
'''
from collections import deque
import time
# https://docs.python.org/2/library/collections.html
class Stats:
def __init__(self, time_window):
# Implement. time_window is in seconds. This is the number of seconds we care about
self.time_window = time_window
self.moving_sum = 0
self.dq = deque()
def purge_dq(self, curr_time_secs):
'''
Removes elements from the deque outside the window of interest
'''
while self.dq:
if (curr_time_secs - self.dq[0][1]) > self.time_window:
self.moving_sum -= self.dq[0][0]
self.dq.popleft()
else:
break
def update(self, metric):
# Implement. You'll do some housekeeping here.
'''
Adds the current metric and updates moving sum before purging the deque
'''
curr_time_secs = int(time.time())
self.purge_dq(curr_time_secs)
self.moving_sum += metric
self.dq.append((metric, curr_time_secs))
def stats(self):
'''
Return the average metric value in the last time_window seconds.
'''
curr_time_secs = int(time.time())
self.purge_dq(curr_time_secs)
return (self.moving_sum/len(self.dq)) if len(self.dq) != 0 else None
print('Testing...')
s = Stats(3)
st = time.time()
time.sleep(0); s.update(3)
time.sleep(0.5); s.update(5)
time.sleep(0.5); s.update(7)
time.sleep(1); assert s.stats() == 5.0 #t = 2
print(f'Avg at t = {int(time.time() - st)} : {s.stats()}')
time.sleep(1); s.update(9)
time.sleep(2); assert s.stats() == 9.0 #t = 5
print(f'Avg at t = {int(time.time() - st)} : {s.stats()}')
time.sleep(5); assert s.stats() == None #t = 10
print(f'Avg at t = {int(time.time() - st)} : {s.stats()}')
print('Test ended')