Files
supabase/apps/studio/data/database-queues/database-queues-metrics-query.ts
T
9d534c9f5a fix: response error codes (#30581)
* fix: response error codes

* upgrade docs

* remove request url modification middleware

* move api routes for self-hosted to platform folder

* remove some lib/common/fetch usage

* docs: use middleware for openapi-fetch (#30600)

Get rid of the unauthedAllowedPost function (I don't think there's any harm in letting any requests that require authentication to just 403, they should be disabled at the React Query level and if not they will fail gracefully enough...)

* fix local count query

* add default values for clone mutation

* fix ts and codegen

* add missing lodash dep to playwright tests

* Fix the playwright tests to match the new folder structure for selfhosted variant.

* remove unused import

* Remove unused state

* remove unused sql debug mutation

* remove unused export

* fix notifications query

* fix jwt updating status

* fix typescript

* save sql snippet after renaming

* update codegen & fix ts error

* override array querySerializer

---------

Co-authored-by: Charis <26616127+charislam@users.noreply.github.com>
Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
Co-authored-by: Ivan Vasilov <vasilov.ivan@gmail.com>
2025-01-20 11:27:38 +08:00

90 lines
2.5 KiB
TypeScript

import { UseQueryOptions, useQuery } from '@tanstack/react-query'
import { executeSql } from 'data/sql/execute-sql-query'
import { ResponseError } from 'types'
import { databaseQueuesKeys } from './keys'
export type DatabaseQueuesMetricsVariables = {
projectRef?: string
connectionString?: string
queueName: string
}
export type PostgresQueueMetric = {
queue_name: string
queue_length: number
method: 'estimated' | 'precise'
}
const preciseMetricsSqlQuery = (queueName: string) => `
set local statement_timeout = '1s';
SELECT
COUNT(*) AS row_count
FROM
"pgmq"."q_${queueName}";
`
const estimateMetricsSqlQuery = (queueName: string) => `
select
reltuples::bigint as estimated_rows
from
pg_class
where
relname = 'q_${queueName}'
and relnamespace = 'pgmq'::regnamespace;
`
export async function getDatabaseQueuesMetrics({
projectRef,
connectionString,
queueName,
}: DatabaseQueuesMetricsVariables) {
if (!projectRef) throw new Error('Project ref is required')
try {
const { result } = await executeSql({
projectRef,
connectionString,
sql: preciseMetricsSqlQuery(queueName),
})
return {
queue_name: queueName,
queue_length: result[0].row_count,
method: 'precise',
} as PostgresQueueMetric
} catch (error: any) {
// if the error is caused because the count timeouted, try to fetch an approximate count
if (error?.message === 'canceling statement due to statement timeout') {
const { result } = await executeSql({
projectRef,
connectionString,
sql: estimateMetricsSqlQuery(queueName),
})
return {
queue_name: queueName,
queue_length: result[0].estimated_rows,
method: 'estimated',
} as PostgresQueueMetric
}
throw error
}
}
export type DatabaseQueuesMetricsData = PostgresQueueMetric
export type DatabaseQueuesMetricsError = ResponseError
export const useQueuesMetricsQuery = <TData = DatabaseQueuesMetricsData>(
{ projectRef, connectionString, queueName }: DatabaseQueuesMetricsVariables,
{
enabled = true,
...options
}: UseQueryOptions<DatabaseQueuesMetricsData, DatabaseQueuesMetricsError, TData> = {}
) =>
useQuery<DatabaseQueuesMetricsData, DatabaseQueuesMetricsError, TData>(
databaseQueuesKeys.metrics(projectRef, queueName),
() => getDatabaseQueuesMetrics({ projectRef, connectionString, queueName }),
{
enabled: enabled && typeof projectRef !== 'undefined',
...options,
}
)