useQueryInsightsIssues.utils.ts 1.5 KB

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