Skip to content

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

perf(logs): load pathname facet counts on demand DEBUG-230 (#51112)

Jordi Enricd2ffd76395c0
diff --git a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.fields.tsx b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.fields.tsx
index 4ae39344de1..aef9b1634e7 100644
--- a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.fields.tsx
+++ b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.fields.tsx
@@ -117,7 +117,7 @@ export const filterFields = [
defaultOpen: false,
options: [],
hasDynamicOptions: true,
- hasAsyncSearch: false,
+ hasAsyncSearch: true,
component: (props: Option) => {
return (
<span className="truncate block w-full text-[0.75rem]" title={props.value as string}>
diff --git a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.bq.ts b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.bq.ts
index 82fb7753a96..bb180254823 100644
--- a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.bq.ts
+++ b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.bq.ts
@@ -32,7 +32,7 @@ const ALL_LOG_TYPES = ['edge', 'postgrest', 'storage', 'postgres', 'edge functio
* to the cheap two-source set. With `=` filters, narrows to those values. With
* `<>` filters, excludes them from the full set.
*/
-const getEffectiveLogTypes = (search: QuerySearchParamsType): string[] => {
+export const getEffectiveLogTypes = (search: QuerySearchParamsType): string[] => {
const filters = parseLogsFilterUrlParams(search.filter).filter((f) => f.column === 'log_type')
if (filters.length === 0) return [...DEFAULT_LOG_TYPES]
const included = filters.filter((f) => f.operator === '=').map((f) => f.value)
@@ -430,6 +430,7 @@ export const getFacetCountCTE = ({
const facetSearchClause = facetSearch
? safeSql`AND ${facetCol} LIKE ${lit('%' + facetSearch + '%')}`
: safeSql``
+ const facetOrder = facet === 'pathname' ? safeSql`ORDER BY count DESC` : safeSql``
const where =
baseConditions.length > 0
@@ -443,6 +444,7 @@ ${cteName} AS (
${where}
${facetSearchClause}
GROUP BY ${facetCol}
+ ${facetOrder}
LIMIT ${lit(MAX_FACETS_QUANTITY)}
)
`
@@ -490,8 +492,7 @@ level_counts AS (
-- Variable facets: open-ended values still need GROUP BY
${getFacetCountCTE({ search, facet: 'method', cteName: safeSql`method_count` })},
-${getFacetCountCTE({ search, facet: 'status', cteName: safeSql`status_count` })},
-${getFacetCountCTE({ search, facet: 'pathname', cteName: safeSql`pathname_count` })}
+${getFacetCountCTE({ search, facet: 'status', cteName: safeSql`status_count` })}
SELECT 'total' AS dimension, 'all' AS value, total AS count FROM log_type_counts
UNION ALL SELECT 'log_type', 'edge', edge_count FROM log_type_counts
@@ -505,7 +506,6 @@ UNION ALL SELECT 'level', 'warning', warning_count FROM level_counts
UNION ALL SELECT 'level', 'error', error_count FROM level_counts
UNION ALL SELECT dimension, value, count FROM method_count
UNION ALL SELECT dimension, value, count FROM status_count
-UNION ALL SELECT dimension, value, count FROM pathname_count
`
}
diff --git a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.test.ts b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.test.ts
index ad779e652ff..a83a982d03c 100644
--- a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.test.ts
+++ b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.test.ts
@@ -306,10 +306,6 @@ describe('UnifiedLogs.queries (OTEL flat)', () => {
for (const lvl of ['success', 'warning', 'error']) {
expect(sql).toContain(`'${lvl}'`)
}
- expect(sql).toContain(`'pathname'`)
- expect(sql).toContain('LIMIT 20')
- // log_type + base + pathname = 3 scans
- expect(sql.match(/FROM logs/g)?.length ?? 0).toBeLessThanOrEqual(4)
})
it('honours an active log_type filter in the total count scan', () => {
diff --git a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.ts b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.ts
index 27d79b6f727..fc923e5e23d 100644
--- a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.ts
+++ b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.ts
@@ -501,6 +501,7 @@ SELECT ${lit(facet)} AS facet, (${facetExpr}) AS value, count() AS count
FROM logs
${whereClause(conditions)}
GROUP BY value
+ORDER BY count DESC
LIMIT ${lit(MAX_FACETS_QUANTITY)}
`
}
@@ -561,10 +562,6 @@ HAVING value != ''
}
blocks.push(scanBlock(baseFacets, whereFor()))
- // pathname is high-cardinality, so it needs its own LIMIT (the endpoint
- // rejects LIMIT BY inside the shared arrayJoin).
- blocks.push(safeSql`(${getFacetCountQuery({ search, facet: 'pathname' })})`)
-
return safeSql`-- unified logs: sidebar facet counts
${joinSqlFragments(blocks, ' UNION ALL ')}`
}
diff --git a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.test.tsx b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.test.tsx
new file mode 100644
index 00000000000..47ce6ef0d35
--- /dev/null
+++ b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.test.tsx
@@ -0,0 +1,87 @@
+import { QueryClient } from '@tanstack/react-query'
+import { fireEvent, screen, waitFor } from '@testing-library/react'
+import { platformComponents as components } from 'api-types'
+import { FeatureFlagContext } from 'common'
+import { mockAnimationsApi } from 'jsdom-testing-mocks'
+import { HttpResponse } from 'msw'
+import { describe, expect, it, vi } from 'vitest'
+
+import { UnifiedLogs } from './UnifiedLogs'
+import { customRender } from '@/tests/lib/custom-render'
+import { addAPIMock } from '@/tests/lib/msw'
+
+type AnalyticsResponse = components['schemas']['AnalyticsResponse_Output']
+type ProjectResponse = components['schemas']['ProjectDetailResponse_Output']
+
+mockAnimationsApi()
+
+describe('UnifiedLogs', () => {
+ it('refreshes pathname options for a sidebar filter change before the URL updates', async () => {
+ let pathnameOption: string | null = null
+ const onUrlUpdate = vi.fn()
+ const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } })
+
+ addAPIMock({
+ method: 'get',
+ path: '/platform/projects/:ref',
+ response: () =>
+ HttpResponse.json<ProjectResponse>({
+ id: 1,
+ ref: 'default',
+ organization_id: 1,
+ name: 'Test Project',
+ status: 'INACTIVE',
+ cloud_provider: 'AWS',
+ region: 'us-east-1',
+ db_host: 'db.default.supabase.co',
+ restUrl: 'https://default.supabase.co/rest/v1/',
+ inserted_at: '2024-01-01T00:00:00Z',
+ updated_at: '2024-01-01T00:00:00Z',
+ subscription_id: 'sub_123',
+ is_branch_enabled: false,
+ is_physical_backups_enabled: false,
+ high_availability: false,
+ integration_source: null,
+ connectionString: null,
+ }),
+ })
+
+ addAPIMock({
+ method: 'post',
+ path: '/platform/projects/:ref/analytics/endpoints/logs.all',
+ response: () =>
+ HttpResponse.json<AnalyticsResponse>({
+ result: pathnameOption === null ? [] : [{ value: pathnameOption, count: 1 }],
+ }),
+ })
+
+ customRender(
+ <FeatureFlagContext.Provider
+ value={{ configcat: { otelUnifiedLogs: false }, posthog: {}, hasLoaded: true }}
+ >
+ <UnifiedLogs />
+ </FeatureFlagContext.Provider>,
+ {
+ nuqs: { hasMemory: true, onUrlUpdate },
+ queryClient,
+ }
+ )
+
+ expect(await screen.findByText('No results found', {}, { timeout: 5_000 })).toBeInTheDocument()
+ await waitFor(() => expect(queryClient.isFetching()).toBe(0), { timeout: 5_000 })
+
+ pathnameOption = '/before'
+ fireEvent.click(screen.getByRole('button', { name: 'Pathname' }))
+ expect(await screen.findByText('/before', {}, { timeout: 5_000 })).toBeInTheDocument()
+ await waitFor(() => expect(queryClient.isFetching()).toBe(0), { timeout: 5_000 })
+ onUrlUpdate.mockClear()
+
+ fireEvent.click(screen.getByRole('button', { name: 'Method' }))
+ pathnameOption = '/after'
+ fireEvent.click(screen.getByRole('checkbox', { name: /POST/ }))
+
+ expect(onUrlUpdate).not.toHaveBeenCalled()
+ expect(screen.queryByText('/before')).not.toBeInTheDocument()
+ expect(await screen.findByText('/after', {}, { timeout: 5_000 })).toBeInTheDocument()
+ }, 15_000)
+})
diff --git a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.tsx b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.tsx
index 79ffa5db357..a7a9c6974e5 100644
--- a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.tsx
+++ b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.tsx
@@ -131,6 +131,7 @@ export const UnifiedLogs = () => {
const [sorting, setSorting] = useState<SortingState>(defaultColumnSorting)
const [columnFilters, setColumnFilters] = useState<ColumnFiltersState>(defaultColumnFilters)
+ const [localFilterOrigin, setLocalFilterOrigin] = useState<string | null>(null)
const [dock, setDock] = useLocalStorageQuery<'bottom' | 'right'>(
LOCAL_STORAGE_KEYS.UNIFIED_LOGS_DOCK,
@@ -168,6 +169,9 @@ export const UnifiedLogs = () => {
}
return parameters
}, [search, showMultigresLogs, computeAvailability.canQueryCompute])
+ const urlFilterScope = JSON.stringify([searchParameters.filter, searchParameters.date])
+
+ useEffect(() => setLocalFilterOrigin(null), [urlFilterScope])
const { selection, selectRow, clearSelection } = useTableRowSelection({
scope: JSON.stringify([projectRef, searchParameters]),
@@ -280,7 +284,10 @@ export const UnifiedLogs = () => {
meta: { getRowClassName },
getRowId: (row) => row.id,
onColumnVisibilityChange: setColumnVisibility,
- onColumnFiltersChange: setColumnFilters,
+ onColumnFiltersChange: (updater) => {
+ setLocalFilterOrigin(urlFilterScope)
+ setColumnFilters(updater)
+ },
onSortingChange: setSorting,
onColumnOrderChange: setColumnOrder,
getSortedRowModel: getSortedRowModel(),
@@ -416,6 +423,7 @@ export const UnifiedLogs = () => {
columnOrder={columnOrder}
columnVisibility={columnVisibility}
searchParameters={searchParameters}
+ hasPendingFilterChange={localFilterOrigin === urlFilterScope}
enableColumnOrdering={true}
isFetching={isFetching}
isError={isError}
diff --git a/apps/studio/components/interfaces/UnifiedLogs/useUnifiedLogsPathnameOptions.ts b/apps/studio/components/interfaces/UnifiedLogs/useUnifiedLogsPathnameOptions.ts
new file mode 100644
index 00000000000..951cda6d2f2
--- /dev/null
+++ b/apps/studio/components/interfaces/UnifiedLogs/useUnifiedLogsPathnameOptions.ts
@@ -0,0 +1,85 @@
+import { useDebounce } from '@uidotdev/usehooks'
+import { useFeatureFlags, useParams } from 'common'
+import { useState } from 'react'
+
+import {
+ columnFiltersToLogsFilters,
+ isLogsFilterColumnValue,
+ logsFiltersToUrlParams,
+} from './UnifiedLogs.filters'
+import { QuerySearchParamsType } from './UnifiedLogs.types'
+import { Option } from '@/components/ui/DataTable/DataTable.types'
+import { useDataTable } from '@/components/ui/DataTable/providers/DataTableProvider'
+import { useUnifiedLogsFacetCountQuery } from '@/data/logs/unified-logs-facet-count-query'
+
+export function useUnifiedLogsPathnameOptions(value: string, options?: Option[]) {
+ const [inputValue, setInputValue] = useState('')
+ const { ref: projectRef } = useParams()
+ const { hasLoaded: flagsLoaded } = useFeatureFlags()
+ const { table, searchParameters, columnFilters, filterFields, hasPendingFilterChange } =
+ useDataTable<unknown, unknown, QuerySearchParamsType>()
+ const debouncedSearch = useDebounce(inputValue, 700)
+
+ const filterableNames = new Set(
+ filterFields.filter((field) => field.type !== 'timerange').map((field) => String(field.value))
+ )
+ const dateValue = columnFilters.find((filter) => filter.id === 'date')?.value
+ const date =
+ Array.isArray(dateValue) &&
+ dateValue.length === 2 &&
+ dateValue.every((value) => value instanceof Date)
+ ? dateValue
+ : null
+ const scopedSearch = hasPendingFilterChange
+ ? {
+ ...searchParameters,
+ filter: logsFiltersToUrlParams(columnFiltersToLogsFilters(columnFilters, filterableNames)),
+ date,
+ }
+ : searchParameters
+ const {
+ data: facetOptions,
+ error,
+ isPending,
+ isError,
+ isFetching,
+ } = useUnifiedLogsFacetCountQuery(
+ {
+ projectRef,
+ search: scopedSearch,
+ facet: value,
+ facetSearch: debouncedSearch,
+ },
+ { enabled: flagsLoaded }
+ )
+
+ const column = table.getColumn(value)
+ const filterValue = columnFilters.find((filter) => filter.id === value)?.value
+ const selectedValues = isLogsFilterColumnValue(filterValue) ? filterValue.values : []
+ const visibleFacetOptions = inputValue === debouncedSearch ? (facetOptions ?? []) : []
+ const selectedOptions = selectedValues.map(
+ (selected) =>
+ visibleFacetOptions.find((option) => option.value === selected) ??
+ options?.find((option) => option.value === selected) ?? {
+ label: selected,
+ value: selected,
+ }
+ )
+ const filterOptions = [
+ ...selectedOptions,
+ ...visibleFacetOptions.filter((option) => !selectedValues.includes(option.value)),
+ ]
+
+ return {
+ column,
+ error,
+ filterOptions,
+ inputValue,
+ isError,
+ isFetching,
+ isLoading: !flagsLoaded || isPending || inputValue !== debouncedSearch,
+ projectRef,
+ selectedValues,
+ setInputValue,
+ }
+}
diff --git a/apps/studio/components/ui/DataTable/DataTableFilters/DataTableFilterCheckboxAsync.test.tsx b/apps/studio/components/ui/DataTable/DataTableFilters/DataTableFilterCheckboxAsync.test.tsx
new file mode 100644
index 00000000000..5a1e2d56dda
--- /dev/null
+++ b/apps/studio/components/ui/DataTable/DataTableFilters/DataTableFilterCheckboxAsync.test.tsx
@@ -0,0 +1,460 @@
+import { QueryClient } from '@tanstack/react-query'
+import { getCoreRowModel, useReactTable, type ColumnFiltersState } from '@tanstack/react-table'
+import { fireEvent, screen, waitFor } from '@testing-library/react'
+import { platformComponents as components } from 'api-types'
+import { FeatureFlagContext } from 'common'
+import { mockAnimationsApi } from 'jsdom-testing-mocks'
+import { HttpResponse } from 'msw'
+import { useEffect, useState } from 'react'
+import { describe, expect, it } from 'vitest'
+
+import { DataTableFilterControls } from './DataTableFilterControls'
+import { buildDefaultColumnFilters } from '@/components/interfaces/UnifiedLogs/UnifiedLogs.filters'
+import { QuerySearchParamsType } from '@/components/interfaces/UnifiedLogs/UnifiedLogs.types'
+import { DataTableCheckboxFilterField } from '@/components/ui/DataTable/DataTable.types'
+import { DataTableProvider } from '@/components/ui/DataTable/providers/DataTableProvider'
+import { useUnifiedLogsFacetCountQuery } from '@/data/logs/unified-logs-facet-count-query'
+import { customRender } from '@/tests/lib/custom-render'
+import { addAPIMock } from '@/tests/lib/msw'
+
+type AnalyticsResponse = components['schemas']['AnalyticsResponse_Output']
+
+const pathnameField = {
+ label: 'Pathname',
+ value: 'pathname',
+ type: 'checkbox',
+ options: [],
+ hasDynamicOptions: true,
+ hasAsyncSearch: true,
+} satisfies DataTableCheckboxFilterField<{ pathname: string; method: string }>
+
+const methodField = {
+ label: 'Method',
+ value: 'method',
+ type: 'checkbox',
+ options: [{ label: 'POST', value: 'POST' }],
+} satisfies DataTableCheckboxFilterField<{ pathname: string; method: string }>
+
+const columns = [
+ { accessorKey: 'pathname', header: 'Pathname' },
+ { accessorKey: 'method', header: 'Method' },
+]
+const initialDate = [new Date('2026-05-08T09:00:00Z'), new Date('2026-05-08T10:00:00Z')]
+const defaultPathnameSearch: QuerySearchParamsType = {
+ filter: null,
+ latency: null,
+ 'timing.dns': null,
+ 'timing.connection': null,
+ 'timing.tls': null,
+ 'timing.ttfb': null,
+ 'timing.transfer': null,
+ date: initialDate,
+ sort: null,
+ size: 40,
+ start: 0,
+ direction: 'next',
+ cursor: new Date('2026-05-08T10:00:00Z'),
+ id: null,
+ show_connection_logs: true,
+ edge_auth: true,
+ edge_storage: true,
+ edge_postgrest: true,
+ user: null,
+}
+const pathnameSearch = (overrides: Partial<QuerySearchParamsType> = {}): QuerySearchParamsType => ({
+ ...defaultPathnameSearch,
+ ...overrides,
+})
+
+function PathnameFilter({
+ search,
+ flagsLoaded = true,
+ useOtel = false,
+}: {
+ search: QuerySearchParamsType
+ flagsLoaded?: boolean
+ useOtel?: boolean
+}) {
+ const [columnFilters, setColumnFilters] = useState<ColumnFiltersState>(() =>
+ buildDefaultColumnFilters(search)
+ )
+ const [localFilterOrigin, setLocalFilterOrigin] = useState<string | null>(null)
+ const urlFilterScope = JSON.stringify([search.filter, search.date])
+ useEffect(() => setLocalFilterOrigin(null), [urlFilterScope])
+ const table = useReactTable({
+ data: [] as { pathname: string; method: string }[],
+ columns,
+ state: { columnFilters },
+ onColumnFiltersChange: (updater) => {
+ setLocalFilterOrigin(urlFilterScope)
+ setColumnFilters(updater)
+ },
+ getCoreRowModel: getCoreRowModel(),
+ })
+
+ return (
+ <FeatureFlagContext.Provider
+ value={{ configcat: { otelUnifiedLogs: useOtel }, posthog: {}, hasLoaded: flagsLoaded }}
+ >
+ <DataTableProvider
+ table={table}
+ error={null}
+ columns={columns}
+ filterFields={[pathnameField, methodField]}
+ columnFilters={columnFilters}
+ searchParameters={search}
+ hasPendingFilterChange={localFilterOrigin === urlFilterScope}
+ isFetching={false}
+ isError={false}
+ isLoading={false}
+ isLoadingCounts={false}
+ >
+ <DataTableFilterControls />
+ </DataTableProvider>
+ </FeatureFlagContext.Provider>
+ )
+}
+
+function ProjectFacetOptions({ projectRef }: { projectRef: string }) {
+ const { data } = useUnifiedLogsFacetCountQuery({
+ projectRef,
+ search: pathnameSearch(),
+ facet: 'pathname',
+ })
+ return (
+ <div>
+ {data?.map((option) => (
+ <span key={option.value}>{option.label}</span>
+ ))}
+ </div>
+ )
+}
+
+mockAnimationsApi()
+
+const addProjectMock = () =>
+ addAPIMock({
+ method: 'get',
+ path: '/platform/projects/:ref',
+ response: {
+ id: 1,
+ ref: 'default',
+ organization_id: 1,
+ name: 'Test Project',
+ status: 'ACTIVE_HEALTHY',
+ cloud_provider: 'AWS',
+ region: 'us-east-1',
+ db_host: 'db.default.supabase.co',
+ restUrl: 'https://default.supabase.co/rest/v1/',
+ inserted_at: '2024-01-01T00:00:00Z',
+ updated_at: '2024-01-01T00:00:00Z',
+ subscription_id: 'sub_123',
+ is_branch_enabled: false,
+ is_physical_backups_enabled: false,
+ high_availability: false,
+ integration_source: null,
+ connectionString: 'postgresql://postgres@localhost:5432/postgres',
+ is_hibernating: false,
+ },
+ })
+
+describe('pathname facet filter', () => {
+ it('waits for feature flags before requesting options', async () => {
+ let requests = 0
+ addAPIMock({
+ method: 'post',
+ path: '/platform/projects/:ref/analytics/endpoints/logs.all',
+ response: () => {
+ requests += 1
+ return HttpResponse.json<AnalyticsResponse>({ result: [{ value: '/ready', count: 1 }] })
+ },
+ })
+
+ const search = pathnameSearch()
+ const { rerender } = customRender(<PathnameFilter search={search} flagsLoaded={false} />)
+
+ fireEvent.click(screen.getByText('Pathname'))
+ expect(requests).toBe(0)
+ rerender(<PathnameFilter search={search} />)
+ expect(await screen.findByText('/ready')).toBeInTheDocument()
+ expect(requests).toBe(1)
+ })
+
+ it('fetches on open and search, then reuses cached options for unchanged inputs', async () => {
+ let requests = 0
+ addAPIMock({
+ method: 'post',
+ path: '/platform/projects/:ref/analytics/endpoints/logs.all',
+ response: () => {
+ requests += 1
+ return HttpResponse.json<AnalyticsResponse>({
+ result: [{ value: requests === 1 ? '/base' : '/api/items', count: 4 }],
+ })
+ },
+ })
+
+ const { rerender } = customRender(<PathnameFilter search={pathnameSearch()} />, {
+ queryClient: new QueryClient({ defaultOptions: { queries: { retry: false } } }),
+ })
+
+ expect(requests).toBe(0)
+ fireEvent.click(screen.getByText('Pathname'))
+ expect(await screen.findByText('/base')).toBeInTheDocument()
+ expect(requests).toBe(1)
+
+ fireEvent.change(screen.getByPlaceholderText('Search'), { target: { value: '/api' } })
+ expect(screen.queryByText('/base')).not.toBeInTheDocument()
+ expect(await screen.findByText('/api/items', {}, { timeout: 3000 })).toBeInTheDocument()
+ expect(requests).toBe(2)
+
+ fireEvent.click(screen.getByText('Pathname'))
+ fireEvent.click(screen.getByText('Pathname'))
+ expect(await screen.findByText('/base')).toBeInTheDocument()
+ expect(requests).toBe(2)
+
+ rerender(
+ <PathnameFilter search={pathnameSearch({ cursor: new Date('2026-05-08T09:30:00Z') })} />
+ )
+ expect(screen.getByText('/base')).toBeInTheDocument()
+ expect(requests).toBe(2)
+ })
+
+ it('clears search on Escape before blurring the input', () => {
+ addAPIMock({
+ method: 'post',
+ path: '/platform/projects/:ref/analytics/endpoints/logs.all',
+ response: () => HttpResponse.json<AnalyticsResponse>({ result: [] }),
+ })
+
+ customRender(<PathnameFilter search={pathnameSearch()} />)
+ fireEvent.click(screen.getByText('Pathname'))
+ const searchInput = screen.getByPlaceholderText('Search')
+ searchInput.focus()
+ fireEvent.change(searchInput, { target: { value: '/api' } })
+
+ fireEvent.keyDown(searchInput, { key: 'Escape' })
+ expect(searchInput).toHaveValue('')
+ expect(searchInput).toHaveFocus()
+
+ fireEvent.keyDown(searchInput, { key: 'Escape' })
+ expect(searchInput).not.toHaveFocus()
+ })
+
+ it('replaces options on URL time-range and filter changes without syncing local filters', async () => {
+ let requests = 0
+ addAPIMock({
+ method: 'post',
+ path: '/platform/projects/:ref/analytics/endpoints/logs.all',
+ response: () => {
+ requests += 1
+ const value = ['/initial', '/later', '/post'][requests - 1]
+ return HttpResponse.json<AnalyticsResponse>({ result: [{ value, count: 2 }] })
+ },
+ })
+
+ const { rerender } = customRender(
+ <PathnameFilter search={pathnameSearch({ filter: ['pathname:eq:/selected'] })} />
+ )
+
+ fireEvent.click(screen.getByText('Pathname'))
+ expect(screen.getByRole('checkbox', { name: /\/selected/ })).toHaveAttribute(
+ 'data-state',
+ 'checked'
+ )
+ expect(await screen.findByText('/initial')).toBeInTheDocument()
+
+ rerender(
+ <PathnameFilter
+ search={pathnameSearch({
+ date: [new Date('2026-05-09T09:00:00Z'), new Date('2026-05-09T10:00:00Z')],
+ filter: ['pathname:eq:/selected'],
+ })}
+ />
+ )
+ expect(screen.queryByText('/initial')).not.toBeInTheDocument()
+ expect(await screen.findByText('/later')).toBeInTheDocument()
+
+ rerender(
+ <PathnameFilter
+ search={pathnameSearch({
+ date: [new Date('2026-05-09T09:00:00Z'), new Date('2026-05-09T10:00:00Z')],
+ filter: ['pathname:eq:/selected', 'method:eq:POST'],
+ })}
+ />
+ )
+ expect(screen.queryByText('/later')).not.toBeInTheDocument()
+ expect(await screen.findByText('/post')).toBeInTheDocument()
+ expect(requests).toBe(3)
+
+ rerender(
+ <PathnameFilter
+ search={pathnameSearch({
+ date: [new Date('2026-05-09T09:00:00Z'), new Date('2026-05-09T10:00:00Z')],
+ filter: ['method:eq:POST'],
+ })}
+ />
+ )
+ expect(screen.getByText('/post')).toBeInTheDocument()
+ expect(requests).toBe(3)
+ })
+
+ it('keeps selected values visible and removable while options load', async () => {
+ addAPIMock({
+ method: 'post',
+ path: '/platform/projects/:ref/analytics/endpoints/logs.all',
+ response: () => HttpResponse.json<AnalyticsResponse>({ result: [] }),
+ })
+
+ customRender(<PathnameFilter search={pathnameSearch({ filter: ['pathname:eq:/selected'] })} />)
+ fireEvent.click(screen.getByText('Pathname'))
+ const selectedCheckbox = screen.getByRole('checkbox', { name: /\/selected/ })
+ expect(selectedCheckbox).toHaveAttribute('data-state', 'checked')
+ fireEvent.click(selectedCheckbox)
+ await waitFor(() => expect(screen.queryByRole('checkbox', { name: /\/selected/ })).toBeNull())
+ })
+
+ it('validates and orders option rows from the response', async () => {
+ addAPIMock({
+ method: 'post',
+ path: '/platform/projects/:ref/analytics/endpoints/logs.all',
+ response: () =>
+ HttpResponse.json<AnalyticsResponse>({
+ result: [
+ { value: '/low', count: '1' },
+ { value: '/high', count: '12' },
+ ],
+ }),
+ })
+
+ customRender(<PathnameFilter search={pathnameSearch()} />)
+ fireEvent.click(screen.getByText('Pathname'))
+ expect(await screen.findByText('/high')).toBeInTheDocument()
+ expect(screen.getAllByText(/^\/(high|low)$/).map((option) => option.textContent)).toEqual([
+ '/high',
+ '/low',
+ ])
+ expect(screen.getByText('12')).toBeInTheDocument()
+ })
+
+ it('shows a validation error while keeping selected paths removable', async () => {
+ addProjectMock()
+ addAPIMock({
+ method: 'post',
+ path: '/platform/projects/:ref/analytics/endpoints/logs.all',
+ response: () =>
+ HttpResponse.json<AnalyticsResponse>({ result: [{ value: '/invalid', count: 'wrong' }] }),
+ })
+
+ customRender(<PathnameFilter search={pathnameSearch({ filter: ['pathname:eq:/selected'] })} />)
+ fireEvent.click(screen.getByText('Pathname'))
+ expect(await screen.findByText('Failed to retrieve pathnames')).toBeInTheDocument()
+ expect(screen.queryByText('/invalid')).not.toBeInTheDocument()
+
+ fireEvent.click(screen.getByRole('checkbox', { name: /\/selected/ }))
+ expect(screen.queryByRole('checkbox', { name: /\/selected/ })).not.toBeInTheDocument()
+ })
+
+ it('shows a validation error when the analytics response omits results', async () => {
+ addProjectMock()
+ addAPIMock({
+ method: 'post',
+ path: '/platform/projects/:ref/analytics/endpoints/logs.all',
+ response: () => HttpResponse.json<AnalyticsResponse>({}),
+ })
+
+ customRender(<PathnameFilter search={pathnameSearch()} />)
+ fireEvent.click(screen.getByText('Pathname'))
+ expect(await screen.findByText('Failed to retrieve pathnames')).toBeInTheDocument()
+ expect(screen.queryByText('No results found')).not.toBeInTheDocument()
+ })
+
+ it.each([null, '', true, false, -1, 1.5, '-1', '1.5'])(
+ 'rejects malformed option count %s without hiding selected paths',
+ async (count) => {
+ addProjectMock()
+ addAPIMock({
+ method: 'post',
+ path: '/platform/projects/:ref/analytics/endpoints/logs.all',
+ response: () =>
+ HttpResponse.json<AnalyticsResponse>({ result: [{ value: '/invalid', count }] }),
+ })
+
+ customRender(
+ <PathnameFilter search={pathnameSearch({ filter: ['pathname:eq:/selected'] })} />
+ )
+ fireEvent.click(screen.getByText('Pathname'))
+ expect(await screen.findByText('Failed to retrieve pathnames')).toBeInTheDocument()
+ expect(screen.queryByText('/invalid')).not.toBeInTheDocument()
+ fireEvent.click(screen.getByRole('checkbox', { name: /\/selected/ }))
+ expect(screen.queryByRole('checkbox', { name: /\/selected/ })).not.toBeInTheDocument()
+ }
+ )
+
+ it.each([
+ { error: 'Analytics unavailable', message: 'Analytics unavailable' },
+ {
+ error: {
+ code: 503,
+ errors: [],
+ message: 'Analytics service unavailable',
+ status: 'UNAVAILABLE',
+ },
+ message: 'Analytics service unavailable',
+ },
+ ])(
+ 'shows analytics error $message without treating missing results as empty options',
+ async ({ error, message }) => {
+ addProjectMock()
+ addAPIMock({
+ method: 'post',
+ path: '/platform/projects/:ref/analytics/endpoints/logs.all',
+ response: () => HttpResponse.json<AnalyticsResponse>({ error }),
+ })
+
+ customRender(<PathnameFilter search={pathnameSearch()} />)
+ fireEvent.click(screen.getByText('Pathname'))
+ expect(await screen.findByText('Failed to retrieve pathnames')).toBeInTheDocument()
+ expect(screen.getByRole('alert')).toHaveTextContent(message)
+ expect(screen.queryByText('No results found')).not.toBeInTheDocument()
+ }
+ )
+
+ it('loads and displays options from the OTEL endpoint when that backend is enabled', async () => {
+ addAPIMock({
+ method: 'post',
+ path: '/platform/projects/:ref/analytics/endpoints/logs.all.otel',
+ response: () =>
+ HttpResponse.json<AnalyticsResponse>({ result: [{ value: '/otel', count: '3' }] }),
+ })
+
+ customRender(<PathnameFilter search={pathnameSearch()} useOtel />)
+ fireEvent.click(screen.getByText('Pathname'))
+ expect(await screen.findByRole('checkbox', { name: /\/otel/ })).toBeInTheDocument()
+ expect(screen.getByText('3')).toBeInTheDocument()
+ })
+
+ it('replaces project options without showing cached paths from another project', async () => {
+ addAPIMock({
+ method: 'post',
+ path: '/platform/projects/:ref/analytics/endpoints/logs.all',
+ response: ({ params }) =>
+ HttpResponse.json<AnalyticsResponse>({
+ result: [{ value: params.ref === 'project-a' ? '/project-a' : '/project-b', count: 1 }],
+ }),
+ })
+
+ const renderProject = (projectRef: string) => (
+ <FeatureFlagContext.Provider
+ value={{ configcat: { otelUnifiedLogs: false }, posthog: {}, hasLoaded: true }}
+ >
+ <ProjectFacetOptions projectRef={projectRef} />
+ </FeatureFlagContext.Provider>
+ )
+ const { rerender } = customRender(renderProject('project-a'))
+ expect(await screen.findByText('/project-a')).toBeInTheDocument()
+
+ rerender(renderProject('project-b'))
+ expect(screen.queryByText('/project-a')).not.toBeInTheDocument()
+ expect(await screen.findByText('/project-b')).toBeInTheDocument()
+ })
+})
diff --git a/apps/studio/components/ui/DataTable/DataTableFilters/DataTableFilterCheckboxAsync.tsx b/apps/studio/components/ui/DataTable/DataTableFilters/DataTableFilterCheckboxAsync.tsx
index 9e4f28aa092..8170a8a306c 100644
--- a/apps/studio/components/ui/DataTable/DataTableFilters/DataTableFilterCheckboxAsync.tsx
+++ b/apps/studio/components/ui/DataTable/DataTableFilters/DataTableFilterCheckboxAsync.tsx
@@ -1,17 +1,12 @@
-import { keepPreviousData } from '@tanstack/react-query'
-import { useDebounce } from '@uidotdev/usehooks'
-import { useParams } from 'common'
import { Loader2, Search } from 'lucide-react'
-import { useState } from 'react'
-import { Checkbox, cn, Label, Skeleton } from 'ui'
+import { Checkbox, cn, Label } from 'ui'
import type { DataTableCheckboxFilterField } from '../DataTable.types'
import { formatCompactNumber } from '../DataTable.utils'
import { InputWithAddons } from '../primitives/InputWithAddons'
-import { useDataTable } from '../providers/DataTableProvider'
-import { isLogsFilterColumnValue } from '@/components/interfaces/UnifiedLogs/UnifiedLogs.filters'
-import { QuerySearchParamsType } from '@/components/interfaces/UnifiedLogs/UnifiedLogs.types'
-import { useUnifiedLogsFacetCountQuery } from '@/data/logs/unified-logs-facet-count-query'
+import { useUnifiedLogsPathnameOptions } from '@/components/interfaces/UnifiedLogs/useUnifiedLogsPathnameOptions'
+import { AlertError } from '@/components/ui/AlertError'
+import { onSearchInputEscape } from '@/lib/keyboard'
export function DataTableFilterCheckboxAsync<TData>({
value: _value,
@@ -19,44 +14,18 @@ export function DataTableFilterCheckboxAsync<TData>({
component: Component,
}: DataTableCheckboxFilterField<TData>) {
const value = _value as string
- const [inputValue, setInputValue] = useState('')
-
- const { table, searchParameters, columnFilters, isLoadingCounts, getFacetedUniqueValues } =
- useDataTable<TData, unknown, QuerySearchParamsType>()
-
- // [Joshen] JFYI for simplicity currently, i'm adding UnifiedLogs logic into this file
- // despite this supposedly being a reusable component - tbh really, this doesn't need to
- // be reusable perhaps unless we plan for this to be used in another area of the dashboard
- // but its too early to say for sure atm.
- const { ref: projectRef } = useParams()
- const debouncedSearch = useDebounce(inputValue, 700)
- const { data: filterOptions = [], isFetching: isFetchingFacetCount } =
- useUnifiedLogsFacetCountQuery(
- {
- projectRef,
- search: searchParameters,
- facet: value,
- facetSearch: debouncedSearch,
- },
- {
- placeholderData: keepPreviousData,
- enabled: debouncedSearch.length > 0,
- initialData: debouncedSearch.length === 0 ? options : undefined,
- }
- )
-
- const column = table.getColumn(value)
- const filterValue = columnFilters.find((i) => i.id === value)?.value
- const facetedValue = getFacetedUniqueValues?.(table, value) || column?.getFacetedUniqueValues()
- // Column filter values are always the wrapped `{ operator, values }` shape.
- const filters = isLogsFilterColumnValue(filterValue) ? filterValue.values : []
-
- if (!options?.length)
- return (
- <div className="flex items-center justify-center px-2 py-4 text-center border border-border rounded-sm">
- <p className="text-xs text-foreground-light">No options available</p>
- </div>
- )
+ const {
+ column,
+ error,
+ filterOptions,
+ inputValue,
+ isError,
+ isFetching,
+ isLoading,
+ projectRef,
+ selectedValues,
+ setInputValue,
+ } = useUnifiedLogsPathnameOptions(value, options)
return (
<div className="grid gap-2">
@@ -65,12 +34,21 @@ export function DataTableFilterCheckboxAsync<TData>({
leading={<Search size={14} className="text-foreground-lighter" />}
containerClassName="h-8 rounded-sm"
value={inputValue}
- trailing={isFetchingFacetCount ? <Loader2 size={12} className="animate-spin" /> : undefined}
+ trailing={isFetching ? <Loader2 size={12} className="animate-spin" /> : undefined}
onChange={(e) => setInputValue(e.target.value)}
+ onKeyDown={onSearchInputEscape(inputValue, setInputValue)}
/>
+ {isError && (
+ <AlertError error={error} subject="Failed to retrieve pathnames" projectRef={projectRef} />
+ )}
+
<div className="max-h-[215px] overflow-y-auto rounded-sm border border-border empty:border-none">
- {filterOptions.length === 0 ? (
+ {filterOptions.length === 0 && isLoading ? (
+ <div className="flex items-center justify-center px-2 py-3 text-center">
+ <Loader2 size={14} className="animate-spin" />
+ </div>
+ ) : filterOptions.length === 0 && !isError ? (
<div className="flex items-center justify-center px-2 py-3 text-center">
<div className="space-y-0.5">
<p className="text-xs text-foreground">No results found</p>
@@ -79,7 +57,7 @@ export function DataTableFilterCheckboxAsync<TData>({
</div>
) : (
filterOptions.map((option, index) => {
- const checked = filters.includes(option.value)
+ const checked = selectedValues.includes(option.value)
return (
<div
@@ -94,8 +72,8 @@ export function DataTableFilterCheckboxAsync<TData>({
checked={checked}
onCheckedChange={(checked) => {
const newValues = checked
- ? [...filters, option.value]
- : filters.filter((value) => option.value !== value)
+ ? [...selectedValues, option.value]
+ : selectedValues.filter((value) => option.value !== value)
column?.setFilterValue(
newValues.length ? { operator: '=', values: newValues } : undefined
)
@@ -113,13 +91,7 @@ export function DataTableFilterCheckboxAsync<TData>({
)}
</div>
<span className="shrink-0 flex items-center justify-center font-mono text-xs group-hover:opacity-0">
- {isLoadingCounts ? (
- <Skeleton className="h-4 w-4" />
- ) : facetedValue?.has(option.value) ? (
- formatCompactNumber(facetedValue.get(option.value) || 0)
- ) : ['log_type', 'method', 'level'].includes(value) ? (
- '0'
- ) : null}
+ {'count' in option ? formatCompactNumber(option.count) : null}
</span>
<button
type="button"
diff --git a/apps/studio/components/ui/DataTable/DataTableFilters/DataTableFilterControls.tsx b/apps/studio/components/ui/DataTable/DataTableFilters/DataTableFilterControls.tsx
index e5d299a0b96..38e71a37472 100644
--- a/apps/studio/components/ui/DataTable/DataTableFilters/DataTableFilterControls.tsx
+++ b/apps/studio/components/ui/DataTable/DataTableFilters/DataTableFilterControls.tsx
@@ -1,4 +1,4 @@
-import { Fragment, type ReactNode } from 'react'
+import { Fragment, useState, type ReactNode } from 'react'
import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from 'ui'
import { DateRangeDisabled } from '../DataTable.types'
@@ -27,13 +27,13 @@ export function DataTableFilterControls({
itemsAfter,
}: DataTableFilterControls) {
const { filterFields, isLoadingCounts } = useDataTable()
+ const [openFilters, setOpenFilters] = useState<string[]>(
+ () =>
+ filterFields?.filter(({ defaultOpen }) => defaultOpen)?.map(({ value }) => value as string) ??
+ []
+ )
return (
- <Accordion
- type="multiple"
- defaultValue={filterFields
- ?.filter(({ defaultOpen }) => defaultOpen)
- ?.map(({ value }) => value as string)}
- >
+ <Accordion type="multiple" value={openFilters} onValueChange={setOpenFilters}>
{filterFields
?.filter((field) => !field.hidden)
.map((field) => {
@@ -50,18 +50,16 @@ export function DataTableFilterControls({
<DataTableFilterResetButton {...field} />
</div>
<AccordionContent>
- {/* REMINDER: avoid the focus state to be cut due to overflow-hidden */}
- {/* REMINDER: need to move within here because of accordion height animation */}
<div className="p-1">
{(() => {
switch (field.type) {
case 'checkbox': {
- // [Joshen] Loader here so that CheckboxAsync can retrieve the data
- // immediately to be set in its react query state
- if (field.hasDynamicOptions && isLoadingCounts) {
+ if (field.hasDynamicOptions && !field.hasAsyncSearch && isLoadingCounts) {
return <DataTableFilterCheckboxLoader />
} else if (field.hasAsyncSearch) {
- return <DataTableFilterCheckboxAsync {...field} />
+ return openFilters.includes(value) ? (
+ <DataTableFilterCheckboxAsync {...field} />
+ ) : null
} else {
return <DataTableFilterCheckbox {...field} />
}
diff --git a/apps/studio/components/ui/DataTable/providers/DataTableProvider.tsx b/apps/studio/components/ui/DataTable/providers/DataTableProvider.tsx
index 91007cfcee2..1bfa45fe2dd 100644
--- a/apps/studio/components/ui/DataTable/providers/DataTableProvider.tsx
+++ b/apps/studio/components/ui/DataTable/providers/DataTableProvider.tsx
@@ -25,6 +25,7 @@ interface DataTableStateContextType<TSearchParams = unknown> {
pagination: PaginationState
enableColumnOrdering: boolean
searchParameters: TSearchParams
+ hasPendingFilterChange?: boolean
openRowId: string | undefined
setOpenRowId: (id: string | undefined) => void
onSelectRow?: (id: string, modifiers?: RowSelectionModifiers) => void
diff --git a/apps/studio/data/logs/unified-logs-facet-count-query.ts b/apps/studio/data/logs/unified-logs-facet-count-query.ts
index 43dde999327..aa644d4b371 100644
--- a/apps/studio/data/logs/unified-logs-facet-count-query.ts
+++ b/apps/studio/data/logs/unified-logs-facet-count-query.ts
@@ -1,4 +1,5 @@
import { useQuery } from '@tanstack/react-query'
+import { z } from 'zod'
import { executeAnalyticsSql } from './execute-analytics-sql'
import { logsKeys } from './keys'
@@ -10,12 +11,16 @@ import {
UnifiedLogsVariables,
useUnifiedLogsBackend,
} from './unified-logs-infinite-query'
+import {
+ logsFiltersToUrlParams,
+ parseLogsFilterUrlParams,
+} from '@/components/interfaces/UnifiedLogs/UnifiedLogs.filters'
import { getFacetCountQuery } from '@/components/interfaces/UnifiedLogs/UnifiedLogs.queries'
import {
+ getEffectiveLogTypes,
getFacetCountCTE,
getUnifiedLogsCTE,
} from '@/components/interfaces/UnifiedLogs/UnifiedLogs.queries.bq'
-import { Option } from '@/components/ui/DataTable/DataTable.types'
import { ResponseError, UseCustomQueryOptions } from '@/types'
type UnifiedLogsFacetCountVariables = UnifiedLogsVariables & {
@@ -24,6 +29,15 @@ type UnifiedLogsFacetCountVariables = UnifiedLogsVariables & {
useOtel?: boolean
}
+const facetRowsSchema = z.array(
+ z.object({
+ value: z.union([z.string(), z.number()]).transform(String),
+ count: z
+ .union([z.number(), z.string().regex(/^\d+$/).transform(Number)])
+ .refine((count) => Number.isSafeInteger(count) && count >= 0),
+ })
+)
+
export async function getUnifiedLogsFacetCount(
{ projectRef, search, facet, facetSearch, useOtel = false }: UnifiedLogsFacetCountVariables,
signal?: AbortSignal
@@ -38,7 +52,7 @@ export async function getUnifiedLogsFacetCount(
const sql = useOtel
? getFacetCountQuery({ search, facet, facetSearch })
: safeSql`
-${getUnifiedLogsCTE()},
+${getUnifiedLogsCTE(getEffectiveLogTypes(search))},
${getFacetCountCTE({ search, facet, facetSearch, cteName })}
SELECT dimension, value, count from ${cteName};
`
@@ -52,11 +66,21 @@ SELECT dimension, value, count from ${cteName};
iso_timestamp_end: isoTimestampEnd,
signal,
})
- return (data.result ?? []) as Option[]
+ if (data.error !== undefined) {
+ throw new Error(typeof data.error === 'string' ? data.error : data.error.message)
+ }
+ return facetRowsSchema
+ .parse(data.result)
+ .sort((a, b) => b.count - a.count)
+ .map((row) => ({
+ label: row.value,
+ value: row.value,
+ count: row.count,
+ }))
}
export type UnifiedLogsFacetCountData = Awaited<ReturnType<typeof getUnifiedLogsFacetCount>>
-export type UnifiedLogsFacetCountError = ResponseError
+export type UnifiedLogsFacetCountError = ResponseError | z.ZodError
export const useUnifiedLogsFacetCountQuery = <TData = UnifiedLogsFacetCountData>(
{ projectRef, search, facet, facetSearch }: UnifiedLogsFacetCountVariables,
@@ -66,13 +90,40 @@ export const useUnifiedLogsFacetCountQuery = <TData = UnifiedLogsFacetCountData>
}: UseCustomQueryOptions<UnifiedLogsFacetCountData, UnifiedLogsFacetCountError, TData> = {}
) => {
const useOtel = useUnifiedLogsBackend()
+ const scopedSearch =
+ facet === 'pathname'
+ ? {
+ ...search,
+ filter: logsFiltersToUrlParams(
+ parseLogsFilterUrlParams(search.filter).filter(({ column }) => column !== facet)
+ ),
+ }
+ : search
+ const facetScope = {
+ ...scopedSearch,
+ latency: null,
+ 'timing.dns': null,
+ 'timing.connection': null,
+ 'timing.tls': null,
+ 'timing.ttfb': null,
+ 'timing.transfer': null,
+ sort: null,
+ size: 40,
+ start: 0,
+ direction: 'next' as const,
+ cursor: new Date(0),
+ id: null,
+ }
return useQuery<UnifiedLogsFacetCountData, UnifiedLogsFacetCountError, TData>({
queryKey: [
- ...logsKeys.unifiedLogsFacetCount(projectRef, facet, facetSearch, search),
+ ...logsKeys.unifiedLogsFacetCount(projectRef, facet, facetSearch, facetScope),
{ otel: useOtel },
],
queryFn: ({ signal }) =>
- getUnifiedLogsFacetCount({ projectRef, search, facet, facetSearch, useOtel }, signal),
+ getUnifiedLogsFacetCount(
+ { projectRef, search: facetScope, facet, facetSearch, useOtel },
+ signal
+ ),
enabled: enabled && typeof projectRef !== 'undefined',
...UNIFIED_LOGS_QUERY_OPTIONS,
...options,