Skip to content

Commit c457df1

Browse files
committed
addressing comments
1 parent fd89c97 commit c457df1

File tree

1 file changed

+39
-28
lines changed

1 file changed

+39
-28
lines changed

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

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ record Doc(String host, String cluster, long timestamp, int requestCount, double
4444
static final float DEVIATION_LIMIT = 0.15f;
4545
// We expect a 10% drop in the rate due to not covering window edges and not triggering
4646
// extrapolation logic in the time series engine.
47-
static final float EXPECTED_DROP_RATE = 0.10f;
47+
static final float EXPECTED_DROP_RATE = 0.12f;
4848
static final int LIMIT = 5;
49+
static final int MAX_HOSTS = 5;
50+
static final int PCT_CHANCE_OF_RESET = 15; // 15% chance of resetting the request count
4951

5052
@Before
5153
public void populateIndex() {
@@ -69,7 +71,7 @@ public void populateIndex() {
6971
)
7072
.get();
7173
final Map<String, Integer> requestCounts = new HashMap<>();
72-
for (int i = 0; i < 5; i++) {
74+
for (int i = 0; i < MAX_HOSTS; i++) {
7375
hostToClusters.put("p" + i, randomFrom("qa", "prod"));
7476
hostToRate.put("p" + i, randomIntBetween(0, 50));
7577
requestCounts.put("p" + i, randomIntBetween(0, 100));
@@ -79,17 +81,16 @@ public void populateIndex() {
7981
int numDocs = between(100, 300);
8082
docs.clear();
8183
// We want docs to span a 6-minute period, so we need to adapt their spacing accordingly.
82-
var tsPerDoc = 360.0 / numDocs; // 6 minutes divided by number of docs
84+
var avgSamplingPeriod = 360.0 / numDocs; // 6 minutes divided by number of docs - then randomized below
8385

8486
for (int i = 0; i < numDocs; i++) {
85-
final var tsChange = randomDoubleBetween(tsPerDoc - 1.0, tsPerDoc + 1.0, true);
87+
final var tsChange = randomDoubleBetween(avgSamplingPeriod - 1.0, avgSamplingPeriod + 1.0, true);
8688
timestamp += Math.round(tsChange * 1000);
8789
// We want a subset of hosts to have docs within a give time point.
8890
var hosts = Set.copyOf(randomSubsetOf(between(2, hostToClusters.size()), hostToClusters.keySet()));
8991
for (String host : hostToClusters.keySet()) {
9092
var requestCount = requestCounts.compute(host, (k, curr) -> {
91-
// 15% chance of reset
92-
if (randomInt(100) <= 15) {
93+
if (randomInt(100) <= PCT_CHANCE_OF_RESET) {
9394
return Math.toIntExact(Math.round(hostToRate.get(k) * tsChange));
9495
} else {
9596
return Math.toIntExact(Math.round((curr == null ? 0 : curr) + hostToRate.get(k) * tsChange));
@@ -165,12 +166,18 @@ private String valuesTable(List<List<Object>> values) {
165166
}
166167

167168
public void testRateWithTimeBucketSumByMin() {
168-
try (var resp = run("TS hosts | STATS sum(rate(request_count)) BY ts=bucket(@timestamp, 1 minute) | SORT ts | LIMIT " + LIMIT)) {
169+
try (
170+
var resp = run(
171+
"TS hosts | STATS sum(rate(request_count)) BY tbucket=bucket(@timestamp, 1 minute) | SORT tbucket | LIMIT " + LIMIT
172+
)
173+
) {
169174
List<List<Object>> values = EsqlTestUtils.getValuesList(resp);
170175
try {
171176
assertThat(
172177
resp.columns(),
173-
equalTo(List.of(new ColumnInfoImpl("sum(rate(request_count))", "double", null), new ColumnInfoImpl("ts", "date", null)))
178+
equalTo(
179+
List.of(new ColumnInfoImpl("sum(rate(request_count))", "double", null), new ColumnInfoImpl("tbucket", "date", null))
180+
)
174181
);
175182
assertThat(values, hasSize(LIMIT));
176183
for (int i = 0; i < values.size(); i++) {
@@ -186,11 +193,13 @@ public void testRateWithTimeBucketSumByMin() {
186193
}
187194

188195
public void testRateWithTimeBucketAvgByMin() {
189-
try (var resp = run("TS hosts | STATS avg(rate(request_count)) BY ts=bucket(@timestamp, 1minute) | SORT ts | LIMIT 5")) {
196+
try (var resp = run("TS hosts | STATS avg(rate(request_count)) BY tbucket=bucket(@timestamp, 1minute) | SORT tbucket | LIMIT 5")) {
190197
try {
191198
assertThat(
192199
resp.columns(),
193-
equalTo(List.of(new ColumnInfoImpl("avg(rate(request_count))", "double", null), new ColumnInfoImpl("ts", "date", null)))
200+
equalTo(
201+
List.of(new ColumnInfoImpl("avg(rate(request_count))", "double", null), new ColumnInfoImpl("tbucket", "date", null))
202+
)
194203
);
195204
List<List<Object>> values = EsqlTestUtils.getValuesList(resp);
196205
assertThat(values, hasSize(LIMIT));
@@ -209,13 +218,15 @@ public void testRateWithTimeBucketAvgByMin() {
209218
public void testRateWithTimeBucketSumByMinAndLimitAsParam() {
210219
try (var resp = run("""
211220
TS hosts
212-
| STATS avg(rate(request_count)) BY ts=bucket(@timestamp, 1minute)
213-
| SORT ts
221+
| STATS avg(rate(request_count)) BY tbucket=bucket(@timestamp, 1minute)
222+
| SORT tbucket
214223
| LIMIT""" + " " + LIMIT)) {
215224
try {
216225
assertThat(
217226
resp.columns(),
218-
equalTo(List.of(new ColumnInfoImpl("avg(rate(request_count))", "double", null), new ColumnInfoImpl("ts", "date", null)))
227+
equalTo(
228+
List.of(new ColumnInfoImpl("avg(rate(request_count))", "double", null), new ColumnInfoImpl("tbucket", "date", null))
229+
)
219230
);
220231
List<List<Object>> values = EsqlTestUtils.getValuesList(resp);
221232
assertThat(values, hasSize(LIMIT));
@@ -234,16 +245,16 @@ public void testRateWithTimeBucketSumByMinAndLimitAsParam() {
234245
public void testRateWithTimeBucketAndClusterSumByMin() {
235246
try (var resp = run("""
236247
TS hosts
237-
| STATS sum(rate(request_count)) BY ts=bucket(@timestamp, 1 minute), cluster
238-
| SORT ts, cluster
248+
| STATS sum(rate(request_count)) BY tbucket=bucket(@timestamp, 1 minute), cluster
249+
| SORT tbucket, cluster
239250
| LIMIT 5""")) {
240251
try {
241252
assertThat(
242253
resp.columns(),
243254
equalTo(
244255
List.of(
245256
new ColumnInfoImpl("sum(rate(request_count))", "double", null),
246-
new ColumnInfoImpl("ts", "date", null),
257+
new ColumnInfoImpl("tbucket", "date", null),
247258
new ColumnInfoImpl("cluster", "keyword", null)
248259
)
249260
)
@@ -269,16 +280,16 @@ public void testRateWithTimeBucketAndClusterSumByMin() {
269280
public void testRateWithTimeBucketAndClusterAvgByMin() {
270281
try (var resp = run("""
271282
TS hosts
272-
| STATS avg(rate(request_count)) BY ts=bucket(@timestamp, 1minute), cluster
273-
| SORT ts, cluster
283+
| STATS avg(rate(request_count)) BY tbucket=bucket(@timestamp, 1minute), cluster
284+
| SORT tbucket, cluster
274285
| LIMIT 5""")) {
275286
try {
276287
assertThat(
277288
resp.columns(),
278289
equalTo(
279290
List.of(
280291
new ColumnInfoImpl("avg(rate(request_count))", "double", null),
281-
new ColumnInfoImpl("ts", "date", null),
292+
new ColumnInfoImpl("tbucket", "date", null),
282293
new ColumnInfoImpl("cluster", "keyword", null)
283294
)
284295
)
@@ -309,8 +320,8 @@ public void testRateWithTimeBucketAndClusterMultipleStatsByMin() {
309320
c = count(rate(request_count)),
310321
max(rate(request_count)),
311322
avg(rate(request_count))
312-
BY ts=bucket(@timestamp, 1minute), cluster
313-
| SORT ts, cluster
323+
BY tbucket=bucket(@timestamp, 1minute), cluster
324+
| SORT tbucket, cluster
314325
| LIMIT 5
315326
| EVAL avg_rate= s/c
316327
| KEEP avg_rate, `max(rate(request_count))`, `avg(rate(request_count))`, ts, cluster
@@ -323,7 +334,7 @@ public void testRateWithTimeBucketAndClusterMultipleStatsByMin() {
323334
new ColumnInfoImpl("avg_rate", "double", null),
324335
new ColumnInfoImpl("max(rate(request_count))", "double", null),
325336
new ColumnInfoImpl("avg(rate(request_count))", "double", null),
326-
new ColumnInfoImpl("ts", "date", null),
337+
new ColumnInfoImpl("tbucket", "date", null),
327338
new ColumnInfoImpl("cluster", "keyword", null)
328339
)
329340
)
@@ -357,8 +368,8 @@ public void testRateWithTimeBucketAndClusterMultipleStatsByMin() {
357368
public void testRateWithTimeBucketAndClusterMultipleMetricsByMin() {
358369
try (var resp = run("""
359370
TS hosts
360-
| STATS sum(rate(request_count)), max(cpu) BY ts=bucket(@timestamp, 1 minute), cluster
361-
| SORT ts, cluster
371+
| STATS sum(rate(request_count)), max(cpu) BY tbucket=bucket(@timestamp, 1 minute), cluster
372+
| SORT tbucket, cluster
362373
| LIMIT 5""")) {
363374
try {
364375
assertThat(
@@ -367,7 +378,7 @@ public void testRateWithTimeBucketAndClusterMultipleMetricsByMin() {
367378
List.of(
368379
new ColumnInfoImpl("sum(rate(request_count))", "double", null),
369380
new ColumnInfoImpl("max(cpu)", "double", null),
370-
new ColumnInfoImpl("ts", "date", null),
381+
new ColumnInfoImpl("tbucket", "date", null),
371382
new ColumnInfoImpl("cluster", "keyword", null)
372383
)
373384
)
@@ -399,8 +410,8 @@ public void testRateWithTimeBucketAndClusterMultipleMetricsByMin() {
399410
public void testRateWithTimeBucketAndClusterMultipleMetricsAvgByMin() {
400411
try (var resp = run("""
401412
TS hosts
402-
| STATS sum(rate(request_count)), avg(cpu) BY ts=bucket(@timestamp, 1 minute), cluster
403-
| SORT ts, cluster
413+
| STATS sum(rate(request_count)), avg(cpu) BY tbucket=bucket(@timestamp, 1 minute), cluster
414+
| SORT tbucket, cluster
404415
| LIMIT 5""")) {
405416
try {
406417
assertThat(
@@ -409,7 +420,7 @@ public void testRateWithTimeBucketAndClusterMultipleMetricsAvgByMin() {
409420
List.of(
410421
new ColumnInfoImpl("sum(rate(request_count))", "double", null),
411422
new ColumnInfoImpl("avg(cpu)", "double", null),
412-
new ColumnInfoImpl("ts", "date", null),
423+
new ColumnInfoImpl("tbucket", "date", null),
413424
new ColumnInfoImpl("cluster", "keyword", null)
414425
)
415426
)

0 commit comments

Comments
 (0)