Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix chart headers (#39542)

Jordi Enriccab167c15144
diff --git a/apps/studio/components/interfaces/Reports/v2/ReportChartV2.test.tsx b/apps/studio/components/interfaces/Reports/v2/ReportChartV2.test.tsx
new file mode 100644
index 00000000000..85b134bb5e0
--- /dev/null
+++ b/apps/studio/components/interfaces/Reports/v2/ReportChartV2.test.tsx
@@ -0,0 +1,35 @@
+import { describe, it, expect } from 'vitest'
+import { computePeriodTotal } from './ReportChartV2'
+
+describe('computePeriodTotal', () => {
+ const attrs = [
+ { attribute: 'SignInAttempts', label: 'Password', enabled: true },
+ { attribute: 'SignInAttempts', label: 'PKCE', enabled: true },
+ { attribute: 'SignInAttempts', label: 'Refresh Token', enabled: true },
+ { attribute: 'SignInAttempts', label: 'ID Token', enabled: true },
+ ]
+
+ it('deduplicates attributes that map to same field', () => {
+ const data = [
+ { timestamp: 1, SignInAttempts: 1 },
+ { timestamp: 2, SignInAttempts: 0 },
+ ]
+ expect(computePeriodTotal(data as any, attrs as any)).toBe(1)
+ })
+
+ it('excludes reference lines, max values, omitted and disabled attributes', () => {
+ const data = [
+ { timestamp: 1, a: 1, b: 2, c: 4, d: 8 },
+ { timestamp: 2, a: 1, b: 2, c: 4, d: 8 },
+ ]
+ const attributes = [
+ { attribute: 'a', enabled: true },
+ { attribute: 'b', provider: 'reference-line', enabled: true },
+ { attribute: 'c', isMaxValue: true, enabled: true },
+ { attribute: 'd', omitFromTotal: true, enabled: true },
+ { attribute: 'e', enabled: false },
+ ]
+ // Only 'a' should count: period total = 1+1 = 2
+ expect(computePeriodTotal(data as any, attributes as any)).toBe(2)
+ })
+})
diff --git a/apps/studio/components/interfaces/Reports/v2/ReportChartV2.tsx b/apps/studio/components/interfaces/Reports/v2/ReportChartV2.tsx
index 01f9ed5f004..b84e320db06 100644
--- a/apps/studio/components/interfaces/Reports/v2/ReportChartV2.tsx
+++ b/apps/studio/components/interfaces/Reports/v2/ReportChartV2.tsx
@@ -26,6 +26,32 @@ export interface ReportChartV2Props {
highlightActions?: ChartHighlightAction[]
}
+// Compute total across entire period over unique attribute keys.
+// Excludes attributes that are disabled, reference lines, max values, or marked omitFromTotal.
+export function computePeriodTotal(chartData: any[], dynamicAttributes: any[]): number {
+ const attributeKeys = Array.from(
+ new Set(
+ (dynamicAttributes as any[])
+ .filter(
+ (a) =>
+ a?.enabled !== false &&
+ a?.provider !== 'reference-line' &&
+ !a?.isMaxValue &&
+ !a?.omitFromTotal
+ )
+ .map((a: any) => a.attribute)
+ )
+ )
+
+ return chartData.reduce((sum: number, row: any) => {
+ const rowTotal = attributeKeys.reduce((acc: number, key: string) => {
+ const value = row?.[key]
+ return acc + (typeof value === 'number' ? value : 0)
+ }, 0)
+ return sum + rowTotal
+ }, 0)
+}
+
export const ReportChartV2 = ({
report,
projectRef,
@@ -72,6 +98,8 @@ export const ReportChartV2 = ({
const chartData = queryResult?.data || []
const dynamicAttributes = queryResult?.attributes || []
+ const headerTotal = computePeriodTotal(chartData, dynamicAttributes)
+
/**
* Depending on the source the timestamp key could be 'timestamp' or 'period_start'
*/
@@ -124,7 +152,7 @@ export const ReportChartV2 = ({
xAxisKey={report.xAxisKey ?? 'timestamp'}
yAxisKey={report.yAxisKey ?? dynamicAttributes[0]?.attribute}
hideHighlightedValue={report.hideHighlightedValue}
- highlightedValue={0}
+ highlightedValue={headerTotal}
title={report.label}
customDateFormat={undefined}
chartStyle={chartStyle}