useQueryInsightsIssues.utils.ts37 lines · main
1import { hasIndexRecommendations } from '../../QueryPerformance/IndexAdvisor/index-advisor.utils'
2import type { QueryPerformanceRow } from '../../QueryPerformance/QueryPerformance.types'
3import { SLOW_QUERY_THRESHOLD_MS } from '../QueryInsightsHealth/QueryInsightsHealth.constants'
4import type { IssueType } from '../QueryInsightsHealth/QueryInsightsHealth.types'
5
6export function classifyQuery(row: QueryPerformanceRow): { issueType: IssueType; hint: string } {
7 // undefined means index advisor is still loading — defer index classification only to avoid
8 // flickering between 'slow' and 'index' as results arrive, but still classify slow queries
9 if (row.index_advisor_result === undefined) {
10 if (row.mean_time > SLOW_QUERY_THRESHOLD_MS) {
11 return { issueType: 'slow', hint: 'Abnormally slow query detected' }
12 }
13 return { issueType: null, hint: '' }
14 }
15
16 const advisorErrors = row.index_advisor_result?.errors
17 if (advisorErrors && advisorErrors.length > 0) {
18 return { issueType: 'error', hint: advisorErrors[0] }
19 }
20
21 if (hasIndexRecommendations(row.index_advisor_result, true)) {
22 const statements = row.index_advisor_result?.index_statements ?? []
23 return {
24 issueType: 'index',
25 hint: `Missing index: ${statements[0] ?? 'Index suggestion available'}`,
26 }
27 }
28
29 if (row.mean_time > SLOW_QUERY_THRESHOLD_MS) {
30 return {
31 issueType: 'slow',
32 hint: `Abnormally slow query detected`,
33 }
34 }
35
36 return { issueType: null, hint: '' }
37}