mirror of
https://github.com/supabase/supabase.git
synced 2026-10-16 14:49:10 +03:00
* Add replica filter for query performance * Small fix * Add ref in dep array * Hide database selector if read replicas is not enabled for project
52 lines
1.4 KiB
TypeScript
52 lines
1.4 KiB
TypeScript
import { useDatabaseSelectorStateSnapshot } from 'state/database-selector'
|
|
import { PRESET_CONFIG } from './Reports.constants'
|
|
import { Presets } from './Reports.types'
|
|
import useDbQuery from 'hooks/analytics/useDbQuery'
|
|
|
|
export type QueryPerformanceSort = {
|
|
column:
|
|
| 'total_time'
|
|
| 'prop_total_time'
|
|
| 'calls'
|
|
| 'avg_rows'
|
|
| 'max_time'
|
|
| 'mean_time'
|
|
| 'min_time'
|
|
order: 'asc' | 'desc'
|
|
}
|
|
|
|
type QueryPerformanceQueryOpts = {
|
|
preset: 'mostFrequentlyInvoked' | 'mostTimeConsuming' | 'slowestExecutionTime' | 'queryHitRate'
|
|
searchQuery?: string
|
|
orderBy?: QueryPerformanceSort
|
|
roles?: string[]
|
|
}
|
|
|
|
export const useQueryPerformanceQuery = ({
|
|
preset,
|
|
orderBy,
|
|
searchQuery = '',
|
|
roles,
|
|
}: QueryPerformanceQueryOpts) => {
|
|
const queryPerfQueries = PRESET_CONFIG[Presets.QUERY_PERFORMANCE]
|
|
const baseSQL = queryPerfQueries.queries[preset]
|
|
|
|
const whereSql = [
|
|
roles !== undefined && roles.length > 0
|
|
? `auth.rolname in (${roles.map((r) => `'${r}'`).join(', ')})`
|
|
: '',
|
|
searchQuery.length > 0 ? `statements.query ~ '${searchQuery}'` : '',
|
|
]
|
|
.filter((x) => x.length > 0)
|
|
.join(' AND ')
|
|
|
|
const orderBySql = orderBy && `ORDER BY ${orderBy.column} ${orderBy.order}`
|
|
const sql = baseSQL.sql([], whereSql.length > 0 ? `WHERE ${whereSql}` : undefined, orderBySql)
|
|
return useDbQuery({
|
|
sql,
|
|
params: undefined,
|
|
where: whereSql,
|
|
orderBy: orderBySql,
|
|
})
|
|
}
|