Files
supabase-vercel-tedd[bot] be19e47cf3 fix(studio): keep homepage service chart order stable across intervals (#53522)
## Problem

On the project homepage, the service usage cards (Postgres, Edge
Functions, Auth, Storage, Realtime, API Gateway) were sorted by request
total for the selected interval. Changing the interval changes the
totals, so the cards reshuffled every time the interval changed (and
while data was loading, when totals are briefly zero).

## Solution

- `buildSortedServiceCards` → `buildServiceCards` in
`ProjectUsage.metrics.ts`: it still drops disabled services and maps
stats, but keeps the order the services are declared in
`ProjectUsageSectionDeltas.tsx` instead of sorting by traffic.
- Removed the now-unused `sortServicesByTraffic` helper and its tests.
- Added a test that the card order is the same when totals change.

## How to verify

On a project's homepage (Studio preview), in the usage section with the
"Total Requests" / "Success Rate" header:

1. Note the order of the service cards.
2. Switch the interval dropdown between "Last 60 minutes", "Last 24
hours", and "Last 7 days".
3. The cards stay in the same order for every interval: Postgres, Edge
Functions, Auth, Storage, Realtime, API Gateway (services that are not
enabled for the project are skipped).
4. Reloading the page with any interval shows the same order, including
while the charts are loading.

## Checks run

- `pnpm turbo run typecheck --filter='...[origin/master]'`: pass
- `vitest run
components/interfaces/ProjectHome/ProjectUsage.metrics.test.ts`: 12
passed
- `eslint` on the changed files: pass
- Prettier check on the changed files: pass
- `pnpm turbo run lint --filter='[origin/master]'`:
`studio#lint:type-checks` ran out of memory twice in the sandbox
(JavaScript heap out of memory).
- `vitest related` for the changed files fails on clean master too, with
an unrelated parse error on wrapper markdown imports.

Co-authored-by: supabase-vercel-tedd[bot] <336548405+supabase-vercel-tedd[bot]@users.noreply.github.com>
2026-11-17 14:25:21 -06:00

84 lines
2.4 KiB
TypeScript

import dayjs from 'dayjs'
import utc from 'dayjs/plugin/utc'
dayjs.extend(utc)
export type LogsBarChartDatum = {
timestamp: string
error_count: number
ok_count: number
warning_count: number
}
export type ChartIntervalKey = '1hr' | '1day' | '7day'
export const sumTotal = (data: LogsBarChartDatum[]): number =>
data.reduce((acc, r) => acc + r.ok_count + r.warning_count + r.error_count, 0)
export const sumWarnings = (data: LogsBarChartDatum[]): number =>
data.reduce((acc, r) => acc + r.warning_count, 0)
export const sumErrors = (data: LogsBarChartDatum[]): number =>
data.reduce((acc, r) => acc + r.error_count, 0)
export const computeSuccessAndNonSuccessRates = (
totalRequests: number,
totalWarnings: number,
totalErrors: number
): { successRate: number; nonSuccessRate: number } => {
if (totalRequests <= 0) return { successRate: 0, nonSuccessRate: 0 }
const nonSuccessRate = ((totalWarnings + totalErrors) / totalRequests) * 100
const successRate = 100 - nonSuccessRate
return { successRate, nonSuccessRate }
}
export const isServiceDisabled = (total: number, isLoading: boolean): boolean =>
!isLoading && total <= 0
export type ServiceCardStats = {
eventChartData: LogsBarChartDatum[]
total: number
warningCount: number
errorCount: number
}
export type ServiceCard<Entry> = Entry & {
data: LogsBarChartDatum[]
total: number
warn: number
err: number
}
/**
* Maps enabled services to card data. Keeps the order of `services`, so the
* cards do not move when the totals change (for example on a new interval).
*/
export const buildServiceCards = <Key extends string, Entry extends { key: Key; enabled: boolean }>(
services: Entry[],
statsByKey: Record<Key, ServiceCardStats>
): ServiceCard<Entry>[] =>
services
.filter((service) => service.enabled)
.map((service) => {
const stats = statsByKey[service.key]
return {
...service,
data: stats.eventChartData,
total: stats.total,
warn: stats.warningCount,
err: stats.errorCount,
}
})
/** One-bucket-wide window for a clicked chart bar; the timestamp is the UTC bucket start. */
export const getBucketLogRange = (
timestamp: string,
interval: ChartIntervalKey
): { start: string; end: string } => {
const unit = interval === '1hr' ? 'minute' : interval === '1day' ? 'hour' : 'day'
return {
start: timestamp,
end: dayjs.utc(timestamp).add(1, unit).toISOString(),
}
}