use of org.apache.druid.query.movingaverage.averagers.LongMeanAveragerFactory in project druid by druid-io.
the class MovingAverageIterableTest method testMissingDaysAtEnd.
@Test
public void testMissingDaysAtEnd() {
System.setProperty("druid.generic.useDefaultValueForNull", "true");
NullHandling.initializeForTests();
Map<String, Object> event1 = new HashMap<>();
Map<String, Object> event2 = new HashMap<>();
List<DimensionSpec> ds = new ArrayList<>();
ds.add(new DefaultDimensionSpec("gender", "gender"));
event1.put("gender", "m");
event1.put("pageViews", 10L);
Row row1 = new MapBasedRow(JAN_1, event1);
event2.put("gender", "m");
event2.put("pageViews", 20L);
Row row2 = new MapBasedRow(JAN_2, event2);
Sequence<RowBucket> seq = Sequences.simple(Arrays.asList(new RowBucket(JAN_1, Collections.singletonList(row1)), new RowBucket(JAN_2, Collections.singletonList(row2)), new RowBucket(JAN_3, Collections.emptyList()), new RowBucket(JAN_4, Collections.emptyList()), new RowBucket(JAN_5, Collections.emptyList()), new RowBucket(JAN_6, Collections.emptyList())));
Iterator<Row> iter = new MovingAverageIterable(seq, ds, Collections.singletonList(new LongMeanAveragerFactory("movingAvgPageViews", 4, 1, "pageViews")), Collections.emptyList(), Collections.singletonList(new LongSumAggregatorFactory("pageViews", "pageViews"))).iterator();
Assert.assertTrue(iter.hasNext());
Row result = iter.next();
Assert.assertEquals(JAN_1, result.getTimestamp());
Assert.assertEquals("m", (result.getDimension("gender")).get(0));
Assert.assertEquals(2.5f, result.getMetric("movingAvgPageViews").floatValue(), 0.0f);
Assert.assertTrue(iter.hasNext());
result = iter.next();
Assert.assertEquals(JAN_2, result.getTimestamp());
Assert.assertEquals("m", (result.getDimension("gender")).get(0));
Assert.assertEquals(7.5f, result.getMetric("movingAvgPageViews").floatValue(), 0.0f);
Assert.assertTrue(iter.hasNext());
result = iter.next();
Assert.assertEquals(JAN_3, result.getTimestamp());
Assert.assertEquals("m", (result.getDimension("gender")).get(0));
Assert.assertEquals(7.5f, result.getMetric("movingAvgPageViews").floatValue(), 0.0f);
Assert.assertTrue(iter.hasNext());
result = iter.next();
Assert.assertEquals(JAN_4, result.getTimestamp());
Assert.assertEquals("m", (result.getDimension("gender")).get(0));
Assert.assertEquals(7.5f, result.getMetric("movingAvgPageViews").floatValue(), 0.0f);
Assert.assertTrue(iter.hasNext());
result = iter.next();
Assert.assertEquals(JAN_5, result.getTimestamp());
Assert.assertEquals("m", (result.getDimension("gender")).get(0));
Assert.assertEquals(5.0f, result.getMetric("movingAvgPageViews").floatValue(), 0.0f);
Assert.assertTrue(iter.hasNext());
result = iter.next();
Assert.assertEquals(JAN_6, result.getTimestamp());
Assert.assertEquals("m", (result.getDimension("gender")).get(0));
Assert.assertEquals(0.0f, result.getMetric("movingAvgPageViews").floatValue(), 0.0f);
Assert.assertFalse(iter.hasNext());
}
use of org.apache.druid.query.movingaverage.averagers.LongMeanAveragerFactory in project druid by druid-io.
the class MovingAverageIterableTest method testMissingDaysInMiddle.
@Test
public void testMissingDaysInMiddle() {
System.setProperty("druid.generic.useDefaultValueForNull", "true");
NullHandling.initializeForTests();
Map<String, Object> event1 = new HashMap<>();
Map<String, Object> event2 = new HashMap<>();
List<DimensionSpec> ds = new ArrayList<>();
ds.add(new DefaultDimensionSpec("gender", "gender"));
event1.put("gender", "m");
event1.put("pageViews", 10L);
Row row1 = new MapBasedRow(JAN_1, event1);
event2.put("gender", "m");
event2.put("pageViews", 20L);
Row row2 = new MapBasedRow(JAN_4, event2);
Sequence<RowBucket> seq = Sequences.simple(Arrays.asList(new RowBucket(JAN_1, Collections.singletonList(row1)), new RowBucket(JAN_2, Collections.emptyList()), new RowBucket(JAN_3, Collections.emptyList()), new RowBucket(JAN_4, Collections.singletonList(row2))));
Iterator<Row> iter = new MovingAverageIterable(seq, ds, Collections.singletonList(new LongMeanAveragerFactory("movingAvgPageViews", 4, 1, "pageViews")), Collections.emptyList(), Collections.singletonList(new LongSumAggregatorFactory("pageViews", "pageViews"))).iterator();
Assert.assertTrue(iter.hasNext());
Row result = iter.next();
Assert.assertEquals("m", (result.getDimension("gender")).get(0));
Assert.assertEquals(2.5f, result.getMetric("movingAvgPageViews").floatValue(), 0.0f);
Assert.assertTrue(iter.hasNext());
result = iter.next();
Assert.assertEquals("m", (result.getDimension("gender")).get(0));
Assert.assertEquals(2.5f, result.getMetric("movingAvgPageViews").floatValue(), 0.0f);
Assert.assertTrue(iter.hasNext());
result = iter.next();
Assert.assertEquals("m", (result.getDimension("gender")).get(0));
Assert.assertEquals(2.5f, result.getMetric("movingAvgPageViews").floatValue(), 0.0f);
Assert.assertTrue(iter.hasNext());
result = iter.next();
Assert.assertEquals("m", (result.getDimension("gender")).get(0));
Assert.assertEquals(7.5f, result.getMetric("movingAvgPageViews").floatValue(), 0.0f);
Assert.assertFalse(iter.hasNext());
}
use of org.apache.druid.query.movingaverage.averagers.LongMeanAveragerFactory in project druid by druid-io.
the class MovingAverageIterableTest method testAveraging.
@Test
public void testAveraging() {
Map<String, Object> event1 = new HashMap<>();
Map<String, Object> event2 = new HashMap<>();
Map<String, Object> event3 = new HashMap<>();
Map<String, Object> event4 = new HashMap<>();
List<DimensionSpec> ds = new ArrayList<>();
ds.add(new DefaultDimensionSpec("gender", "gender"));
event1.put("gender", "m");
event1.put("pageViews", 10L);
Row row1 = new MapBasedRow(JAN_1, event1);
event2.put("gender", "m");
event2.put("pageViews", 20L);
Row row2 = new MapBasedRow(JAN_2, event2);
event3.put("gender", "m");
event3.put("pageViews", 30L);
Row row3 = new MapBasedRow(JAN_3, event3);
event4.put("gender", "f");
event4.put("pageViews", 40L);
Row row4 = new MapBasedRow(JAN_3, event4);
float retval = 14.5f;
Sequence<RowBucket> seq = Sequences.simple(Arrays.asList(new RowBucket(JAN_1, Collections.singletonList(row1)), new RowBucket(JAN_2, Collections.singletonList(row2)), new RowBucket(JAN_3, Arrays.asList(row3, row4))));
Iterator<Row> iter = new MovingAverageIterable(seq, ds, Arrays.asList(new ConstantAveragerFactory("costPageViews", 7, retval), new LongMeanAveragerFactory("movingAvgPageViews", 7, 1, "pageViews")), Collections.emptyList(), Collections.singletonList(new LongSumAggregatorFactory("pageViews", "pageViews"))).iterator();
Assert.assertTrue(iter.hasNext());
Row caResult = iter.next();
Assert.assertEquals(JAN_1, caResult.getTimestamp());
Assert.assertEquals("m", (caResult.getDimension("gender")).get(0));
Assert.assertEquals(retval, caResult.getMetric("costPageViews").floatValue(), 0.0f);
Assert.assertEquals(1.4285715f, caResult.getMetric("movingAvgPageViews").floatValue(), 0.0f);
Assert.assertTrue(iter.hasNext());
caResult = iter.next();
Assert.assertEquals("m", (caResult.getDimension("gender")).get(0));
Assert.assertEquals(4.285714f, caResult.getMetric("movingAvgPageViews").floatValue(), 0.0f);
Assert.assertTrue(iter.hasNext());
caResult = iter.next();
Assert.assertEquals("m", (caResult.getDimension("gender")).get(0));
Assert.assertEquals(8.571428f, caResult.getMetric("movingAvgPageViews").floatValue(), 0.0f);
Assert.assertTrue(iter.hasNext());
caResult = iter.next();
Assert.assertEquals("f", (caResult.getDimension("gender")).get(0));
Assert.assertEquals(5.714285850f, caResult.getMetric("movingAvgPageViews").floatValue(), 0.0f);
Assert.assertFalse(iter.hasNext());
}
use of org.apache.druid.query.movingaverage.averagers.LongMeanAveragerFactory in project druid by druid-io.
the class MovingAverageIterableTest method testWithFilteredAggregation.
@Test
public void testWithFilteredAggregation() {
Map<String, Object> event1 = new HashMap<>();
Map<String, Object> event2 = new HashMap<>();
List<DimensionSpec> ds = new ArrayList<>();
ds.add(new DefaultDimensionSpec("gender", "gender"));
event1.put("gender", "m");
event1.put("pageViews", 10L);
Row row1 = new MapBasedRow(JAN_1, event1);
event2.put("gender", "m");
event2.put("pageViews", 20L);
Row row2 = new MapBasedRow(JAN_4, event2);
Sequence<RowBucket> seq = Sequences.simple(Arrays.asList(new RowBucket(JAN_1, Collections.singletonList(row1)), new RowBucket(JAN_2, Collections.emptyList()), new RowBucket(JAN_3, Collections.emptyList()), new RowBucket(JAN_4, Collections.singletonList(row2))));
AveragerFactory averagerfactory = new LongMeanAveragerFactory("movingAvgPageViews", 4, 1, "pageViews");
AggregatorFactory aggregatorFactory = new LongSumAggregatorFactory("pageViews", "pageViews");
DimFilter filter = new SelectorDimFilter("gender", "m", null);
FilteredAggregatorFactory filteredAggregatorFactory = new FilteredAggregatorFactory(aggregatorFactory, filter);
Iterator<Row> iter = new MovingAverageIterable(seq, ds, Collections.singletonList(averagerfactory), Collections.emptyList(), Collections.singletonList(filteredAggregatorFactory)).iterator();
Assert.assertTrue(iter.hasNext());
Row result = iter.next();
Assert.assertEquals("m", (result.getDimension("gender")).get(0));
Assert.assertEquals(2.5f, result.getMetric("movingAvgPageViews").floatValue(), 0.0f);
Assert.assertTrue(iter.hasNext());
result = iter.next();
Assert.assertEquals("m", (result.getDimension("gender")).get(0));
Assert.assertEquals(2.5f, result.getMetric("movingAvgPageViews").floatValue(), 0.0f);
Assert.assertTrue(iter.hasNext());
result = iter.next();
Assert.assertEquals("m", (result.getDimension("gender")).get(0));
Assert.assertEquals(2.5f, result.getMetric("movingAvgPageViews").floatValue(), 0.0f);
Assert.assertTrue(iter.hasNext());
result = iter.next();
Assert.assertEquals("m", (result.getDimension("gender")).get(0));
Assert.assertEquals(7.5f, result.getMetric("movingAvgPageViews").floatValue(), 0.0f);
Assert.assertFalse(iter.hasNext());
}
Aggregations