Spaces:
Sleeping
Sleeping
| from __future__ import annotations | |
| import logging | |
| from datetime import datetime, timezone | |
| from typing import Any | |
| logger = logging.getLogger("monitoring.telemetry") | |
| class MonitoringEngine: | |
| def __init__(self) -> None: | |
| self.events: list[dict[str, Any]] = [] | |
| async def record_event( | |
| self, | |
| event: str, | |
| project_id: str | None = None, | |
| status: str = "info", | |
| metadata: dict[str, Any] | None = None, | |
| ) -> dict[str, Any]: | |
| record = { | |
| "event": event, | |
| "project_id": project_id, | |
| "status": status, | |
| "metadata": metadata or {}, | |
| "timestamp": datetime.now(timezone.utc).isoformat(), | |
| } | |
| self.events.append(record) | |
| if len(self.events) > 1000: | |
| del self.events[:-1000] | |
| logger.info("telemetry=%s", record) | |
| return record | |
| async def get_events( | |
| self, | |
| project_id: str | None = None, | |
| ) -> list[dict[str, Any]]: | |
| if project_id is None: | |
| return list(self.events) | |
| return [ | |
| event | |
| for event in self.events | |
| if event.get("project_id") == project_id | |
| ] | |
| monitoring_engine = MonitoringEngine() | |