The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
fix: granularity improvements (#41925)
Jordi Enric6123e00e6c5a
diff --git a/apps/studio/data/analytics/constants.ts b/apps/studio/data/analytics/constants.tsindex 8cbd7c413ec..ade8047f249 100644--- a/apps/studio/data/analytics/constants.ts+++ b/apps/studio/data/analytics/constants.ts@@ -14,4 +14,4 @@ export interface AnalyticsData {hasNoData?: boolean}-export type AnalyticsInterval = '1m' | '5m' | '10m' | '30m' | '1h' | '1d'+export type AnalyticsInterval = '1m' | '2m' | '5m' | '10m' | '30m' | '1h' | '1d'diff --git a/apps/studio/data/analytics/infra-monitoring-queries.test.ts b/apps/studio/data/analytics/infra-monitoring-queries.test.tsindex 56b510b8232..be9d0d3198c 100644--- a/apps/studio/data/analytics/infra-monitoring-queries.test.ts+++ b/apps/studio/data/analytics/infra-monitoring-queries.test.ts@@ -1,3 +1,4 @@+import dayjs from 'dayjs'import { describe, expect, it } from 'vitest'import type {@@ -6,6 +7,7 @@ import type {InfraMonitoringSingleResponse,} from './infra-monitoring-query'import {+ aggregate1MinTo2Min,mapResponseToAnalyticsData,mapMultiResponseToAnalyticsData,} from './infra-monitoring-queries'@@ -170,3 +172,342 @@ describe('mapMultiResponseToAnalyticsData (deprecated)', () => {expect(mapMultiResponseToAnalyticsData).toBe(mapResponseToAnalyticsData)})})++describe('2m interval aggregation', () => {+ it('aggregates 1m data points into 2m buckets for multi-attribute response', () => {+ const mock1MinData: InfraMonitoringMultiResponse = {+ data: [+ {+ period_start: '2024-01-02 03:00:00',+ values: { max_cpu_usage: '10', ram_usage: '100' },+ },+ {+ period_start: '2024-01-02 03:01:00',+ values: { max_cpu_usage: '20', ram_usage: '200' },+ },+ {+ period_start: '2024-01-02 03:02:00',+ values: { max_cpu_usage: '15', ram_usage: '150' },+ },+ {+ period_start: '2024-01-02 03:03:00',+ values: { max_cpu_usage: '25', ram_usage: '250' },+ },+ ],+ series: {+ max_cpu_usage: { format: 'percent', total: 70, totalAverage: 17.5, yAxisLimit: 100 },+ ram_usage: { format: 'bytes', total: 700, totalAverage: 175, yAxisLimit: 200 },+ },+ }++ const responseWith2m = {+ ...mock1MinData,+ _originalInterval: '2m',+ } as InfraMonitoringMultiData & { _originalInterval?: string }++ const result = mapResponseToAnalyticsData(responseWith2m, ['max_cpu_usage', 'ram_usage'])++ expect(result.max_cpu_usage?.data).toHaveLength(2)+ // Check that values are correctly averaged+ expect(result.max_cpu_usage?.data[0].max_cpu_usage).toBe(15) // (10 + 20) / 2+ expect(result.max_cpu_usage?.data[1].max_cpu_usage).toBe(20) // (15 + 25) / 2+ // Check that buckets are on even minutes+ expect(dayjs(result.max_cpu_usage?.data[0].period_start).minute() % 2).toBe(0)+ expect(dayjs(result.max_cpu_usage?.data[1].period_start).minute() % 2).toBe(0)++ expect(result.ram_usage?.data[0].ram_usage).toBe(150) // (100 + 200) / 2+ expect(result.ram_usage?.data[1].ram_usage).toBe(200) // (150 + 250) / 2+ })++ it('aggregates 1m data points into 2m buckets for single-attribute response', () => {+ const mock1MinData: InfraMonitoringSingleResponse = {+ data: [+ { period_start: '2024-01-02 03:00:00', disk_io_budget: '10' },+ { period_start: '2024-01-02 03:01:00', disk_io_budget: '20' },+ { period_start: '2024-01-02 03:02:00', disk_io_budget: '15' },+ { period_start: '2024-01-02 03:03:00', disk_io_budget: '25' },+ ],+ format: 'percent',+ total: 70,+ totalAverage: 17.5,+ yAxisLimit: 100,+ }++ const responseWith2m = {+ ...mock1MinData,+ _originalInterval: '2m',+ } as InfraMonitoringMultiData & { _originalInterval?: string }++ const result = mapResponseToAnalyticsData(responseWith2m, ['disk_io_budget'])++ expect(result.disk_io_budget?.data).toHaveLength(2)+ // Check that values are correctly averaged+ expect(result.disk_io_budget?.data[0].disk_io_budget).toBe(15) // (10 + 20) / 2+ expect(result.disk_io_budget?.data[1].disk_io_budget).toBe(20) // (15 + 25) / 2+ // Check that buckets are on even minutes+ expect(dayjs(result.disk_io_budget?.data[0].period_start).minute() % 2).toBe(0)+ expect(dayjs(result.disk_io_budget?.data[1].period_start).minute() % 2).toBe(0)+ })++ it('does not aggregate when interval is not 2m', () => {+ const result = mapResponseToAnalyticsData(mockMultiResponse, ['max_cpu_usage', 'ram_usage'])++ expect(result.max_cpu_usage?.data).toHaveLength(2)+ expect(result.max_cpu_usage?.data[0].max_cpu_usage).toBe(1.5)+ expect(result.max_cpu_usage?.data[1].max_cpu_usage).toBe(0)+ })++ it('handles odd number of 1m data points', () => {+ const mock1MinData: InfraMonitoringMultiResponse = {+ data: [+ {+ period_start: '2024-01-02 03:00:00',+ values: { max_cpu_usage: '10' },+ },+ {+ period_start: '2024-01-02 03:01:00',+ values: { max_cpu_usage: '20' },+ },+ {+ period_start: '2024-01-02 03:02:00',+ values: { max_cpu_usage: '15' },+ },+ ],+ series: {+ max_cpu_usage: { format: 'percent', total: 45, totalAverage: 15, yAxisLimit: 100 },+ },+ }++ const responseWith2m = {+ ...mock1MinData,+ _originalInterval: '2m',+ } as InfraMonitoringMultiData & { _originalInterval?: string }++ const result = mapResponseToAnalyticsData(responseWith2m, ['max_cpu_usage'])++ expect(result.max_cpu_usage?.data).toHaveLength(2)+ expect(result.max_cpu_usage?.data[0].max_cpu_usage).toBe(15) // (10 + 20) / 2+ expect(result.max_cpu_usage?.data[1].max_cpu_usage).toBe(15) // single point in second bucket+ })+})++describe('aggregate1MinTo2Min', () => {+ it('aggregates 1-minute data points into 2-minute buckets with correct averages', () => {+ const dataPoints = [+ {+ period_start: '2024-01-02T03:00:00Z',+ periodStartFormatted: '03:00 02 Jan',+ max_cpu_usage: 10,+ ram_usage: 100,+ },+ {+ period_start: '2024-01-02T03:01:00Z',+ periodStartFormatted: '03:01 02 Jan',+ max_cpu_usage: 20,+ ram_usage: 200,+ },+ {+ period_start: '2024-01-02T03:02:00Z',+ periodStartFormatted: '03:02 02 Jan',+ max_cpu_usage: 15,+ ram_usage: 150,+ },+ {+ period_start: '2024-01-02T03:03:00Z',+ periodStartFormatted: '03:03 02 Jan',+ max_cpu_usage: 25,+ ram_usage: 250,+ },+ ]++ const result = aggregate1MinTo2Min(dataPoints)++ expect(result).toHaveLength(2)+ // Check that first bucket contains 03:00 and 03:01 data averaged+ expect(result[0].max_cpu_usage).toBe(15) // (10 + 20) / 2+ expect(result[0].ram_usage).toBe(150) // (100 + 200) / 2+ expect(result[0].periodStartFormatted).toBeDefined()+ expect(dayjs(result[0].period_start).minute() % 2).toBe(0) // Should be even minute++ // Check that second bucket contains 03:02 and 03:03 data averaged+ expect(result[1].max_cpu_usage).toBe(20) // (15 + 25) / 2+ expect(result[1].ram_usage).toBe(200) // (150 + 250) / 2+ expect(result[1].periodStartFormatted).toBeDefined()+ expect(dayjs(result[1].period_start).minute() % 2).toBe(0) // Should be even minute+ })++ it('handles odd number of data points correctly', () => {+ const dataPoints = [+ {+ period_start: '2024-01-02T03:00:00Z',+ max_cpu_usage: 10,+ },+ {+ period_start: '2024-01-02T03:01:00Z',+ max_cpu_usage: 20,+ },+ {+ period_start: '2024-01-02T03:02:00Z',+ max_cpu_usage: 15,+ },+ ]++ const result = aggregate1MinTo2Min(dataPoints)++ expect(result).toHaveLength(2)+ expect(result[0].max_cpu_usage).toBe(15) // (10 + 20) / 2+ expect(result[1].max_cpu_usage).toBe(15) // single point in second bucket+ })++ it('handles single data point', () => {+ const dataPoints = [+ {+ period_start: '2024-01-02T03:00:00Z',+ max_cpu_usage: 10,+ },+ ]++ const result = aggregate1MinTo2Min(dataPoints)++ expect(result).toHaveLength(1)+ expect(result[0].max_cpu_usage).toBe(10)+ expect(result[0].periodStartFormatted).toBeDefined()+ })++ it('handles empty array', () => {+ const result = aggregate1MinTo2Min([])+ expect(result).toHaveLength(0)+ })++ it('handles multiple attributes correctly', () => {+ const dataPoints = [+ {+ period_start: '2024-01-02T03:00:00Z',+ attr1: 5,+ attr2: 10,+ attr3: 15,+ },+ {+ period_start: '2024-01-02T03:01:00Z',+ attr1: 10,+ attr2: 20,+ attr3: 30,+ },+ {+ period_start: '2024-01-02T03:02:00Z',+ attr1: 15,+ attr2: 30,+ attr3: 45,+ },+ ]++ const result = aggregate1MinTo2Min(dataPoints)++ expect(result).toHaveLength(2)+ expect(result[0].attr1).toBe(7.5) // (5 + 10) / 2+ expect(result[0].attr2).toBe(15) // (10 + 20) / 2+ expect(result[0].attr3).toBe(22.5) // (15 + 30) / 2+ expect(result[1].attr1).toBe(15) // single point+ expect(result[1].attr2).toBe(30)+ expect(result[1].attr3).toBe(45)+ })++ it('ignores non-numeric values when aggregating', () => {+ const dataPoints = [+ {+ period_start: '2024-01-02T03:00:00Z',+ max_cpu_usage: 10,+ label: 'test', // string value+ },+ {+ period_start: '2024-01-02T03:01:00Z',+ max_cpu_usage: 20,+ label: 'test2',+ },+ ]++ const result = aggregate1MinTo2Min(dataPoints)++ expect(result).toHaveLength(1)+ expect(result[0].max_cpu_usage).toBe(15)+ expect(result[0].label).toBeUndefined()+ })++ it('handles data points across different 2-minute buckets', () => {+ const dataPoints = [+ {+ period_start: '2024-01-02T03:00:00Z',+ value: 10,+ },+ {+ period_start: '2024-01-02T03:01:00Z',+ value: 20,+ },+ {+ period_start: '2024-01-02T03:02:00Z',+ value: 15,+ },+ {+ period_start: '2024-01-02T03:03:00Z',+ value: 25,+ },+ {+ period_start: '2024-01-02T03:04:00Z',+ value: 30,+ },+ {+ period_start: '2024-01-02T03:05:00Z',+ value: 35,+ },+ ]++ const result = aggregate1MinTo2Min(dataPoints)++ expect(result).toHaveLength(3)+ // First bucket: 03:00 and 03:01+ expect(result[0].value).toBe(15) // (10 + 20) / 2+ expect(dayjs(result[0].period_start).minute() % 2).toBe(0)+ // Second bucket: 03:02 and 03:03+ expect(result[1].value).toBe(20) // (15 + 25) / 2+ expect(dayjs(result[1].period_start).minute() % 2).toBe(0)+ // Third bucket: 03:04 and 03:05+ expect(result[2].value).toBe(32.5) // (30 + 35) / 2+ expect(dayjs(result[2].period_start).minute() % 2).toBe(0)+ })++ it('sorts results by period_start', () => {+ const dataPoints = [+ {+ period_start: '2024-01-02T03:02:00Z',+ value: 15,+ },+ {+ period_start: '2024-01-02T03:00:00Z',+ value: 10,+ },+ {+ period_start: '2024-01-02T03:01:00Z',+ value: 20,+ },+ ]++ const result = aggregate1MinTo2Min(dataPoints)++ expect(result).toHaveLength(2)+ expect(dayjs(result[0].period_start).isBefore(dayjs(result[1].period_start))).toBe(true)+ })++ it('always includes periodStartFormatted in result', () => {+ const dataPoints = [+ {+ period_start: '2024-01-02T03:00:00Z',+ max_cpu_usage: 10,+ },+ ]++ const result = aggregate1MinTo2Min(dataPoints)++ expect(result[0].periodStartFormatted).toBeDefined()+ expect(typeof result[0].periodStartFormatted).toBe('string')+ })+})diff --git a/apps/studio/data/analytics/infra-monitoring-queries.ts b/apps/studio/data/analytics/infra-monitoring-queries.tsindex 3d0386abb6b..0b6d2bbb9eb 100644--- a/apps/studio/data/analytics/infra-monitoring-queries.ts+++ b/apps/studio/data/analytics/infra-monitoring-queries.ts@@ -3,7 +3,7 @@ import type { UseQueryResult } from '@tanstack/react-query'import dayjs from 'dayjs'-import type { AnalyticsData, AnalyticsInterval } from './constants'+import type { AnalyticsData, AnalyticsInterval, DataPoint } from './constants'import type {InfraMonitoringAttribute,InfraMonitoringMultiResponse,@@ -40,7 +40,7 @@ export function useInfraMonitoringQueries(endDate: string,interval: AnalyticsInterval,databaseIdentifier: string | undefined,- data: any,+ data: unknown,isVisible: boolean) {const shouldFetch = data === undefined && isVisible@@ -77,18 +77,99 @@ export function useInfraMonitoringQueries(}))}+type AggregatedBucket = {+ period_start: string+ periodStartFormatted: string+ _counts: Record<string, number>+ [key: string]: string | number | Record<string, number>+}++type DataPointWithFormatted = DataPoint & {+ periodStartFormatted: string+}++export const aggregate1MinTo2Min = (dataPoints: DataPoint[]): 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])),+ }+ // 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 (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, period_start, periodStartFormatted, ...rest } = bucket+ const averaged: DataPointWithFormatted = {+ period_start,+ periodStartFormatted,+ }+ valueKeys.forEach((key) => {+ 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): Record<string, AnalyticsData> {+ const needs2MinAggregation = (response as { _originalInterval?: '2m' })._originalInterval === '2m'+// Handle multi-attribute response formatif (isMultiResponse(response)) {return attributes.reduce<Record<string, AnalyticsData>>((acc, attribute) => {const metadata = response.series?.[attribute]if (!metadata) return acc- const dataPoints = response.data.map((point) => {+ let dataPoints = response.data.map((point) => {const value = point.values?.[attribute]return {period_start: point.period_start,@@ -97,6 +178,10 @@ export function mapResponseToAnalyticsData(}})+ if (needs2MinAggregation) {+ dataPoints = aggregate1MinTo2Min(dataPoints)+ }+acc[attribute] = {data: dataPoints,format: metadata.format,@@ -115,7 +200,7 @@ export function mapResponseToAnalyticsData(const attribute = attributes[0]if (!attribute) return {}- const dataPoints = singleResponse.data.map((point) => {+ let dataPoints = singleResponse.data.map((point) => {const value = point[attribute]return {period_start: point.period_start,@@ -124,6 +209,10 @@ export function mapResponseToAnalyticsData(}})+ if (needs2MinAggregation) {+ dataPoints = aggregate1MinTo2Min(dataPoints)+ }+return {[attribute]: {data: dataPoints,diff --git a/apps/studio/data/analytics/infra-monitoring-query.ts b/apps/studio/data/analytics/infra-monitoring-query.tsindex edf14ba4ce8..9a30306425b 100644--- a/apps/studio/data/analytics/infra-monitoring-query.ts+++ b/apps/studio/data/analytics/infra-monitoring-query.ts@@ -54,12 +54,14 @@ export type InfraMonitoringMultiResponse = {// API returns different shapes based on attribute countexport type InfraMonitoringResponse = InfraMonitoringSingleResponse | InfraMonitoringMultiResponse+type InfraMonitoringInterval = AnalyticsInterval | '2m'+export type InfraMonitoringMultiVariables = {projectRef?: stringattributes: InfraMonitoringAttribute[]startDate?: stringendDate?: string- interval?: AnalyticsInterval+ interval?: InfraMonitoringIntervaldatabaseIdentifier?: string}@@ -79,6 +81,10 @@ export async function getInfraMonitoringAttributes(if (!startDate) throw new Error('Start date is required')if (!endDate) throw new Error('End date is required')+ // Backend doesn't support 2m granularity, so request 1m and aggregate in frontend+ const is2MinInterval = interval === '2m'+ const requestInterval: AnalyticsInterval = is2MinInterval ? '1m' : (interval as AnalyticsInterval)+const { data, error } = await get('/platform/projects/{ref}/infra-monitoring', {params: {path: { ref: projectRef },@@ -87,7 +93,7 @@ export async function getInfraMonitoringAttributes(attributes,startDate,endDate,- interval,+ interval: requestInterval,databaseIdentifier,} as any,},@@ -95,7 +101,13 @@ export async function getInfraMonitoringAttributes(})if (error) handleError(error)- return data as unknown as InfraMonitoringResponse++ const response = data as unknown as InfraMonitoringResponse & { _originalInterval?: '2m' }+ if (is2MinInterval) {+ response._originalInterval = '2m'+ }++ return response}export type InfraMonitoringError = unknowndiff --git a/apps/studio/hooks/misc/useReportDateRange.ts b/apps/studio/hooks/misc/useReportDateRange.tsindex 1f035362f98..c55756b4008 100644--- a/apps/studio/hooks/misc/useReportDateRange.ts+++ b/apps/studio/hooks/misc/useReportDateRange.ts@@ -176,13 +176,16 @@ export const useReportDateRange = (return getDefaultHelper().helper.text}, [timestampStartValue, timestampEndValue, helperTextValue, getDefaultHelper])- const handleIntervalGranularity = (from: string, to: string) => {+ const handleIntervalGranularity = (from: string, to: string): AnalyticsInterval => {const diffInDays = dayjs(to).diff(from, 'day', true)const diffInHours = dayjs(to).diff(from, 'hour', true)if (useV2Granularity) {- if (diffInHours <= 24) return '1m'- if (diffInDays <= 7) return '10m'+ if (diffInHours <= 1) return '1m'+ if (diffInHours <= 12) return '2m'+ if (diffInHours <= 24) return '10m'+ if (diffInDays <= 7) return '1h'+ return '1d'}const conditions = {