|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "container/heap" |
| 5 | + "sort" |
| 6 | + |
| 7 | + "github.com/newrelic/infra-integrations-sdk/v3/log" |
| 8 | + "github.com/newrelic/nri-mssql/src/args" |
| 9 | +) |
| 10 | + |
| 11 | +// QueryHeap implements heap.Interface for EnrichedSlowQueryDetails |
| 12 | +// This is a min-heap, so we can efficiently maintain the top K slowest queries |
| 13 | +type QueryHeap []EnrichedSlowQueryDetails |
| 14 | + |
| 15 | +func (h QueryHeap) Len() int { return len(h) } |
| 16 | +func (h QueryHeap) Less(i, j int) bool { return h[i].AvgElapsedTimeMS < h[j].AvgElapsedTimeMS } // Min-heap |
| 17 | +func (h QueryHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } |
| 18 | + |
| 19 | +func (h *QueryHeap) Push(x interface{}) { |
| 20 | + *h = append(*h, x.(EnrichedSlowQueryDetails)) |
| 21 | +} |
| 22 | + |
| 23 | +func (h *QueryHeap) Pop() interface{} { |
| 24 | + old := *h |
| 25 | + n := len(old) |
| 26 | + x := old[n-1] |
| 27 | + *h = old[0 : n-1] |
| 28 | + return x |
| 29 | +} |
| 30 | + |
| 31 | +// FilterSlowQueriesByThresholdHeap - MOST EFFICIENT version using heap |
| 32 | +// Time Complexity: O(n log k) where n = total queries, k = count threshold |
| 33 | +// Space Complexity: O(k) |
| 34 | +func FilterSlowQueriesByThresholdHeap(enrichedQueries []EnrichedSlowQueryDetails, args args.ArgumentList) []EnrichedSlowQueryDetails { |
| 35 | + if len(enrichedQueries) == 0 { |
| 36 | + log.Debug("No slow queries to filter") |
| 37 | + return enrichedQueries |
| 38 | + } |
| 39 | + |
| 40 | + thresholdMs := float64(args.QueryMonitoringResponseTimeThreshold) |
| 41 | + countLimit := args.QueryMonitoringCountThreshold |
| 42 | + |
| 43 | + if countLimit <= 0 { |
| 44 | + countLimit = len(enrichedQueries) |
| 45 | + } |
| 46 | + |
| 47 | + // Use a min-heap to efficiently maintain top K queries |
| 48 | + h := &QueryHeap{} |
| 49 | + heap.Init(h) |
| 50 | + |
| 51 | + filteredCount := 0 |
| 52 | + |
| 53 | + for _, query := range enrichedQueries { |
| 54 | + if query.AvgElapsedTimeMS >= thresholdMs { |
| 55 | + filteredCount++ |
| 56 | + |
| 57 | + if h.Len() < countLimit { |
| 58 | + // Heap not full, just add the query |
| 59 | + heap.Push(h, query) |
| 60 | + } else if query.AvgElapsedTimeMS > (*h)[0].AvgElapsedTimeMS { |
| 61 | + // Query is slower than the fastest query in our top-K, replace it |
| 62 | + heap.Pop(h) |
| 63 | + heap.Push(h, query) |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + log.Debug("Filtered %d queries out of %d based on response time threshold %.2f ms", |
| 69 | + filteredCount, len(enrichedQueries), thresholdMs) |
| 70 | + |
| 71 | + // Convert heap to slice and sort in descending order |
| 72 | + result := make([]EnrichedSlowQueryDetails, h.Len()) |
| 73 | + for i := len(result) - 1; i >= 0; i-- { |
| 74 | + result[i] = heap.Pop(h).(EnrichedSlowQueryDetails) |
| 75 | + } |
| 76 | + |
| 77 | + log.Debug("Returning top %d slowest queries", len(result)) |
| 78 | + return result |
| 79 | +} |
| 80 | + |
| 81 | +// FilterSlowQueriesByThresholdPartialSort - Alternative efficient approach |
| 82 | +// Time Complexity: O(n + k log k) where n = total queries, k = count threshold |
| 83 | +// Space Complexity: O(n) but only sorts the top k elements |
| 84 | +func FilterSlowQueriesByThresholdPartialSort(enrichedQueries []EnrichedSlowQueryDetails, args args.ArgumentList) []EnrichedSlowQueryDetails { |
| 85 | + if len(enrichedQueries) == 0 { |
| 86 | + log.Debug("No slow queries to filter") |
| 87 | + return enrichedQueries |
| 88 | + } |
| 89 | + |
| 90 | + // Step 1: Filter by threshold |
| 91 | + thresholdMs := float64(args.QueryMonitoringResponseTimeThreshold) |
| 92 | + filteredQueries := make([]EnrichedSlowQueryDetails, 0, len(enrichedQueries)) |
| 93 | + |
| 94 | + for _, query := range enrichedQueries { |
| 95 | + if query.AvgElapsedTimeMS >= thresholdMs { |
| 96 | + filteredQueries = append(filteredQueries, query) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + log.Debug("Filtered %d queries out of %d based on response time threshold %.2f ms", |
| 101 | + len(filteredQueries), len(enrichedQueries), thresholdMs) |
| 102 | + |
| 103 | + if len(filteredQueries) == 0 { |
| 104 | + return []EnrichedSlowQueryDetails{} |
| 105 | + } |
| 106 | + |
| 107 | + // Step 2: Use partial sort - only sort as much as needed |
| 108 | + countLimit := args.QueryMonitoringCountThreshold |
| 109 | + if countLimit <= 0 || countLimit > len(filteredQueries) { |
| 110 | + countLimit = len(filteredQueries) |
| 111 | + } |
| 112 | + |
| 113 | + // Use nth_element equivalent - partial sort |
| 114 | + if countLimit < len(filteredQueries) { |
| 115 | + // Use Go's sort.Slice with a smaller slice for efficiency |
| 116 | + sort.Slice(filteredQueries, func(i, j int) bool { |
| 117 | + return filteredQueries[i].AvgElapsedTimeMS > filteredQueries[j].AvgElapsedTimeMS |
| 118 | + }) |
| 119 | + filteredQueries = filteredQueries[:countLimit] |
| 120 | + } else { |
| 121 | + // Sort all if we need all of them |
| 122 | + sort.Slice(filteredQueries, func(i, j int) bool { |
| 123 | + return filteredQueries[i].AvgElapsedTimeMS > filteredQueries[j].AvgElapsedTimeMS |
| 124 | + }) |
| 125 | + } |
| 126 | + |
| 127 | + log.Debug("Returning top %d slowest queries", len(filteredQueries)) |
| 128 | + return filteredQueries |
| 129 | +} |
| 130 | + |
| 131 | +// FilterSlowQueriesByThresholdQuickSelect - Using QuickSelect algorithm |
| 132 | +// Time Complexity: O(n) average case, O(n²) worst case |
| 133 | +// Space Complexity: O(1) |
| 134 | +func FilterSlowQueriesByThresholdQuickSelect(enrichedQueries []EnrichedSlowQueryDetails, args args.ArgumentList) []EnrichedSlowQueryDetails { |
| 135 | + if len(enrichedQueries) == 0 { |
| 136 | + log.Debug("No slow queries to filter") |
| 137 | + return enrichedQueries |
| 138 | + } |
| 139 | + |
| 140 | + // Step 1: Filter by threshold |
| 141 | + thresholdMs := float64(args.QueryMonitoringResponseTimeThreshold) |
| 142 | + filteredQueries := make([]EnrichedSlowQueryDetails, 0, len(enrichedQueries)) |
| 143 | + |
| 144 | + for _, query := range enrichedQueries { |
| 145 | + if query.AvgElapsedTimeMS >= thresholdMs { |
| 146 | + filteredQueries = append(filteredQueries, query) |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + if len(filteredQueries) == 0 { |
| 151 | + return []EnrichedSlowQueryDetails{} |
| 152 | + } |
| 153 | + |
| 154 | + countLimit := args.QueryMonitoringCountThreshold |
| 155 | + if countLimit <= 0 || countLimit > len(filteredQueries) { |
| 156 | + countLimit = len(filteredQueries) |
| 157 | + } |
| 158 | + |
| 159 | + // Step 2: Use quickselect to find the kth largest elements |
| 160 | + if countLimit < len(filteredQueries) { |
| 161 | + quickSelect(filteredQueries, 0, len(filteredQueries)-1, countLimit-1) |
| 162 | + filteredQueries = filteredQueries[:countLimit] |
| 163 | + } |
| 164 | + |
| 165 | + // Step 3: Sort only the selected elements |
| 166 | + sort.Slice(filteredQueries, func(i, j int) bool { |
| 167 | + return filteredQueries[i].AvgElapsedTimeMS > filteredQueries[j].AvgElapsedTimeMS |
| 168 | + }) |
| 169 | + |
| 170 | + return filteredQueries |
| 171 | +} |
| 172 | + |
| 173 | +// quickSelect implements the QuickSelect algorithm to find the kth largest element |
| 174 | +func quickSelect(queries []EnrichedSlowQueryDetails, left, right, k int) { |
| 175 | + if left == right { |
| 176 | + return |
| 177 | + } |
| 178 | + |
| 179 | + pivotIndex := partition(queries, left, right) |
| 180 | + |
| 181 | + if k == pivotIndex { |
| 182 | + return |
| 183 | + } else if k < pivotIndex { |
| 184 | + quickSelect(queries, left, pivotIndex-1, k) |
| 185 | + } else { |
| 186 | + quickSelect(queries, pivotIndex+1, right, k) |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +// partition rearranges the slice so that elements greater than pivot are on the left |
| 191 | +func partition(queries []EnrichedSlowQueryDetails, left, right int) int { |
| 192 | + pivot := queries[right].AvgElapsedTimeMS |
| 193 | + i := left |
| 194 | + |
| 195 | + for j := left; j < right; j++ { |
| 196 | + if queries[j].AvgElapsedTimeMS > pivot { // Greater than for descending order |
| 197 | + queries[i], queries[j] = queries[j], queries[i] |
| 198 | + i++ |
| 199 | + } |
| 200 | + } |
| 201 | + queries[i], queries[right] = queries[right], queries[i] |
| 202 | + return i |
| 203 | +} |
0 commit comments