shared_metrics.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. """``/api/shared-metrics/consent``: the dashboard's first-run shared-metrics offer.
  2. The twin of Desktop's composer strip and the terminal's offer: one answer per profile, the same
  3. two config.yaml keys (``telemetry.shared_metrics.enabled`` / ``.send``), read and written through
  4. ``hermes_cli.observability.shared_metrics_consent`` for the ``?profile=`` being managed.
  5. """
  6. from __future__ import annotations
  7. import asyncio
  8. from typing import Optional
  9. from fastapi import APIRouter
  10. from pydantic import BaseModel
  11. from hermes_cli.web_routers._common import config_write_scope, scoped_to_thread
  12. router = APIRouter()
  13. class ConsentAnswer(BaseModel):
  14. enabled: bool
  15. send: bool = False
  16. def _read_consent() -> dict:
  17. """``{enabled, send, decided, managed}``; a managed install cannot save, so it is never offered."""
  18. from hermes_cli.config import is_managed, read_raw_config
  19. from hermes_cli.observability.shared_metrics_consent import consent_state
  20. return {**consent_state(read_raw_config()), "managed": is_managed()}
  21. @router.get("/api/shared-metrics/consent")
  22. async def get_shared_metrics_consent(profile: Optional[str] = None):
  23. return await scoped_to_thread(profile, _read_consent)
  24. @router.put("/api/shared-metrics/consent")
  25. async def put_shared_metrics_consent(body: ConsentAnswer, profile: Optional[str] = None):
  26. from hermes_cli.observability.shared_metrics_consent import save_consent
  27. def _write() -> dict:
  28. with config_write_scope(profile):
  29. save_consent(body.enabled, body.send)
  30. return _read_consent()
  31. return await asyncio.to_thread(_write)