StatsCollector: Full flake8 and mypy pass

A function name refactor touches other files.
This commit is contained in:
Athanasius 2021-11-04 16:37:55 +00:00
parent f81fdf91ee
commit 693ad95fb3
4 changed files with 36 additions and 18 deletions

View File

@ -234,7 +234,7 @@ def stats() -> str:
:return: JSON stats data
"""
stats_current = stats_collector.getSummary()
stats_current = stats_collector.get_summary()
stats_current["version"] = Settings.EDDN_VERSION
return simplejson.dumps(stats_current)

View File

@ -360,7 +360,7 @@ def stats() -> str:
:return: JSON stats data
"""
stats_current = stats_collector.getSummary()
stats_current = stats_collector.get_summary()
stats_current["version"] = Settings.EDDN_VERSION
return simplejson.dumps(stats_current)

View File

@ -75,7 +75,7 @@ def stats() -> str:
:return: JSON stats data
"""
stats = stats_collector.getSummary()
stats = stats_collector.get_summary()
stats["version"] = Settings.EDDN_VERSION
return simplejson.dumps(stats)

View File

@ -1,17 +1,16 @@
# coding: utf8
"""Handle various stats about uploads."""
from collections import deque
from datetime import datetime
from itertools import islice
from threading import Lock, Thread
from time import sleep
from typing import Any, Dict
class StatsCollector(Thread):
'''
Collects simple statistics and aggregates them over the number of minutes
you choose, up to one hour.
'''
"""Collect simple statistics and aggregate them."""
def __init__(self):
super(StatsCollector, self).__init__()
@ -23,41 +22,60 @@ class StatsCollector(Thread):
self.lock = Lock()
self.starttime = 0
self.start_time = 0
def run(self):
self.starttime = datetime.utcnow()
def run(self) -> None:
"""Update statistics once a minute."""
self.start_time = datetime.utcnow()
while True:
sleep(60)
with self.lock:
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 tally(self, key):
def tally(self, key: str) -> None:
"""
Add one to the count of the given key.
:param key: Key for affected data.
"""
with self.lock:
if key not in self.current:
self.current[key] = 1
else:
self.current[key] += 1
def getCount(self, key, minutes):
def get_count(self, key: str, minutes: int) -> int:
"""
Get current count for given key over requested time period.
:param key: Key for requested data.
:param minutes: How many minutes back in time we want the count for.
:returns: Count for the requested data.
"""
if key in self.history:
return sum(islice(self.history[key], 0, min(minutes, self.max_minutes)))
return 0
def getSummary(self):
summary = {}
def get_summary(self) -> Dict[str, Any]:
"""
Get a summary of all current data.
:returns: A Dict of the summary data.
"""
summary: Dict[str, Any] = {}
for key in self.current.keys():
summary[key] = {
"1min": self.getCount(key, 1),
"5min": self.getCount(key, 5),
"60min": self.getCount(key, 60)
"1min": self.get_count(key, 1),
"5min": self.get_count(key, 5),
"60min": self.get_count(key, 60)
}
summary['uptime'] = int((datetime.utcnow() - self.starttime).total_seconds())
summary['uptime'] = int((datetime.utcnow() - self.start_time).total_seconds())
return summary