File size: 1,306 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
import { NextResponse } from "next/server";
import { requireManagementAuth } from "@/lib/api/requireManagementAuth";
import {
  getAutoRoutingTotalCount,
  getAutoRoutingVariantBreakdown,
  getAutoRoutingTopProviders,
} from "@/lib/db/usageLogs";

export const dynamic = "force-dynamic";

/**
 * GET /api/analytics/auto-routing
 * Returns auto-routing usage statistics and metrics.
 */
export async function GET(request: Request) {
  const authError = await requireManagementAuth(request);
  if (authError) return authError;
  try {
    // Query usage_logs for auto/ prefix requests
    const totalRequests = getAutoRoutingTotalCount();

    // Variant breakdown
    const variantRows = getAutoRoutingVariantBreakdown();

    const variantBreakdown: Record<string, number> = {};
    variantRows.forEach((row) => {
      variantBreakdown[row.variant] = row.count;
    });

    // Top providers (from LKGP cache or usage logs)
    const topProviders = getAutoRoutingTopProviders();

    return NextResponse.json({
      totalRequests: totalRequests.count,
      variantBreakdown,
      topProviders,
    });
  } catch (error) {
    console.error("Auto-routing analytics error:", error);
    return NextResponse.json({
      totalRequests: 0,
      variantBreakdown: {},
      topProviders: [],
    });
  }
}