mirror of
https://github.com/supabase/supabase.git
synced 2026-09-06 09:59:03 +08:00
Second of a stack. **Stacked on #49069** — review that one first; this PR's diff only makes sense on top of it. Base will retarget to `master` automatically when #49069 merges. Net `-72` lines. No behavior change beyond the one noted at the bottom. ## The problem The query-source registry carried its own `LogTimeRange` type and `logTimeRangeSchema`, which had drifted from the notebook wire schema's copy in four ways: | | wire schema | registry | |---|---|---| | discriminant | `_tag: 'relative_time_range'` | `type: 'relative'` | | absolute bounds | `start` / `end` | `from` / `to` | | relative units | minute…year | minute, hour, day | | validation | none | positive int, end-after-start | Two definitions of one concept, neither convertible to the other without a lossy mapping — and the notebook query cell was papering over it by discarding a log cell's persisted range and substituting a default. ## What changed #49069 moved the validations onto the wire schema's `timeRangeSchema` and exported it. This PR deletes the registry's copy and points every consumer at `TimeRange`. The registry keeps what is genuinely runtime: endpoints, labels, availability, defaults. The field renames ripple mechanically through the logs date-picker helpers, the time-range submenu, `useLogsCustomRange`, the SQL editor's session state, and their tests. Coverage for the absolute-range and unit rules moved to `notebook-schema.test.ts` in #49069, alongside the schema that now owns them. `ExplorerQuerySourceMenu` also drops its hand-rolled custom-range construction in favor of `customDateRangeToLogTimeRange`, which already existed and does the same clamping. ## One behavior change `logTimeRangeToDatePickerValue` now renders a range whose unit has no picker preset (week, month, year — allowed by the wire schema, not offered in the UI) as a resolved absolute range, instead of trying and failing to build a helper for it. Previously unreachable, since the registry's narrower unit set made those ranges unrepresentable. ## Verification Typecheck, Prettier, and the lint ratchet clean. 401 tests pass across the notebook schema, query sources, the logs source components, the SQL editor, and the Explorer surfaces. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved log time-range handling across Explorer and SQL Editor. * Custom date ranges now display and resolve correctly, including clamping invalid ranges. * Unsupported relative time units are converted to compatible absolute date-picker values. * **Refactor** * Standardized log queries on a shared time-range format for more consistent validation and behavior. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
126 lines
4.0 KiB
TypeScript
126 lines
4.0 KiB
TypeScript
import dayjs from 'dayjs'
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import {
|
|
customDateRangeToLogTimeRange,
|
|
datePickerValueToLogTimeRange,
|
|
logTimeRangesEqual,
|
|
logTimeRangeToDatePickerValue,
|
|
resolveLogTimeRange,
|
|
} from './LogTimeRange.utils'
|
|
import { generateDynamicHelper } from '@/components/interfaces/Settings/Logs/Logs.datePickerHelpers'
|
|
import { timeRangeSchema, type TimeRange } from '@/data/content/notebooks/notebook-schema'
|
|
import { DEFAULT_LOG_TIME_RANGE } from '@/data/query-sources/query-source-registry'
|
|
|
|
/** Absolute bounds are branded ISO strings, so build them through the schema. */
|
|
const absolute = (start: string, end: string): TimeRange =>
|
|
timeRangeSchema.parse({ _tag: 'absolute_time_range', start, end })
|
|
|
|
describe('LogTimeRange.utils', () => {
|
|
beforeEach(() => {
|
|
vi.useFakeTimers()
|
|
vi.setSystemTime(new Date('2025-01-08T12:00:00.000Z'))
|
|
})
|
|
|
|
afterEach(() => vi.useRealTimers())
|
|
|
|
it.each([
|
|
['30m', 30, 'minute'],
|
|
['2h', 2, 'hour'],
|
|
['7d', 7, 'day'],
|
|
] as const)('round-trips the picker helper for %s', (_, amount, unit) => {
|
|
const helper = generateDynamicHelper(amount, unit)
|
|
const pickerValue = {
|
|
from: helper.calcFrom(),
|
|
to: helper.calcTo(),
|
|
isHelper: true,
|
|
text: helper.text,
|
|
}
|
|
const range: TimeRange = { _tag: 'relative_time_range', amount, unit }
|
|
|
|
expect(datePickerValueToLogTimeRange(pickerValue)).toEqual(range)
|
|
expect(logTimeRangeToDatePickerValue(range)).toEqual(pickerValue)
|
|
})
|
|
|
|
it('renders a relative unit the picker has no preset for as a resolved absolute range', () => {
|
|
expect(
|
|
logTimeRangeToDatePickerValue({ _tag: 'relative_time_range', amount: 2, unit: 'month' })
|
|
).toEqual({
|
|
from: dayjs().subtract(2, 'month').toISOString(),
|
|
to: dayjs().toISOString(),
|
|
isHelper: false,
|
|
})
|
|
})
|
|
|
|
it('falls back to the default when a custom value has no valid start', () => {
|
|
expect(datePickerValueToLogTimeRange({ from: '', to: '', isHelper: false })).toEqual(
|
|
DEFAULT_LOG_TIME_RANGE
|
|
)
|
|
})
|
|
|
|
it('uses now when an absolute helper has an empty end', () => {
|
|
expect(
|
|
datePickerValueToLogTimeRange({
|
|
from: '2025-01-01T00:00:00.000Z',
|
|
to: '',
|
|
isHelper: true,
|
|
text: 'Custom',
|
|
})
|
|
).toEqual({
|
|
_tag: 'absolute_time_range',
|
|
start: '2025-01-01T00:00:00.000Z',
|
|
end: '2025-01-08T12:00:00.000Z',
|
|
})
|
|
})
|
|
|
|
it('compares relative and absolute ranges structurally', () => {
|
|
expect(
|
|
logTimeRangesEqual(
|
|
{ _tag: 'relative_time_range', amount: 1, unit: 'hour' },
|
|
{ _tag: 'relative_time_range', amount: 1, unit: 'hour' }
|
|
)
|
|
).toBe(true)
|
|
expect(
|
|
logTimeRangesEqual(
|
|
{ _tag: 'relative_time_range', amount: 1, unit: 'hour' },
|
|
{ _tag: 'relative_time_range', amount: 1, unit: 'day' }
|
|
)
|
|
).toBe(false)
|
|
expect(
|
|
logTimeRangesEqual(
|
|
absolute('2025-01-01T00:00:00.000Z', '2025-01-02T00:00:00.000Z'),
|
|
absolute('2025-01-01T00:00:00.000Z', '2025-01-02T00:00:00.000Z')
|
|
)
|
|
).toBe(true)
|
|
})
|
|
|
|
it('resolves a relative range against the current time', () => {
|
|
expect(resolveLogTimeRange({ _tag: 'relative_time_range', amount: 2, unit: 'day' })).toEqual({
|
|
from: dayjs().subtract(2, 'day').toISOString(),
|
|
to: dayjs().toISOString(),
|
|
})
|
|
})
|
|
|
|
it('passes an absolute range through unchanged', () => {
|
|
const range = absolute('2025-01-01T00:00:00.000Z', '2025-01-02T00:00:00.000Z')
|
|
expect(resolveLogTimeRange(range)).toEqual({
|
|
from: '2025-01-01T00:00:00.000Z',
|
|
to: '2025-01-02T00:00:00.000Z',
|
|
})
|
|
})
|
|
|
|
it('clamps a custom range ending today to now', () => {
|
|
const from = new Date('2025-01-07T09:00:00.000Z')
|
|
expect(
|
|
customDateRangeToLogTimeRange({
|
|
from,
|
|
to: new Date('2025-01-08T09:00:00.000Z'),
|
|
})
|
|
).toEqual({
|
|
_tag: 'absolute_time_range',
|
|
start: dayjs(from).startOf('day').toISOString(),
|
|
end: '2025-01-08T12:00:00.000Z',
|
|
})
|
|
})
|
|
})
|