| 1 | """Diagnostics for debugging.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | from collections import Counter |
| 5 | from collections.abc import Mapping |
| 6 | from typing import Any |
| 7 | |
| 8 | |
| 9 | class Diagnostics: |
| 10 | """Information for RTSP to Web Client libraries.""" |
| 11 | |
| 12 | def __init__(self) -> None: |
| 13 | """Initialize Diagnostics.""" |
| 14 | self._counter: Counter = Counter() |
| 15 | |
| 16 | def increment(self, key: str) -> None: |
| 17 | """Increment a counter for the specified key/event.""" |
| 18 | self._counter.update(Counter({key: 1})) |
| 19 | |
| 20 | def as_dict(self) -> Mapping[str, Any]: |
| 21 | """Return diagnostics as a debug dictionary.""" |
| 22 | return {k: self._counter[k] for k in self._counter} |
| 23 | |
| 24 | def reset(self) -> None: |
| 25 | """Clear all diagnostics, for testing.""" |
| 26 | self._counter = Counter() |
| 27 | |
| 28 | DISCOVERY_DIAGNOSTICS = Diagnostics() |
| 29 | WEB_DIAGNOSTICS = Diagnostics() |
| 30 | WEBRTC_DIAGNOSTICS = Diagnostics() |
| 31 | |
| 32 | MAP = { |
| 33 | "discovery": DISCOVERY_DIAGNOSTICS, |
| 34 | "web": WEB_DIAGNOSTICS, |
| 35 | "webrtc": WEBRTC_DIAGNOSTICS, |
| 36 | } |
| 37 | |
| 38 | |
| 39 | def reset() -> None: |
| 40 | """Clear all diagnostics, for testing.""" |
| 41 | for diagnostics in MAP.values(): |
| 42 | diagnostics.reset() |
| 43 | |
| 44 | |
| 45 | def get_diagnostics() -> dict[str, Any]: |
| 46 | return { k: v.as_dict() for (k, v) in MAP.items() } |