File size: 13,715 Bytes
6111b2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/**

 * Compression Analytics Tab

 *

 * Shows compression request stats from call_logs (request_type = 'compression'),

 * mode breakdown, provider breakdown, and cost/token savings summary.

 */

"use client";

import { useEffect, useState } from "react";
import { useTranslations } from "next-intl";

interface CompressionAnalyticsSummary {
  totalRequests: number;
  totalTokensSaved: number;
  avgSavingsPct: number;
  avgDurationMs: number;
  byMode: Record<string, { count: number; tokensSaved: number; avgSavingsPct: number }>;
  byProvider: Record<string, { count: number; tokensSaved: number }>;
  last24h: Array<{ hour: string; count: number; tokensSaved: number }>;
  validationFallbacks: number;
  realUsage: {
    requestsWithReceipts: number;
    promptTokens: number;
    completionTokens: number;
    totalTokens: number;
    cacheReadTokens: number;
    cacheWriteTokens: number;
    estimatedUsdSaved: number;
    bySource: Record<string, number>;
  };
}

function StatCard({

  icon,

  label,

  value,

  sub,

}: {

  icon: string;

  label: string;

  value: string | number;

  sub?: string;

}) {
  return (
    <div className="card p-4 flex flex-col gap-1">

      <div className="flex items-center gap-2 text-text-muted text-sm">

        <span className="material-symbols-outlined text-[18px]">{icon}</span>

        {label}

      </div>

      <div className="text-2xl font-bold text-text">{value}</div>

      {sub && <div className="text-xs text-text-muted">{sub}</div>}

    </div>
  );
}

function ModeBar({

  mode,

  count,

  total,

  tokensSaved,

}: {

  mode: string;

  count: number;

  total: number;

  tokensSaved: number;

}) {
  const pct = total > 0 ? Math.round((count / total) * 100) : 0;
  return (
    <div className="flex flex-col gap-1">

      <div className="flex justify-between text-sm">

        <span className="font-medium text-text capitalize">{mode}</span>

        <span className="text-text-muted">

          {count} requests · {tokensSaved.toLocaleString()} tokens saved

        </span>

      </div>

      <div className="h-2 rounded-full bg-bg-muted overflow-hidden">

        <div

          className="h-full rounded-full bg-primary transition-all"

          style={{ width: `${pct}%` }}

        />

      </div>

      <div className="text-xs text-text-muted text-right">{pct}%</div>

    </div>
  );
}

function ProviderBar({

  provider,

  count,

  total,

  tokensSaved,

}: {

  provider: string;

  count: number;

  total: number;

  tokensSaved: number;

}) {
  const pct = total > 0 ? Math.round((count / total) * 100) : 0;
  return (
    <div className="flex flex-col gap-1">

      <div className="flex justify-between text-sm">

        <span className="font-medium text-text">{provider}</span>

        <span className="text-text-muted">

          {count} requests · {tokensSaved.toLocaleString()} tokens saved

        </span>

      </div>

      <div className="h-2 rounded-full bg-bg-muted overflow-hidden">

        <div

          className="h-full rounded-full bg-primary transition-all"

          style={{ width: `${pct}%` }}

        />

      </div>

      <div className="text-xs text-text-muted text-right">{pct}%</div>

    </div>
  );
}

export default function CompressionAnalyticsTab() {
  const t = useTranslations("analytics");
  const [stats, setStats] = useState<CompressionAnalyticsSummary | null>(null);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);
  const [since, setSince] = useState<"24h" | "7d" | "30d" | "all">("24h");

  useEffect(() => {
    fetch(`/api/analytics/compression?since=${since}`)
      .then((r) => r.json())
      .then((d) => {
        setStats(d);
        setLoading(false);
      })
      .catch((e) => {
        setError(e.message);
        setLoading(false);
      });
  }, [since]);

  if (loading) {
    return (
      <div className="flex items-center justify-center py-16 text-text-muted">

        <span className="material-symbols-outlined animate-spin mr-2">progress_activity</span>

        Loading compression analytics…

      </div>
    );
  }

  if (error || !stats) {
    return (
      <div className="card p-6 text-center text-text-muted">

        <span className="material-symbols-outlined text-[32px] mb-2 block">compress</span>

        {error || "No compression data yet."}

        <p className="text-xs mt-2">

          Compression requests will appear here after the first request via /v1/chat/completions

          with compression enabled.

        </p>

      </div>
    );
  }

  const modes = Object.entries(stats.byMode).sort(([, a], [, b]) => b.count - a.count);
  const providers = Object.entries(stats.byProvider).sort(([, a], [, b]) => b.count - a.count);

  // Calculate max tokens for hourly chart scaling
  const maxTokensPerHour = Math.max(...stats.last24h.map((h) => h.tokensSaved), 1);
  const maxCountPerHour = Math.max(...stats.last24h.map((h) => h.count), 1);

  return (
    <div className="flex flex-col gap-6">

      {/* Time Range Selector */}

      <div className="flex gap-2">

        {(["24h", "7d", "30d", "all"] as const).map((range) => (

          <button

            key={range}

            onClick={() => setSince(range)}

            className={`px-3 py-1 rounded text-sm transition-colors ${

              since === range

                ? "bg-primary text-primary-foreground"

                : "bg-bg-muted text-text-muted hover:bg-bg-muted/80"

            }`}

          >

            {range === "24h"

              ? "Last 24h"

              : range === "7d"

                ? "Last 7d"

                : range === "30d"

                  ? "Last 30d"

                  : "All time"}

          </button>

        ))}

      </div>



      {/* KPI Cards */}

      <div className="grid grid-cols-2 md:grid-cols-6 gap-4">

        <StatCard

          icon="compress"

          label={t("compressionAnalyticsTotalRequests")}

          value={stats.totalRequests.toLocaleString()}

        />

        <StatCard

          icon="token"

          label={t("compressionAnalyticsTokensSaved")}

          value={stats.totalTokensSaved.toLocaleString()}

        />

        <StatCard

          icon="percent"

          label={t("compressionAnalyticsAvgSavings")}

          value={`${stats.avgSavingsPct}%`}

        />

        <StatCard

          icon="timer"

          label={t("compressionAnalyticsAvgDuration")}

          value={`${stats.avgDurationMs}ms`}

        />

        <StatCard

          icon="receipt_long"

          label={t("compressionAnalyticsReceipts")}

          value={stats.realUsage.requestsWithReceipts.toLocaleString()}

          sub={`${stats.realUsage.totalTokens.toLocaleString()} real tokens`}

        />

        <StatCard

          icon="verified"

          label={t("compressionAnalyticsFallbacks")}

          value={stats.validationFallbacks.toLocaleString()}

          sub="validation restores"

        />

      </div>



      {stats.realUsage.requestsWithReceipts > 0 && (

        <div className="card p-5">

          <h3 className="font-semibold text-text mb-4 flex items-center gap-2">

            <span className="material-symbols-outlined text-primary text-[20px]">receipt_long</span>

            Real Usage Receipts

          </h3>

          <div className="grid grid-cols-1 md:grid-cols-5 gap-4 text-sm">

            <div>

              <div className="text-text-muted">{t("compressionAnalyticsPromptTokens")}</div>

              <div className="text-lg font-semibold text-text">

                {stats.realUsage.promptTokens.toLocaleString()}

              </div>

            </div>

            <div>

              <div className="text-text-muted">{t("compressionAnalyticsCompletionTokens")}</div>

              <div className="text-lg font-semibold text-text">

                {stats.realUsage.completionTokens.toLocaleString()}

              </div>

            </div>

            <div>

              <div className="text-text-muted">{t("compressionAnalyticsTotalTokens")}</div>

              <div className="text-lg font-semibold text-text">

                {stats.realUsage.totalTokens.toLocaleString()}

              </div>

            </div>

            <div>

              <div className="text-text-muted">{t("compressionAnalyticsCacheTokens")}</div>

              <div className="text-lg font-semibold text-text">

                {(

                  (stats.realUsage.cacheReadTokens ?? 0) + (stats.realUsage.cacheWriteTokens ?? 0)

                ).toLocaleString()}

              </div>

            </div>

            <div>

              <div className="text-text-muted">Sources</div>

              <div className="text-lg font-semibold text-text">

                {Object.entries(stats.realUsage.bySource)

                  .map(([source, count]) => `${source}: ${count}`)

                  .join(", ")}

              </div>

            </div>

          </div>

        </div>

      )}



      {/* Mode Breakdown */}

      {modes.length > 0 && (

        <div className="card p-5">

          <h3 className="font-semibold text-text mb-4 flex items-center gap-2">

            <span className="material-symbols-outlined text-primary text-[20px]">tune</span>

            Mode Breakdown

          </h3>

          <div className="flex flex-col gap-4">

            {modes.map(([mode, data]) => (

              <ModeBar

                key={mode}

                mode={mode}

                count={data.count}

                total={stats.totalRequests}

                tokensSaved={data.tokensSaved}

              />

            ))}

          </div>

        </div>

      )}



      {/* Provider Breakdown */}

      {providers.length > 0 && (

        <div className="card p-5">

          <h3 className="font-semibold text-text mb-4 flex items-center gap-2">

            <span className="material-symbols-outlined text-primary text-[20px]">hub</span>

            Provider Breakdown

          </h3>

          <div className="flex flex-col gap-4">

            {providers.map(([prov, data]) => (

              <ProviderBar

                key={prov}

                provider={prov}

                count={data.count}

                total={stats.totalRequests}

                tokensSaved={data.tokensSaved}

              />

            ))}

          </div>

        </div>

      )}



      {/* Last 24h Hourly Chart (CSS-only height-based bars) */}

      {stats.last24h.length > 0 && (

        <div className="card p-5">

          <h3 className="font-semibold text-text mb-4 flex items-center gap-2">

            <span className="material-symbols-outlined text-primary text-[20px]">show_chart</span>

            Last 24 Hours (Activity)

          </h3>

          <div className="flex items-end gap-2 h-48">

            {stats.last24h.map((entry, idx) => {

              const countPct = (entry.count / maxCountPerHour) * 100;

              const tokenPct = (entry.tokensSaved / maxTokensPerHour) * 100;

              return (

                <div key={idx} className="flex-1 flex flex-col items-center gap-2">

                  <div

                    className="w-full rounded-t-sm bg-gradient-to-b from-primary to-primary/70 transition-all hover:opacity-80 cursor-pointer group relative"

                    style={{ height: `${Math.max(countPct, 5)}%` }}

                    title={`${entry.hour}: ${entry.count} requests, ${entry.tokensSaved.toLocaleString()} tokens saved`}

                  >

                    <div className="absolute -top-6 left-0 right-0 opacity-0 group-hover:opacity-100 transition-opacity text-xs text-text-muted whitespace-nowrap text-center">

                      {entry.count}

                    </div>

                  </div>

                  <div className="text-xs text-text-muted rotate-45 origin-left">

                    {entry.hour.substring(11, 13)}

                  </div>

                </div>

              );

            })}

          </div>

          <div className="mt-4 grid grid-cols-2 gap-2 text-xs text-text-muted">

            <div>Max requests/hour: {maxCountPerHour}</div>

            <div>Max tokens/hour: {maxTokensPerHour.toLocaleString()}</div>

          </div>

        </div>

      )}



      {/* Empty state */}

      {stats.totalRequests === 0 && (

        <div className="card p-8 text-center text-text-muted">

          <span className="material-symbols-outlined text-[48px] mb-3 block text-primary opacity-50">

            compress

          </span>

          <p className="font-medium text-text">{t("compressionAnalyticsNoDataYet")}</p>

          <p className="text-sm mt-1">

            Use <code className="bg-bg-muted px-1 rounded">POST /v1/chat/completions</code> with

            compression configuration to start tracking compression analytics.

          </p>

        </div>

      )}



      {/* Info note */}

      <div className="text-xs text-text-muted border border-border rounded-lg p-3 flex items-start gap-2">

        <span className="material-symbols-outlined text-[16px] text-blue-500 mt-0.5">info</span>

        <span>

          <strong>Compression analytics:</strong> Token savings tracked per mode (off, lite,

          standard, aggressive, ultra, RTK, stacked), engine, compression combo, and provider. Hover

          over charts for details. Use the time selector to view different time periods.

        </span>

      </div>

    </div>
  );
}