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

119 lines
3.7 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import {
buildServiceCards,
computeSuccessAndNonSuccessRates,
getBucketLogRange,
isServiceDisabled,
sumErrors,
sumTotal,
sumWarnings,
} from './ProjectUsage.metrics'
describe('ProjectUsage.metrics', () => {
const data = [
{ timestamp: '2025-10-22T13:00:00Z', ok_count: 90, warning_count: 5, error_count: 5 },
{ timestamp: '2025-10-22T13:01:00Z', ok_count: 50, warning_count: 10, error_count: 0 },
]
it('sum helpers compute totals correctly', () => {
expect(sumTotal(data)).toBe(160)
expect(sumWarnings(data)).toBe(15)
expect(sumErrors(data)).toBe(5)
})
it('computeSuccessAndNonSuccessRates returns expected percentages', () => {
const total = sumTotal(data)
const warns = sumWarnings(data)
const errs = sumErrors(data)
const { successRate, nonSuccessRate } = computeSuccessAndNonSuccessRates(total, warns, errs)
// success = 160 - (15 + 5) = 140 -> 87.5%
expect(successRate).toBeCloseTo(87.5)
expect(nonSuccessRate).toBeCloseTo(12.5)
})
it('computeSuccessAndNonSuccessRates returns zeroes when there are no requests', () => {
expect(computeSuccessAndNonSuccessRates(0, 0, 0)).toEqual({ successRate: 0, nonSuccessRate: 0 })
})
describe('isServiceDisabled', () => {
it('is not disabled while loading', () => {
expect(isServiceDisabled(0, true)).toBe(false)
})
it('is disabled when settled with no traffic', () => {
expect(isServiceDisabled(0, false)).toBe(true)
})
it('is not disabled when there is traffic', () => {
expect(isServiceDisabled(42, false)).toBe(false)
})
})
describe('getBucketLogRange', () => {
const timestamp = '2024-01-15T12:00:00.000Z'
it('returns the bucket start unchanged and a one-minute window for 1hr', () => {
expect(getBucketLogRange(timestamp, '1hr')).toEqual({
start: timestamp,
end: '2024-01-15T12:01:00.000Z',
})
})
it('returns a one-hour window for 1day', () => {
expect(getBucketLogRange(timestamp, '1day')).toEqual({
start: timestamp,
end: '2024-01-15T13:00:00.000Z',
})
})
it('returns a one-day window for 7day', () => {
expect(getBucketLogRange(timestamp, '7day')).toEqual({
start: timestamp,
end: '2024-01-16T12:00:00.000Z',
})
})
it('parses a timestamp without a timezone designator as UTC', () => {
expect(getBucketLogRange('2024-01-15T12:00:00', '1hr').end).toBe('2024-01-15T12:01:00.000Z')
})
})
describe('buildServiceCards', () => {
const stats = (total: number, warningCount = 0, errorCount = 0) => ({
eventChartData: [],
total,
warningCount,
errorCount,
})
it('drops disabled services, maps stats, and keeps the input order', () => {
const cards = buildServiceCards(
[
{ key: 'a', enabled: true },
{ key: 'b', enabled: false },
{ key: 'c', enabled: true },
],
{ a: stats(10, 1, 2), b: stats(999), c: stats(50) }
)
expect(cards.map((c) => c.key)).toEqual(['a', 'c'])
expect(cards[0]).toMatchObject({ key: 'a', total: 10, warn: 1, err: 2, data: [] })
})
it('keeps the same order when the totals change', () => {
const services: { key: 'a' | 'b' | 'c'; enabled: boolean }[] = [
{ key: 'a', enabled: true },
{ key: 'b', enabled: true },
{ key: 'c', enabled: true },
]
const first = buildServiceCards(services, { a: stats(1), b: stats(50), c: stats(10) })
const second = buildServiceCards(services, { a: stats(100), b: stats(0), c: stats(5) })
expect(first.map((c) => c.key)).toEqual(['a', 'b', 'c'])
expect(second.map((c) => c.key)).toEqual(['a', 'b', 'c'])
})
})
})