Files
supabase/apps/studio/components/interfaces/Reports/Reports.utils.test.ts
Drew McLellan 3aa85cc774 feat(studio): show warning and error counts on the edge function status codes chart (#53171)
## Problem

On the edge functions report, the Edge Function Status Codes chart shows
a single combined count in its header, which is hard to read when
responses include errors. The count would be more intuitive if it were
broken down by status code.

Resolves FE-4025.

## Solution

Add **Warnings** (4xx) and **Errors** (5xx) counts to the right of the
total in the chart header. This matches the request cards on the project
home page, and uses the same warning and error definitions as the Logs
Explorer filters. Per-code detail stays in the legend and tooltip.

- The counts show the period totals by default and follow the hovered
point, like the total does.
- Charts opt in through a new optional `getHeaderStats` on
`ReportConfig`, passed through `ReportChartV2` and `ComposedChart` to
`ChartHeader`. Only the status codes report sets it, so other reports
are unchanged.
- The counts are summed from the existing pivoted status code series
(`getStatusClassCounts`), so the log queries are untouched and both the
BigQuery and otel paths work.
- The chart's info tooltip now explains what warnings and errors mean.

<img width="756" height="305" alt="Image"
src="https://github.com/user-attachments/assets/808085f5-abd9-43ec-903b-dfa0d5e4f598"
/>

## Review instructions

1. Run Studio and open **Observability → Edge Functions** for a project
that has edge function traffic.
2. On the **Edge Function Status Codes** chart, check that **Warnings**
and **Errors** appear next to the total, and that they match the 4xx and
5xx rows in the legend.
3. Hover the chart. The total, Warnings and Errors should all follow the
hovered point, then return to the period totals when you move away.
4. Check that **Total Edge Function Invocations**, **Execution Time**
and **Invocations by Region** show no Warnings or Errors.
5. Try a narrow viewport to check the header doesn't crowd the buttons.

Unit tests cover the counting and the header (period totals, hover, and
charts without stats): `pnpm exec vitest run
components/interfaces/Reports components/ui/Charts`.

## Checklist

Check all before review:

- [ ] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [ ] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
2026-11-10 13:39:13 +00:00

81 lines
2.7 KiB
TypeScript

import dayjs from 'dayjs'
import utc from 'dayjs/plugin/utc'
import { describe, expect, it } from 'vitest'
import { formatTimestamp, getStatusClassCounts, safeDecodeURIComponent } from './Reports.utils'
dayjs.extend(utc)
describe('safeDecodeURIComponent', () => {
it('decodes a valid percent-encoded string', () => {
expect(safeDecodeURIComponent('a%20b')).toBe('a b')
})
it('returns the original string when percent-encoding is malformed', () => {
expect(safeDecodeURIComponent('?discount=100%')).toBe('?discount=100%')
})
it('handles an empty string', () => {
expect(safeDecodeURIComponent('')).toBe('')
})
})
describe('formatTimestamp', () => {
it('formats milliseconds timestamp correctly', () => {
const timestamp = 1640995200000 // 2022-01-01 00:00:00 UTC in milliseconds
const result = formatTimestamp(timestamp, { returnUtc: true })
expect(result).toBe('Jan 1, 12:00am')
})
it('formats microseconds timestamp correctly', () => {
const timestamp = 1640995200000000 // 2022-01-01 00:00:00 UTC in microseconds
const result = formatTimestamp(timestamp, { returnUtc: true })
expect(result).toBe('Jan 1, 12:00am')
})
it('formats seconds timestamp correctly', () => {
const timestamp = 1640995200 // 2022-01-01 00:00:00 UTC in seconds
const result = formatTimestamp(timestamp, { returnUtc: true })
expect(result).toBe('Jan 1, 12:00am')
})
it('handles string timestamp input', () => {
const timestamp = '1640995200000'
const result = formatTimestamp(timestamp, { returnUtc: true })
expect(result).toBe('Jan 1, 12:00am')
})
it('handles invalid string timestamp', () => {
const timestamp = 'invalid-timestamp'
const result = formatTimestamp(timestamp, { returnUtc: true })
expect(result).toBe('Invalid Date')
})
it('handles zero timestamp', () => {
const timestamp = 0
const result = formatTimestamp(timestamp, { returnUtc: true })
expect(result).toBe('Jan 1, 12:00am')
})
})
describe('getStatusClassCounts', () => {
it('sums 4xx as warnings and 5xx as errors across rows', () => {
const rows = [
{ timestamp: '2026-07-28T09:00:00.000Z', '200': 10, '404': 2, '500': 1 },
{ timestamp: '2026-07-28T09:01:00.000Z', '200': 5, '400': 3, '502': 4, '520': 1 },
]
expect(getStatusClassCounts(rows)).toEqual({ warnings: 5, errors: 6 })
})
it('ignores 1xx, 2xx and 3xx codes, the timestamp, and non-numeric values', () => {
const rows = [{ timestamp: '2026-07-28T09:00:00.000Z', '200': 7, '301': 2, '404': 'n/a' }]
expect(getStatusClassCounts(rows)).toEqual({ warnings: 0, errors: 0 })
})
it('returns zero counts when there are no rows', () => {
expect(getStatusClassCounts([])).toEqual({ warnings: 0, errors: 0 })
})
})