mirror of
https://github.com/supabase/supabase.git
synced 2026-10-14 21:59:16 +03:00
* init * hovercard * adds button to install index advisor * hover card now now insert indexes * update * moved hook * align alert dialog to design syste, * Update index-advisor.utils.ts * shows all index statements now * Update query-performance.tsx * Some refactors * Clean up * Fix * One last nit refactor --------- Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
58 lines
1.4 KiB
TypeScript
58 lines
1.4 KiB
TypeScript
import useDbQuery from 'hooks/analytics/useDbQuery'
|
|
import { PRESET_CONFIG } from './Reports.constants'
|
|
import { Presets } from './Reports.types'
|
|
|
|
export type QueryPerformanceSort = {
|
|
column:
|
|
| 'total_time'
|
|
| 'prop_total_time'
|
|
| 'calls'
|
|
| 'avg_rows'
|
|
| 'max_time'
|
|
| 'mean_time'
|
|
| 'min_time'
|
|
order: 'asc' | 'desc'
|
|
}
|
|
|
|
export type QueryPerformanceQueryOpts = {
|
|
preset: 'mostFrequentlyInvoked' | 'mostTimeConsuming' | 'slowestExecutionTime' | 'queryHitRate'
|
|
searchQuery?: string
|
|
orderBy?: QueryPerformanceSort
|
|
roles?: string[]
|
|
runIndexAdvisor?: boolean
|
|
}
|
|
|
|
export const useQueryPerformanceQuery = ({
|
|
preset,
|
|
orderBy,
|
|
searchQuery = '',
|
|
roles,
|
|
runIndexAdvisor = false,
|
|
}: 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,
|
|
runIndexAdvisor
|
|
)
|
|
return useDbQuery({
|
|
sql,
|
|
params: undefined,
|
|
where: whereSql,
|
|
orderBy: orderBySql,
|
|
})
|
|
}
|