Files
Jordi Enric 4825c6d9f5 fix(studio): clarify disk IO balance FDBKPRI-2885 (#51943)
## Problem

Infrastructure and Observability label consumed disk IO burst credits as
“Disk IO,” so a value of 1% appears inconsistent with a remaining
balance of 99% in AWS or Grafana.

Both surfaces request `disk_io_consumption`, which is the inverse of
`disk_io_budget`.

## Fix

Show `disk_io_budget` as “Disk IO balance” and explain that it
represents the lower of the remaining throughput and IOPS burst
balances. Infrastructure shows the lowest sampled balance over seven
days; Observability shows the selected-window average.

Keep Infrastructure's overall Compute score based on consumed credits,
so a full balance stays healthy and a low balance still warns. Missing
balance remains unavailable.

Update the compute and disk guide and disk IO troubleshooting page to
explain the remaining balance, its direction, and the different
aggregation windows.

## How to test

- Open Infrastructure settings for a project with burstable IO. Verify
the balance headline and chart show remaining credits, with 100% full
and 0% exhausted.
- Open Observability and verify “Disk IO balance” shows the average
remaining balance for the selected window.
- Hover the labels to check the explanation and aggregation window.
- Verify a high balance does not inflate Compute usage, a low balance
raises the warning, and missing balance does not appear as 0% or 100%.
- Focused unit and MSW component tests: 79 passed, including shuffled
runs with retries disabled; regression mutations caught wrong metric
selection, reversed pressure, and maximum-versus-minimum selection.

## Validation

Studio typecheck, changed-file ESLint, and Prettier pass locally.
Verified both surfaces on the deployed preview, including Observability
refresh and the balance tooltip.

Documentation formatting and review pass. The local markdown build is
blocked by missing generated federated content and its GitHub
credentials; docs CI validates the deployed build.

## Screenshots

Same project on `supabase.green` (before) and the PR preview (after),
captured October 20. Infrastructure uses the same seven-day window;
Observability uses Last 24 hours.

### Infrastructure

| Before | After |
| --- | --- |
| ![Infrastructure
before](https://github.com/user-attachments/assets/be38665c-702d-4c93-854a-a4c7c6f799a2)
| ![Infrastructure
after](https://github.com/user-attachments/assets/41d45ce1-0df5-41e5-8e3f-63c14fd30dcc)
|

### Observability

| Before | After |
| --- | --- |
| ![Observability
before](https://github.com/user-attachments/assets/5a4493f5-f7b1-4bdb-9783-f70f076b8706)
| ![Observability
after](https://github.com/user-attachments/assets/99fea639-4fc7-4fd1-98cd-5a58b69c4ffa)
|

Observability balance explanation:

![Observability balance
tooltip](https://github.com/user-attachments/assets/5c327daa-4eb7-4752-8e0c-1cbc2f64dd86)

Fixes FDBKPRI-2885
2026-10-21 15:41:56 +02:00

162 lines
5.5 KiB
TypeScript

import type { InfraMonitoringResponse } from '@/data/analytics/infra-monitoring-query'
export type UsageMetricStatus = 'default' | 'warning' | 'negative'
export type ComputeUsageChartDatum = {
timestamp: string
maxCpuUsage: number
ramUsage: number
diskIoBalance?: number
}
export type DiskUsageChartDatum = {
timestamp: string
databaseBytes: number
walBytes: number
systemBytes: number
diskSizeBytes: number
databaseUsagePercent: number
walUsagePercent: number
systemUsagePercent: number
}
/** Coerces an API value (string | number | undefined) into a finite number, defaulting to 0. */
export const toNumber = (value: string | number | undefined) => {
const parsedValue = Number(value)
return Number.isFinite(parsedValue) ? parsedValue : 0
}
/** Constrains a percentage to the [0, 100] range so charts never overflow their axis. */
export const clampPercentage = (value: number) => Math.min(Math.max(value, 0), 100)
export const formatUsagePercent = (value: number | undefined) =>
value === undefined ? '—' : `${value.toFixed(0)}%`
export const getUsageMetricStatus = (value: number | undefined): UsageMetricStatus => {
if (value === undefined) return 'default'
if (value >= 90) return 'negative'
if (value >= 75) return 'warning'
return 'default'
}
export const getWorstUsageMetricStatus = (
...values: Array<number | undefined>
): UsageMetricStatus => {
const statuses = values.map(getUsageMetricStatus)
if (statuses.includes('negative')) return 'negative'
if (statuses.includes('warning')) return 'warning'
return 'default'
}
/** Returns the highest numeric value for a given key across all data points, or undefined when empty. */
export const getPeakChartValue = <T extends Record<string, unknown>>(
data: T[],
dataKey: keyof T
): number | undefined => {
const values = data
.map((point) => point[dataKey] as unknown)
.filter((value): value is number => typeof value === 'number')
if (values.length === 0) return undefined
return Math.max(...values)
}
/**
* Transforms a raw infra-monitoring response into the compute and disk chart series.
* Disk points without a known disk size are dropped so usage percentages stay meaningful.
*/
export const buildUsageChartData = (
usageData: InfraMonitoringResponse | undefined
): { computeChartData: ComputeUsageChartDatum[]; diskChartData: DiskUsageChartDatum[] } => {
if (!usageData || !('series' in usageData)) {
return { computeChartData: [], diskChartData: [] }
}
const computeChartData = usageData.data.map((point) => {
const datum: ComputeUsageChartDatum = {
timestamp: point.period_start,
maxCpuUsage: clampPercentage(toNumber(point.values.max_cpu_usage)),
ramUsage: clampPercentage(toNumber(point.values.ram_usage)),
}
const balance = point.values.disk_io_budget
const parsedBalance = balance?.trim() ? Number(balance) : undefined
if (parsedBalance !== undefined && Number.isFinite(parsedBalance)) {
datum.diskIoBalance = clampPercentage(parsedBalance)
}
return datum
})
const diskChartData = usageData.data.flatMap((point) => {
const databaseBytes = toNumber(point.values.pg_database_size)
const walBytes = toNumber(point.values.disk_fs_used_wal)
const systemBytes = toNumber(point.values.disk_fs_used_system)
const totalBytes = toNumber(point.values.disk_fs_size)
if (totalBytes <= 0) return []
return {
timestamp: point.period_start,
databaseBytes,
walBytes,
systemBytes,
diskSizeBytes: totalBytes,
databaseUsagePercent: clampPercentage((databaseBytes / totalBytes) * 100),
walUsagePercent: clampPercentage((walBytes / totalBytes) * 100),
systemUsagePercent: clampPercentage((systemBytes / totalBytes) * 100),
}
})
return { computeChartData, diskChartData }
}
/** Derives peak compute values and the worst-case status for metrics supported by the instance. */
export const getComputeUsageSummary = (
data: ComputeUsageChartDatum[],
includeDiskIo: boolean = true
) => {
const peakCpuUsage = getPeakChartValue(data, 'maxCpuUsage')
const peakMemoryUsage = getPeakChartValue(data, 'ramUsage')
const balances = data.flatMap(({ diskIoBalance }) =>
diskIoBalance === undefined ? [] : [diskIoBalance]
)
const minDiskIoBalance = balances.length > 0 ? Math.min(...balances) : undefined
const peakDiskIoConsumption = minDiskIoBalance === undefined ? undefined : 100 - minDiskIoBalance
const supportedPeaks = [
peakCpuUsage,
peakMemoryUsage,
...(includeDiskIo ? [peakDiskIoConsumption] : []),
]
const peaks = supportedPeaks.filter((value): value is number => value !== undefined)
const peakComputeUsage = peaks.length > 0 ? Math.max(...peaks) : undefined
return {
peakCpuUsage,
peakMemoryUsage,
minDiskIoBalance,
diskIoStatus: getUsageMetricStatus(peakDiskIoConsumption),
peakComputeUsage,
status: getWorstUsageMetricStatus(...supportedPeaks),
}
}
/** Derives the latest used/total bytes, overall usage percentage, and status for the disk card. */
export const getDiskUsageSummary = (data: DiskUsageChartDatum[]) => {
const latestDataPoint = data[data.length - 1]
const usedBytes =
(latestDataPoint?.databaseBytes ?? 0) +
(latestDataPoint?.walBytes ?? 0) +
(latestDataPoint?.systemBytes ?? 0)
const sizeBytes = latestDataPoint?.diskSizeBytes ?? 0
const usagePercent = sizeBytes > 0 ? clampPercentage((usedBytes / sizeBytes) * 100) : undefined
return {
latestDataPoint,
usedBytes,
sizeBytes,
usagePercent,
status: getUsageMetricStatus(usagePercent),
}
}