StatsAction extends Action<StatsRequest> { @Override public ActionResponse execute(StatsRequest parameter) { SearchRequestBuilder searchRequestBuilder = getRequestBuilder(parameter); try { SearchResponse response = searchRequestBuilder.execute() .actionGet(getGetQueryTimeout()); return getResponse(response, parameter); } catch (ElasticsearchException e) { throw FoxtrotExceptions.createQueryExecutionException(parameter, e); } } StatsAction(StatsRequest parameter, AnalyticsLoader analyticsLoader); @Override void preprocess(); @Override String getMetricKey(); @Override String getRequestCacheKey(); @Override void validateImpl(StatsRequest parameter); @Override ActionResponse execute(StatsRequest parameter); @Override SearchRequestBuilder getRequestBuilder(StatsRequest parameter); @Override ActionResponse getResponse(org.elasticsearch.action.ActionResponse response, StatsRequest parameter);  }
@Test public void testStatsActionWithoutNesting() throws FoxtrotException { StatsRequest request = new StatsRequest(); request.setTable(TestUtils.TEST_TABLE_NAME); request.setField("battery"); StatsResponse statsResponse = StatsResponse.class.cast(getQueryExecutor().execute(request)); assertNotNull(statsResponse); assertNotNull(statsResponse.getResult()); assertEquals(150, statsResponse.getResult() .getStats() .get("sum") .intValue()); assertEquals(5, statsResponse.getResult() .getStats() .get("count") .intValue()); assertNull(statsResponse.getBuckets()); assertNotNull(statsResponse.getResult().getPercentiles()); }
@Test public void testStatsActionWithoutNestingNoPercentile() throws FoxtrotException { StatsRequest request = new StatsRequest(); request.setTable(TestUtils.TEST_TABLE_NAME); request.setField("battery"); request.setFlags(EnumSet.of(AnalyticsRequestFlags.STATS_SKIP_PERCENTILES)); StatsResponse statsResponse = StatsResponse.class.cast(getQueryExecutor().execute(request)); assertNotNull(statsResponse); assertNotNull(statsResponse.getResult()); assertEquals(150, statsResponse.getResult() .getStats() .get("sum") .intValue()); assertEquals(5, statsResponse.getResult() .getStats() .get("count") .intValue()); assertNull(statsResponse.getBuckets()); assertNull(statsResponse.getResult().getPercentiles()); }
@Test public void testStatsActionWithoutNestingNonNumericField() throws FoxtrotException { StatsRequest request = new StatsRequest(); request.setTable(TestUtils.TEST_TABLE_NAME); request.setField("os"); request.setFilters(Collections.singletonList(new EqualsFilter("os", "android"))); StatsResponse statsResponse = StatsResponse.class.cast(getQueryExecutor().execute(request)); assertNotNull(statsResponse); assertNotNull(statsResponse.getResult()); assertEquals(2, statsResponse.getResult() .getStats() .get("count") .intValue()); assertNull(statsResponse.getBuckets()); assertNull(statsResponse.getResult().getPercentiles()); }
@Test public void testStatsActionNoExtendedStat() throws FoxtrotException { StatsRequest request = new StatsRequest(); request.setTable(TestUtils.TEST_TABLE_NAME); request.setField("battery"); request.setStats(EnumSet.allOf(Stat.class) .stream() .filter(x -> !x.isExtended()) .collect(Collectors.toSet())); StatsResponse statsResponse = StatsResponse.class.cast(getQueryExecutor().execute(request)); assertNotNull(statsResponse); assertNotNull(statsResponse.getResult()); assertEquals(5, statsResponse.getResult() .getStats() .size()); }
@Test public void testStatsActionOnlyCountStat() throws FoxtrotException { StatsRequest request = new StatsRequest(); request.setTable(TestUtils.TEST_TABLE_NAME); request.setField("battery"); request.setStats(Collections.singleton(Stat.COUNT)); StatsResponse statsResponse = StatsResponse.class.cast(getQueryExecutor().execute(request)); assertNotNull(statsResponse); assertNotNull(statsResponse.getResult()); assertEquals(1, statsResponse.getResult() .getStats() .size()); assertTrue(statsResponse.getResult() .getStats() .containsKey("count")); }
@Test public void testStatsActionOnlyMaxStat() throws FoxtrotException { StatsRequest request = new StatsRequest(); request.setTable(TestUtils.TEST_TABLE_NAME); request.setField("battery"); request.setStats(Collections.singleton(Stat.MAX)); StatsResponse statsResponse = StatsResponse.class.cast(getQueryExecutor().execute(request)); assertNotNull(statsResponse); assertNotNull(statsResponse.getResult()); assertEquals(1, statsResponse.getResult() .getStats() .size()); assertTrue(statsResponse.getResult() .getStats() .containsKey("max")); }
@Test public void testStatsActionOnlyMinStat() throws FoxtrotException { StatsRequest request = new StatsRequest(); request.setTable(TestUtils.TEST_TABLE_NAME); request.setField("battery"); request.setStats(Collections.singleton(Stat.MIN)); StatsResponse statsResponse = StatsResponse.class.cast(getQueryExecutor().execute(request)); assertNotNull(statsResponse); assertNotNull(statsResponse.getResult()); assertEquals(1, statsResponse.getResult() .getStats() .size()); assertTrue(statsResponse.getResult() .getStats() .containsKey("min")); }
@Test public void testStatsActionOnlyAvgStat() throws FoxtrotException { StatsRequest request = new StatsRequest(); request.setTable(TestUtils.TEST_TABLE_NAME); request.setField("battery"); request.setStats(Collections.singleton(Stat.AVG)); StatsResponse statsResponse = StatsResponse.class.cast(getQueryExecutor().execute(request)); assertNotNull(statsResponse); assertNotNull(statsResponse.getResult()); assertEquals(1, statsResponse.getResult() .getStats() .size()); assertTrue(statsResponse.getResult() .getStats() .containsKey("avg")); }
@Test public void testStatsActionOnlySumStat() throws FoxtrotException { StatsRequest request = new StatsRequest(); request.setTable(TestUtils.TEST_TABLE_NAME); request.setField("battery"); request.setStats(Collections.singleton(Stat.SUM)); StatsResponse statsResponse = StatsResponse.class.cast(getQueryExecutor().execute(request)); assertNotNull(statsResponse); assertNotNull(statsResponse.getResult()); assertTrue(statsResponse.getResult() .getStats() .containsKey("sum")); }
@Test public void testStatsActionOnlyOnePercentile() throws FoxtrotException { StatsRequest request = new StatsRequest(); request.setTable(TestUtils.TEST_TABLE_NAME); request.setField("battery"); request.setPercentiles(Collections.singletonList(5d)); BetweenFilter betweenFilter = new BetweenFilter(); betweenFilter.setFrom(1L); betweenFilter.setTo(System.currentTimeMillis()); betweenFilter.setTemporal(true); betweenFilter.setField("_timestamp"); request.setFilters(Collections.<Filter>singletonList(betweenFilter)); StatsResponse statsResponse = (StatsResponse)getQueryExecutor().execute(request); assertNotNull(statsResponse); assertNotNull(statsResponse.getResult()); assertEquals(1, statsResponse.getResult() .getPercentiles() .size()); assertTrue(statsResponse.getResult() .getPercentiles() .containsKey(5d)); }
@Test public void testStatsActionWithNesting() throws FoxtrotException { StatsRequest request = new StatsRequest(); request.setTable(TestUtils.TEST_TABLE_NAME); request.setField("battery"); request.setNesting(Lists.newArrayList("os")); StatsResponse statsResponse = StatsResponse.class.cast(getQueryExecutor().execute(request)); assertNotNull(statsResponse); assertNotNull(statsResponse.getResult()); assertEquals(3, statsResponse.getBuckets() .size()); for(BucketResponse bucketResponse : statsResponse.getBuckets()) { assertNotNull(bucketResponse.getResult()); } }
@Test public void testStatsActionWithMultiLevelNesting() throws FoxtrotException { StatsRequest request = new StatsRequest(); request.setTable(TestUtils.TEST_TABLE_NAME); request.setField("battery"); request.setNesting(Lists.newArrayList("os", "version")); StatsResponse statsResponse = StatsResponse.class.cast(getQueryExecutor().execute(request)); assertNotNull(statsResponse); assertNotNull(statsResponse.getResult()); assertEquals(3, statsResponse.getBuckets() .size()); for(BucketResponse bucketResponse : statsResponse.getBuckets()) { assertNull(bucketResponse.getResult()); assertNotNull(bucketResponse.getBuckets()); } }
@Test(expected = MalformedQueryException.class) public void testStatsActionNullTable() throws FoxtrotException { StatsRequest request = new StatsRequest(); request.setTable(null); request.setField("battery"); request.setNesting(Lists.newArrayList("os", "version")); getQueryExecutor().execute(request); }
@Test(expected = MalformedQueryException.class) public void testStatsActionNullField() throws FoxtrotException { StatsRequest request = new StatsRequest(); request.setTable(TestUtils.TEST_TABLE_NAME); request.setField(null); request.setNesting(Lists.newArrayList("os", "version")); getQueryExecutor().execute(request); }