mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 08:29:01 +00:00
## 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  ### Chart tooltip with full commitment capacity  ### Problematic warning and connected threshold lines  ## 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.
260 lines
8.2 KiB
TypeScript
260 lines
8.2 KiB
TypeScript
import type { UseQueryResult } from '@tanstack/react-query'
|
|
import dayjs from 'dayjs'
|
|
import { useMemo } from 'react'
|
|
|
|
import type { AnalyticsData, AnalyticsInterval, DataPoint } from './constants'
|
|
import type {
|
|
InfraMonitoringAttribute,
|
|
InfraMonitoringMultiResponse,
|
|
InfraMonitoringSingleResponse,
|
|
} from './infra-monitoring-query'
|
|
import {
|
|
InfraMonitoringError,
|
|
InfraMonitoringMultiData,
|
|
useInfraMonitoringAttributesQuery,
|
|
} from './infra-monitoring-query'
|
|
|
|
// TODO(raulb): Remove isMultiResponse type guard once API always returns multi-attribute format.
|
|
/**
|
|
* Type guard to check if response is the multi-attribute format.
|
|
* Multi-format has `series` property, single-format has metadata directly on response.
|
|
*/
|
|
function isMultiResponse(
|
|
response: InfraMonitoringMultiData
|
|
): response is InfraMonitoringMultiResponse {
|
|
return 'series' in response
|
|
}
|
|
|
|
const DEFAULT_DATE_FORMAT = 'HH:mm DD MMM'
|
|
|
|
type InfraQueryResult = Pick<
|
|
UseQueryResult<AnalyticsData, InfraMonitoringError>,
|
|
'data' | 'error' | 'isError' | 'isFetching' | 'isLoading' | 'status'
|
|
>
|
|
|
|
export function useInfraMonitoringQueries(
|
|
attributes: InfraMonitoringAttribute[],
|
|
ref: string | string[] | undefined,
|
|
startDate: string,
|
|
endDate: string,
|
|
interval: AnalyticsInterval,
|
|
databaseIdentifier: string | undefined,
|
|
data: unknown,
|
|
isVisible: boolean,
|
|
preserveMissingAttribute?: string
|
|
) {
|
|
const shouldFetch = data === undefined && isVisible
|
|
const hasAttributes = attributes.length > 0
|
|
|
|
const query = useInfraMonitoringAttributesQuery(
|
|
{
|
|
projectRef: ref as string,
|
|
attributes,
|
|
startDate,
|
|
endDate,
|
|
interval,
|
|
databaseIdentifier,
|
|
},
|
|
{ enabled: shouldFetch && hasAttributes }
|
|
)
|
|
|
|
const seriesByAttribute = useMemo(() => {
|
|
if (!query.data) return undefined
|
|
return mapResponseToAnalyticsData(query.data, attributes, undefined, preserveMissingAttribute)
|
|
}, [query.data, attributes, preserveMissingAttribute])
|
|
|
|
if (!hasAttributes) {
|
|
return []
|
|
}
|
|
|
|
return attributes.map<InfraQueryResult>((attribute) => {
|
|
const metricError =
|
|
query.data && 'errors' in query.data ? query.data.errors?.[attribute] : undefined
|
|
return {
|
|
data: seriesByAttribute?.[attribute],
|
|
error: query.error ?? metricError ?? null,
|
|
isError: query.isError || !!metricError,
|
|
isFetching: query.isFetching,
|
|
isLoading: query.isLoading,
|
|
status: metricError ? 'error' : query.status,
|
|
}
|
|
})
|
|
}
|
|
|
|
type AggregatedBucket = {
|
|
period_start: string
|
|
periodStartFormatted: string
|
|
_counts: Record<string, number>
|
|
_missingAttributes: Set<string>
|
|
[key: string]: string | number | Set<string> | Record<string, number>
|
|
}
|
|
|
|
type DataPointWithFormatted = DataPoint & {
|
|
periodStartFormatted: string
|
|
}
|
|
|
|
function shouldPreserveMissingAttribute(attribute: string, preserveMissingAttribute?: string) {
|
|
return attribute === 'ram_commit_limit' || attribute === preserveMissingAttribute
|
|
}
|
|
|
|
export const aggregate1MinTo2Min = (
|
|
dataPoints: DataPoint[],
|
|
preserveMissingAttribute?: string
|
|
): DataPointWithFormatted[] => {
|
|
const aggregated = new Map<string, AggregatedBucket>()
|
|
const valueKeys = new Set<string>()
|
|
|
|
// Collect all value keys
|
|
dataPoints.forEach((point) => {
|
|
Object.keys(point).forEach((key) => {
|
|
if (
|
|
key !== 'period_start' &&
|
|
key !== 'periodStartFormatted' &&
|
|
typeof point[key] === 'number'
|
|
) {
|
|
valueKeys.add(key)
|
|
}
|
|
})
|
|
})
|
|
|
|
// Aggregate by 2min buckets (average the 1min values)
|
|
dataPoints.forEach((point) => {
|
|
const timestamp = dayjs(point.period_start)
|
|
const twoMinBucket = timestamp.startOf('minute').subtract(timestamp.minute() % 2, 'minute')
|
|
const bucketKey = twoMinBucket.toISOString()
|
|
|
|
if (!aggregated.has(bucketKey)) {
|
|
const initialBucket: AggregatedBucket = {
|
|
period_start: bucketKey,
|
|
periodStartFormatted: twoMinBucket.format('HH:mm DD MMM'),
|
|
_counts: Object.fromEntries(Array.from(valueKeys).map((key) => [key, 0])),
|
|
_missingAttributes: new Set(),
|
|
}
|
|
// Initialize all value keys to 0
|
|
valueKeys.forEach((key) => {
|
|
initialBucket[key] = 0
|
|
})
|
|
aggregated.set(bucketKey, initialBucket)
|
|
}
|
|
|
|
const bucket = aggregated.get(bucketKey)!
|
|
valueKeys.forEach((key) => {
|
|
const value = point[key]
|
|
if (
|
|
shouldPreserveMissingAttribute(key, preserveMissingAttribute) &&
|
|
typeof value !== 'number'
|
|
) {
|
|
bucket._missingAttributes.add(key)
|
|
}
|
|
if (typeof value === 'number') {
|
|
const currentValue = typeof bucket[key] === 'number' ? bucket[key] : 0
|
|
bucket[key] = currentValue + value
|
|
bucket._counts[key] = (bucket._counts[key] || 0) + 1
|
|
}
|
|
})
|
|
})
|
|
|
|
// Calculate averages
|
|
const result: DataPointWithFormatted[] = Array.from(aggregated.values()).map((bucket) => {
|
|
const { _counts, _missingAttributes, period_start, periodStartFormatted, ...rest } = bucket
|
|
const averaged: DataPointWithFormatted = {
|
|
period_start,
|
|
periodStartFormatted,
|
|
}
|
|
valueKeys.forEach((key) => {
|
|
if (
|
|
shouldPreserveMissingAttribute(key, preserveMissingAttribute) &&
|
|
(_missingAttributes.has(key) || _counts[key] === 0)
|
|
)
|
|
return
|
|
if (_counts[key] > 0) {
|
|
const restValue = rest[key]
|
|
averaged[key] = typeof restValue === 'number' ? restValue / _counts[key] : 0
|
|
} else {
|
|
averaged[key] = typeof rest[key] === 'number' ? rest[key] : 0
|
|
}
|
|
})
|
|
return averaged
|
|
})
|
|
|
|
return result.sort((a, b) => dayjs(a.period_start).valueOf() - dayjs(b.period_start).valueOf())
|
|
}
|
|
|
|
export function mapResponseToAnalyticsData(
|
|
response: InfraMonitoringMultiData,
|
|
attributes: InfraMonitoringAttribute[],
|
|
dateFormat: string = DEFAULT_DATE_FORMAT,
|
|
preserveMissingAttribute?: string
|
|
): Record<string, AnalyticsData> {
|
|
const needs2MinAggregation = (response as { _originalInterval?: '2m' })._originalInterval === '2m'
|
|
|
|
// Handle multi-attribute response format
|
|
if (isMultiResponse(response)) {
|
|
return attributes.reduce<Record<string, AnalyticsData>>((acc, attribute) => {
|
|
const metadata = response.series?.[attribute]
|
|
if (!metadata) return acc
|
|
|
|
let dataPoints = response.data.map((point) => {
|
|
const value = point.values?.[attribute]
|
|
const dataPoint: DataPointWithFormatted = {
|
|
period_start: point.period_start,
|
|
periodStartFormatted: dayjs(point.period_start).format(dateFormat),
|
|
}
|
|
const numericValue = Number(value ?? 0)
|
|
const isMissingPreservedValue =
|
|
shouldPreserveMissingAttribute(attribute, preserveMissingAttribute) &&
|
|
(value == null || !Number.isFinite(numericValue))
|
|
if (!isMissingPreservedValue) dataPoint[attribute] = numericValue
|
|
return dataPoint
|
|
})
|
|
|
|
if (needs2MinAggregation) {
|
|
dataPoints = aggregate1MinTo2Min(dataPoints, preserveMissingAttribute)
|
|
}
|
|
|
|
acc[attribute] = {
|
|
data: dataPoints,
|
|
format: metadata.format,
|
|
total: metadata.total,
|
|
yAxisLimit: metadata.yAxisLimit,
|
|
}
|
|
|
|
return acc
|
|
}, {})
|
|
}
|
|
|
|
// TODO(raulb): Remove single-attribute response handling once API always returns multi-attribute format.
|
|
// Handle single-attribute response format
|
|
// API returns this format when only 1 attribute is requested
|
|
const singleResponse = response as InfraMonitoringSingleResponse
|
|
const attribute = attributes[0]
|
|
if (!attribute) return {}
|
|
|
|
let dataPoints = singleResponse.data.map((point) => {
|
|
const value = point[attribute]
|
|
const dataPoint: DataPointWithFormatted = {
|
|
period_start: point.period_start,
|
|
periodStartFormatted: dayjs(point.period_start).format(dateFormat),
|
|
}
|
|
const numericValue = Number(value ?? 0)
|
|
const isMissingPreservedValue =
|
|
shouldPreserveMissingAttribute(attribute, preserveMissingAttribute) &&
|
|
(value == null || !Number.isFinite(numericValue))
|
|
if (!isMissingPreservedValue) dataPoint[attribute] = numericValue
|
|
return dataPoint
|
|
})
|
|
|
|
if (needs2MinAggregation) {
|
|
dataPoints = aggregate1MinTo2Min(dataPoints, preserveMissingAttribute)
|
|
}
|
|
|
|
return {
|
|
[attribute]: {
|
|
data: dataPoints,
|
|
format: singleResponse.format,
|
|
total: singleResponse.total,
|
|
yAxisLimit: singleResponse.yAxisLimit,
|
|
},
|
|
}
|
|
}
|