analyticsApi.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { apiUrl, getAgentHeaders } from "@/lib/config";
  2. export interface AnalyticsDailyRow {
  3. date: string;
  4. widget_opens: number;
  5. sessions: number;
  6. messages: number;
  7. ai_replies: number;
  8. }
  9. export interface AnalyticsTotals {
  10. widget_opens: number;
  11. sessions: number;
  12. messages: number;
  13. ai_replies: number;
  14. ai_failed: number;
  15. ai_failure_rate_percent: number;
  16. kb_hits: number;
  17. kb_hit_rate_percent: number;
  18. max_ai_rounds: number;
  19. sessions_with_ai: number;
  20. ai_participation_rate_percent: number;
  21. ai_to_human_sessions: number;
  22. ai_to_human_rate_percent: number;
  23. human_to_ai_sessions: number;
  24. human_to_ai_rate_percent: number;
  25. sessions_with_ai_user_msg: number;
  26. sessions_with_human_user_msg: number;
  27. }
  28. export interface AnalyticsSummaryResponse {
  29. from: string;
  30. to: string;
  31. totals: AnalyticsTotals;
  32. daily: AnalyticsDailyRow[];
  33. note: string;
  34. }
  35. export async function fetchAnalyticsSummary(
  36. from?: string,
  37. to?: string
  38. ): Promise<AnalyticsSummaryResponse> {
  39. const q = new URLSearchParams();
  40. if (from) q.set("from", from);
  41. if (to) q.set("to", to);
  42. const qs = q.toString();
  43. const url = `${apiUrl("/agent/analytics/summary")}${qs ? `?${qs}` : ""}`;
  44. const res = await fetch(url, { headers: getAgentHeaders() });
  45. if (!res.ok) {
  46. const j = await res.json().catch(() => ({}));
  47. throw new Error((j as { error?: string }).error || `请求失败 ${res.status}`);
  48. }
  49. return res.json();
  50. }