Repository navigation
feat: add admin org stats - #2904
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
📝 WalkthroughWalkthroughAdds an admin organisation statistics page, a TRPC endpoint returning paginated per-organisation monthly counts, a Remix page with debounced search/claim/period filters synced to URL, and a DataTable component with URL-driven sorting and pagination. ChangesOrganisation Stats Feature
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
Adds an admin-facing “Organisation Stats” view to browse monthly organisation usage, including server-side filtering/sorting and a small shared utility for deriving the current UTC month period.
Changes:
- Add a new admin TRPC route (
admin.organisation.findStats) backed by a Kysely query overOrganisationMonthlyStatwith filters (period/query/claim) and sortable columns. - Add a new Remix admin page (
/admin/organisation-stats) plus navigation entry and a data table UI with sorting/pagination. - Introduce
currentMonthlyPeriod()as a shared universal helper and update existing callers to use it.
Reviewed changes
Copilot reviewed 8 out of 9 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/trpc/server/admin-router/router.ts | Wires the new organisation.findStats admin endpoint into the admin router. |
| packages/trpc/server/admin-router/reset-organisation-monthly-stat.ts | Switches to the new shared currentMonthlyPeriod() helper. |
| packages/trpc/server/admin-router/find-organisation-stats.types.ts | Defines request/response schemas for the new org stats endpoint. |
| packages/trpc/server/admin-router/find-organisation-stats.ts | Implements the new org stats query (filters, ordering, pagination). |
| packages/lib/universal/monthly-period.ts | Adds currentMonthlyPeriod() helper returning UTC YYYY-MM. |
| packages/lib/server-only/rate-limit/check-monthly-quota.ts | Switches to the new shared currentMonthlyPeriod() helper. |
| apps/remix/app/routes/_authenticated+/admin+/organisation-stats._index.tsx | Adds the new admin page with period/claim filters and search param syncing. |
| apps/remix/app/routes/_authenticated+/admin+/_layout.tsx | Adds nav link entry for the new “Organisation Stats” page. |
| apps/remix/app/components/tables/admin-organisation-stats-table.tsx | Adds the sortable/paginated stats table using the new TRPC endpoint. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| period: z | ||
| .string() | ||
| .regex(/^\d{4}-\d{2}$/, 'Period must be in YYYY-MM format.') | ||
| .describe('Filter stats by UTC calendar month in `YYYY-MM` form, e.g. "2026-05".') | ||
| .optional(), |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@apps/remix/app/routes/_authenticated`+/admin+/organisation-stats._index.tsx:
- Around line 71-86: The search effect currently updates query but doesn't reset
pagination, which can leave users on an invalid page; inside the useEffect that
builds params from searchParams, compare the current query value
(searchParams.get('query') || '') to debouncedSearchQuery and if they differ,
call params.delete('page') before calling setSearchParams; keep the existing
guard that returns when params.toString() === searchParams?.toString() and still
only call setSearchParams(params) when something actually changed (use the
existing debouncedSearchQuery, searchParams, setSearchParams, pathname,
useEffect symbols).
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: 4727eaa3-cb71-4790-b838-c00f0ff5d431
📒 Files selected for processing (9)
apps/remix/app/components/tables/admin-organisation-stats-table.tsxapps/remix/app/routes/_authenticated+/admin+/_layout.tsxapps/remix/app/routes/_authenticated+/admin+/organisation-stats._index.tsxpackages/lib/server-only/rate-limit/check-monthly-quota.tspackages/lib/universal/monthly-period.tspackages/trpc/server/admin-router/find-organisation-stats.tspackages/trpc/server/admin-router/find-organisation-stats.types.tspackages/trpc/server/admin-router/reset-organisation-monthly-stat.tspackages/trpc/server/admin-router/router.ts
c97bcfa to
1ac51c9
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/trpc/server/admin-router/find-organisation-stats.ts`:
- Around line 116-138: The current pagination loses the total when rows is empty
because count is read from the windowed totalRows of the first row; update the
logic in find-organisation-stats.ts so that if rows.length === 0 you run a
dedicated cheap count query (reusing the same filter conditions used to build
dataQuery) to populate count (or alternatively always run that separate COUNT(*)
query alongside dataQuery), then compute totalPages = Math.ceil(count / perPage)
and return the response (the relevant symbols: dataQuery, rows, count, page,
perPage, totalPages, FindResultResponse) so an empty data page still reports the
correct total.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: 8c361608-de31-46a0-89b4-33bf8aa25e5e
📒 Files selected for processing (9)
apps/remix/app/components/tables/admin-organisation-stats-table.tsxapps/remix/app/routes/_authenticated+/admin+/_layout.tsxapps/remix/app/routes/_authenticated+/admin+/organisation-stats._index.tsxpackages/lib/server-only/rate-limit/check-monthly-quota.tspackages/lib/universal/monthly-period.tspackages/trpc/server/admin-router/find-organisation-stats.tspackages/trpc/server/admin-router/find-organisation-stats.types.tspackages/trpc/server/admin-router/reset-organisation-monthly-stat.tspackages/trpc/server/admin-router/router.ts
| const rows = await dataQuery.execute(); | ||
|
|
||
| const count = rows.length > 0 ? Number(rows[0].totalRows) : 0; | ||
|
|
||
| const data = rows.map((row) => ({ | ||
| id: row.id, | ||
| organisationId: row.organisationId, | ||
| organisationName: row.organisationName, | ||
| originalClaimId: row.originalClaimId, | ||
| period: row.period, | ||
| documentCount: Number(row.documentCount), | ||
| emailCount: Number(row.emailCount), | ||
| apiCount: Number(row.apiCount), | ||
| totalCount: Number(row.totalCount), | ||
| })); | ||
|
|
||
| return { | ||
| data, | ||
| count, | ||
| currentPage: Math.max(page, 1), | ||
| perPage, | ||
| totalPages: Math.ceil(count / perPage), | ||
| } satisfies FindResultResponse<typeof data>; |
There was a problem hiding this comment.
Total count is lost on empty pages, breaking pagination.
count is read from the windowed totalRows of the first returned row. When offset exceeds available rows (e.g. direct navigation to an out-of-range page, or a larger perPage after filtering), rows is empty, so count collapses to 0 and totalPages to 0 — the footer then misreports totals and the user can get stranded on an empty page with no way back via pagination.
Run a dedicated count query (cheap, no window function) when the data page is empty, or always derive count separately.
🐛 Proposed fix sketch
const rows = await dataQuery.execute();
- const count = rows.length > 0 ? Number(rows[0].totalRows) : 0;
+ let count = rows.length > 0 ? Number(rows[0].totalRows) : 0;
+
+ // The windowed totalRows is only available on returned rows. For an
+ // out-of-range page the result set is empty, so fall back to a count query.
+ if (rows.length === 0 && offset > 0) {
+ const { total } = await baseQuery
+ .select((eb) => eb.fn.countAll().as('total'))
+ .executeTakeFirstOrThrow();
+
+ count = Number(total);
+ }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@packages/trpc/server/admin-router/find-organisation-stats.ts` around lines
116 - 138, The current pagination loses the total when rows is empty because
count is read from the windowed totalRows of the first row; update the logic in
find-organisation-stats.ts so that if rows.length === 0 you run a dedicated
cheap count query (reusing the same filter conditions used to build dataQuery)
to populate count (or alternatively always run that separate COUNT(*) query
alongside dataQuery), then compute totalPages = Math.ceil(count / perPage) and
return the response (the relevant symbols: dataQuery, rows, count, page,
perPage, totalPages, FindResultResponse) so an empty data page still reports the
correct total.
Description
Add ability to view the monthly org stats