feat: harden analytics dashboard

This commit is contained in:
2026-05-27 15:19:31 +08:00
parent 5e0ca6a504
commit 356039d9cf
34 changed files with 1424 additions and 879 deletions

View File

@@ -1,36 +1,57 @@
This is a [Next.js](https://nextjs.org) project bootstrapped with [`create-next-app`](https://nextjs.org/docs/app/api-reference/cli/create-next-app).
# New API Analytics
## Getting Started
Internal analytics dashboard for New API usage logs. The app reads PostgreSQL log data and visualizes calls, token consumption, model usage, channel usage, per-user aggregation, and raw request logs.
First, run the development server:
## Requirements
- Bun for local development and tests.
- Node.js 20 for the production Docker image.
- PostgreSQL database with New API tables described in `docs/database.md`.
- `PG_CONNECTION_STRING` set to a PostgreSQL connection string.
## Local Development
```bash
npm run dev
# or
yarn dev
# or
pnpm dev
# or
bun dev
bun install
bun run dev
```
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
Open `http://localhost:3000`.
You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.
## Verification
This project uses [`next/font`](https://nextjs.org/docs/app/building-your-application/optimizing/fonts) to automatically optimize and load [Geist](https://vercel.com/font), a new font family for Vercel.
```bash
bun test
bun run lint
bun run build
```
## Learn More
`bun test` covers parser helpers, metric conversion, query behavior, cache behavior, theme helpers, and selected API route guardrails.
To learn more about Next.js, take a look at the following resources:
## Configuration
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
Create an environment file or export:
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js) - your feedback and contributions are welcome!
```bash
PG_CONNECTION_STRING=postgres://user:password@host:5432/database
```
## Deploy on Vercel
The app uses this variable in `lib/db.ts` to create a `pg` connection pool.
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
## Deployment
Check out our [Next.js deployment documentation](https://nextjs.org/docs/app/building-your-application/deploying) for more details.
The included Dockerfile builds a standalone Next.js output and starts `server.js` on port `8019`.
```bash
docker compose up -d --build
```
`docker-compose.yml` expects `.env.production` and an external Docker network named `sinobridge`.
## Operational Notes
- API query parameters are validated at the route layer. Invalid integers or reversed date ranges return HTTP 400.
- Ranking limits are capped at 100 rows per request.
- Log page size is capped at 200 rows per request.
- Query results are cached in-process for 120 seconds with a 500-entry cap.
- Metric definitions and quota conversion are documented in `docs/metrics.md`.