File size: 6,283 Bytes
cd8bd0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"use client";

import { useEffect, useState } from "react";
import { Card } from "@/shared/components";
import { useTranslations } from "next-intl";

interface AutoRoutingStats {
  totalRequests: number;
  variantBreakdown: Record<string, number>;
  avgSelectionScore: number;
  topProviders: Array<{ provider: string; count: number }>;
  explorationRate: number;
  lkgpHitRate: number;
}

export default function AutoRoutingAnalyticsTab() {
  const [stats, setStats] = useState<AutoRoutingStats | null>(null);
  const [loading, setLoading] = useState(true);
  const t = useTranslations("analytics");

  useEffect(() => {
    fetch("/api/analytics/auto-routing")
      .then((res) => res.json())
      .then((data) => {
        setStats(data);
        setLoading(false);
      })
      .catch(() => setLoading(false));
  }, []);

  if (loading) {
    return (
      <Card>
        <div className="animate-pulse space-y-4">
          <div className="h-4 bg-border rounded w-1/4"></div>
          <div className="h-20 bg-border rounded"></div>
        </div>
      </Card>
    );
  }

  if (!stats) {
    return (
      <Card>
        <div className="text-center py-8 text-text-muted">
          No auto-routing analytics available. Make requests using the auto/ prefix to see metrics.
        </div>
      </Card>
    );
  }

  return (
    <div className="space-y-6">
      {/* Summary Cards */}
      <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4">
        <Card className="p-4">
          <div className="flex items-center gap-3">
            <div className="p-2 rounded-lg bg-indigo-500/10 text-indigo-500">
              <span className="material-symbols-outlined text-[20px]">auto_awesome</span>
            </div>
            <div>
              <p className="text-sm text-text-muted">{t("autoRoutingTotalAutoRequests")}</p>
              <p className="text-2xl font-bold">{stats.totalRequests.toLocaleString()}</p>
            </div>
          </div>
        </Card>

        <Card className="p-4">
          <div className="flex items-center gap-3">
            <div className="p-2 rounded-lg bg-green-500/10 text-green-500">
              <span className="material-symbols-outlined text-[20px]">target</span>
            </div>
            <div>
              <p className="text-sm text-text-muted">{t("autoRoutingAvgSelectionScore")}</p>
              <p className="text-2xl font-bold">{(stats.avgSelectionScore * 100).toFixed(1)}%</p>
            </div>
          </div>
        </Card>

        <Card className="p-4">
          <div className="flex items-center gap-3">
            <div className="p-2 rounded-lg bg-amber-500/10 text-amber-500">
              <span className="material-symbols-outlined text-[20px]">explore</span>
            </div>
            <div>
              <p className="text-sm text-text-muted">{t("autoRoutingExplorationRate")}</p>
              <p className="text-2xl font-bold">{(stats.explorationRate * 100).toFixed(1)}%</p>
            </div>
          </div>
        </Card>

        <Card className="p-4">
          <div className="flex items-center gap-3">
            <div className="p-2 rounded-lg bg-emerald-500/10 text-emerald-500">
              <span className="material-symbols-outlined text-[20px]">history</span>
            </div>
            <div>
              <p className="text-sm text-text-muted">{t("autoRoutingLkgpHitRate")}</p>
              <p className="text-2xl font-bold">{(stats.lkgpHitRate * 100).toFixed(1)}%</p>
            </div>
          </div>
        </Card>
      </div>

      {/* Variant Breakdown */}
      <Card className="p-6">
        <h3 className="text-lg font-semibold mb-4">{t("autoRoutingRequestsByVariant")}</h3>
        <div className="space-y-3">
          {Object.entries(stats.variantBreakdown).map(([variant, count]) => {
            const percentage = stats.totalRequests > 0 ? (count / stats.totalRequests) * 100 : 0;
            return (
              <div key={variant} className="flex items-center gap-3">
                <div className="w-32 text-sm font-medium capitalize">{variant || "default"}</div>
                <div className="flex-1 h-3 bg-border rounded-full overflow-hidden">
                  <div
                    className="h-full bg-indigo-500 rounded-full transition-all"
                    style={{ width: `${percentage}%` }}
                  />
                </div>
                <div className="w-20 text-sm text-text-muted text-right">
                  {count.toLocaleString()} ({percentage.toFixed(1)}%)
                </div>
              </div>
            );
          })}
        </div>
      </Card>

      {/* Top Providers */}
      <Card className="p-6">
        <h3 className="text-lg font-semibold mb-4">{t("autoRoutingTopRoutedProviders")}</h3>
        <div className="overflow-x-auto">
          <table className="w-full text-sm">
            <thead>
              <tr className="border-b border-border">
                <th className="text-left py-2 px-3 font-medium">Provider</th>
                <th className="text-right py-2 px-3 font-medium">Requests</th>
                <th className="text-right py-2 px-3 font-medium">Share</th>
              </tr>
            </thead>
            <tbody>
              {stats.topProviders.map((provider, index) => {
                const percentage =
                  stats.totalRequests > 0 ? (provider.count / stats.totalRequests) * 100 : 0;
                return (
                  <tr key={provider.provider} className="border-b border-border/50">
                    <td className="py-2 px-3">
                      <div className="flex items-center gap-2">
                        <span className="text-text-muted">#{index + 1}</span>
                        <span className="font-medium">{provider.provider}</span>
                      </div>
                    </td>
                    <td className="text-right py-2 px-3">{provider.count.toLocaleString()}</td>
                    <td className="text-right py-2 px-3 text-text-muted">
                      {percentage.toFixed(1)}%
                    </td>
                  </tr>
                );
              })}
            </tbody>
          </table>
        </div>
      </Card>
    </div>
  );
}