mirror of
https://github.com/supabase/supabase.git
synced 2026-10-15 14:19:11 +03:00
## I have read the [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) file. YES ## What kind of change does this PR introduce? Refactor / type safety improvement ## What is the current behavior? The legacy log query stack (`genDefaultQuery`, `genCountQuery`, `genChartQuery`, `genWhereStatement`, `useLogsPreview`, `useSingleLog`) builds SQL from raw strings with no type-level guarantee that values are safely interpolated. Identifier helpers (`bqIdent`, `bqDottedIdent`, `clickhouseIdent`, `clickhouseDottedIdent`) are duplicated across BigQuery and ClickHouse variants, and `bqDottedIdent` wraps the entire dotted path in one backtick pair (`` `request.pathname` ``), which BigQuery treats as a literal column name rather than a UNNEST alias field — causing runtime query failures on dotted filter keys. ## What is the new behavior? - All gen functions return `SafeLogSqlFragment` and all callers route through `executeAnalyticsSql`, enforcing compile-time SQL provenance tracking across the legacy stack. - `bqIdent` / `bqDottedIdent` / `clickhouseIdent` / `clickhouseDottedIdent` are replaced by a single `quotedIdent` function that backtick-quotes each segment individually (e.g. `` `request`.`pathname` ``). ClickHouse natively accepts backticks, so one function serves both engines and the dotted-path quoting bug is fixed. - `SQL_FILTER_TEMPLATES` entries are converted to `SafeLogSqlFragment` (static via `safeSql`, dynamic via `safeSql` + `analyticsLiteral`). - `buildWhereClauses` is extracted as a private helper returning `SafeLogSqlFragment[]` so the pg_cron path can merge clauses without unsafe slice-and-cast. ## Additional context <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Refactor** * Logs query generation migrated to safer, engine-agnostic SQL fragments, typed filter templates, and unified identifier quoting for stronger injection protection and more consistent queries. * Logs preview and single-log retrieval now execute analytics SQL end-to-end using the unified executor. * **New Features** * Analytics SQL executor can call the backend via GET or POST and accepts method selection. * **Tests** * Updated tests to validate unified identifier quoting and safe-SQL helper behavior. <!-- review_stack_entry_start --> [](https://app.coderabbit.ai/change-stack/supabase/supabase/pull/46351?utm_source=github_walkthrough&utm_medium=github&utm_campaign=change_stack) <!-- review_stack_entry_end --> <!-- end of auto-generated comment: release notes by coderabbit.ai -->
267 lines
7.1 KiB
TypeScript
267 lines
7.1 KiB
TypeScript
import { useInfiniteQuery, useQuery } from '@tanstack/react-query'
|
|
import dayjs from 'dayjs'
|
|
import { useCallback, useMemo, useState } from 'react'
|
|
|
|
import { useFillTimeseriesSorted } from './useFillTimeseriesSorted'
|
|
import { useLogsUrlState } from './useLogsUrlState'
|
|
import useTimeseriesUnixToIso from './useTimeseriesUnixToIso'
|
|
import {
|
|
getDefaultHelper,
|
|
LogsTableName,
|
|
PREVIEWER_DATEPICKER_HELPERS,
|
|
} from '@/components/interfaces/Settings/Logs/Logs.constants'
|
|
import type {
|
|
Count,
|
|
EventChart,
|
|
EventChartData,
|
|
Filters,
|
|
LogData,
|
|
Logs,
|
|
LogsEndpointParams,
|
|
} from '@/components/interfaces/Settings/Logs/Logs.types'
|
|
import {
|
|
genChartQuery,
|
|
genCountQuery,
|
|
genDefaultQuery,
|
|
} from '@/components/interfaces/Settings/Logs/Logs.utils'
|
|
import { executeAnalyticsSql } from '@/data/logs/execute-analytics-sql'
|
|
|
|
interface LogsPreviewHook {
|
|
logData: LogData[]
|
|
error: string | Object | null
|
|
newCount: number
|
|
isSuccess: boolean
|
|
isLoading: boolean
|
|
isLoadingOlder: boolean
|
|
filters: Filters
|
|
params: LogsEndpointParams
|
|
oldestTimestamp?: string
|
|
eventChartData: EventChartData[]
|
|
loadOlder: () => void
|
|
refresh: () => void
|
|
}
|
|
|
|
function useLogsPreview({
|
|
projectRef,
|
|
table,
|
|
filterOverride,
|
|
limit,
|
|
}: {
|
|
projectRef: string
|
|
table: LogsTableName
|
|
filterOverride?: Filters
|
|
limit?: number
|
|
}): LogsPreviewHook {
|
|
const defaultHelper = getDefaultHelper(PREVIEWER_DATEPICKER_HELPERS)
|
|
const [latestRefresh, setLatestRefresh] = useState(new Date().toISOString())
|
|
|
|
const {
|
|
timestampStart: urlTimestampStart,
|
|
timestampEnd: urlTimestampEnd,
|
|
filters: urlFilters,
|
|
search,
|
|
} = useLogsUrlState()
|
|
|
|
const timestampStart = useMemo(
|
|
() => urlTimestampStart || defaultHelper.calcFrom(),
|
|
[urlTimestampStart, defaultHelper]
|
|
)
|
|
const timestampEnd = useMemo(
|
|
() => urlTimestampEnd || defaultHelper.calcTo(),
|
|
[urlTimestampEnd, defaultHelper]
|
|
)
|
|
|
|
const mergedFilters = useMemo(
|
|
() => ({
|
|
...urlFilters,
|
|
...filterOverride,
|
|
...(search ? { search_query: search } : {}),
|
|
}),
|
|
[JSON.stringify(urlFilters), JSON.stringify(filterOverride), search]
|
|
)
|
|
|
|
const defaultSql = useMemo(
|
|
() => genDefaultQuery(table, mergedFilters, limit),
|
|
[table, mergedFilters, limit]
|
|
)
|
|
|
|
const params: LogsEndpointParams = useMemo(
|
|
() => ({
|
|
iso_timestamp_start: timestampStart,
|
|
iso_timestamp_end: timestampEnd,
|
|
sql: defaultSql,
|
|
}),
|
|
[timestampStart, timestampEnd, defaultSql]
|
|
)
|
|
|
|
const queryKey = useMemo(
|
|
() => ['projects', projectRef, 'logs', params, defaultSql],
|
|
[projectRef, params, defaultSql]
|
|
)
|
|
|
|
const {
|
|
data,
|
|
isSuccess,
|
|
isLoading,
|
|
isRefetching,
|
|
error: rqError,
|
|
fetchNextPage,
|
|
isFetchingNextPage,
|
|
refetch,
|
|
} = useInfiniteQuery({
|
|
queryKey,
|
|
queryFn: async ({ signal, pageParam }) => {
|
|
const data = await executeAnalyticsSql({
|
|
projectRef,
|
|
endpoint: '/platform/projects/{ref}/analytics/endpoints/logs.all',
|
|
sql: defaultSql,
|
|
iso_timestamp_start: params.iso_timestamp_start ?? '',
|
|
iso_timestamp_end: (pageParam || params.iso_timestamp_end) ?? '',
|
|
method: 'get',
|
|
signal,
|
|
})
|
|
return data as unknown as Logs
|
|
},
|
|
refetchOnWindowFocus: false,
|
|
initialPageParam: undefined as string | undefined,
|
|
getNextPageParam(lastPage) {
|
|
if ((lastPage.result?.length ?? 0) === 0) {
|
|
return undefined
|
|
}
|
|
const len = lastPage.result.length
|
|
const { timestamp: tsLimit }: LogData = lastPage.result[len - 1]
|
|
const isoTsLimit = dayjs.utc(Number(tsLimit / 1000)).toISOString()
|
|
return isoTsLimit
|
|
},
|
|
})
|
|
|
|
const { logData, error, oldestTimestamp } = useMemo(() => {
|
|
let logData: LogData[] = []
|
|
let error: null | string | object = rqError ? (rqError as any).message : null
|
|
|
|
data?.pages?.forEach((response) => {
|
|
if (response?.result) {
|
|
logData = [...logData, ...response.result]
|
|
}
|
|
if (!error && response?.error) {
|
|
error = response.error
|
|
}
|
|
})
|
|
|
|
const oldestTimestamp = logData.length > 0 ? logData[logData.length - 1]?.timestamp : undefined
|
|
|
|
return { logData, error, oldestTimestamp }
|
|
}, [data?.pages])
|
|
|
|
const countQuerySql = useMemo(() => genCountQuery(table, mergedFilters), [table, mergedFilters])
|
|
const countQueryKey = useMemo(
|
|
() => [
|
|
'projects',
|
|
projectRef,
|
|
'logs-count',
|
|
{
|
|
projectRef,
|
|
sql: countQuerySql,
|
|
iso_timestamp_start: latestRefresh,
|
|
iso_timestamp_end: timestampEnd,
|
|
table,
|
|
mergedFilters,
|
|
},
|
|
],
|
|
[projectRef, countQuerySql, latestRefresh, timestampEnd, table, mergedFilters]
|
|
)
|
|
|
|
const { data: countData } = useQuery({
|
|
queryKey: countQueryKey,
|
|
queryFn: async ({ signal }) => {
|
|
const data = await executeAnalyticsSql({
|
|
projectRef,
|
|
endpoint: '/platform/projects/{ref}/analytics/endpoints/logs.all',
|
|
sql: countQuerySql,
|
|
iso_timestamp_start: latestRefresh,
|
|
iso_timestamp_end: timestampEnd ?? '',
|
|
method: 'get',
|
|
signal,
|
|
})
|
|
return data as unknown as Count
|
|
},
|
|
refetchOnWindowFocus: false,
|
|
refetchInterval: 60000,
|
|
enabled: !error && data && data?.pages?.length > 0 ? true : false,
|
|
})
|
|
|
|
const newCount = countData?.result?.[0]?.count ?? 0
|
|
|
|
const chartQuery = useMemo(
|
|
() => genChartQuery(table, params, mergedFilters),
|
|
[table, params, mergedFilters]
|
|
)
|
|
const chartQueryKey = useMemo(
|
|
() => [
|
|
'projects',
|
|
projectRef,
|
|
'logs-chart',
|
|
{
|
|
projectRef,
|
|
sql: chartQuery,
|
|
iso_timestamp_start: timestampStart,
|
|
iso_timestamp_end: timestampEnd,
|
|
},
|
|
],
|
|
[projectRef, chartQuery, timestampStart, timestampEnd]
|
|
)
|
|
|
|
const { data: eventChartResponse, refetch: refreshEventChart } = useQuery({
|
|
queryKey: chartQueryKey,
|
|
queryFn: async ({ signal }) => {
|
|
const data = await executeAnalyticsSql({
|
|
projectRef,
|
|
endpoint: '/platform/projects/{ref}/analytics/endpoints/logs.all',
|
|
sql: chartQuery,
|
|
iso_timestamp_start: timestampStart,
|
|
iso_timestamp_end: timestampEnd ?? '',
|
|
method: 'get',
|
|
signal,
|
|
})
|
|
return data as unknown as EventChart
|
|
},
|
|
refetchOnWindowFocus: false,
|
|
})
|
|
|
|
const refresh = useCallback(async () => {
|
|
setLatestRefresh(new Date().toISOString())
|
|
refreshEventChart()
|
|
refetch()
|
|
}, [refetch, refreshEventChart])
|
|
|
|
const normalizedEventChartData = useTimeseriesUnixToIso(
|
|
eventChartResponse?.result ?? [],
|
|
'timestamp'
|
|
)
|
|
|
|
const { data: eventChartData, error: eventChartError } = useFillTimeseriesSorted({
|
|
data: normalizedEventChartData,
|
|
timestampKey: 'timestamp',
|
|
valueKey: 'count',
|
|
defaultValue: 0,
|
|
startDate: timestampStart,
|
|
endDate: timestampEnd ?? new Date().toISOString(),
|
|
})
|
|
|
|
return {
|
|
newCount,
|
|
logData,
|
|
isSuccess,
|
|
isLoading: isLoading || isRefetching,
|
|
isLoadingOlder: isFetchingNextPage,
|
|
error: error || eventChartError,
|
|
filters: mergedFilters,
|
|
params,
|
|
oldestTimestamp: oldestTimestamp ? String(oldestTimestamp) : undefined,
|
|
eventChartData,
|
|
refresh,
|
|
loadOlder: () => fetchNextPage(),
|
|
}
|
|
}
|
|
export default useLogsPreview
|