-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsyspce_bucket.py
78 lines (55 loc) · 2.2 KB
/
syspce_bucket.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import datetime
import logging
log = logging.getLogger('sysmoncorrelator')
#----------------------------------------------------------------------------#
# Bucket class (FIFO) used for managing actions done by processes in a #
#period of time #
#----------------------------------------------------------------------------#
class Bucket(object):
def __init__(self, max_size, time_period, bucket_name):
self.max_size = max_size
self.time_period = datetime.timedelta(seconds = time_period)
self.action = []
self.alert = True
self.bucket_name = bucket_name
def insertAction(self, date):
# If bucket is disabled, no alert
if self.alert == False:
log.debug("Bucket %s disabled, already notified" % self.bucket_name)
return False
date = datetime.datetime.strptime(date, '%Y-%m-%d %H:%M:%S.%f')
# First action
if not self.action:
self.action.append(date)
if self.max_size < 2 and self.alert:
self.alert = False
return True
else:
return False
#Check delta between first element and new element
while self.action and (date - self.action[0]) > self.time_period:
self.action.pop(0)
self.action.append(date)
if len(self.action) >= self.max_size:
self.alert = False
return True
else:
return False
def actionExists(self, date):
date = datetime.datetime.strptime(date, '%Y-%m-%d %H:%M:%S.%f')
if date in self.action:
return True
else:
return False
class BucketSystem(object):
def __init__(self):
self.buckets = {}
def getBucket(self, bucket_name):
if self.buckets.has_key(bucket_name):
return self.buckets[bucket_name]
else:
return False
def createBucket(self, bucket_name, max_size, time_period):
bucket = Bucket(max_size, time_period, bucket_name)
self.buckets[bucket_name] = bucket
return bucket