blob: 750aa77b1a94dcc795ece3aae4d96896453294ca (
plain)
| 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
 | from datetime import datetime, timedelta
ERROR_STATE_DELTA = timedelta(minutes=15)
class ErrorState(object):
    def __init__(self, min_timedelta = ERROR_STATE_DELTA):
        self.min_timedelta = min_timedelta
        self.count = 0
        self.timestamp = None
    def incr(self):
        if self.count == 0:
            self.timestamp = datetime.utcnow()
        self.count += 1
    def reset(self):
        self.count = 0
        self.timestamp = None
    def is_triggered(self):
        if self.timestamp is None:
            return False
        delta = datetime.utcnow() - self.timestamp
        if delta >= self.min_timedelta:
            return True
        return False
 |