statsMerge merge the stats from 2 responses this function is similar to matrixMerge
(shouldSumStats bool, resps []*PrometheusResponse)
| 213 | |
| 214 | // statsMerge merge the stats from 2 responses this function is similar to matrixMerge |
| 215 | func statsMerge(shouldSumStats bool, resps []*PrometheusResponse) *PrometheusResponseStats { |
| 216 | output := map[int64]*PrometheusResponseQueryableSamplesStatsPerStep{} |
| 217 | hasStats := false |
| 218 | var peakSamples int64 |
| 219 | for _, resp := range resps { |
| 220 | if resp.Data.Stats == nil { |
| 221 | continue |
| 222 | } |
| 223 | |
| 224 | hasStats = true |
| 225 | if resp.Data.Stats.Samples == nil { |
| 226 | continue |
| 227 | } |
| 228 | |
| 229 | for _, s := range resp.Data.Stats.Samples.TotalQueryableSamplesPerStep { |
| 230 | if shouldSumStats { |
| 231 | if stats, ok := output[s.GetTimestampMs()]; ok { |
| 232 | stats.Value += s.Value |
| 233 | } else { |
| 234 | output[s.GetTimestampMs()] = s |
| 235 | } |
| 236 | } else { |
| 237 | output[s.GetTimestampMs()] = s |
| 238 | } |
| 239 | } |
| 240 | peakSamples = max(peakSamples, resp.Data.Stats.Samples.PeakSamples) |
| 241 | } |
| 242 | |
| 243 | if !hasStats { |
| 244 | return nil |
| 245 | } |
| 246 | keys := make([]int64, 0, len(output)) |
| 247 | for key := range output { |
| 248 | keys = append(keys, key) |
| 249 | } |
| 250 | |
| 251 | slices.Sort(keys) |
| 252 | |
| 253 | result := &PrometheusResponseStats{Samples: &PrometheusResponseSamplesStats{}} |
| 254 | for _, key := range keys { |
| 255 | result.Samples.TotalQueryableSamplesPerStep = append(result.Samples.TotalQueryableSamplesPerStep, output[key]) |
| 256 | result.Samples.TotalQueryableSamples += output[key].Value |
| 257 | } |
| 258 | result.Samples.PeakSamples = peakSamples |
| 259 | |
| 260 | return result |
| 261 | } |
| 262 | |
| 263 | type sortPlan int |
| 264 |
no test coverage detected