Files

47 lines
1.3 KiB
Python

from dataclasses import dataclass, field
from datetime import datetime, timezone
from app.models import AnalyticsSummary, FeedbackRequest
@dataclass
class FeedbackEvent:
message_id: str
helped: bool
query: str
system: str | None
confidence: float
created_at: str
class AnalyticsStore:
def __init__(self) -> None:
self._events: list[FeedbackEvent] = []
def record(self, request: FeedbackRequest) -> FeedbackEvent:
event = FeedbackEvent(
message_id=request.message_id,
helped=request.helped,
query=request.query,
system=request.system,
confidence=request.confidence,
created_at=datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S"),
)
self._events.append(event)
return event
def summary(self) -> AnalyticsSummary:
total = len(self._events)
helped = sum(1 for e in self._events if e.helped)
not_helped = total - helped
deflection = round((helped / total) * 100, 1) if total else 0.0
return AnalyticsSummary(
total_feedback=total,
helped_count=helped,
not_helped_count=not_helped,
deflection_rate=deflection,
)
analytics_store = AnalyticsStore()