WithStatements.utils.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import {
  2. filterProtectedSchemaIndexAdvisorResult,
  3. queryInvolvesProtectedSchemas,
  4. } from '../IndexAdvisor/index-advisor.utils'
  5. import { QueryPerformanceRow } from '../QueryPerformance.types'
  6. export const transformStatementDataToRows = (
  7. data: QueryPerformanceRow[],
  8. filterIndexAdvisor: boolean = false
  9. ): QueryPerformanceRow[] => {
  10. if (!data || data.length === 0) return []
  11. const totalTimeAcrossAllQueries = data.reduce((sum, row) => sum + (row.total_time || 0), 0)
  12. return data
  13. .map((row) => {
  14. const filteredIndexAdvisorResult = row.index_advisor_result
  15. ? filterProtectedSchemaIndexAdvisorResult(row.index_advisor_result)
  16. : null
  17. return {
  18. query: row.query,
  19. rolname: row.rolname || undefined,
  20. calls: row.calls || 0,
  21. mean_time: row.mean_time || 0,
  22. min_time: row.min_time || 0,
  23. max_time: row.max_time || 0,
  24. total_time: row.total_time || 0,
  25. rows_read: row.rows_read || 0,
  26. cache_hit_rate: row.cache_hit_rate || 0,
  27. prop_total_time:
  28. totalTimeAcrossAllQueries > 0 ? (row.total_time / totalTimeAcrossAllQueries) * 100 : 0,
  29. index_advisor_result: filteredIndexAdvisorResult,
  30. }
  31. })
  32. .filter((row) => {
  33. if (filterIndexAdvisor) {
  34. const hasValidRecommendations = row.index_advisor_result !== null
  35. const involvesProtectedSchemas = queryInvolvesProtectedSchemas(row.query)
  36. if (involvesProtectedSchemas && !hasValidRecommendations) {
  37. return false
  38. }
  39. }
  40. return true
  41. })
  42. }