airi/server/apps/api/src
RainbowBird fe0b8fa73f
feat(api-server): index llm_request_log by created_at (#3457)
## Summary

Add the index `llm_request_log_created_idx` on
`llm_request_log(created_at)`.

The admin API reads the request log by time window across all users:
request statistics, and since
[proj-airi/backend#68](https://github.com/proj-airi/backend/pull/68) the
provider health query and minute/hour windows. The table has indexes
only for `(user_id, created_at)`, `(user_id, request_id)`, `(state,
started_at)` and `(gateway, generation_id)`. Without a user filter,
those queries scan the whole table. The Go Ent mirror in
proj-airi/backend already declares this index, but no migration created
it.

## Change map

- `server/apps/api/src/schemas/llm-request-log.ts`: the new index.
- `server/apps/api/drizzle/0034_llm_request_log_created_index.sql` and
`meta/`: generated by `drizzle-kit generate --name
llm_request_log_created_index`. The only statement is the new index. I
changed it to `CREATE INDEX IF NOT EXISTS`.

## Boundaries and risks

- The migrator runs pending migrations in one transaction, so the
migration cannot use `CONCURRENTLY`. A plain `CREATE INDEX` blocks
writes to `llm_request_log` while it builds. Request log rows are
written after the response is sent, so a short block delays log writes,
not user requests.
- To avoid the block, run this on production before the deploy. The
migration then skips the existing index:

  ```sql
CREATE INDEX CONCURRENTLY IF NOT EXISTS "llm_request_log_created_idx" ON
"llm_request_log" USING btree ("created_at");
  ```

- Rollback: `DROP INDEX IF EXISTS "llm_request_log_created_idx";`. No
data changes.

## Verification

In rc, on a clone at a0d1b2ce0:

- `drizzle-kit generate`: no change other than the new index.
- `pnpm -F @proj-airi/api-server typecheck`: pass.
- `server/apps/api` Vitest: 88 files passed, 2 skipped; 975 tests
passed, 4 skipped.
- `eslint` on the changed schema file: no errors.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 16:28:46 +08:00
..
libs chore: upgrade injeca to 0.3 and move lifecycle hooks into build (#3412) 2026-11-06 14:53:50 +08:00
middlewares refactor(server): share one error model between the API and Auth services (#2881) 2026-10-17 17:34:37 +08:00
otel feat(api-server): report GenAI metrics with the OTel semantic convention names (#3450) 2026-11-08 19:32:21 +00:00
routes feat(api-server): report GenAI metrics with the OTel semantic convention names (#3450) 2026-11-08 19:32:21 +00:00
schemas feat(api-server): index llm_request_log by created_at (#3457) 2026-11-09 16:28:46 +08:00
scripts/otel feat(api-server): report GenAI metrics with the OTel semantic convention names (#3450) 2026-11-08 19:32:21 +00:00
services feat(api-server): report GenAI metrics with the OTel semantic convention names (#3450) 2026-11-08 19:32:21 +00:00
types refactor: remove five any types from api-server, ui-loading-screens, and ui-transitions (#3333) 2026-11-03 16:22:07 +08:00
utils feat(api-server): report GenAI metrics with the OTel semantic convention names (#3450) 2026-11-08 19:32:21 +00:00
app-type.ts chore: migrate services to integration folder 2026-08-02 20:12:13 +08:00
app.test.ts feat(api): add Redis Streams infrastructure and publish account events (#3025) 2026-10-19 02:05:06 +08:00
app.ts chore: upgrade injeca to 0.5.1 (#3436) 2026-11-08 06:41:31 +08:00
main.ts refactor(server): split independent auth service (#2202) 2026-08-12 23:33:58 +08:00
server.ts Revert "style: lint" 2026-08-26 20:13:10 +08:00