File size: 2,762 Bytes
4c2a557
a572854
 
4c2a557
 
a572854
 
 
 
 
4c2a557
 
 
 
 
a572854
 
4c2a557
 
 
 
 
 
 
a572854
4c2a557
 
 
 
 
 
 
 
 
 
 
 
a572854
4c2a557
 
 
 
 
 
 
 
 
 
 
 
a572854
4c2a557
 
 
 
 
a572854
4c2a557
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a572854
 
 
4c2a557
 
 
 
 
 
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
import { NextResponse } from "next/server";
import { query } from "@/lib/db/client";
import { verifyApiToken } from "@/lib/auth";

export async function GET(request: Request) {
  const authError = verifyApiToken(request);
  if (authError) {
    return authError;
  }

  try {
    const { searchParams } = new URL(request.url);
    const startTime = searchParams.get("startTime");
    const endTime = searchParams.get("endTime");

    console.log("Query params:", [startTime, endTime]);

    const timeFilter =
      startTime && endTime ? `WHERE use_time >= $1 AND use_time <= $2` : "";

    const params = startTime && endTime ? [startTime, endTime] : [];

    const [modelResult, userResult, timeRangeResult, statsResult] =
      await Promise.all([
        query(
          `
        SELECT 
          model_name,
          COUNT(*) as total_count,
          COALESCE(SUM(cost), 0) as total_cost
        FROM user_usage_records
        ${timeFilter}
        GROUP BY model_name
        ORDER BY total_cost DESC
      `,
          params
        ),
        query(
          `
        SELECT 
          nickname,
          COUNT(*) as total_count,
          COALESCE(SUM(cost), 0) as total_cost
        FROM user_usage_records
        ${timeFilter}
        GROUP BY nickname
        ORDER BY total_cost DESC
      `,
          params
        ),
        query(`
        SELECT 
          MIN(use_time) as min_time,
          MAX(use_time) as max_time
        FROM user_usage_records
      `),
        query(
          `
        SELECT 
          COALESCE(SUM(input_tokens + output_tokens), 0) as total_tokens,
          COUNT(*) as total_calls
        FROM user_usage_records
        ${timeFilter}
      `,
          params
        ),
      ]);

    const formattedData = {
      models: modelResult.rows.map((row) => ({
        model_name: row.model_name,
        total_count: parseInt(row.total_count),
        total_cost: parseFloat(row.total_cost),
      })),
      users: userResult.rows.map((row) => ({
        nickname: row.nickname,
        total_count: parseInt(row.total_count),
        total_cost: parseFloat(row.total_cost),
      })),
      timeRange: {
        minTime: timeRangeResult.rows[0].min_time,
        maxTime: timeRangeResult.rows[0].max_time,
      },
      stats: {
        totalTokens: parseInt(statsResult.rows[0].total_tokens),
        totalCalls: parseInt(statsResult.rows[0].total_calls),
      },
    };

    return NextResponse.json(formattedData);
  } catch (error) {
    console.error("Fail to fetch usage records:", error);
    if (error instanceof Error) {
      console.error("[DB Query Error]", error);
    }
    return NextResponse.json(
      { error: "Fail to fetch usage records" },
      { status: 500 }
    );
  }
}