Files
supabase/apps/studio/components/ui/Charts/Charts.utils.test.ts
Jordi Enric 8ba2f0a63b feat(studio): clarify memory commitment risk thresholds FE-4467 (#51799)
## Problem

The memory commitment chart presents the kernel commit limit as a hard
failure threshold, even though committed memory can remain unused.

Closes
[FE-4467](https://linear.app/supabase/issue/FE-4467/update-memory-commitment-ui-with-risk-zones).

## Fix

Show only the highest risk threshold exceeded anywhere in the selected
time range: Acknowledgment (yellow), Problematic (orange), or Severe
(red). Draw its dashed line and place a matching Warning badge in the
card header with keyboard-accessible guidance. Show neither a warning
line nor a badge when no threshold is exceeded; equality alone does not
trigger a warning.

Thresholds remain 50%, 80%, and 90% of estimated physical RAM plus 1 GiB
of swap, using each timestamp's capacity. Missing readings stay
unavailable. Remove in-chart labels and keep only Committed in the
source legend.

Keep the vertical range anchored at zero and full estimated capacity,
expanding for observed overcommitment. A neutral dashed capacity line
follows historical infrastructure changes. Dashed references render
above the bars and area fills. The hover tooltip identifies Full
commitment capacity at that timestamp and excludes it from Total.

The developer toolbar includes a Metrics tab with a Memory commitment
selector: Default (real data), Warn, Problematic, and Severe. Simulated
states include a capacity change and apply only to the selected
project’s memory chart. Overrides are in-memory, leave real query data
untouched, and are enabled only in local/staging. Both Studio runtimes
register the tab.

## Screenshots

### Capacity changes with connected dashed lines

![Capacity changes with connected dashed
lines](https://github.com/user-attachments/assets/6773f250-4189-4039-aaeb-d82404751e39)

### Chart tooltip with full commitment capacity

![Chart tooltip with full commitment
capacity](https://github.com/user-attachments/assets/c1f02232-c04f-4646-ac51-83c9d2815bcf)

### Problematic warning and connected threshold lines

![Problematic warning and connected threshold
lines](https://github.com/user-attachments/assets/3d48f1f9-a74d-43c5-a9bc-d8f274ea5d28)

## How to test

1. Open Observability > Database and inspect Memory commitment in bar
and line views.
2. With no threshold exceeded, confirm no warning badge or threshold
line appears.
3. In the developer toolbar, open Metrics and select Warn, Problematic,
then Severe. Confirm the matching badge and only the highest crossed
warning threshold appear. Close the toolbar to inspect the chart; the
selected state remains active.
4. Hover or focus Warning to read the matching threshold and guidance.
5. Confirm the neutral capacity line steps down midway through each
simulated range.
6. Select Default (real data) to restore live readings. Reloading also
clears simulations.

Validation: 168 affected tests passed with retries disabled and shuffled
runs. Production-guard and incorrect-risk-level mutations failed as
expected; tests passed after restoring the implementation. Formatting
and code review passed.
2026-10-22 14:30:49 +02:00

243 lines
7.4 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { computeYAxisDomain, normalizeStackedSeriesData } from './Charts.utils'
const IOPS_DATA = [
{ timestamp: 1, disk_iops_write: 40, disk_iops_read: 80, disk_iops_max: 100 },
{ timestamp: 2, disk_iops_write: 10, disk_iops_read: 20, disk_iops_max: 100 },
{ timestamp: 3, disk_iops_write: 5, disk_iops_read: 5, disk_iops_max: 100 },
]
const IOPS_VISIBLE = ['disk_iops_write', 'disk_iops_read']
describe('computeYAxisDomain', () => {
describe('memory commitment axis', () => {
const GIB = 1024 ** 3
const memoryBelowCapacity = {
isPercentage: false,
showMaxValue: false,
yMaxFromVisible: 2 * GIB,
thresholdZoneMax: 8 * GIB,
maxAttributeKey: undefined,
showMaxLine: false,
data: [{ ram_commit_used: 2 * GIB }],
visibleAttributeNames: ['ram_commit_used'],
}
it('shows commitment against full capacity so low usage does not appear near the limit', () => {
expect(computeYAxisDomain(memoryBelowCapacity)).toEqual([0, 8 * GIB])
})
it('keeps the capacity scale when Committed is hidden', () => {
expect(computeYAxisDomain({ ...memoryBelowCapacity, visibleAttributeNames: [] })).toEqual([
0,
8 * GIB,
])
})
it('keeps above-capacity commitment visible', () => {
expect(
computeYAxisDomain({
...memoryBelowCapacity,
data: [{ ram_commit_used: 10 * GIB }],
visibleAttributeNames: ['ram_commit_used'],
})
).toEqual([0, 10 * GIB])
})
})
describe('CPU percentage axis with the maximum hidden', () => {
it('scales from zero to the observed 75% instead of the 100% limit', () => {
expect(
computeYAxisDomain({
isPercentage: true,
showMaxValue: false,
yMaxFromVisible: 75,
maxAttributeKey: 'cpu_usage_max',
showMaxLine: false,
data: [{ cpu_busy: 75, cpu_usage_max: 100 }],
visibleAttributeNames: ['cpu_busy'],
})
).toEqual([0, 75])
})
it('keeps the observed 60% scale even when the maximum series exists', () => {
expect(
computeYAxisDomain({
isPercentage: true,
showMaxValue: false,
yMaxFromVisible: 60,
maxAttributeKey: 'cpu_usage_max',
showMaxLine: true,
data: [{ cpu_busy: 60, cpu_usage_max: 100 }],
visibleAttributeNames: ['cpu_busy'],
})
).toEqual([0, 60])
})
})
describe('disk operations without a visible limit', () => {
it('uses automatic scaling when no limit metric is configured', () => {
expect(
computeYAxisDomain({
isPercentage: false,
showMaxValue: false,
yMaxFromVisible: 80,
maxAttributeKey: undefined,
showMaxLine: false,
data: IOPS_DATA,
visibleAttributeNames: IOPS_VISIBLE,
})
).toEqual(['auto', 'auto'])
})
it('uses automatic scaling when the limit line is hidden', () => {
expect(
computeYAxisDomain({
isPercentage: false,
showMaxValue: true,
yMaxFromVisible: 80,
maxAttributeKey: 'disk_iops_max',
showMaxLine: false,
data: IOPS_DATA,
visibleAttributeNames: IOPS_VISIBLE,
})
).toEqual(['auto', 'auto'])
})
})
describe('disk operations before the limit loads', () => {
it('uses automatic scaling while the limit is zero', () => {
const dataWithZeroMax = IOPS_DATA.map((p) => ({ ...p, disk_iops_max: 0 }))
expect(
computeYAxisDomain({
isPercentage: false,
showMaxValue: true,
yMaxFromVisible: 80,
maxAttributeKey: 'disk_iops_max',
showMaxLine: true,
data: dataWithZeroMax,
visibleAttributeNames: IOPS_VISIBLE,
})
).toEqual(['auto', 'auto'])
})
})
describe('charts with a visible limit', () => {
it('shows the 100-operation limit when stacked reads and writes stay below it', () => {
expect(
computeYAxisDomain({
isPercentage: false,
showMaxValue: true,
yMaxFromVisible: 40,
maxAttributeKey: 'disk_iops_max',
showMaxLine: true,
data: [
{ disk_iops_write: 30, disk_iops_read: 40, disk_iops_max: 100 },
{ disk_iops_write: 20, disk_iops_read: 30, disk_iops_max: 100 },
],
visibleAttributeNames: IOPS_VISIBLE,
})
).toEqual([0, 100])
})
it('expands to 120 operations when 40 writes plus 80 reads exceed the 100-operation limit', () => {
expect(
computeYAxisDomain({
isPercentage: false,
showMaxValue: true,
yMaxFromVisible: 80,
maxAttributeKey: 'disk_iops_max',
showMaxLine: true,
data: IOPS_DATA,
visibleAttributeNames: IOPS_VISIBLE,
})
).toEqual([0, 120])
})
it('shows the 60-connection limit when one connection series peaks at 52', () => {
const data = [
{ pg_stat_database_num_backends: 45, max_db_connections: 60 },
{ pg_stat_database_num_backends: 52, max_db_connections: 60 },
]
expect(
computeYAxisDomain({
isPercentage: false,
showMaxValue: true,
yMaxFromVisible: 52,
maxAttributeKey: 'max_db_connections',
showMaxLine: true,
data,
visibleAttributeNames: ['pg_stat_database_num_backends'],
})
).toEqual([0, 60])
})
it('ignores missing or invalid reads and writes without losing the limit', () => {
const data = [
{ disk_iops_write: 'bad', disk_iops_read: null, disk_iops_max: 100 },
{ disk_iops_max: 100 },
{ disk_iops_write: 20, disk_iops_read: 30, disk_iops_max: 100 },
]
expect(
computeYAxisDomain({
isPercentage: false,
showMaxValue: true,
yMaxFromVisible: 30,
maxAttributeKey: 'disk_iops_max',
showMaxLine: true,
data,
visibleAttributeNames: IOPS_VISIBLE,
})
).toEqual([0, 100])
})
})
})
describe('normalizeStackedSeriesData', () => {
it('shows fractional CPU shares accurately', () => {
const normalized = normalizeStackedSeriesData({
data: [{ system: 1, user: 1, idle: 4 }],
attributeNames: ['system', 'user', 'idle'],
})
expect(normalized[0].system).toBeCloseTo(100 / 6)
expect(normalized[0].user).toBeCloseTo(100 / 6)
expect(normalized[0].idle).toBeCloseTo(400 / 6)
})
it('keeps zero CPU usage at zero', () => {
const empty = [{ system: 0, user: 0, idle: 0 }]
expect(
normalizeStackedSeriesData({
data: empty,
attributeNames: ['system', 'user', 'idle'],
})
).toEqual(empty)
})
it('leaves unselected series unchanged', () => {
const data = [{ system: 50, user: 30, timestamp: 1000 }]
expect(
normalizeStackedSeriesData({
data,
attributeNames: [],
})
).toEqual(data)
})
it('keeps normalized CPU history at its original timestamps', () => {
const data = [{ system: 60, user: 80, period_start: '2024-01-01', timestamp: 1000 }]
const normalized = normalizeStackedSeriesData({
data,
attributeNames: ['system', 'user'],
})
expect(normalized[0].period_start).toBe('2024-01-01')
expect(normalized[0].timestamp).toBe(1000)
expect(Number(normalized[0].system) + Number(normalized[0].user)).toBeCloseTo(100)
})
})