-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Add block loader tests for aggregate_metric_double #127119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...lasticsearch/xpack/aggregatemetric/mapper/AggregateMetricDoubleFieldBlockLoaderTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.aggregatemetric.mapper; | ||
|
|
||
| import org.elasticsearch.index.mapper.BlockLoaderTestCase; | ||
| import org.elasticsearch.logsdb.datageneration.datasource.DataSourceHandler; | ||
| import org.elasticsearch.logsdb.datageneration.datasource.DataSourceRequest; | ||
| import org.elasticsearch.logsdb.datageneration.datasource.DataSourceResponse; | ||
| import org.elasticsearch.plugins.Plugin; | ||
| import org.elasticsearch.xpack.aggregatemetric.AggregateMetricMapperPlugin; | ||
| import org.elasticsearch.xpack.aggregatemetric.mapper.datageneration.AggregateMetricDoubleDataSourceHandler; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
||
| public class AggregateMetricDoubleFieldBlockLoaderTests extends BlockLoaderTestCase { | ||
| public AggregateMetricDoubleFieldBlockLoaderTests(Params params) { | ||
| super("aggregate_metric_double", List.of(new AggregateMetricDoubleDataSourceHandler(), new DataSourceHandler() { | ||
| @Override | ||
| public DataSourceResponse.ObjectArrayGenerator handle(DataSourceRequest.ObjectArrayGenerator request) { | ||
| // aggregate_metric_double does not support multiple values in a document so we can't have object arrays | ||
| return new DataSourceResponse.ObjectArrayGenerator(Optional::empty); | ||
| } | ||
| }), params); | ||
| } | ||
|
|
||
| @Override | ||
| protected Object expected(Map<String, Object> fieldMapping, Object value, TestContext testContext) { | ||
| if (value instanceof Map<?, ?> map) { | ||
| var expected = new HashMap<String, Object>(map.size()); | ||
|
|
||
| // put explicit `null` for metrics that are not present, this is how the block looks like | ||
| Arrays.stream(AggregateMetricDoubleFieldMapper.Metric.values()) | ||
| .map(AggregateMetricDoubleFieldMapper.Metric::toString) | ||
| .sorted() | ||
| .forEach(m -> { | ||
| expected.put(m, map.get(m)); | ||
| }); | ||
|
|
||
| return expected; | ||
| } | ||
|
|
||
| // malformed or null, return "empty row" | ||
| return new HashMap<>() { | ||
| { | ||
| put("min", null); | ||
| put("max", null); | ||
| put("sum", null); | ||
| put("value_count", null); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| protected Collection<? extends Plugin> getPlugins() { | ||
| return List.of(new AggregateMetricMapperPlugin()); | ||
| } | ||
| } |
71 changes: 71 additions & 0 deletions
71
...h/xpack/aggregatemetric/mapper/datageneration/AggregateMetricDoubleDataSourceHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.aggregatemetric.mapper.datageneration; | ||
|
|
||
| import org.elasticsearch.logsdb.datageneration.datasource.DataSourceHandler; | ||
| import org.elasticsearch.logsdb.datageneration.datasource.DataSourceRequest; | ||
| import org.elasticsearch.logsdb.datageneration.datasource.DataSourceResponse; | ||
| import org.elasticsearch.test.ESTestCase; | ||
| import org.elasticsearch.xpack.aggregatemetric.mapper.AggregateMetricDoubleFieldMapper; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
|
|
||
| public class AggregateMetricDoubleDataSourceHandler implements DataSourceHandler { | ||
| @Override | ||
| public DataSourceResponse.AggregateMetricDoubleGenerator handle(DataSourceRequest.AggregateMetricDoubleGenerator request) { | ||
| return new DataSourceResponse.AggregateMetricDoubleGenerator(() -> { | ||
| var metricContainer = new HashMap<String, Number>(); | ||
|
|
||
| // min and max must make sense - max has to be gte min | ||
| double min = ESTestCase.randomDoubleBetween(-Double.MAX_VALUE, 1_000_000_000, false); | ||
| double max = ESTestCase.randomDoubleBetween(min, Double.MAX_VALUE, true); | ||
|
|
||
| metricContainer.put("min", min); | ||
| metricContainer.put("max", max); | ||
| metricContainer.put("sum", ESTestCase.randomDouble()); | ||
| metricContainer.put("value_count", ESTestCase.randomIntBetween(1, Integer.MAX_VALUE)); | ||
|
|
||
| return metricContainer; | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public DataSourceResponse.LeafMappingParametersGenerator handle(DataSourceRequest.LeafMappingParametersGenerator request) { | ||
| if (request.fieldType().equals("aggregate_metric_double") == false) { | ||
| return null; | ||
| } | ||
|
|
||
| return new DataSourceResponse.LeafMappingParametersGenerator(() -> { | ||
| var map = new HashMap<String, Object>(); | ||
|
|
||
| List<AggregateMetricDoubleFieldMapper.Metric> metrics = ESTestCase.randomNonEmptySubsetOf( | ||
| Arrays.asList(AggregateMetricDoubleFieldMapper.Metric.values()) | ||
| ); | ||
|
|
||
| map.put("metrics", metrics.stream().map(Enum::toString).toList()); | ||
| map.put("default_metric", metrics.get(ESTestCase.randomIntBetween(0, metrics.size() - 1))); | ||
|
|
||
| if (ESTestCase.randomBoolean()) { | ||
| map.put("ignore_malformed", ESTestCase.randomBoolean()); | ||
| } | ||
|
|
||
| return map; | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public DataSourceResponse.FieldDataGenerator handle(DataSourceRequest.FieldDataGenerator request) { | ||
| if (request.fieldType().equals("aggregate_metric_double") == false) { | ||
| return null; | ||
| } | ||
|
|
||
| return new DataSourceResponse.FieldDataGenerator(new AggregateMetricDoubleFieldDataGenerator(request.dataSource())); | ||
| } | ||
| } |
61 changes: 61 additions & 0 deletions
61
.../xpack/aggregatemetric/mapper/datageneration/AggregateMetricDoubleFieldDataGenerator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.aggregatemetric.mapper.datageneration; | ||
|
|
||
| import org.elasticsearch.logsdb.datageneration.FieldDataGenerator; | ||
| import org.elasticsearch.logsdb.datageneration.datasource.DataSource; | ||
| import org.elasticsearch.logsdb.datageneration.datasource.DataSourceRequest; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.function.Function; | ||
| import java.util.function.Supplier; | ||
|
|
||
| public class AggregateMetricDoubleFieldDataGenerator implements FieldDataGenerator { | ||
| private final Supplier<Map<String, Number>> metrics; | ||
| private final Function<Supplier<Object>, Supplier<Object>> nullWrapper; | ||
| private final Function<Supplier<Object>, Supplier<Object>> nullAndMalformedWrapper; | ||
|
|
||
| public AggregateMetricDoubleFieldDataGenerator(DataSource dataSource) { | ||
| this.metrics = dataSource.get(new DataSourceRequest.AggregateMetricDoubleGenerator()).generator(); | ||
|
|
||
| this.nullWrapper = dataSource.get(new DataSourceRequest.NullWrapper()).wrapper(); | ||
|
|
||
| var strings = dataSource.get(new DataSourceRequest.StringGenerator()).generator(); | ||
| var malformed = dataSource.get(new DataSourceRequest.MalformedWrapper(strings::get)).wrapper(); | ||
| this.nullAndMalformedWrapper = malformed.andThen(nullWrapper); | ||
| } | ||
|
|
||
| @Override | ||
| @SuppressWarnings("unchecked") | ||
| public Object generateValue(Map<String, Object> fieldMapping) { | ||
| if (fieldMapping == null) { | ||
| // this field can't be properly mapped with dynamic mapping | ||
| return null; | ||
| } | ||
|
|
||
| // metrics returned have all metric fields but we only need those that appear in the mapping | ||
| Supplier<Object> metricsAdjustedForMapping = () -> { | ||
| var metric = metrics.get(); | ||
|
|
||
| var adjusted = new HashMap<String, Number>(); | ||
| for (var metricName : (List<String>) fieldMapping.get("metrics")) { | ||
| adjusted.put(metricName, metric.get(metricName)); | ||
| } | ||
|
|
||
| return adjusted; | ||
| }; | ||
|
|
||
| if ((Boolean) fieldMapping.getOrDefault("ignore_malformed", false)) { | ||
| return nullAndMalformedWrapper.apply(metricsAdjustedForMapping).get(); | ||
| } | ||
|
|
||
| return nullWrapper.apply(metricsAdjustedForMapping).get(); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this not get used in the tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, we always append to individual blocks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(which to me is a sign that it should be a dedicated block type)