Skip to content

Commit 1c00f5f

Browse files
committed
Initial steps of implementing Derivative using linear regression
1 parent 6235ffa commit 1c00f5f

File tree

6 files changed

+467
-0
lines changed

6 files changed

+467
-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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,3 +593,25 @@ max_cost:integer | pod:keyword | time_bucket:datetime | max_cluster_cost:int
593593
1209 | three | 2024-05-10T00:00:00.000Z | 1209 | staging
594594
973 | two | 2024-05-10T00:00:00.000Z | 1209 | staging
595595
;
596+
597+
derivative_of_gauge_metric
598+
required_capability: ts_command_v0
599+
600+
TS k8s
601+
| STATS max_deriv = max(deriv(network.cost)) BY time_bucket = bucket(@timestamp,5minute), pod
602+
| SORT pod, time_bucket
603+
| LIMIT 10
604+
;
605+
606+
max_deriv:double | time_bucket:datetime | pod:keyword
607+
2.7573529411764707E-5 | 2024-05-10T00:00:00.000Z | one
608+
-9.657960185083621E-6 | 2024-05-10T00:05:00.000Z | one
609+
2.6771965095388827E-5 | 2024-05-10T00:10:00.000Z | one
610+
4.405946549223768E-5 | 2024-05-10T00:15:00.000Z | one
611+
8.564814814814814E-5 | 2024-05-10T00:20:00.000Z | one
612+
9.27740599107712E-5 | 2024-05-10T00:00:00.000Z | three
613+
3.80263223304832E-5 | 2024-05-10T00:05:00.000Z | three
614+
-1.9699890788329952E-5 | 2024-05-10T00:10:00.000Z | three
615+
-1.2087599544937429E-6 | 2024-05-10T00:15:00.000Z | three
616+
NaN | 2024-05-10T00:20:00.000Z | three
617+
;

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;
@@ -523,6 +524,7 @@ private static FunctionDefinition[][] functions() {
523524
def(Irate.class, uni(Irate::new), "irate"),
524525
def(Idelta.class, uni(Idelta::new), "idelta"),
525526
def(Delta.class, uni(Delta::new), "delta"),
527+
def(Deriv.class, uni(Deriv::new), "deriv"),
526528
def(Increase.class, uni(Increase::new), "increase"),
527529
def(MaxOverTime.class, uni(MaxOverTime::new), "max_over_time"),
528530
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)