Repository navigation
feat(ui): add public /apps leaderboard page #2170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e9396fd
feat(ui): add public /apps leaderboard page
smakosh dbab83b
chore(autofix): apply diff
steebchen a42f1ba
Merge branch 'main' into feat/apps-page
smakosh 7a996fe
Merge branch 'main' into feat/apps-page
smakosh 122f316
Merge remote-tracking branch 'origin/main' into feat/apps-page
smakosh 0353944
feat(apps): use globalSourceStats for /apps page
smakosh 2969a01
Merge branch 'main' into feat/apps-page
smakosh 02f06d3
Merge branch 'feat/apps-page' of github.com:theopenco/llmgateway into…
smakosh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import { createRoute, OpenAPIHono } from "@hono/zod-openapi"; | ||
| import { z } from "zod"; | ||
|
|
||
| import { db, isNotNull, sql, tables } from "@llmgateway/db"; | ||
|
|
||
| import type { ServerTypes } from "@/vars.js"; | ||
|
|
||
| export const publicApps = new OpenAPIHono<ServerTypes>(); | ||
|
|
||
| const appStatSchema = z.object({ | ||
| source: z.string(), | ||
| totalTokens: z.number(), | ||
| totalRequests: z.number(), | ||
| lastUsedAt: z.string().nullable(), | ||
| }); | ||
|
|
||
| const listAppsRoute = createRoute({ | ||
| method: "get", | ||
| path: "/", | ||
| request: { | ||
| query: z.object({ | ||
| limit: z | ||
| .string() | ||
| .optional() | ||
| .transform((v) => (v ? Number.parseInt(v, 10) : 100)) | ||
| .pipe(z.number().int().min(1).max(500)), | ||
| }), | ||
| }, | ||
| responses: { | ||
| 200: { | ||
| content: { | ||
| "application/json": { | ||
| schema: z.object({ | ||
| apps: z.array(appStatSchema), | ||
| totalApps: z.number(), | ||
| totalTokens: z.number(), | ||
| totalRequests: z.number(), | ||
| }), | ||
| }, | ||
| }, | ||
| description: | ||
| "Aggregated token usage per app/source across all LLM Gateway traffic.", | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| publicApps.openapi(listAppsRoute, async (c) => { | ||
| const { limit } = c.req.valid("query"); | ||
|
|
||
| const rows = await db | ||
| .select({ | ||
| source: tables.log.source, | ||
| totalTokens: sql<string>`COALESCE(SUM(CAST(${tables.log.totalTokens} AS NUMERIC)), 0)`, | ||
| totalRequests: sql<string>`COUNT(*)`, | ||
| lastUsedAt: sql<Date | null>`MAX(${tables.log.createdAt})`, | ||
| }) | ||
| .from(tables.log) | ||
| .where(isNotNull(tables.log.source)) | ||
| .groupBy(tables.log.source) | ||
| .orderBy( | ||
| sql`COALESCE(SUM(CAST(${tables.log.totalTokens} AS NUMERIC)), 0) DESC`, | ||
| ) | ||
| .limit(limit); | ||
|
|
||
| const apps = rows | ||
| .filter((r): r is typeof r & { source: string } => r.source !== null) | ||
| .map((r) => ({ | ||
| source: r.source, | ||
| totalTokens: Number(r.totalTokens) || 0, | ||
| totalRequests: Number(r.totalRequests) || 0, | ||
| lastUsedAt: r.lastUsedAt ? new Date(r.lastUsedAt).toISOString() : null, | ||
| })); | ||
|
|
||
| const totalTokens = apps.reduce((sum, a) => sum + a.totalTokens, 0); | ||
| const totalRequests = apps.reduce((sum, a) => sum + a.totalRequests, 0); | ||
|
|
||
| return c.json({ | ||
| apps, | ||
| totalApps: apps.length, | ||
| totalTokens, | ||
| totalRequests, | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.