|
28 | 28 | import java.util.Locale;
|
29 | 29 | import java.util.Map;
|
30 | 30 | import java.util.Optional;
|
| 31 | +import java.util.Queue; |
31 | 32 | import java.util.Set;
|
32 | 33 | import java.util.stream.Collectors;
|
33 | 34 |
|
@@ -262,17 +263,50 @@ private Metric getMetric(String name, Map<String, Object> metric, Mapper.TypePar
|
262 | 263 | .collect(Collectors.toList());
|
263 | 264 | metric.remove(STATS);
|
264 | 265 | if (metricStrings.isEmpty()) {
|
265 |
| - metricTypes = new ArrayList<>(StarTreeIndexSettings.DEFAULT_METRICS_LIST.get(context.getSettings())); |
266 |
| - } else { |
267 |
| - Set<MetricStat> metricSet = new LinkedHashSet<>(); |
268 |
| - for (String metricString : metricStrings) { |
269 |
| - metricSet.add(MetricStat.fromTypeName(metricString)); |
270 |
| - } |
271 |
| - metricTypes = new ArrayList<>(metricSet); |
| 266 | + metricStrings = new ArrayList<>(StarTreeIndexSettings.DEFAULT_METRICS_LIST.get(context.getSettings())); |
| 267 | + } |
| 268 | + // Add all required metrics initially |
| 269 | + Set<MetricStat> metricSet = new LinkedHashSet<>(); |
| 270 | + for (String metricString : metricStrings) { |
| 271 | + MetricStat metricStat = MetricStat.fromTypeName(metricString); |
| 272 | + metricSet.add(metricStat); |
| 273 | + addBaseMetrics(metricStat, metricSet); |
272 | 274 | }
|
| 275 | + addEligibleDerivedMetrics(metricSet); |
| 276 | + metricTypes = new ArrayList<>(metricSet); |
273 | 277 | return new Metric(name, metricTypes);
|
274 | 278 | }
|
275 | 279 |
|
| 280 | + /** |
| 281 | + * Add base metrics of derived metric to metric set |
| 282 | + */ |
| 283 | + private void addBaseMetrics(MetricStat metricStat, Set<MetricStat> metricSet) { |
| 284 | + if (metricStat.isDerivedMetric()) { |
| 285 | + Queue<MetricStat> metricQueue = new LinkedList<>(metricStat.getBaseMetrics()); |
| 286 | + while (metricQueue.isEmpty() == false) { |
| 287 | + MetricStat metric = metricQueue.poll(); |
| 288 | + if (metric.isDerivedMetric() && !metricSet.contains(metric)) { |
| 289 | + metricQueue.addAll(metric.getBaseMetrics()); |
| 290 | + } |
| 291 | + metricSet.add(metric); |
| 292 | + } |
| 293 | + } |
| 294 | + } |
| 295 | + |
| 296 | + /** |
| 297 | + * Add derived metrics if all associated base metrics are present |
| 298 | + */ |
| 299 | + private void addEligibleDerivedMetrics(Set<MetricStat> metricStats) { |
| 300 | + for (MetricStat metric : MetricStat.values()) { |
| 301 | + if (metric.isDerivedMetric() && !metricStats.contains(metric)) { |
| 302 | + List<MetricStat> sourceMetrics = metric.getBaseMetrics(); |
| 303 | + if (metricStats.containsAll(sourceMetrics)) { |
| 304 | + metricStats.add(metric); |
| 305 | + } |
| 306 | + } |
| 307 | + } |
| 308 | + } |
| 309 | + |
276 | 310 | @Override
|
277 | 311 | protected List<Parameter<?>> getParameters() {
|
278 | 312 | return List.of(config);
|
|
0 commit comments