Files
supabase/apps/studio/components/ui/Charts/ComposedChartThresholdZones.utils.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

141 lines
4.0 KiB
TypeScript

import type { DataKey } from 'recharts/types/util/types'
import type { Datum } from './Charts.types'
import type { ThresholdZone } from './ComposedChart.utils'
export type ThresholdZoneRun = {
startIndex: number
endIndex: number
lower: number
upper: number
}
export function getThresholdZoneRuns(ranges: ([number, number] | null)[]): ThresholdZoneRun[] {
return ranges.reduce<ThresholdZoneRun[]>((runs, range, index) => {
if (!range) return runs
const previous = runs[runs.length - 1]
if (
previous?.endIndex === index - 1 &&
previous.lower === range[0] &&
previous.upper === range[1]
) {
previous.endIndex = index
return runs
}
runs.push({ startIndex: index, endIndex: index, lower: range[0], upper: range[1] })
return runs
}, [])
}
export function getThresholdZoneXBounds(
run: ThresholdZoneRun,
data: Datum[],
hasVisibleBars: boolean,
dataKey?: DataKey<Datum>
) {
const getValue = (index: number): string | number | undefined => {
if (dataKey === undefined) return index
if (typeof dataKey === 'function') return dataKey(data[index])
return data[index]?.[dataKey]
}
const endIndex = hasVisibleBars ? run.endIndex : run.endIndex + 1
return {
x1: run.startIndex === 0 ? undefined : getValue(run.startIndex),
x2: run.endIndex === data.length - 1 ? undefined : getValue(endIndex),
}
}
export function getThresholdZoneYBounds(run: ThresholdZoneRun, previousRun?: ThresholdZoneRun) {
if (!previousRun || previousRun.endIndex !== run.startIndex - 1) {
return { y1: run.lower, y2: run.lower }
}
return { y1: run.lower, y2: previousRun.lower }
}
export function getHighestCrossedThresholdZone(
data: Datum[],
zones: { runs: ThresholdZoneRun[]; isAlwaysVisible?: boolean }[],
attribute?: string
): number | undefined {
if (!attribute) return undefined
for (let zoneIndex = zones.length - 1; zoneIndex >= 0; zoneIndex--) {
if (zones[zoneIndex].isAlwaysVisible) continue
for (const run of zones[zoneIndex].runs) {
if (!Number.isFinite(run.lower)) continue
for (let sampleIndex = run.startIndex; sampleIndex <= run.endIndex; sampleIndex++) {
const value = data[sampleIndex]?.[attribute]
if (
typeof value === 'number' &&
Number.isFinite(value) &&
value >= 0 &&
value > run.lower
) {
return zoneIndex
}
}
}
}
return undefined
}
export function getThresholdZoneMaximum(zones: { runs: ThresholdZoneRun[] }[]): number {
return zones.reduce(
(maximum, zone) =>
zone.runs.reduce(
(zoneMaximum, run) =>
Number.isFinite(run.upper) ? Math.max(zoneMaximum, run.upper) : zoneMaximum,
maximum
),
0
)
}
export function getThresholdZoneChart(
data: Datum[],
thresholdZones?: ThresholdZone[],
thresholdZoneAttribute?: string
) {
const zones =
thresholdZones?.map((zone) => ({
...zone,
runs: getThresholdZoneRuns(zone.getRanges(data)),
})) ?? []
const selectedIndex = getHighestCrossedThresholdZone(data, zones, thresholdZoneAttribute)
const selectedZone = selectedIndex === undefined ? undefined : zones[selectedIndex]
let visibleZones = zones
if (thresholdZoneAttribute) {
visibleZones = zones.filter((zone) => zone.isAlwaysVisible || zone === selectedZone)
}
const maxValue = getThresholdZoneMaximum(zones)
return { zones: visibleZones, maxValue, selectedZone }
}
type ThresholdZoneValue = {
label: string
color: string
value: number
}
export function getThresholdZoneValues(
point: Datum | undefined,
zones: ThresholdZone[] | undefined
): ThresholdZoneValue[] {
if (!point || !zones) return []
const values: ThresholdZoneValue[] = []
for (const zone of zones) {
const range = zone.getRanges([point])[0]
if (!range) continue
const [value] = range
if (!Number.isFinite(value)) continue
values.push({ label: zone.tooltipLabel ?? zone.label, color: zone.color, value })
}
return values
}