useQueryInsightsScore.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { useMemo } from 'react'
  2. import {
  3. HIGH_CALL_THRESHOLD,
  4. SCORE_DEDUCTIONS,
  5. } from '../QueryInsightsHealth/QueryInsightsHealth.constants'
  6. import type { ClassifiedQuery } from '../QueryInsightsHealth/QueryInsightsHealth.types'
  7. import { getHealthLevel } from '../QueryInsightsHealth/QueryInsightsHealth.utils'
  8. export function useQueryInsightsScore({
  9. errors,
  10. indexIssues,
  11. slowQueries,
  12. }: {
  13. errors: ClassifiedQuery[]
  14. indexIssues: ClassifiedQuery[]
  15. slowQueries: ClassifiedQuery[]
  16. }) {
  17. return useMemo(() => {
  18. let score = 100
  19. score -= errors.length * SCORE_DEDUCTIONS.error
  20. score -= indexIssues.reduce(
  21. (acc, q) =>
  22. acc +
  23. (q.calls > HIGH_CALL_THRESHOLD
  24. ? SCORE_DEDUCTIONS.indexHighCalls
  25. : SCORE_DEDUCTIONS.indexLowCalls),
  26. 0
  27. )
  28. score -= slowQueries.reduce(
  29. (acc, q) =>
  30. acc +
  31. (q.calls > HIGH_CALL_THRESHOLD / 2
  32. ? SCORE_DEDUCTIONS.slowHighCalls
  33. : SCORE_DEDUCTIONS.slowLowCalls),
  34. 0
  35. )
  36. score = Math.max(0, Math.min(100, score))
  37. return {
  38. score,
  39. level: getHealthLevel(score),
  40. }
  41. }, [errors, indexIssues, slowQueries])
  42. }