Files
supabase/apps/studio/components/interfaces/QueryInsights/hooks/useQueryInsightsIssues.utils.ts
T
7ed8ab83a8 feat(studio): query insights improvements (#43109)
## I have read the
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
file.

YES

## What kind of change does this PR introduce?

This introduces Query Insights. It's the first edition of possible
future updates. This takes our old prototype and builds upon it for a
more action driven insights view.

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: Ali Waseem <waseema393@gmail.com>
2026-03-13 15:09:26 +00:00

38 lines
1.5 KiB
TypeScript

import type { QueryPerformanceRow } from '../../QueryPerformance/QueryPerformance.types'
import { hasIndexRecommendations } from '../../QueryPerformance/IndexAdvisor/index-advisor.utils'
import { SLOW_QUERY_THRESHOLD_MS } from '../QueryInsightsHealth/QueryInsightsHealth.constants'
import type { IssueType } from '../QueryInsightsHealth/QueryInsightsHealth.types'
export function classifyQuery(row: QueryPerformanceRow): { issueType: IssueType; hint: string } {
// undefined means index advisor is still loading — defer index classification only to avoid
// flickering between 'slow' and 'index' as results arrive, but still classify slow queries
if (row.index_advisor_result === undefined) {
if (row.mean_time > SLOW_QUERY_THRESHOLD_MS) {
return { issueType: 'slow', hint: 'Abnormally slow query detected' }
}
return { issueType: null, hint: '' }
}
const advisorErrors = row.index_advisor_result?.errors
if (advisorErrors && advisorErrors.length > 0) {
return { issueType: 'error', hint: advisorErrors[0] }
}
if (hasIndexRecommendations(row.index_advisor_result, true)) {
const statements = row.index_advisor_result?.index_statements ?? []
return {
issueType: 'index',
hint: `Missing index: ${statements[0] ?? 'Index suggestion available'}`,
}
}
if (row.mean_time > SLOW_QUERY_THRESHOLD_MS) {
return {
issueType: 'slow',
hint: `Abnormally slow query detected`,
}
}
return { issueType: null, hint: '' }
}