packages/server/src/routes/stats.ts 1.0 K raw
1
import { Hono } from "hono";
2
import type { Bindings } from "../types";
3
4
const stats = new Hono<{ Bindings: Bindings }>();
5
6
stats.get("/", async (c) => {
7
  try {
8
    const db = c.env.DB;
9
    const [records, pdsCache, recordCache, pubCache] = await Promise.all([
10
      db
11
        .prepare("SELECT COUNT(*) as count FROM repo_records")
12
        .first<{ count: number }>(),
13
      db
14
        .prepare("SELECT COUNT(*) as count FROM pds_cache")
15
        .first<{ count: number }>(),
16
      db
17
        .prepare("SELECT COUNT(*) as count FROM record_cache")
18
        .first<{ count: number }>(),
19
      db
20
        .prepare("SELECT COUNT(*) as count FROM publication_cache")
21
        .first<{ count: number }>(),
22
    ]);
23
24
    return c.json({
25
      repo_records: records?.count || 0,
26
      pds_cache: pdsCache?.count || 0,
27
      record_cache: recordCache?.count || 0,
28
      publication_cache: pubCache?.count || 0,
29
    });
30
  } catch (error) {
31
    return c.json(
32
      { error: "Failed to fetch stats", details: String(error) },
33
      500
34
    );
35
  }
36
});
37
38
export default stats;