|
@@ -26,16 +26,18 @@ import java.time.ZoneId;
|
|
|
import java.time.ZonedDateTime;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Collections;
|
|
|
+import java.util.HashSet;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
import java.util.function.Predicate;
|
|
|
import java.util.stream.IntStream;
|
|
|
|
|
|
import static java.util.Collections.emptyList;
|
|
|
import static java.util.Collections.singletonList;
|
|
|
-import static java.util.Collections.singletonMap;
|
|
|
import static java.util.stream.Collectors.toList;
|
|
|
import static org.hamcrest.Matchers.equalTo;
|
|
|
+import static org.hamcrest.Matchers.hasEntry;
|
|
|
import static org.hamcrest.Matchers.hasSize;
|
|
|
|
|
|
public class InternalTopMetricsTests extends InternalAggregationTestCase<InternalTopMetrics> {
|
|
@@ -50,7 +52,7 @@ public class InternalTopMetricsTests extends InternalAggregationTestCase<Interna
|
|
|
|
|
|
public void testEmptyIsNotMapped() {
|
|
|
InternalTopMetrics empty = InternalTopMetrics.buildEmptyAggregation(
|
|
|
- randomAlphaOfLength(5), randomAlphaOfLength(2), emptyList(), null);
|
|
|
+ randomAlphaOfLength(5), randomMetricNames(between(1, 5)), emptyList(), null);
|
|
|
assertFalse(empty.isMapped());
|
|
|
}
|
|
|
|
|
@@ -60,8 +62,8 @@ public class InternalTopMetricsTests extends InternalAggregationTestCase<Interna
|
|
|
}
|
|
|
|
|
|
public void testToXContentDoubleSortValue() throws IOException {
|
|
|
- InternalTopMetrics tm = new InternalTopMetrics("test", sortOrder, "test", 1,
|
|
|
- List.of(new InternalTopMetrics.TopMetric(DocValueFormat.RAW, SortValue.from(1.0), 1.0)), emptyList(), null);
|
|
|
+ InternalTopMetrics tm = new InternalTopMetrics("test", sortOrder, singletonList("test"), 1,
|
|
|
+ List.of(new InternalTopMetrics.TopMetric(DocValueFormat.RAW, SortValue.from(1.0), new double[] {1.0})), emptyList(), null);
|
|
|
assertThat(Strings.toString(tm, true, true), equalTo(
|
|
|
"{\n" +
|
|
|
" \"test\" : {\n" +
|
|
@@ -83,8 +85,8 @@ public class InternalTopMetricsTests extends InternalAggregationTestCase<Interna
|
|
|
DocValueFormat sortFormat = new DocValueFormat.DateTime(DateFormatter.forPattern("strict_date_time"), ZoneId.of("UTC"),
|
|
|
DateFieldMapper.Resolution.MILLISECONDS);
|
|
|
SortValue sortValue = SortValue.from(ZonedDateTime.parse("2007-12-03T10:15:30Z").toInstant().toEpochMilli());
|
|
|
- InternalTopMetrics tm = new InternalTopMetrics("test", sortOrder, "test", 1,
|
|
|
- List.of(new InternalTopMetrics.TopMetric(sortFormat, sortValue, 1.0)), emptyList(), null);
|
|
|
+ InternalTopMetrics tm = new InternalTopMetrics("test", sortOrder, singletonList("test"), 1,
|
|
|
+ List.of(new InternalTopMetrics.TopMetric(sortFormat, sortValue, new double[] {1.0})), emptyList(), null);
|
|
|
assertThat(Strings.toString(tm, true, true), equalTo(
|
|
|
"{\n" +
|
|
|
" \"test\" : {\n" +
|
|
@@ -102,11 +104,34 @@ public class InternalTopMetricsTests extends InternalAggregationTestCase<Interna
|
|
|
"}"));
|
|
|
}
|
|
|
|
|
|
- public void testToXContentManyValues() throws IOException {
|
|
|
- InternalTopMetrics tm = new InternalTopMetrics("test", sortOrder, "test", 2,
|
|
|
+ public void testToXContentManyMetrics() throws IOException {
|
|
|
+ InternalTopMetrics tm = new InternalTopMetrics("test", sortOrder, List.of("foo", "bar", "baz"), 1,
|
|
|
+ List.of(new InternalTopMetrics.TopMetric(DocValueFormat.RAW, SortValue.from(1.0), new double[] {1.0, 2.0, 3.0})),
|
|
|
+ emptyList(), null);
|
|
|
+ assertThat(Strings.toString(tm, true, true), equalTo(
|
|
|
+ "{\n" +
|
|
|
+ " \"test\" : {\n" +
|
|
|
+ " \"top\" : [\n" +
|
|
|
+ " {\n" +
|
|
|
+ " \"sort\" : [\n" +
|
|
|
+ " 1.0\n" +
|
|
|
+ " ],\n" +
|
|
|
+ " \"metrics\" : {\n" +
|
|
|
+ " \"foo\" : 1.0,\n" +
|
|
|
+ " \"bar\" : 2.0,\n" +
|
|
|
+ " \"baz\" : 3.0\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }\n" +
|
|
|
+ " ]\n" +
|
|
|
+ " }\n" +
|
|
|
+ "}"));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testToXContentManyTopMetrics() throws IOException {
|
|
|
+ InternalTopMetrics tm = new InternalTopMetrics("test", sortOrder, singletonList("test"), 2,
|
|
|
List.of(
|
|
|
- new InternalTopMetrics.TopMetric(DocValueFormat.RAW, SortValue.from(1.0), 1.0),
|
|
|
- new InternalTopMetrics.TopMetric(DocValueFormat.RAW, SortValue.from(2.0), 2.0)),
|
|
|
+ new InternalTopMetrics.TopMetric(DocValueFormat.RAW, SortValue.from(1.0), new double[] {1.0}),
|
|
|
+ new InternalTopMetrics.TopMetric(DocValueFormat.RAW, SortValue.from(2.0), new double[] {2.0})),
|
|
|
emptyList(), null);
|
|
|
assertThat(Strings.toString(tm, true, true), equalTo(
|
|
|
"{\n" +
|
|
@@ -144,17 +169,18 @@ public class InternalTopMetricsTests extends InternalAggregationTestCase<Interna
|
|
|
@Override
|
|
|
protected InternalTopMetrics createTestInstance(String name, List<PipelineAggregator> pipelineAggregators,
|
|
|
Map<String, Object> metaData) {
|
|
|
- String metricName = randomAlphaOfLength(5);
|
|
|
+ int metricCount = between(1, 5);
|
|
|
+ List<String> metricNames = randomMetricNames(metricCount);
|
|
|
int size = between(1, 100);
|
|
|
- List<InternalTopMetrics.TopMetric> topMetrics = randomTopMetrics(between(0, size));
|
|
|
- return new InternalTopMetrics(name, sortOrder, metricName, size, topMetrics, pipelineAggregators, metaData);
|
|
|
+ List<InternalTopMetrics.TopMetric> topMetrics = randomTopMetrics(between(0, size), metricCount);
|
|
|
+ return new InternalTopMetrics(name, sortOrder, metricNames, size, topMetrics, pipelineAggregators, metaData);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected InternalTopMetrics mutateInstance(InternalTopMetrics instance) throws IOException {
|
|
|
String name = instance.getName();
|
|
|
SortOrder sortOrder = instance.getSortOrder();
|
|
|
- String metricName = instance.getMetricName();
|
|
|
+ List<String> metricNames = instance.getMetricNames();
|
|
|
int size = instance.getSize();
|
|
|
List<InternalTopMetrics.TopMetric> topMetrics = instance.getTopMetrics();
|
|
|
switch (randomInt(4)) {
|
|
@@ -166,19 +192,21 @@ public class InternalTopMetricsTests extends InternalAggregationTestCase<Interna
|
|
|
Collections.reverse(topMetrics);
|
|
|
break;
|
|
|
case 2:
|
|
|
- metricName = randomAlphaOfLength(6);
|
|
|
+ metricNames = new ArrayList<>(metricNames);
|
|
|
+ metricNames.set(randomInt(metricNames.size() - 1), randomAlphaOfLength(6));
|
|
|
break;
|
|
|
case 3:
|
|
|
size = randomValueOtherThan(size, () -> between(1, 100));
|
|
|
break;
|
|
|
case 4:
|
|
|
int fixedSize = size;
|
|
|
- topMetrics = randomValueOtherThan(topMetrics, () -> randomTopMetrics(between(1, fixedSize)));
|
|
|
+ int fixedMetricsSize = metricNames.size();
|
|
|
+ topMetrics = randomValueOtherThan(topMetrics, () -> randomTopMetrics(between(1, fixedSize), fixedMetricsSize));
|
|
|
break;
|
|
|
default:
|
|
|
throw new IllegalArgumentException("bad mutation");
|
|
|
}
|
|
|
- return new InternalTopMetrics(name, sortOrder, metricName, size, topMetrics,
|
|
|
+ return new InternalTopMetrics(name, sortOrder, metricNames, size, topMetrics,
|
|
|
instance.pipelineAggregators(), instance.getMetaData());
|
|
|
}
|
|
|
|
|
@@ -198,7 +226,11 @@ public class InternalTopMetricsTests extends InternalAggregationTestCase<Interna
|
|
|
Object expectedSort = internalTop.getSortFormat() == DocValueFormat.RAW ?
|
|
|
internalTop.getSortValue().getKey() : internalTop.getSortValue().format(internalTop.getSortFormat());
|
|
|
assertThat(parsedTop.getSort(), equalTo(singletonList(expectedSort)));
|
|
|
- assertThat(parsedTop.getMetrics(), equalTo(singletonMap(aggregation.getMetricName(), internalTop.getMetricValue())));
|
|
|
+ assertThat(parsedTop.getMetrics().keySet(), hasSize(aggregation.getMetricNames().size()));
|
|
|
+ for (int m = 0; m < aggregation.getMetricNames().size(); m++) {
|
|
|
+ assertThat(parsedTop.getMetrics(),
|
|
|
+ hasEntry(aggregation.getMetricNames().get(m), internalTop.getMetricValues()[m]));
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -213,17 +245,31 @@ public class InternalTopMetricsTests extends InternalAggregationTestCase<Interna
|
|
|
List<InternalTopMetrics.TopMetric> winners = metrics.size() > first.getSize() ? metrics.subList(0, first.getSize()) : metrics;
|
|
|
assertThat(reduced.getName(), equalTo(first.getName()));
|
|
|
assertThat(reduced.getSortOrder(), equalTo(first.getSortOrder()));
|
|
|
- assertThat(reduced.getMetricName(), equalTo(first.getMetricName()));
|
|
|
+ assertThat(reduced.getMetricNames(), equalTo(first.getMetricNames()));
|
|
|
assertThat(reduced.getTopMetrics(), equalTo(winners));
|
|
|
}
|
|
|
|
|
|
- private List<InternalTopMetrics.TopMetric> randomTopMetrics(int length) {
|
|
|
+ private List<InternalTopMetrics.TopMetric> randomTopMetrics(int length, int metricCount) {
|
|
|
return IntStream.range(0, length)
|
|
|
- .mapToObj(i -> new InternalTopMetrics.TopMetric(randomNumericDocValueFormat(), randomSortValue(), randomDouble()))
|
|
|
+ .mapToObj(i -> new InternalTopMetrics.TopMetric(
|
|
|
+ randomNumericDocValueFormat(), randomSortValue(), randomMetricValues(metricCount)
|
|
|
+ ))
|
|
|
.sorted((lhs, rhs) -> sortOrder.reverseMul() * lhs.getSortValue().compareTo(rhs.getSortValue()))
|
|
|
.collect(toList());
|
|
|
}
|
|
|
|
|
|
+ static List<String> randomMetricNames(int metricCount) {
|
|
|
+ Set<String> names = new HashSet<>(metricCount);
|
|
|
+ while (names.size() < metricCount) {
|
|
|
+ names.add(randomAlphaOfLength(5));
|
|
|
+ }
|
|
|
+ return new ArrayList<>(names);
|
|
|
+ }
|
|
|
+
|
|
|
+ private double[] randomMetricValues(int metricCount) {
|
|
|
+ return IntStream.range(0, metricCount).mapToDouble(i -> randomDouble()).toArray();
|
|
|
+ }
|
|
|
+
|
|
|
private static SortValue randomSortValue() {
|
|
|
if (randomBoolean()) {
|
|
|
return SortValue.from(randomLong());
|