mirror of
https://github.com/supabase/supabase.git
synced 2026-05-22 17:00:43 +08:00
* foreign-key-constraints * update entity-types stale time * schemas query * deprecate useExecuteSqlQuery * users count query * database size query * indexes query * keywords query * migrations query * table columns * database functions * database roles query * fdws query * replication lag query * ongoing queries query * vault secrets query * remove unneeded staleTime: 0 * max connections query * fix entity types key in tests * Some fixes --------- Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
56 lines
1.6 KiB
TypeScript
56 lines
1.6 KiB
TypeScript
import { useQuery, UseQueryOptions } from '@tanstack/react-query'
|
|
import { executeSql, ExecuteSqlError } from '../sql/execute-sql-query'
|
|
import { sqlKeys } from './keys'
|
|
|
|
type OngoingQuery = {
|
|
pid: number
|
|
query: string
|
|
query_start: string
|
|
}
|
|
|
|
export const getOngoingQueriesSql = () => {
|
|
const sql = /* SQL */ `
|
|
select pid, query, query_start from pg_stat_activity where state = 'active' and datname = 'postgres';
|
|
`.trim()
|
|
|
|
return sql
|
|
}
|
|
|
|
export type OngoingQueriesVariables = {
|
|
projectRef?: string
|
|
connectionString?: string
|
|
}
|
|
|
|
export async function getOngoingQueries(
|
|
{ projectRef, connectionString }: OngoingQueriesVariables,
|
|
signal?: AbortSignal
|
|
) {
|
|
const sql = getOngoingQueriesSql().trim()
|
|
|
|
const { result } = await executeSql(
|
|
{ projectRef, connectionString, sql, queryKey: ['ongoing-queries'] },
|
|
signal
|
|
)
|
|
|
|
return (result ?? []).filter((x: OngoingQuery) => !x.query.startsWith(sql)) as OngoingQuery[]
|
|
}
|
|
|
|
export type OngoingQueriesData = Awaited<ReturnType<typeof getOngoingQueries>>
|
|
export type OngoingQueriesError = ExecuteSqlError
|
|
|
|
export const useOngoingQueriesQuery = <TData = OngoingQueriesData>(
|
|
{ projectRef, connectionString }: OngoingQueriesVariables,
|
|
{
|
|
enabled = true,
|
|
...options
|
|
}: UseQueryOptions<OngoingQueriesData, OngoingQueriesError, TData> = {}
|
|
) =>
|
|
useQuery<OngoingQueriesData, OngoingQueriesError, TData>(
|
|
sqlKeys.ongoingQueries(projectRef),
|
|
({ signal }) => getOngoingQueries({ projectRef, connectionString }, signal),
|
|
{
|
|
enabled: enabled && typeof projectRef !== 'undefined',
|
|
...options,
|
|
}
|
|
)
|