File size: 1,179 Bytes
df56b50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Usage stats API routes.
 *
 * GET /admin/usage-stats/summary  — current cumulative totals
 * GET /admin/usage-stats/history  — time-series delta data points
 */

import { Hono } from "hono";
import type { AccountPool } from "../../auth/account-pool.js";
import type { UsageStatsStore } from "../../auth/usage-stats.js";

export function createUsageStatsRoutes(
  pool: AccountPool,
  statsStore: UsageStatsStore,
): Hono {
  const app = new Hono();

  app.get("/admin/usage-stats/summary", (c) => {
    return c.json(statsStore.getSummary(pool));
  });

  app.get("/admin/usage-stats/history", (c) => {
    const granularity = c.req.query("granularity") ?? "hourly";
    if (granularity !== "raw" && granularity !== "hourly" && granularity !== "daily") {
      c.status(400);
      return c.json({ error: "Invalid granularity. Must be raw, hourly, or daily." });
    }

    const hoursStr = c.req.query("hours") ?? "24";
    const hours = Math.min(Math.max(1, parseInt(hoursStr, 10) || 24), 168);

    const data_points = statsStore.getHistory(hours, granularity);

    return c.json({
      granularity,
      hours,
      data_points,
    });
  });

  return app;
}