37 lines
1.0 KiB
Python
37 lines
1.0 KiB
Python
from __future__ import annotations
|
|
|
|
import threading
|
|
from datetime import datetime, timezone
|
|
|
|
|
|
class RuntimeStats:
|
|
def __init__(self) -> None:
|
|
self._lock = threading.Lock()
|
|
self._data = {
|
|
"tzsp_datagrams": 0,
|
|
"tzsp_decode_errors": 0,
|
|
"tzsp_unsupported": 0,
|
|
"frames_injected": 0,
|
|
"inject_errors": 0,
|
|
"eve_events": 0,
|
|
"eve_alerts": 0,
|
|
"eve_parse_errors": 0,
|
|
"block_attempts": 0,
|
|
"block_success": 0,
|
|
"block_errors": 0,
|
|
"last_packet_at": None,
|
|
"last_alert_at": None,
|
|
}
|
|
|
|
def inc(self, key: str, amount: int = 1) -> None:
|
|
with self._lock:
|
|
self._data[key] = int(self._data.get(key, 0)) + amount
|
|
|
|
def stamp(self, key: str) -> None:
|
|
with self._lock:
|
|
self._data[key] = datetime.now(timezone.utc).isoformat()
|
|
|
|
def snapshot(self) -> dict:
|
|
with self._lock:
|
|
return dict(self._data)
|