forked from zapr-oss/druidry
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix for zapr-oss#160 - additional aggregators
Add - DoubleMeanAggregator - StdDevPostAggregator - VarianceAggregator
- Loading branch information
1 parent
9879ce8
commit f3fab2c
Showing
8 changed files
with
579 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/main/java/in/zapr/druid/druidry/aggregator/DoubleMeanAggregator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright 2018-present Red Brick Lane Marketing Solutions Pvt. Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package in.zapr.druid.druidry.aggregator; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.google.common.base.Preconditions; | ||
|
||
import lombok.Builder; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.NonNull; | ||
|
||
|
||
|
||
@Getter | ||
@EqualsAndHashCode(callSuper = true) | ||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
public class DoubleMeanAggregator extends DruidAggregator { | ||
|
||
private static final String DOUBLE_MEAN_TYPE_AGGREGATOR = "doubleMean"; | ||
|
||
private final String fieldName; | ||
private final String expression; | ||
|
||
@Builder | ||
private DoubleMeanAggregator(@NonNull String name, String fieldName, String expression) { | ||
this.type = DOUBLE_MEAN_TYPE_AGGREGATOR; | ||
this.name = name; | ||
this.fieldName = fieldName; | ||
this.expression = expression; | ||
|
||
Preconditions.checkArgument( | ||
fieldName == null ^ expression == null, | ||
"Must have a valid, non-null fieldName or expression" | ||
); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/main/java/in/zapr/druid/druidry/aggregator/VarianceAggregator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright 2018-present Red Brick Lane Marketing Solutions Pvt. Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package in.zapr.druid.druidry.aggregator; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.google.common.base.Preconditions; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.NonNull; | ||
import lombok.Builder; | ||
|
||
|
||
|
||
@Getter | ||
@EqualsAndHashCode(callSuper = true) | ||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
public class VarianceAggregator extends DruidAggregator { | ||
private static final String VARIANCE_TYPE_AGGREGATOR = "variance"; | ||
|
||
private final String fieldName; | ||
private final String expression; | ||
|
||
// One of "float", "double", "long", "variance"; defaults to "float". | ||
private final String inputType; | ||
|
||
// Use "population" to get variance_pop rather than variance_sample, which is default. | ||
private final String estimator; | ||
|
||
@Builder | ||
private VarianceAggregator(@NonNull String name, String fieldName, String expression, String inputType, String estimator) { | ||
this.type = VARIANCE_TYPE_AGGREGATOR; | ||
this.name = name; | ||
this.fieldName = fieldName; | ||
this.expression = expression; | ||
this.inputType = inputType; | ||
this.estimator = estimator; | ||
|
||
Preconditions.checkArgument( | ||
fieldName == null ^ expression == null, | ||
"Must have a valid, non-null fieldName or expression" | ||
); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/in/zapr/druid/druidry/postAggregator/StdDevPostAggregator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2018-present Red Brick Lane Marketing Solutions Pvt. Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package in.zapr.druid.druidry.postAggregator; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
|
||
import lombok.Builder; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.NonNull; | ||
|
||
|
||
@Getter | ||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
@EqualsAndHashCode(callSuper = true) | ||
public class StdDevPostAggregator extends DruidPostAggregator { | ||
|
||
private static final String STDDEV_POST_AGGREGATOR_TYPE = "stddev"; | ||
private final String fieldName; | ||
|
||
@Builder | ||
StdDevPostAggregator(@NonNull String name, | ||
@NonNull String fieldName) { | ||
this.type = STDDEV_POST_AGGREGATOR_TYPE; | ||
this.name = name; | ||
this.fieldName = fieldName; | ||
} | ||
|
||
} |
159 changes: 159 additions & 0 deletions
159
src/test/java/in/zapr/druid/druidry/aggregator/DoubleMeanAggregatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/* | ||
* Copyright 2018-present Red Brick Lane Marketing Solutions Pvt. Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package in.zapr.druid.druidry.aggregator; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
import org.skyscreamer.jsonassert.JSONAssert; | ||
import org.skyscreamer.jsonassert.JSONCompareMode; | ||
import org.testng.Assert; | ||
import org.testng.annotations.BeforeClass; | ||
import org.testng.annotations.Test; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
public class DoubleMeanAggregatorTest { | ||
|
||
private static ObjectMapper objectMapper; | ||
|
||
@BeforeClass | ||
public void init() { | ||
objectMapper = new ObjectMapper(); | ||
} | ||
|
||
@Test | ||
public void testAllFields() throws JsonProcessingException, JSONException { | ||
|
||
DoubleMeanAggregator doubleMeanAggregator = | ||
DoubleMeanAggregator.builder().name("CarpeDiem").fieldName("someField").build(); | ||
|
||
JSONObject jsonObject = new JSONObject(); | ||
jsonObject.put("type", "doubleMean"); | ||
jsonObject.put("name", "CarpeDiem"); | ||
jsonObject.put("fieldName", "someField"); | ||
|
||
String actualJSON = objectMapper.writeValueAsString(doubleMeanAggregator); | ||
String expectedJSON = jsonObject.toString(); | ||
JSONAssert.assertEquals(expectedJSON, actualJSON, JSONCompareMode.NON_EXTENSIBLE); | ||
} | ||
|
||
@Test | ||
public void testAllButFieldName() throws JSONException, JsonProcessingException { | ||
|
||
DoubleMeanAggregator doubleMeanAggregator = | ||
DoubleMeanAggregator.builder() | ||
.name("CarpeDiem") | ||
.expression("(\"foo\" / \"bar\")") | ||
.build(); | ||
|
||
JSONObject jsonObject = new JSONObject(); | ||
jsonObject.put("type", "doubleMean"); | ||
jsonObject.put("name", "CarpeDiem"); | ||
jsonObject.put("expression", "(\"foo\" / \"bar\")"); | ||
|
||
String actualJSON = objectMapper.writeValueAsString(doubleMeanAggregator); | ||
String expectedJSON = jsonObject.toString(); | ||
JSONAssert.assertEquals(expectedJSON, actualJSON, JSONCompareMode.NON_EXTENSIBLE); | ||
} | ||
|
||
@Test(expectedExceptions = NullPointerException.class) | ||
public void testNullName() throws JsonProcessingException, JSONException { | ||
|
||
DoubleMeanAggregator doubleMeanAggregator = | ||
DoubleMeanAggregator.builder() | ||
.fieldName("anotherField") | ||
.build(); | ||
} | ||
|
||
@Test | ||
public void testEqualsPositive() { | ||
DoubleMeanAggregator aggregator1 = | ||
DoubleMeanAggregator.builder() | ||
.name("name") | ||
.fieldName("field") | ||
.build(); | ||
|
||
DoubleMeanAggregator aggregator2 = | ||
DoubleMeanAggregator.builder() | ||
.name("name") | ||
.fieldName("field") | ||
.build(); | ||
|
||
DoubleMeanAggregator aggregator3 = | ||
DoubleMeanAggregator.builder() | ||
.name("name") | ||
.expression("(\"foo\" / \"bar\")") | ||
.build(); | ||
|
||
DoubleMeanAggregator aggregator4 = | ||
DoubleMeanAggregator.builder() | ||
.name("name") | ||
.expression("(\"foo\" / \"bar\")") | ||
.build(); | ||
|
||
Assert.assertEquals(aggregator1, aggregator2); | ||
Assert.assertEquals(aggregator3, aggregator4); | ||
} | ||
|
||
@Test | ||
public void testEqualsNegative() { | ||
DoubleMeanAggregator aggregator1 = | ||
DoubleMeanAggregator.builder() | ||
.name("name") | ||
.fieldName("field") | ||
.build(); | ||
|
||
DoubleMeanAggregator aggregator2 = | ||
DoubleMeanAggregator.builder() | ||
.name("name1") | ||
.fieldName("field1") | ||
.build(); | ||
|
||
DoubleMeanAggregator aggregator3 = | ||
DoubleMeanAggregator.builder() | ||
.name("name") | ||
.expression("(\"foo\" / \"bar\")") | ||
.build(); | ||
|
||
DoubleMeanAggregator aggregator4 = | ||
DoubleMeanAggregator.builder() | ||
.name("name") | ||
.expression("(\"foo\" / \"baz\")") | ||
.build(); | ||
|
||
Assert.assertNotEquals(aggregator1, aggregator2); | ||
Assert.assertNotEquals(aggregator3, aggregator4); | ||
} | ||
|
||
@Test | ||
public void testEqualsWithAnotherSubClass() { | ||
DoubleMeanAggregator aggregator1 = | ||
DoubleMeanAggregator.builder() | ||
.name("name") | ||
.fieldName("field") | ||
.build(); | ||
|
||
CountAggregator aggregator2 = new CountAggregator("countAgg1"); | ||
|
||
Assert.assertNotEquals(aggregator1, aggregator2); | ||
} | ||
|
||
} |
Oops, something went wrong.