Let the StatsCollector collect arbitrary stats.

This commit is contained in:
James Muscat 2015-04-13 18:26:04 +01:00
parent bd0afbc0c8
commit 4bab5e3606

View File

@ -15,6 +15,10 @@ class StatsCollector(Thread):
inboundMessages = 0
outboundMessages = 0
current = {}
history = {}
inboundHistory = deque(maxlen=max_minutes)
outboundHistory = deque(maxlen=max_minutes)
@ -32,6 +36,11 @@ class StatsCollector(Thread):
self.outboundHistory.appendleft(self.outboundMessages)
self.inboundMessages = 0
self.outboundMessages = 0
for key in self.current.keys():
if key not in self.history:
self.history[key] = deque(maxlen=self.max_minutes)
self.history[key].appendleft(self.current[key])
self.current[key] = 0
def tallyInbound(self):
with self.lock:
@ -41,14 +50,25 @@ class StatsCollector(Thread):
with self.lock:
self.outboundMessages += 1
def tally(self, key):
if key not in self.current:
self.current[key] = 1
else:
self.current[key] += 1
def getInboundCount(self, minutes):
return sum(islice(self.inboundHistory, 0, min(minutes, self.max_minutes)))
def getOutboundCount(self, minutes):
return sum(islice(self.outboundHistory, 0, min(minutes, self.max_minutes)))
def getCount(self, key, minutes):
if key in self.history:
return sum(islice(self.history[key], 0, min(minutes, self.max_minutes)))
return 0
def getSummary(self):
return {
summary = {
"inbound": {
"1min": self.getInboundCount(1),
"5min": self.getInboundCount(5),
@ -58,5 +78,14 @@ class StatsCollector(Thread):
"1min": self.getOutboundCount(1),
"5min": self.getOutboundCount(5),
"60min": self.getOutboundCount(60)
},
}
}
for key in self.current.keys():
summary[key] = {
"1min": self.getCount(key, 1),
"5min": self.getCount(key, 5),
"60min": self.getCount(key, 60)
}
return summary