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