Skip to content

Commit f6978b1

Browse files
Add support for $expMovingAvg aggregation operator.
The SpEL support for this one is missing due to the differing argument map (N, alpha). Closes: #3718
1 parent 9665585 commit f6978b1

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AccumulatorOperators.java

+91
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,36 @@ public StdDevSamp stdDevSamp() {
142142
return usesFieldRef() ? StdDevSamp.stdDevSampOf(fieldReference) : StdDevSamp.stdDevSampOf(expression);
143143
}
144144

145+
/**
146+
* Creates new {@link AggregationExpression} that calculates the exponential moving average of numeric values
147+
* considering the given number of historical documents with significant weight.
148+
*
149+
* @param numberOfHistoricalDocuments the number of historical documents.
150+
* @return new instance of {@link ExpMovingAvg}.
151+
* @since 3.3
152+
*/
153+
public ExpMovingAvg expMovingAvg(int numberOfHistoricalDocuments) {
154+
155+
ExpMovingAvg expMovingAvg = usesFieldRef() ? ExpMovingAvg.expMovingAvgOf(fieldReference)
156+
: ExpMovingAvg.expMovingAvgOf(expression);
157+
return expMovingAvg.N(numberOfHistoricalDocuments);
158+
}
159+
160+
/**
161+
* Creates new {@link AggregationExpression} that calculates the exponential moving average of numeric values
162+
* applying the given exponential decay value.
163+
*
164+
* @param exponentialDecayValue the decay value.
165+
* @return new instance of {@link ExpMovingAvg}.
166+
* @since 3.3
167+
*/
168+
public ExpMovingAvg expMovingAvg(double exponentialDecayValue) {
169+
170+
ExpMovingAvg expMovingAvg = usesFieldRef() ? ExpMovingAvg.expMovingAvgOf(fieldReference)
171+
: ExpMovingAvg.expMovingAvgOf(expression);
172+
return expMovingAvg.alpha(exponentialDecayValue);
173+
}
174+
145175
private boolean usesFieldRef() {
146176
return fieldReference != null;
147177
}
@@ -658,4 +688,65 @@ public Document toDocument(Object value, AggregationOperationContext context) {
658688
return super.toDocument(value, context);
659689
}
660690
}
691+
692+
/**
693+
* {@link ExpMovingAvg} calculates the exponential moving average of numeric values.
694+
*
695+
* @author Christoph Strobl
696+
* @since 3.3
697+
*/
698+
public static class ExpMovingAvg extends AbstractAggregationExpression {
699+
700+
private ExpMovingAvg(Object value) {
701+
super(value);
702+
}
703+
704+
/**
705+
* Create a new {@link ExpMovingAvg} by defining the field holding the value to be used as input.
706+
*
707+
* @param fieldReference must not be {@literal null}.
708+
* @return new instance of {@link ExpMovingAvg}.
709+
*/
710+
public static ExpMovingAvg expMovingAvgOf(String fieldReference) {
711+
return new ExpMovingAvg(Collections.singletonMap("input", Fields.field(fieldReference)));
712+
}
713+
714+
/**
715+
* Create a new {@link ExpMovingAvg} by defining the {@link AggregationExpression expression} to compute the value
716+
* to be used as input.
717+
*
718+
* @param expression must not be {@literal null}.
719+
* @return new instance of {@link ExpMovingAvg}.
720+
*/
721+
public static ExpMovingAvg expMovingAvgOf(AggregationExpression expression) {
722+
return new ExpMovingAvg(Collections.singletonMap("input", expression));
723+
}
724+
725+
/**
726+
* Define the number of historical documents with significant mathematical weight. <br />
727+
* Specify either {@link #N(int) N} or {@link #alpha(double) aplha}. Not both!
728+
*
729+
* @param numberOfHistoricalDocuments
730+
* @return new instance of {@link ExpMovingAvg}.
731+
*/
732+
public ExpMovingAvg N/*umber of historical documents*/(int numberOfHistoricalDocuments) {
733+
return new ExpMovingAvg(append("N", numberOfHistoricalDocuments));
734+
}
735+
736+
/**
737+
* Define the exponential decay value. <br />
738+
* Specify either {@link #alpha(double) aplha} or {@link #N(int) N}. Not both!
739+
*
740+
* @param exponentialDecayValue
741+
* @return new instance of {@link ExpMovingAvg}.
742+
*/
743+
public ExpMovingAvg alpha(double exponentialDecayValue) {
744+
return new ExpMovingAvg(append("alpha", exponentialDecayValue));
745+
}
746+
747+
@Override
748+
protected String getMongoMethod() {
749+
return "$expMovingAvg";
750+
}
751+
}
661752
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core.aggregation;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
import static org.springframework.data.mongodb.core.aggregation.AccumulatorOperators.*;
20+
21+
import org.bson.Document;
22+
import org.junit.jupiter.api.Test;
23+
24+
/**
25+
* @author Christoph Strobl
26+
*/
27+
class AccumulatorOperatorsUnitTests {
28+
29+
@Test // GH-3718
30+
void rendersExpMovingAvgWithN/*umberOfHistoricDocuments*/() {
31+
32+
assertThat(valueOf("price").expMovingAvg(2).toDocument(Aggregation.DEFAULT_CONTEXT))
33+
.isEqualTo(Document.parse("{ $expMovingAvg: { input: \"$price\", N: 2 } }"));
34+
}
35+
36+
@Test // GH-3718
37+
void rendersExpMovingAvgWithAlpha() {
38+
39+
assertThat(valueOf("price").expMovingAvg(0.75).toDocument(Aggregation.DEFAULT_CONTEXT))
40+
.isEqualTo(Document.parse("{ $expMovingAvg: { input: \"$price\", alpha: 0.75 } }"));
41+
}
42+
43+
}

0 commit comments

Comments
 (0)