Skip to content

Commit 0354991

Browse files
committed
Add comment
1 parent 5caebfd commit 0354991

File tree

5 files changed

+65
-25
lines changed

5 files changed

+65
-25
lines changed

x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/aggregation/RateDoubleAggregator.java

Lines changed: 13 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/aggregation/RateFloatAggregator.java

Lines changed: 13 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/aggregation/RateIntAggregator.java

Lines changed: 13 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/aggregation/RateLongAggregator.java

Lines changed: 13 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/aggregation/X-RateAggregator.java.st

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ public class Rate$Type$Aggregator {
321321
}
322322
}
323323

324-
private static double computeRate($Type$RateState state, long unitInMillis) {
324+
private static double computeRateWithoutExtrapolate($Type$RateState state, long unitInMillis) {
325325
final int len = state.entries();
326326
final long firstTS = state.timestamps[state.timestamps.length - 1];
327327
final long lastTS = state.timestamps[0];
@@ -336,7 +336,16 @@ public class Rate$Type$Aggregator {
336336
return (lastValue - firstValue) * unitInMillis / (lastTS - firstTS);
337337
}
338338

339-
private static double computeRate($Type$RateState state, long rangeStart, long rangeEnd, long unitInMillis) {
339+
/**
340+
* Credit to PromQL for this extrapolation algorithm:
341+
* If samples are close enough to the rangeStart and rangeEnd, we extrapolate the rate all the way to the boundary in question.
342+
* "Close enough" is defined as "up to 10% more than the average duration between samples within the range".
343+
* Essentially, we assume a more or less regular spacing between samples. If we don't see a sample where we would expect one,
344+
* we assume the series does not cover the whole range but starts and/or ends within the range.
345+
* We still extrapolate the rate in this case, but not all the way to the boundary, only by half of the average duration between
346+
* samples (which is our guess for where the series actually starts or ends).
347+
*/
348+
private static double extrapolateRate($Type$RateState state, long rangeStart, long rangeEnd, long unitInMillis) {
340349
final int len = state.entries();
341350
final long firstTS = state.timestamps[state.timestamps.length - 1];
342351
final long lastTS = state.timestamps[0];
@@ -354,7 +363,6 @@ public class Rate$Type$Aggregator {
354363
double startGap = firstTS - rangeStart;
355364
if (startGap > 0) {
356365
if (startGap > averageSampleInterval * 1.1) {
357-
// limit to half of the average sample interval if samples are far from the boundary
358366
startGap = averageSampleInterval / 2.0;
359367
}
360368
firstValue = Math.max(0.0, firstValue - startGap * slope);
@@ -382,9 +390,9 @@ public class Rate$Type$Aggregator {
382390
int len = state.entries();
383391
final double rate;
384392
if (evalContext instanceof TimeSeriesGroupingAggregatorEvaluationContext tsContext) {
385-
rate = computeRate(state, tsContext.rangeStartInMillis(groupId), tsContext.rangeEndInMillis(groupId), unitInMillis);
393+
rate = extrapolateRate(state, tsContext.rangeStartInMillis(groupId), tsContext.rangeEndInMillis(groupId), unitInMillis);
386394
} else {
387-
rate = computeRate(state, unitInMillis);
395+
rate = computeRateWithoutExtrapolate(state, unitInMillis);
388396
}
389397
rates.appendDouble(rate);
390398
}

0 commit comments

Comments
 (0)