Files
supabase/apps/studio/data/logs/unified-logs-count-query.ts
Charis df742bdb3b refactor(studio): rename ResponseError to StudioError (#52707)
## What

Moves the `ResponseError` class (`types/base.ts`) and the classified
error classes (`types/api-errors.ts`) into
`lib/errors/studio-errors.ts`, and renames `ResponseError` to
`StudioError`.

## Why

Studio's error classification is being expanded to cover errors that
don't come from API responses (thrown network failures, aborted
requests, unexpected client errors). The API-specific name and location
no longer fit. Landing the rename on its own keeps the follow-up PR,
which introduces the classified error model, small and reviewable.

## Notes for reviewers

- Mechanical change, no behavior change. Most of the diff is a one-line
import change per file plus import re-sorting by Prettier.
- Files that only use the type now use `import type { StudioError }`.
- `ResponseError` is no longer re-exported from `@/types`; there are no
compatibility shims at the old paths.
- Unrelated identifiers like `QueryResponseError` and
`parseResponseErrors` are unchanged.
2026-11-03 11:09:30 -05:00

106 lines
3.5 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { executeAnalyticsSql } from './execute-analytics-sql'
import { logsKeys } from './keys'
import { logsAllEndpointUrl, pickLogsQueryBuilder } from './logs-endpoint'
import {
getUnifiedLogsISOStartEnd,
UNIFIED_LOGS_QUERY_OPTIONS,
UnifiedLogsVariables,
useUnifiedLogsBackend,
} from './unified-logs-infinite-query'
import { getLogsCountQuery } from '@/components/interfaces/UnifiedLogs/UnifiedLogs.queries'
import { getLogsCountQuery as getLogsCountQueryBq } from '@/components/interfaces/UnifiedLogs/UnifiedLogs.queries.bq'
import { FacetMetadataSchema } from '@/components/interfaces/UnifiedLogs/UnifiedLogs.schema'
import { handleError } from '@/data/fetchers'
import type { StudioError } from '@/lib/errors/studio-errors'
import { UseCustomQueryOptions } from '@/types'
// Trimmed client-side because the OTEL endpoint rejects LIMIT BY, so the count
// query can't cap rows per facet in SQL.
const MAX_FACET_ROWS = 20
export async function getUnifiedLogsCount(
{ projectRef, search, useOtel = false }: UnifiedLogsVariables & { useOtel?: boolean },
signal?: AbortSignal
) {
if (typeof projectRef === 'undefined') {
throw new Error('projectRef is required for getUnifiedLogsCount')
}
const sql = pickLogsQueryBuilder(useOtel, getLogsCountQuery, getLogsCountQueryBq)(search)
const { isoTimestampStart, isoTimestampEnd } = getUnifiedLogsISOStartEnd(search)
const endpoint = logsAllEndpointUrl(useOtel)
const data = await executeAnalyticsSql({
projectRef,
endpoint,
sql,
iso_timestamp_start: isoTimestampStart,
iso_timestamp_end: isoTimestampEnd,
signal,
})
if (data.error) handleError(typeof data.error === 'string' ? new Error(data.error) : data.error)
const facets: Record<string, FacetMetadataSchema> = {}
const countsByFacet: Record<string, Map<string, number>> = {}
let totalRowCount = 0
if (data?.result) {
data.result.forEach((row: any) => {
const facet = row.facet
const value = row.value
const count = Number(row.count || 0)
if (facet === 'total' && value === 'all') {
totalRowCount = count
}
if (!countsByFacet[facet]) {
countsByFacet[facet] = new Map()
}
countsByFacet[facet].set(value, count)
})
}
Object.entries(countsByFacet).forEach(([facet, countsMap]) => {
if (facet === 'total') return
const rows = Array.from(countsMap.entries())
.map(([value, count]) => ({ value, total: count }))
.sort((a, b) => b.total - a.total)
.slice(0, MAX_FACET_ROWS)
const facetTotal = rows.reduce((sum, row) => sum + row.total, 0)
facets[facet] = {
total: facetTotal,
rows,
}
})
return { totalRowCount, facets }
}
export type UnifiedLogsCountData = Awaited<ReturnType<typeof getUnifiedLogsCount>>
export type UnifiedLogsCountError = StudioError
export const useUnifiedLogsCountQuery = <TData = UnifiedLogsCountData>(
{ projectRef, search }: UnifiedLogsVariables,
{
enabled = true,
...options
}: UseCustomQueryOptions<UnifiedLogsCountData, UnifiedLogsCountError, TData> = {}
) => {
const useOtel = useUnifiedLogsBackend(search)
return useQuery<UnifiedLogsCountData, UnifiedLogsCountError, TData>({
queryKey: [...logsKeys.unifiedLogsCount(projectRef, search), { otel: useOtel }],
queryFn: ({ signal }) => getUnifiedLogsCount({ projectRef, search, useOtel }, signal),
enabled: enabled && typeof projectRef !== 'undefined',
...UNIFIED_LOGS_QUERY_OPTIONS,
...options,
})
}