Files
supabase/apps/studio/data/database/activity-query.ts
T
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

123 lines
3.5 KiB
TypeScript

import { getPgStatActivitySql } from '@supabase/pg-meta'
import { useQuery } from '@tanstack/react-query'
import { databaseKeys } from './keys'
import { unguarded } from '@/data/fetchers'
import { executeSql } from '@/data/sql/execute-sql-mutation'
import type { StudioError } from '@/lib/errors/studio-errors'
import { UseCustomQueryOptions } from '@/types'
type LockWaitEvent =
| 'relation'
| 'extend'
| 'page'
| 'tuple'
| 'transactionid'
| 'virtualxid'
| 'speculative token'
| 'object'
| 'userlock'
| 'advisory'
| 'applytransaction'
type ClientWaitEvent = 'ClientRead' | 'ClientWrite' | 'WalSenderWaitForWAL' | 'WalSenderWriteData'
type TimeoutWaitEvent =
| 'BaseBackupThrottle'
| 'CheckpointWriteDelay'
| 'PgSleep'
| 'RecoveryApplyDelay'
| 'VacuumDelay'
| 'VacuumTruncate'
type ActivityWaitEvent =
| 'ArchiverMain'
| 'AutoVacuumMain'
| 'BgWriterHibernate'
| 'BgWriterMain'
| 'CheckpointerMain'
| 'LogicalApplyMain'
| 'LogicalLauncherMain'
| 'WalReceiverMain'
| 'WalSenderMain'
| 'WalWriterMain'
type BufferPinWaitEvent = 'BufferPin'
type IOWaitEvent = string
type IPCWaitEvent = string
type LWLockWaitEvent = string
/**
* wait_event_type: What the session is blocked on
* wait_event: The specific event within that wait type
* */
type WaitEvent =
| { wait_event_type: 'Lock'; wait_event: LockWaitEvent }
| { wait_event_type: 'Client'; wait_event: ClientWaitEvent }
| { wait_event_type: 'Timeout'; wait_event: TimeoutWaitEvent }
| { wait_event_type: 'Activity'; wait_event: ActivityWaitEvent }
| { wait_event_type: 'BufferPin'; wait_event: BufferPinWaitEvent }
| { wait_event_type: 'Extension'; wait_event: string }
| { wait_event_type: 'IO'; wait_event: IOWaitEvent }
| { wait_event_type: 'IPC'; wait_event: IPCWaitEvent }
| { wait_event_type: 'LWLock'; wait_event: LWLockWaitEvent }
| { wait_event_type: null; wait_event: null }
export type DatabaseActivity = {
pid: number
role_name: string
application_name: string
blocked_by: number[]
query: string | null
backend_start: string
query_start: string | null
transaction_start: string | null
state_change: string | null
state:
| 'idle'
| 'active'
| 'idle in transaction'
| 'idle in transaction (aborted)'
| 'fastpath function call'
| 'disabled'
| null
} & WaitEvent
export type DatabaseActivityVariables = {
projectRef?: string
connectionString?: string | null
}
export async function getDatabaseActivity(
{ projectRef, connectionString }: DatabaseActivityVariables,
signal?: AbortSignal
) {
const sql = getPgStatActivitySql()
const { result } = await executeSql(
{ projectRef, connectionString, sql, queryKey: ['activity'], grant: unguarded.migrating },
signal
)
return (result ?? []).filter(
(x: DatabaseActivity) => !x.query?.startsWith(sql)
) as DatabaseActivity[]
}
export type DatabaseActivityData = Awaited<ReturnType<typeof getDatabaseActivity>>
export type DatabaseActivityError = StudioError
export const useDatabaseActivityQuery = <TData = DatabaseActivityData>(
{ projectRef, connectionString }: DatabaseActivityVariables,
{
enabled = true,
...options
}: UseCustomQueryOptions<DatabaseActivityData, DatabaseActivityError, TData> = {}
) =>
useQuery<DatabaseActivityData, DatabaseActivityError, TData>({
queryKey: databaseKeys.databaseActivity(projectRef),
queryFn: ({ signal }) => getDatabaseActivity({ projectRef, connectionString }, signal),
enabled: enabled && typeof projectRef !== 'undefined',
...options,
})