32 lines
891 B
Python
32 lines
891 B
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import asyncio
|
||
|
|
from typing import TYPE_CHECKING
|
||
|
|
|
||
|
|
from yuxi.utils.logging_config import logger
|
||
|
|
|
||
|
|
if TYPE_CHECKING:
|
||
|
|
from yuxi.channels.services.runtime_state import RuntimeState
|
||
|
|
|
||
|
|
|
||
|
|
class StatsCollector:
|
||
|
|
def __init__(self, state: RuntimeState):
|
||
|
|
self._state = state
|
||
|
|
|
||
|
|
async def run(self, interval: float = 60) -> None:
|
||
|
|
logger.info("StatsCollector started")
|
||
|
|
while True:
|
||
|
|
await asyncio.sleep(interval)
|
||
|
|
try:
|
||
|
|
self._collect()
|
||
|
|
except asyncio.CancelledError:
|
||
|
|
break
|
||
|
|
except Exception:
|
||
|
|
logger.exception("StatsCollector error")
|
||
|
|
|
||
|
|
def _collect(self) -> None:
|
||
|
|
logger.debug(
|
||
|
|
f"Stats: channels={self._state.active_channels}, "
|
||
|
|
f"requests={self._state.request_count}, errors={self._state.error_count}"
|
||
|
|
)
|