From 693ad95fb372ac0f7fc27b7f387f409594c6b1ec Mon Sep 17 00:00:00 2001 From: Athanasius Date: Thu, 4 Nov 2021 16:37:55 +0000 Subject: [PATCH] StatsCollector: Full flake8 and mypy pass A function name refactor touches other files. --- src/eddn/Bouncer.py | 2 +- src/eddn/Gateway.py | 2 +- src/eddn/Relay.py | 2 +- src/eddn/core/StatsCollector.py | 48 ++++++++++++++++++++++----------- 4 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/eddn/Bouncer.py b/src/eddn/Bouncer.py index ce823e6..4cbb6bd 100644 --- a/src/eddn/Bouncer.py +++ b/src/eddn/Bouncer.py @@ -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) diff --git a/src/eddn/Gateway.py b/src/eddn/Gateway.py index 2648546..567fca2 100644 --- a/src/eddn/Gateway.py +++ b/src/eddn/Gateway.py @@ -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) diff --git a/src/eddn/Relay.py b/src/eddn/Relay.py index 83624dc..4c84d0e 100644 --- a/src/eddn/Relay.py +++ b/src/eddn/Relay.py @@ -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) diff --git a/src/eddn/core/StatsCollector.py b/src/eddn/core/StatsCollector.py index 9ce7f05..840453a 100644 --- a/src/eddn/core/StatsCollector.py +++ b/src/eddn/core/StatsCollector.py @@ -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