Skip to content

Commit 1e73748

Browse files
committed
Initial steps of implementing Derivative using linear regression
1 parent d066e86 commit 1e73748

File tree

6 files changed

+466
-0
lines changed

6 files changed

+466
-0
lines changed

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

Lines changed: 296 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.elasticsearch.compute.aggregation;
2+
3+
class SimpleLinearRegressionWithTimeseries {
4+
long count;
5+
double sumVal;
6+
long sumTs;
7+
double sumTsVal;
8+
long sumTsSq;
9+
10+
SimpleLinearRegressionWithTimeseries() {
11+
this.count = 0;
12+
this.sumVal = 0.0;
13+
this.sumTs = 0;
14+
this.sumTsVal = 0.0;
15+
this.sumTsSq = 0;
16+
}
17+
18+
void add(long ts, double val) {
19+
count++;
20+
sumVal += val;
21+
sumTs += ts;
22+
sumTsVal += ts * val;
23+
sumTsSq += ts * ts;
24+
}
25+
26+
double slope() {
27+
if (count <= 1) {
28+
return Double.NaN;
29+
}
30+
double numerator = count * sumTsVal - sumTs * sumVal;
31+
double denominator = count * sumTsSq - sumTs * sumTs;
32+
if (denominator == 0) {
33+
return Double.NaN;
34+
}
35+
return numerator / denominator;
36+
}
37+
38+
double intercept() {
39+
if (count == 0) {
40+
return 0.0; // or handle as needed
41+
}
42+
return (sumVal - slope() * sumTs) / count;
43+
}
44+
45+
}

x-pack/plugin/esql/qa/testFixtures/src/main/resources/k8s-timeseries.csv-spec

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,7 @@ max_cost:integer | pod:keyword | time_bucket:datetime | max_cluster_cost:int
594594
973 | two | 2024-05-10T00:00:00.000Z | 1209 | staging
595595
;
596596

597+
597598
max_of_stddev_over_time
598599
required_capability: ts_command_v0
599600
required_capability: variance_stddev_over_time
@@ -720,4 +721,24 @@ mx:integer | tbucket:datetime
720721
1716 | 2024-05-10T00:00:00.000Z
721722
;
722723

724+
derivative_of_gauge_metric
725+
required_capability: ts_command_v0
723726

727+
TS k8s
728+
| STATS max_deriv = max(deriv(network.cost)) BY time_bucket = bucket(@timestamp,5minute), pod
729+
| SORT pod, time_bucket
730+
| LIMIT 10
731+
;
732+
733+
max_deriv:double | time_bucket:datetime | pod:keyword
734+
2.7573529411764707E-5 | 2024-05-10T00:00:00.000Z | one
735+
-9.657960185083621E-6 | 2024-05-10T00:05:00.000Z | one
736+
2.6771965095388827E-5 | 2024-05-10T00:10:00.000Z | one
737+
4.405946549223768E-5 | 2024-05-10T00:15:00.000Z | one
738+
8.564814814814814E-5 | 2024-05-10T00:20:00.000Z | one
739+
9.27740599107712E-5 | 2024-05-10T00:00:00.000Z | three
740+
3.80263223304832E-5 | 2024-05-10T00:05:00.000Z | three
741+
-1.9699890788329952E-5 | 2024-05-10T00:10:00.000Z | three
742+
-1.2087599544937429E-6 | 2024-05-10T00:15:00.000Z | three
743+
NaN | 2024-05-10T00:20:00.000Z | three
744+
;

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/EsqlFunctionRegistry.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.elasticsearch.xpack.esql.expression.function.aggregate.CountDistinctOverTime;
2828
import org.elasticsearch.xpack.esql.expression.function.aggregate.CountOverTime;
2929
import org.elasticsearch.xpack.esql.expression.function.aggregate.Delta;
30+
import org.elasticsearch.xpack.esql.expression.function.aggregate.Deriv;
3031
import org.elasticsearch.xpack.esql.expression.function.aggregate.First;
3132
import org.elasticsearch.xpack.esql.expression.function.aggregate.FirstOverTime;
3233
import org.elasticsearch.xpack.esql.expression.function.aggregate.Idelta;
@@ -531,6 +532,7 @@ private static FunctionDefinition[][] functions() {
531532
def(Irate.class, uni(Irate::new), "irate"),
532533
def(Idelta.class, uni(Idelta::new), "idelta"),
533534
def(Delta.class, uni(Delta::new), "delta"),
535+
def(Deriv.class, uni(Deriv::new), "deriv"),
534536
def(Increase.class, uni(Increase::new), "increase"),
535537
def(MaxOverTime.class, uni(MaxOverTime::new), "max_over_time"),
536538
def(MinOverTime.class, uni(MinOverTime::new), "min_over_time"),

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/aggregate/AggregateWritables.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public static List<NamedWriteableRegistry.Entry> getNamedWriteables() {
2929
Idelta.ENTRY,
3030
Increase.ENTRY,
3131
Delta.ENTRY,
32+
Deriv.ENTRY,
3233
Sample.ENTRY,
3334
SpatialCentroid.ENTRY,
3435
SpatialExtent.ENTRY,

0 commit comments

Comments
 (0)