Skip to content

Commit 0e2de20

Browse files
committed
fixed computation
1 parent 9fc14e1 commit 0e2de20

File tree

2 files changed

+16
-24
lines changed

2 files changed

+16
-24
lines changed

x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/RandomizedTimeSeriesIT.java

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
@SuppressWarnings("unchecked")
5050
public class RandomizedTimeSeriesIT extends AbstractEsqlIntegTestCase {
5151

52-
private static final Long NUM_DOCS = 1000L;
52+
private static final Long NUM_DOCS = 2000L;
5353
private static final String DATASTREAM_NAME = "tsit_ds";
5454
private List<XContentBuilder> documents = null;
5555
private TSDataGenerationHelper dataGenerationHelper;
@@ -205,7 +205,7 @@ public void testGroupBySubset() {
205205
values(metrics.gauge_hdd.bytes.used),
206206
max(max_over_time(metrics.gauge_hdd.bytes.used)),
207207
min(min_over_time(metrics.gauge_hdd.bytes.used)),
208-
count(count_over_time(metrics.gauge_hdd.bytes.used)),
208+
sum(count_over_time(metrics.gauge_hdd.bytes.used)),
209209
sum(sum_over_time(metrics.gauge_hdd.bytes.used)),
210210
avg(avg_over_time(metrics.gauge_hdd.bytes.used))
211211
BY tbucket=bucket(@timestamp, 1 minute), %s
@@ -217,20 +217,13 @@ public void testGroupBySubset() {
217217
var rowKey = getRowKey(row, dimensions, 6);
218218
var docValues = valuesInWindow(groups.get(rowKey), "gauge_hdd.bytes.used");
219219
// Max of int is always int, so we can safely round the result.
220-
var valuesAsInts = docValues.stream().map(Integer::valueOf).toList();
220+
var valuesAsInts = docValues.stream().toList();
221221
assertThat(valuesAsInts, containsInAnyOrder(docValues.toArray()));
222222
assertThat(row.get(1), equalTo(Math.round(aggregateValuesInWindow(docValues, Agg.MAX))));
223223
assertThat(row.get(2), equalTo(Math.round(aggregateValuesInWindow(docValues, Agg.MIN))));
224-
// TODO: Enable assertions after we fix the computation.
225-
// assertThat(row.get(3), equalTo((long) docValues.size()));
224+
assertThat(row.get(3), equalTo((long) docValues.size()));
226225
assertThat(row.get(4), equalTo(aggregateValuesInWindow(docValues, Agg.SUM).longValue()));
227-
// We check the expected vs ES-calculated average. We divide them to normalize the error
228-
// and allow for a 20% error margin.
229-
// Double esAvg = (Double) row.get(5);
230-
// Double expectedAvg = aggregateValuesInWindow(docValues, Agg.AVG);
231-
// var ratio = esAvg / expectedAvg;
232-
// assertThat(ratio, closeTo(1, 0.25));
233-
226+
assertThat(row.get(5), equalTo(aggregateValuesInWindow(docValues, Agg.SUM) / (double) docValues.size()));
234227
}
235228
}
236229
}
@@ -248,7 +241,7 @@ public void testGroupByNothing() {
248241
values(metrics.gauge_hdd.bytes.used),
249242
max(max_over_time(metrics.gauge_hdd.bytes.used)),
250243
min(min_over_time(metrics.gauge_hdd.bytes.used)),
251-
count(count_over_time(metrics.gauge_hdd.bytes.used)),
244+
sum(count_over_time(metrics.gauge_hdd.bytes.used)),
252245
sum(sum_over_time(metrics.gauge_hdd.bytes.used)),
253246
avg(avg_over_time(metrics.gauge_hdd.bytes.used))
254247
BY tbucket=bucket(@timestamp, 1 minute)
@@ -260,19 +253,13 @@ public void testGroupByNothing() {
260253
var windowStart = windowStart(row.get(6), 60);
261254
var docValues = valuesInWindow(groups.get(List.of(Long.toString(windowStart))), "gauge_hdd.bytes.used");
262255
// Make sure that expected timestamps and values are present
263-
var valuesAsInts = docValues.stream().map(Integer::valueOf).toList();
256+
var valuesAsInts = docValues.stream().toList();
264257
assertThat(valuesAsInts, containsInAnyOrder(docValues.toArray()));
265258
assertThat(row.get(1), equalTo(Math.round(aggregateValuesInWindow(docValues, Agg.MAX))));
266259
assertThat(row.get(2), equalTo(Math.round(aggregateValuesInWindow(docValues, Agg.MIN))));
267-
// TODO: Enable assertions after we fix the computation.
268-
// assertThat(row.get(3), equalTo((long) docValues.size()));
260+
assertThat(row.get(3), equalTo((long) docValues.size()));
269261
assertThat(row.get(4), equalTo(aggregateValuesInWindow(docValues, Agg.SUM).longValue()));
270-
// We check the expected vs ES-calculated average. We divide them to normalize the error
271-
// and allow for a 20% error margin.
272-
// Double esAvg = (Double) row.get(5);
273-
// Double expectedAvg = aggregateValuesInWindow(docValues, Agg.AVG);
274-
// var ratio = esAvg / expectedAvg;
275-
// assertThat(ratio, closeTo(1, 0.25));
262+
assertThat(row.get(5), equalTo(aggregateValuesInWindow(docValues, Agg.SUM) / (double) docValues.size()));
276263
}
277264
}
278265
}

x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/TSDataGenerationHelper.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,13 @@ private static Object randomDimensionValue(String dimensionName) {
5151
// Metrics coming into our system have a pre-set group of attributes.
5252
// Making a list-to-set-to-list to ensure uniqueness.
5353
this.numDocs = numDocs;
54-
attributesForMetrics = List.copyOf(Set.copyOf(ESTestCase.randomList(1, 300, () -> ESTestCase.randomAlphaOfLengthBetween(2, 30))));
55-
numTimeSeries = ESTestCase.randomIntBetween(10, (int) Math.sqrt(numDocs));
54+
var maxAttributes = (int) Math.sqrt(numDocs);
55+
attributesForMetrics = List.copyOf(
56+
Set.copyOf(ESTestCase.randomList(1, maxAttributes, () -> ESTestCase.randomAlphaOfLengthBetween(2, 30)))
57+
);
58+
var maxTimeSeries = (int) Math.sqrt(numDocs);
59+
var minTimeSeries = Math.max(1, maxTimeSeries / 4);
60+
numTimeSeries = ESTestCase.randomIntBetween(minTimeSeries, maxTimeSeries);
5661
// allTimeSeries contains the list of dimension-values for each time series.
5762
List<List<Tuple<String, Object>>> allTimeSeries = IntStream.range(0, numTimeSeries).mapToObj(tsIdx -> {
5863
List<String> dimensionsInMetric = ESTestCase.randomNonEmptySubsetOf(attributesForMetrics);

0 commit comments

Comments
 (0)