The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
fix iops chart max value (#43182)
Jordi Enricf9f002140198
diff --git a/apps/studio/components/ui/Charts/Charts.utils.test.ts b/apps/studio/components/ui/Charts/Charts.utils.test.tsnew file mode 100644index 00000000000..7ff56eddc37--- /dev/null+++ b/apps/studio/components/ui/Charts/Charts.utils.test.ts@@ -0,0 +1,174 @@+import { describe, expect, it } from 'vitest'++import { computeYAxisDomain } from './Charts.utils'++const IOPS_DATA = [+ { timestamp: 1, disk_iops_write: 1200, disk_iops_read: 24203, disk_iops_max: 25000 },+ { timestamp: 2, disk_iops_write: 400, disk_iops_read: 3200, disk_iops_max: 25000 },+ { timestamp: 3, disk_iops_write: 100, disk_iops_read: 900, disk_iops_max: 25000 },+]++const IOPS_VISIBLE = ['disk_iops_write', 'disk_iops_read']++describe('computeYAxisDomain', () => {+ describe('percentage charts with max line hidden', () => {+ it('returns [0, yMaxFromVisible] to zoom in on the data', () => {+ 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('still zooms in even when a maxAttributeKey is present', () => {+ 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('no max reference line', () => {+ it('returns auto when maxAttributeKey is undefined', () => {+ expect(+ computeYAxisDomain({+ isPercentage: false,+ showMaxValue: false,+ yMaxFromVisible: 5000,+ maxAttributeKey: undefined,+ showMaxLine: false,+ data: IOPS_DATA,+ visibleAttributeNames: IOPS_VISIBLE,+ })+ ).toEqual(['auto', 'auto'])+ })++ it('returns auto when showMaxLine is false', () => {+ expect(+ computeYAxisDomain({+ isPercentage: false,+ showMaxValue: true,+ yMaxFromVisible: 5000,+ maxAttributeKey: 'disk_iops_max',+ showMaxLine: false,+ data: IOPS_DATA,+ visibleAttributeNames: IOPS_VISIBLE,+ })+ ).toEqual(['auto', 'auto'])+ })+ })++ describe('max reference line not yet loaded (value is 0)', () => {+ it('returns auto when diskConfig has not loaded and reference line value is 0', () => {+ const dataWithZeroMax = IOPS_DATA.map((p) => ({ ...p, disk_iops_max: 0 }))+ expect(+ computeYAxisDomain({+ isPercentage: false,+ showMaxValue: true,+ yMaxFromVisible: 5000,+ maxAttributeKey: 'disk_iops_max',+ showMaxLine: true,+ data: dataWithZeroMax,+ visibleAttributeNames: IOPS_VISIBLE,+ })+ ).toEqual(['auto', 'auto'])+ })+ })++ describe('explicit domain with reference line', () => {+ it('uses the reference line value when bars stay below it', () => {+ // All stacked bar totals (1000, 500) are well below maxRefValue (25000)+ expect(+ computeYAxisDomain({+ isPercentage: false,+ showMaxValue: true,+ yMaxFromVisible: 800,+ maxAttributeKey: 'disk_iops_max',+ showMaxLine: true,+ data: [+ { disk_iops_write: 400, disk_iops_read: 600, disk_iops_max: 25000 },+ { disk_iops_write: 200, disk_iops_read: 300, disk_iops_max: 25000 },+ ],+ visibleAttributeNames: IOPS_VISIBLE,+ })+ ).toEqual([0, 25000])+ })++ it('uses the stacked bar total when it exceeds the reference line', () => {+ // Stacked total at first point: 24203 + 1200 = 25403 > 25000+ expect(+ computeYAxisDomain({+ isPercentage: false,+ showMaxValue: true,+ yMaxFromVisible: 24203,+ maxAttributeKey: 'disk_iops_max',+ showMaxLine: true,+ data: IOPS_DATA,+ visibleAttributeNames: IOPS_VISIBLE,+ })+ ).toEqual([0, 25403])+ })++ it('domain min is always 0', () => {+ const [min] = computeYAxisDomain({+ isPercentage: false,+ showMaxValue: true,+ yMaxFromVisible: 100,+ maxAttributeKey: 'disk_iops_max',+ showMaxLine: true,+ data: IOPS_DATA,+ visibleAttributeNames: IOPS_VISIBLE,+ }) as [number, number]+ expect(min).toBe(0)+ })++ it('works for database connections chart (single bar series, no stacking)', () => {+ 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('handles non-numeric values in data gracefully', () => {+ const data = [+ { disk_iops_write: 'bad', disk_iops_read: null, disk_iops_max: 25000 },+ { disk_iops_write: 500, disk_iops_read: 1000, disk_iops_max: 25000 },+ ]+ expect(+ computeYAxisDomain({+ isPercentage: false,+ showMaxValue: true,+ yMaxFromVisible: 1000,+ maxAttributeKey: 'disk_iops_max',+ showMaxLine: true,+ data: data as Record<string, unknown>[],+ visibleAttributeNames: IOPS_VISIBLE,+ })+ ).toEqual([0, 25000])+ })+ })+})diff --git a/apps/studio/components/ui/Charts/Charts.utils.tsx b/apps/studio/components/ui/Charts/Charts.utils.tsxindex 42cdbb3b97f..c3645cca40e 100644--- a/apps/studio/components/ui/Charts/Charts.utils.tsx+++ b/apps/studio/components/ui/Charts/Charts.utils.tsx@@ -114,6 +114,66 @@ export const timestampFormatter = (return dayjs(value).format(format)}+/**+ * Computes the Y-axis domain for a ComposedChart that may contain stacked Bar components+ * and an optional max-value reference Line.+ *+ * Recharts' `['auto', 'auto']` domain does not correctly include a Line component's values+ * when stacked Bars are present — the domain is derived only from the bar data, so the+ * reference line (e.g. Max IOPS) and any bars that exceed it get visually clipped.+ * This function returns an explicit `[0, max]` domain when a visible reference line exists.+ *+ * @example+ * // Max IOPS reference line at 25 000, bars reach up to 25 403+ * computeYAxisDomain({ maxAttributeKey: 'disk_iops_max', showMaxLine: true, ... })+ * // → [0, 25403]+ *+ * // Percentage chart zoomed in (no max line toggle)+ * computeYAxisDomain({ isPercentage: true, showMaxValue: false, yMaxFromVisible: 75, ... })+ * // → [0, 75]+ *+ * // No max reference line — let Recharts auto-scale+ * computeYAxisDomain({ maxAttributeKey: undefined, ... })+ * // → ['auto', 'auto']+ */+export function computeYAxisDomain({+ isPercentage,+ showMaxValue,+ yMaxFromVisible,+ maxAttributeKey,+ showMaxLine,+ data,+ visibleAttributeNames,+}: {+ isPercentage: boolean+ showMaxValue: boolean+ yMaxFromVisible: number+ maxAttributeKey: string | undefined+ showMaxLine: boolean+ data: Record<string, unknown>[]+ visibleAttributeNames: string[]+}): [number, number] | ['auto', 'auto'] {+ if (isPercentage && !showMaxValue) return [0, yMaxFromVisible]+ if (!maxAttributeKey || !showMaxLine) return ['auto', 'auto']++ const maxRefValue = data.reduce((max, point) => {+ const val = point[maxAttributeKey]+ return typeof val === 'number' ? Math.max(max, val) : max+ }, 0)++ if (maxRefValue <= 0) return ['auto', 'auto']++ const maxStackedTotal = data.reduce((max, point) => {+ const total = visibleAttributeNames.reduce((sum, name) => {+ const val = point[name]+ return sum + (typeof val === 'number' ? val : 0)+ }, 0)+ return Math.max(max, total)+ }, 0)++ return [0, Math.max(maxRefValue, maxStackedTotal)]+}+/*** Hook to create common wrapping components, perform data transformations* returns a Container component and the minHeight setdiff --git a/apps/studio/components/ui/Charts/ComposedChart.tsx b/apps/studio/components/ui/Charts/ComposedChart.tsxindex 10483b9705f..c2d9872dabe 100644--- a/apps/studio/components/ui/Charts/ComposedChart.tsx+++ b/apps/studio/components/ui/Charts/ComposedChart.tsx@@ -1,11 +1,12 @@import dayjs from 'dayjs'import { formatBytes } from 'lib/helpers'import { useTheme } from 'next-themes'-import { ComponentProps, useEffect, useState } from 'react'+import { ComponentProps, useEffect, useMemo, useState } from 'react'import {Area,Bar,CartesianGrid,+ Customized,Label,Line,ComposedChart as RechartComposedChart,@@ -14,11 +15,10 @@ import {Tooltip,XAxis,YAxis,- Customized,} from 'recharts'-import { CategoricalChartState } from 'recharts/types/chart/types'import { cn } from 'ui'+import { ChartHeader } from './ChartHeader'import { ChartHighlightAction, ChartHighlightActions } from './ChartHighlightActions'import {@@ -29,12 +29,12 @@ import {updateStackedChartColors,} from './Charts.constants'import { CommonChartProps, Datum } from './Charts.types'-import { formatPercentage, numberFormatter, useChartSize } from './Charts.utils'+import { computeYAxisDomain, formatPercentage, numberFormatter, useChartSize } from './Charts.utils'import {+ calculateTotalChartAggregate,CustomLabel,CustomTooltip,MultiAttribute,- calculateTotalChartAggregate,} from './ComposedChart.utils'import NoDataPlaceholder from './NoDataPlaceholder'import { ChartHighlight } from './useChartHighlight'@@ -338,6 +338,28 @@ export function ComposedChart({)const yDomain = [0, yMaxFromVisible]+ const yAxisDomain = useMemo(+ () =>+ computeYAxisDomain({+ isPercentage,+ showMaxValue,+ yMaxFromVisible,+ maxAttributeKey: maxAttribute?.attribute,+ showMaxLine: _showMaxValue,+ data,+ visibleAttributeNames: visibleAttributes.map((a) => a.name),+ }),+ [+ isPercentage,+ showMaxValue,+ yMaxFromVisible,+ maxAttribute,+ _showMaxValue,+ data,+ visibleAttributes,+ ]+ )+if (data.length === 0) {return (<NoDataPlaceholder@@ -435,7 +457,7 @@ export function ComposedChart({hide={hideYAxis}axisLine={{ stroke: CHART_COLORS.AXIS }}tickLine={{ stroke: CHART_COLORS.AXIS }}- domain={isPercentage && !showMaxValue ? yDomain : ['auto', 'auto']}+ domain={yAxisDomain}key={yAxisKey}/><XAxisdiff --git a/apps/studio/data/reports/database-charts.ts b/apps/studio/data/reports/database-charts.tsindex 0b51863d731..852be9ae43b 100644--- a/apps/studio/data/reports/database-charts.ts+++ b/apps/studio/data/reports/database-charts.ts@@ -149,7 +149,7 @@ export const getReportAttributesV2: (showGrid: true,showMaxValue: true,YAxisProps: {- width: 50,+ width: 55,tickFormatter: (value: any) => compactNumberFormatter(value),},defaultChartStyle: 'bar',