page.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. "use client";
  2. import { useCallback, useEffect, useMemo, useState } from "react";
  3. import {
  4. fetchAnalyticsSummary,
  5. type AnalyticsDailyRow,
  6. type AnalyticsSummaryResponse,
  7. } from "@/features/agent/services/analyticsApi";
  8. import { Button } from "@/components/ui/button";
  9. import { toast } from "@/hooks/useToast";
  10. function formatPercent(n: number) {
  11. if (Number.isNaN(n)) return "—";
  12. return `${n.toFixed(2)}%`;
  13. }
  14. function StatCard({
  15. title,
  16. value,
  17. sub,
  18. }: {
  19. title: string;
  20. value: string | number;
  21. sub?: string;
  22. }) {
  23. return (
  24. <div className="rounded-xl border border-border/60 bg-card p-4 shadow-sm">
  25. <div className="text-xs font-medium text-muted-foreground">{title}</div>
  26. <div className="mt-1 text-2xl font-semibold tabular-nums">{value}</div>
  27. {sub ? <div className="mt-1 text-xs text-muted-foreground">{sub}</div> : null}
  28. </div>
  29. );
  30. }
  31. function DailyBars({
  32. daily,
  33. field,
  34. label,
  35. color,
  36. }: {
  37. daily: AnalyticsDailyRow[];
  38. field: keyof Pick<
  39. AnalyticsDailyRow,
  40. "widget_opens" | "sessions" | "messages" | "ai_replies"
  41. >;
  42. label: string;
  43. color: string;
  44. }) {
  45. const max = useMemo(() => {
  46. let m = 1;
  47. for (const row of daily) {
  48. const v = Number(row[field]) || 0;
  49. if (v > m) m = v;
  50. }
  51. return m;
  52. }, [daily, field]);
  53. if (daily.length === 0) {
  54. return <p className="text-sm text-muted-foreground">暂无数据</p>;
  55. }
  56. return (
  57. <div>
  58. <div className="mb-2 text-sm font-medium text-foreground">{label}</div>
  59. <div className="flex h-36 items-end gap-1 border-b border-border/40 pb-1">
  60. {daily.map((row) => {
  61. const v = Number(row[field]) || 0;
  62. const h = Math.round((v / max) * 100);
  63. return (
  64. <div
  65. key={row.date}
  66. className="flex min-w-0 flex-1 flex-col items-center justify-end gap-1"
  67. title={`${row.date}: ${v}`}
  68. >
  69. <div
  70. className="w-full max-w-[28px] rounded-t transition-all"
  71. style={{
  72. height: `${Math.max(h, v > 0 ? 8 : 0)}%`,
  73. backgroundColor: color,
  74. minHeight: v > 0 ? 4 : 0,
  75. }}
  76. />
  77. <span className="truncate text-[10px] text-muted-foreground">
  78. {row.date.slice(5)}
  79. </span>
  80. </div>
  81. );
  82. })}
  83. </div>
  84. </div>
  85. );
  86. }
  87. export default function AnalyticsPage(_props: { embedded?: boolean }) {
  88. const [from, setFrom] = useState(() => {
  89. const d = new Date();
  90. d.setDate(d.getDate() - 6);
  91. return d.toISOString().slice(0, 10);
  92. });
  93. const [to, setTo] = useState(() => new Date().toISOString().slice(0, 10));
  94. const [data, setData] = useState<AnalyticsSummaryResponse | null>(null);
  95. const [loading, setLoading] = useState(true);
  96. const load = useCallback(async () => {
  97. setLoading(true);
  98. try {
  99. const res = await fetchAnalyticsSummary(from, to);
  100. setData(res);
  101. } catch (e) {
  102. toast.error((e as Error).message);
  103. setData(null);
  104. } finally {
  105. setLoading(false);
  106. }
  107. }, [from, to]);
  108. useEffect(() => {
  109. void load();
  110. }, [load]);
  111. const t = data?.totals;
  112. return (
  113. <div
  114. className="flex flex-col min-h-0 overflow-auto p-4 max-w-6xl mx-auto w-full"
  115. >
  116. <div className="mb-6 flex flex-wrap items-end gap-4">
  117. <div>
  118. <h1 className="text-xl font-semibold tracking-tight">数据报表</h1>
  119. <p className="text-sm text-muted-foreground mt-1">
  120. 访客小窗与 AI 客服统计(按上海时区自然日,不含「知识库测试」内部会话)
  121. </p>
  122. </div>
  123. <div className="flex flex-wrap items-center gap-2 ml-auto">
  124. <label className="text-xs text-muted-foreground flex items-center gap-1">
  125. <input
  126. type="date"
  127. value={from}
  128. onChange={(e) => setFrom(e.target.value)}
  129. className="rounded-md border border-input bg-background px-2 py-1 text-sm"
  130. />
  131. </label>
  132. <label className="text-xs text-muted-foreground flex items-center gap-1">
  133. <input
  134. type="date"
  135. value={to}
  136. onChange={(e) => setTo(e.target.value)}
  137. className="rounded-md border border-input bg-background px-2 py-1 text-sm"
  138. />
  139. </label>
  140. <Button size="sm" onClick={() => void load()} disabled={loading}>
  141. {loading ? "加载中…" : "查询"}
  142. </Button>
  143. </div>
  144. </div>
  145. {data && (
  146. <p className="text-xs text-muted-foreground mb-4">{data.note}</p>
  147. )}
  148. {t && (
  149. <>
  150. <div className="grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-4 gap-3 mb-6">
  151. <StatCard title="小窗打开次数" value={t.widget_opens} sub="需前端埋点,历史数据可能为 0" />
  152. <StatCard title="新建会话数" value={t.sessions} />
  153. <StatCard title="消息数" value={t.messages} />
  154. <StatCard title="AI 回复次数" value={t.ai_replies} />
  155. <StatCard title="AI 失败次数" value={t.ai_failed} />
  156. <StatCard title="AI 失败率" value={formatPercent(t.ai_failure_rate_percent)} sub="占 AI 回复条数" />
  157. <StatCard title="知识库命中次数" value={t.kb_hits} />
  158. <StatCard title="知识库命中率" value={formatPercent(t.kb_hit_rate_percent)} sub="占成功 AI 回复" />
  159. <StatCard title="最大 AI 对话轮数" value={t.max_ai_rounds} sub="单会话内用户+AI 一轮" />
  160. <StatCard title="AI 参与会话" value={t.sessions_with_ai} sub={`占新建会话 ${formatPercent(t.ai_participation_rate_percent)}`} />
  161. <StatCard title="AI→人工(会话数)" value={t.ai_to_human_sessions} sub={`占有过 AI 发言的会话 ${formatPercent(t.ai_to_human_rate_percent)}`} />
  162. <StatCard title="人工→AI(会话数)" value={t.human_to_ai_sessions} sub={`占有过人工发言的会话 ${formatPercent(t.human_to_ai_rate_percent)}`} />
  163. </div>
  164. <div className="grid grid-cols-1 lg:grid-cols-2 gap-8 rounded-xl border border-border/60 bg-card p-4">
  165. <DailyBars
  166. daily={data!.daily}
  167. field="widget_opens"
  168. label="每日小窗打开"
  169. color="rgb(34 197 94)"
  170. />
  171. <DailyBars
  172. daily={data!.daily}
  173. field="sessions"
  174. label="每日新建会话"
  175. color="rgb(59 130 246)"
  176. />
  177. <DailyBars
  178. daily={data!.daily}
  179. field="messages"
  180. label="每日消息数"
  181. color="rgb(168 85 247)"
  182. />
  183. <DailyBars
  184. daily={data!.daily}
  185. field="ai_replies"
  186. label="每日 AI 回复"
  187. color="rgb(249 115 22)"
  188. />
  189. </div>
  190. </>
  191. )}
  192. {!loading && !t && (
  193. <p className="text-sm text-muted-foreground">暂无数据或加载失败</p>
  194. )}
  195. </div>
  196. );
  197. }