Skip to content

Commit 87489bf

Browse files
committed
refactor: move analytics endpoint to public
1 parent 001b9dc commit 87489bf

2 files changed

Lines changed: 35 additions & 63 deletions

File tree

src/routes/v1/admin.ts

Lines changed: 1 addition & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
import { Hono } from "hono";
22
import type { AppContext } from "@/types/app.js";
3-
import {
4-
queryRawRows,
5-
queryStats,
6-
queryAnalytics,
7-
startMigration,
8-
VALID_GRANULARITIES,
9-
ALLOWED_FILTERS,
10-
type Granularity,
11-
} from "@/analytics.js";
3+
import { startMigration } from "@/analytics.js";
124

135
const admin = new Hono<AppContext>();
146

@@ -19,59 +11,6 @@ admin.use("*", async (c, next) => {
1911
await next();
2012
});
2113

22-
admin.get("/telemetry/system-info", async (c) => {
23-
const limit = Math.min(Number(c.req.query("limit") || 100), 500);
24-
const offset = Number(c.req.query("offset") || 0);
25-
26-
const data = await queryRawRows(limit, offset);
27-
28-
return c.json({ data, limit, offset });
29-
});
30-
31-
admin.get("/telemetry/system-info/stats", async (c) => {
32-
const granularity = (c.req.query("granularity") || "daily") as Granularity;
33-
if (!VALID_GRANULARITIES.includes(granularity)) {
34-
return c.json(
35-
{
36-
error: `Invalid granularity. Must be one of: ${VALID_GRANULARITIES.join(", ")}`,
37-
},
38-
400,
39-
);
40-
}
41-
42-
const periods = Math.min(Number(c.req.query("periods") || 30), 365);
43-
44-
const data = await queryStats(granularity, periods);
45-
46-
return c.json({ data, granularity, periods });
47-
});
48-
49-
admin.get("/analytics", async (c) => {
50-
const granularity = (c.req.query("granularity") || "daily") as Granularity;
51-
if (!VALID_GRANULARITIES.includes(granularity)) {
52-
return c.json(
53-
{
54-
error: `Invalid granularity. Must be one of: ${VALID_GRANULARITIES.join(", ")}`,
55-
},
56-
400,
57-
);
58-
}
59-
60-
const periods = Math.min(Number(c.req.query("periods") || 30), 365);
61-
62-
const filters: Record<string, string> = {};
63-
for (const key of Object.keys(ALLOWED_FILTERS)) {
64-
const value = c.req.query(key);
65-
if (value) {
66-
filters[key] = value;
67-
}
68-
}
69-
70-
const data = await queryAnalytics(granularity, periods, filters);
71-
72-
return c.json({ data, filters, granularity, periods });
73-
});
74-
7514
admin.post("/telemetry/migrate", (c) => {
7615
if (!startMigration()) {
7716
return c.json({ error: "Migration already in progress" }, 409);

src/routes/v1/telemetry.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@ import { Hono } from "hono";
22
import { zValidator } from "@hono/zod-validator";
33
import { rateLimiter } from "hono-rate-limiter";
44
import type { AppContext } from "@/types/app.js";
5-
import { upsertSystemInfo, deleteUserData } from "@/analytics.js";
5+
import {
6+
upsertSystemInfo,
7+
deleteUserData,
8+
queryAnalytics,
9+
VALID_GRANULARITIES,
10+
ALLOWED_FILTERS,
11+
type Granularity,
12+
} from "@/analytics.js";
613
import { systemInfoSchema, forgetSchema } from "@/schemas/telemetry.js";
714

815
const telemetry = new Hono<AppContext>();
@@ -48,4 +55,30 @@ telemetry.post(
4855
},
4956
);
5057

58+
telemetry.get("/analytics", async (c) => {
59+
const granularity = (c.req.query("granularity") || "daily") as Granularity;
60+
if (!VALID_GRANULARITIES.includes(granularity)) {
61+
return c.json(
62+
{
63+
error: `Invalid granularity. Must be one of: ${VALID_GRANULARITIES.join(", ")}`,
64+
},
65+
400,
66+
);
67+
}
68+
69+
const periods = Math.min(Number(c.req.query("periods") || 30), 365);
70+
71+
const filters: Record<string, string> = {};
72+
for (const key of Object.keys(ALLOWED_FILTERS)) {
73+
const value = c.req.query(key);
74+
if (value) {
75+
filters[key] = value;
76+
}
77+
}
78+
79+
const data = await queryAnalytics(granularity, periods, filters);
80+
81+
return c.json({ data, filters, granularity, periods });
82+
});
83+
5184
export default telemetry;

0 commit comments

Comments
 (0)