Skip to content

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

fix(studio): prevent API Gateway report tab crash (#50847)

Jordi Enric881c12430172
diff --git a/apps/studio/components/interfaces/Settings/Logs/Logs.utils.ts b/apps/studio/components/interfaces/Settings/Logs/Logs.utils.ts
index 72e98b9487f..1e626a8eab6 100644
--- a/apps/studio/components/interfaces/Settings/Logs/Logs.utils.ts
+++ b/apps/studio/components/interfaces/Settings/Logs/Logs.utils.ts
@@ -581,11 +581,20 @@ export const fillTimeseries = (
})
}
- if (timeseriesData.length <= 1 && !(min && max)) return timeseriesData
- const dates: unknown[] = timeseriesData.map((datum) => dayjs.utc(datum[timestampKey]))
+ if (timeseriesData.length === 1 && !(min && max)) {
+ const timestamp = timeseriesData[0][timestampKey]
+ timeseriesData[0][timestampKey] = isUnixMicro(timestamp)
+ ? unixMicroToIsoTimestamp(timestamp)
+ : dayjs.utc(timestamp).toISOString()
+ return timeseriesData
+ }
+ const dates = timeseriesData.map((datum) => {
+ const timestamp = datum[timestampKey]
+ return dayjs.utc(isUnixMicro(timestamp) ? unixMicroToIsoTimestamp(timestamp) : timestamp)
+ })
- const maxDate = max ? dayjs.utc(max) : dayjs.utc(Math.max.apply(null, dates as number[]))
- const minDate = min ? dayjs.utc(min) : dayjs.utc(Math.min.apply(null, dates as number[]))
+ const maxDate = max ? dayjs.utc(max) : dayjs.utc(Math.max(...dates.map((date) => date.valueOf())))
+ const minDate = min ? dayjs.utc(min) : dayjs.utc(Math.min(...dates.map((date) => date.valueOf())))
// When no data exists but min/max are provided, we need to determine truncation from the time range
const truncationSamples = timeseriesData.length > 0 ? dates : [minDate, maxDate]
@@ -601,10 +610,10 @@ export const fillTimeseries = (
truncation = unitMap[unitChar]
} else {
// Fallback for invalid format
- truncation = getTimestampTruncation(truncationSamples as Dayjs[])
+ truncation = getTimestampTruncation(truncationSamples)
}
} else {
- truncation = getTimestampTruncation(truncationSamples as Dayjs[])
+ truncation = getTimestampTruncation(truncationSamples)
}
// If no data exists and no interval specified, default to minute precision
@@ -612,11 +621,8 @@ export const fillTimeseries = (
truncation = 'minute'
}
- const newData = timeseriesData.map((datum) => {
- const timestamp = datum[timestampKey]
- const iso = isUnixMicro(timestamp)
- ? unixMicroToIsoTimestamp(timestamp)
- : dayjs.utc(timestamp).toISOString()
+ const newData = timeseriesData.map((datum, index) => {
+ const iso = dates[index].toISOString()
if (Array.isArray(valueKey) && valueKey.length === 0) {
return { [timestampKey]: iso }
@@ -628,8 +634,7 @@ export const fillTimeseries = (
let currentDate = minDate
while (currentDate.isBefore(maxDate) || currentDate.isSame(maxDate)) {
- const found = dates.find((d) => {
- const d_date = d as Dayjs
+ const found = dates.find((d_date) => {
return (
d_date.year() === currentDate.year() &&
d_date.month() === currentDate.month() &&
diff --git a/apps/studio/data/reports/api-report-query.ts b/apps/studio/data/reports/api-report-query.ts
index 03cc9e71fc4..09fbc71743d 100644
--- a/apps/studio/data/reports/api-report-query.ts
+++ b/apps/studio/data/reports/api-report-query.ts
@@ -3,13 +3,13 @@ import { FeatureFlagContext, useFlag, useParams } from 'common'
import isEqual from 'lodash/isEqual'
import { useContext, useState } from 'react'
-import { PRESET_CONFIG } from '@/components/interfaces/Reports/Reports.constants'
+import {
+ PRESET_CONFIG,
+ REPORTS_DATEPICKER_HELPERS,
+} from '@/components/interfaces/Reports/Reports.constants'
import type { ReportFilterItem } from '@/components/interfaces/Reports/Reports.types'
import { getLogsSql } from '@/components/interfaces/Reports/Reports.utils'
-import {
- EXPLORER_DATEPICKER_HELPERS,
- getDefaultHelper,
-} from '@/components/interfaces/Settings/Logs/Logs.constants'
+import { getDefaultHelper } from '@/components/interfaces/Settings/Logs/Logs.constants'
import type { LogData, LogsEndpointParams } from '@/components/interfaces/Settings/Logs/Logs.types'
import { executeAnalyticsSql } from '@/data/logs/execute-analytics-sql'
import { logsAllEndpointUrl } from '@/data/logs/logs-endpoint'
@@ -68,7 +68,7 @@ export const useApiReport = () => {
const { hasLoaded: hasLoadedFlags } = useContext(FeatureFlagContext)
const [filters, setFilters] = useState<ReportFilterItem[]>([])
const [requestParams, setRequestParams] = useState<ApiReportRequestParams>(() => {
- const defaultDateRange = getDefaultHelper(EXPLORER_DATEPICKER_HELPERS)
+ const defaultDateRange = getDefaultHelper(REPORTS_DATEPICKER_HELPERS)
return {
iso_timestamp_start: defaultDateRange.calcFrom(),
iso_timestamp_end: defaultDateRange.calcTo(),
diff --git a/apps/studio/tests/features/logs/Logs.utils.test.ts b/apps/studio/tests/features/logs/Logs.utils.test.ts
index ad628c43a2e..c94fd16700e 100644
--- a/apps/studio/tests/features/logs/Logs.utils.test.ts
+++ b/apps/studio/tests/features/logs/Logs.utils.test.ts
@@ -139,6 +139,53 @@ describe('fillTimeseries', () => {
})
})
+ test('should bound sparse microsecond timestamps when the end date is empty', () => {
+ const start = dayjs.utc('2026-09-24T06:00:00.000Z')
+ const data = [
+ { timestamp: start.valueOf() * 1000, value: 1 },
+ { timestamp: start.add(1, 'hour').valueOf() * 1000, value: 2 },
+ ]
+
+ const result = fillTimeseries(data, 'timestamp', 'value', 0, start.toISOString(), '')
+
+ expect(result).toHaveLength(2)
+ expect(result.map((point) => point.timestamp)).toEqual([
+ start.toISOString(),
+ start.add(1, 'hour').toISOString(),
+ ])
+ expect(result.map((point) => point.value)).toEqual([1, 2])
+ })
+
+ test('should preserve sparse microsecond points within explicit bounds', () => {
+ const start = dayjs.utc('2026-09-24T06:00:00.000Z')
+ const end = start.add(1, 'hour')
+ const data = [
+ { timestamp: start.valueOf() * 1000, value: 1 },
+ { timestamp: end.valueOf() * 1000, value: 2 },
+ ]
+
+ const result = fillTimeseries(
+ data,
+ 'timestamp',
+ 'value',
+ 0,
+ start.toISOString(),
+ end.toISOString()
+ )
+
+ expect(result).toHaveLength(2)
+ expect(result.map((point) => point.value)).toEqual([1, 2])
+ })
+
+ test('should normalize a single microsecond point without an end date', () => {
+ const timestamp = dayjs.utc('2026-09-24T06:00:00.000Z')
+ const data = [{ timestamp: timestamp.valueOf() * 1000, value: 1 }]
+
+ expect(fillTimeseries(data, 'timestamp', 'value', 0, timestamp.toISOString(), '')).toEqual([
+ { timestamp: timestamp.toISOString(), value: 1 },
+ ])
+ })
+
test('should handle mixed timestamp formats', () => {
const data = [
{ timestamp: '2023-01-01T00:00:00.000Z', value: 1 },