mirror of
https://github.com/supabase/supabase.git
synced 2026-05-08 09:50:33 -04:00
7f5865872a
## Context Enforce `noUnusedLocals` and `noUnusedParameters` in tsconfig.json + fix all related issues
50 lines
1.2 KiB
TypeScript
50 lines
1.2 KiB
TypeScript
import { useMemo } from 'react'
|
|
|
|
import useDbQuery from '@/hooks/analytics/useDbQuery'
|
|
|
|
export function buildSlowQueriesCountSql(): string {
|
|
return `
|
|
-- observability-slow-queries-count
|
|
set search_path to public, extensions;
|
|
|
|
SELECT
|
|
CASE
|
|
WHEN EXISTS (
|
|
SELECT 1 FROM pg_extension WHERE extname = 'pg_stat_statements'
|
|
)
|
|
THEN (
|
|
SELECT count(*)::int
|
|
FROM pg_stat_statements
|
|
WHERE total_exec_time + total_plan_time > 1000
|
|
)
|
|
ELSE 0
|
|
END AS slow_queries_count;
|
|
`
|
|
}
|
|
|
|
export const useSlowQueriesCount = (_projectRef?: string, refreshKey: number = 0) => {
|
|
// refreshKey is used in useMemo to force recomputation when refresh is triggered
|
|
const sql = useMemo(
|
|
() => buildSlowQueriesCountSql(),
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
[refreshKey]
|
|
)
|
|
|
|
const { data, isLoading, error } = useDbQuery({
|
|
sql,
|
|
})
|
|
|
|
const slowQueriesCount = useMemo(() => {
|
|
if (!data || !Array.isArray(data) || data.length === 0) {
|
|
return 0
|
|
}
|
|
return data[0]?.slow_queries_count ?? 0
|
|
}, [data])
|
|
|
|
return {
|
|
slowQueriesCount,
|
|
isLoading,
|
|
error,
|
|
}
|
|
}
|