mirror of
https://git.ianrenton.com/ian/spothole.git
synced 2026-09-21 14:57:42 +00:00
Autogenerated type safety parameterisation of all methods
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from collections import deque
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
@@ -10,12 +12,12 @@ class WebServerMetrics:
|
||||
"""Tracker for web server metrics. Stores the times pages and API endpoints were accessed for an hour, so we
|
||||
can display the rate of requests per hour, and also updates the equivalent Prometheus counters."""
|
||||
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
self.status = "Starting"
|
||||
self._page_access_times = deque()
|
||||
self._api_access_times = deque()
|
||||
self._page_access_times: deque[datetime] = deque()
|
||||
self._api_access_times: deque[datetime] = deque()
|
||||
|
||||
def record(self, path: str, status_code: int):
|
||||
def record(self, path: str, status_code: int) -> None:
|
||||
"""Records data for a request, depending on whether it's a page, API, or other request, and making sure
|
||||
the response code isn't 404. Also sets the status of the web server."""
|
||||
|
||||
@@ -37,7 +39,7 @@ class WebServerMetrics:
|
||||
return self._count_and_prune_last_hour(self._api_access_times)
|
||||
|
||||
@staticmethod
|
||||
def _count_and_prune_last_hour(access_times: deque) -> int:
|
||||
def _count_and_prune_last_hour(access_times: deque[datetime]) -> int:
|
||||
cutoff = datetime.now(pytz.UTC) - timedelta(hours=1)
|
||||
while access_times and access_times[0] < cutoff:
|
||||
access_times.popleft()
|
||||
|
||||
Reference in New Issue
Block a user