StatsTrendAction extends Action<StatsTrendRequest> { @Override public ActionResponse execute(StatsTrendRequest parameter) { SearchRequestBuilder searchRequestBuilder = getRequestBuilder(parameter); try { SearchResponse response = searchRequestBuilder.execute() .actionGet(getGetQueryTimeout()); return getResponse(response, parameter); } catch (ElasticsearchException e) { throw FoxtrotExceptions.createQueryExecutionException(parameter, e); } } StatsTrendAction(StatsTrendRequest parameter, AnalyticsLoader analyticsLoader); @Override void preprocess(); @Override String getMetricKey(); @Override String getRequestCacheKey(); @Override void validateImpl(StatsTrendRequest parameter); @Override ActionResponse execute(StatsTrendRequest parameter); @Override SearchRequestBuilder getRequestBuilder(StatsTrendRequest parameter); @Override ActionResponse getResponse(org.elasticsearch.action.ActionResponse response, StatsTrendRequest parameter);  }
@Test public void testStatsTrendActionWithoutNesting() throws FoxtrotException, JsonProcessingException { StatsTrendRequest request = new StatsTrendRequest(); request.setTable(TestUtils.TEST_TABLE_NAME); request.setTimestamp("_timestamp"); request.setField("battery"); BetweenFilter betweenFilter = new BetweenFilter(); betweenFilter.setFrom(1L); betweenFilter.setTo(System.currentTimeMillis()); betweenFilter.setTemporal(true); betweenFilter.setField("_timestamp"); request.setFilters(Collections.<Filter>singletonList(betweenFilter)); StatsTrendResponse statsTrendResponse = StatsTrendResponse.class.cast(getQueryExecutor().execute(request)); filterNonZeroCounts(statsTrendResponse); assertNotNull(statsTrendResponse); assertNotNull(statsTrendResponse.getResult()); assertEquals(5, statsTrendResponse.getResult() .size()); assertEquals(8, statsTrendResponse.getResult() .get(0) .getStats() .size()); assertEquals(7, statsTrendResponse.getResult() .get(0) .getPercentiles() .size()); assertNull(statsTrendResponse.getBuckets()); }
@Test public void testStatsTrendActionWithoutNestingSkipPercentile() throws FoxtrotException, JsonProcessingException { StatsTrendRequest request = new StatsTrendRequest(); request.setTable(TestUtils.TEST_TABLE_NAME); request.setTimestamp("_timestamp"); request.setField("battery"); request.setFlags(Collections.singleton(AnalyticsRequestFlags.STATS_SKIP_PERCENTILES)); BetweenFilter betweenFilter = new BetweenFilter(); betweenFilter.setFrom(1L); betweenFilter.setTo(System.currentTimeMillis()); betweenFilter.setTemporal(true); betweenFilter.setField("_timestamp"); request.setFilters(Collections.singletonList(betweenFilter)); StatsTrendResponse statsTrendResponse = StatsTrendResponse.class.cast(getQueryExecutor().execute(request)); filterNonZeroCounts(statsTrendResponse); assertNotNull(statsTrendResponse); assertNotNull(statsTrendResponse.getResult()); assertEquals(5, statsTrendResponse.getResult() .size()); assertEquals(8, statsTrendResponse.getResult() .get(0) .getStats() .size()); assertNull(statsTrendResponse.getResult() .get(0) .getPercentiles()); assertNull(statsTrendResponse.getBuckets()); }
@Test public void testStatsTrendActionNoExtendedStat() throws FoxtrotException, JsonProcessingException { StatsTrendRequest request = new StatsTrendRequest(); request.setTable(TestUtils.TEST_TABLE_NAME); request.setTimestamp("_timestamp"); request.setField("battery"); request.setStats(EnumSet.allOf(Stat.class) .stream() .filter(x -> !x.isExtended()) .collect(Collectors.toSet())); BetweenFilter betweenFilter = new BetweenFilter(); betweenFilter.setFrom(1L); betweenFilter.setTo(System.currentTimeMillis()); betweenFilter.setTemporal(true); betweenFilter.setField("_timestamp"); request.setFilters(Collections.<Filter>singletonList(betweenFilter)); StatsTrendResponse statsTrendResponse = StatsTrendResponse.class.cast(getQueryExecutor().execute(request)); filterNonZeroCounts(statsTrendResponse); assertNotNull(statsTrendResponse); assertNotNull(statsTrendResponse.getResult()); assertEquals(5, statsTrendResponse.getResult() .size()); assertEquals(5, statsTrendResponse.getResult() .get(0) .getStats() .size()); assertEquals(7, statsTrendResponse.getResult() .get(0) .getPercentiles() .size()); assertNull(statsTrendResponse.getBuckets()); }
@Test public void testStatsTrendActionOnlyCountStat() throws FoxtrotException { StatsTrendRequest request = new StatsTrendRequest(); request.setTable(TestUtils.TEST_TABLE_NAME); request.setTimestamp("_timestamp"); request.setField("battery"); request.setStats(Collections.singleton(Stat.COUNT)); BetweenFilter betweenFilter = new BetweenFilter(); betweenFilter.setFrom(1L); betweenFilter.setTo(System.currentTimeMillis()); betweenFilter.setTemporal(true); betweenFilter.setField("_timestamp"); request.setFilters(Collections.<Filter>singletonList(betweenFilter)); StatsTrendResponse statsTrendResponse = StatsTrendResponse.class.cast(getQueryExecutor().execute(request)); filterNonZeroCounts(statsTrendResponse); assertNotNull(statsTrendResponse); assertNotNull(statsTrendResponse.getResult()); assertEquals(1, statsTrendResponse.getResult() .get(0) .getStats() .size()); assertTrue(statsTrendResponse.getResult() .get(0) .getStats() .containsKey("count")); assertNull(statsTrendResponse.getBuckets()); }
@Test public void testStatsTrendActionOnlyMaxStat() throws FoxtrotException { StatsTrendRequest request = new StatsTrendRequest(); request.setTable(TestUtils.TEST_TABLE_NAME); request.setTimestamp("_timestamp"); request.setField("battery"); request.setStats(Collections.singleton(Stat.MAX)); BetweenFilter betweenFilter = new BetweenFilter(); betweenFilter.setFrom(1L); betweenFilter.setTo(System.currentTimeMillis()); betweenFilter.setTemporal(true); betweenFilter.setField("_timestamp"); request.setFilters(Collections.<Filter>singletonList(betweenFilter)); StatsTrendResponse statsTrendResponse = StatsTrendResponse.class.cast(getQueryExecutor().execute(request)); filterNonZeroCounts(statsTrendResponse); assertNotNull(statsTrendResponse); assertNotNull(statsTrendResponse.getResult()); assertEquals(1, statsTrendResponse.getResult() .get(0) .getStats() .size()); assertTrue(statsTrendResponse.getResult() .get(0) .getStats() .containsKey("max")); assertNull(statsTrendResponse.getBuckets()); }
@Test public void testStatsTrendActionOnlyMinStat() throws FoxtrotException { StatsTrendRequest request = new StatsTrendRequest(); request.setTable(TestUtils.TEST_TABLE_NAME); request.setTimestamp("_timestamp"); request.setField("battery"); request.setStats(Collections.singleton(Stat.MIN)); BetweenFilter betweenFilter = new BetweenFilter(); betweenFilter.setFrom(1L); betweenFilter.setTo(System.currentTimeMillis()); betweenFilter.setTemporal(true); betweenFilter.setField("_timestamp"); request.setFilters(Collections.<Filter>singletonList(betweenFilter)); StatsTrendResponse statsTrendResponse = StatsTrendResponse.class.cast(getQueryExecutor().execute(request)); filterNonZeroCounts(statsTrendResponse); assertNotNull(statsTrendResponse); assertNotNull(statsTrendResponse.getResult()); assertEquals(1, statsTrendResponse.getResult() .get(0) .getStats() .size()); assertTrue(statsTrendResponse.getResult() .get(0) .getStats() .containsKey("min")); assertNull(statsTrendResponse.getBuckets()); }
@Test public void testStatsTrendActionOnlyAvgStat() throws FoxtrotException { StatsTrendRequest request = new StatsTrendRequest(); request.setTable(TestUtils.TEST_TABLE_NAME); request.setTimestamp("_timestamp"); request.setField("battery"); request.setStats(Collections.singleton(Stat.AVG)); BetweenFilter betweenFilter = new BetweenFilter(); betweenFilter.setFrom(1L); betweenFilter.setTo(System.currentTimeMillis()); betweenFilter.setTemporal(true); betweenFilter.setField("_timestamp"); request.setFilters(Collections.<Filter>singletonList(betweenFilter)); StatsTrendResponse statsTrendResponse = StatsTrendResponse.class.cast(getQueryExecutor().execute(request)); filterNonZeroCounts(statsTrendResponse); assertNotNull(statsTrendResponse); assertNotNull(statsTrendResponse.getResult()); assertEquals(1, statsTrendResponse.getResult() .get(0) .getStats() .size()); assertTrue(statsTrendResponse.getResult() .get(0) .getStats() .containsKey("avg")); assertNull(statsTrendResponse.getBuckets()); }
@Test public void testStatsTrendActionOnlySumStat() throws FoxtrotException, JsonProcessingException { StatsTrendRequest request = new StatsTrendRequest(); request.setTable(TestUtils.TEST_TABLE_NAME); request.setTimestamp("_timestamp"); request.setField("battery"); request.setStats(Collections.singleton(Stat.SUM)); BetweenFilter betweenFilter = new BetweenFilter(); betweenFilter.setFrom(1L); betweenFilter.setTo(System.currentTimeMillis()); betweenFilter.setTemporal(true); betweenFilter.setField("_timestamp"); request.setFilters(Collections.<Filter>singletonList(betweenFilter)); StatsTrendResponse statsTrendResponse = StatsTrendResponse.class.cast(getQueryExecutor().execute(request)); filterNonZeroCounts(statsTrendResponse); assertNotNull(statsTrendResponse); assertNotNull(statsTrendResponse.getResult()); assertEquals(1, statsTrendResponse.getResult() .get(0) .getStats() .size()); assertTrue(statsTrendResponse.getResult() .get(0) .getStats() .containsKey("sum")); assertNull(statsTrendResponse.getBuckets()); }
@Test public void testStatsTrendActionOnlyOnePercentile() throws FoxtrotException { StatsTrendRequest request = new StatsTrendRequest(); request.setTable(TestUtils.TEST_TABLE_NAME); request.setTimestamp("_timestamp"); 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)); StatsTrendResponse statsTrendResponse = StatsTrendResponse.class.cast(getQueryExecutor().execute(request)); filterNonZeroCounts(statsTrendResponse); assertNotNull(statsTrendResponse); assertNotNull(statsTrendResponse.getResult()); assertEquals(1, statsTrendResponse.getResult() .get(0) .getPercentiles() .size()); assertTrue(statsTrendResponse.getResult() .get(0) .getPercentiles() .containsKey(5d)); }
@Test public void testStatsTrendActionWithNesting() throws FoxtrotException { StatsTrendRequest request = new StatsTrendRequest(); request.setTable(TestUtils.TEST_TABLE_NAME); request.setTimestamp("_timestamp"); request.setField("battery"); request.setNesting(Lists.newArrayList("os")); BetweenFilter betweenFilter = new BetweenFilter(); betweenFilter.setFrom(1L); betweenFilter.setTo(System.currentTimeMillis()); betweenFilter.setTemporal(true); betweenFilter.setField("_timestamp"); request.setFilters(Collections.<Filter>singletonList(betweenFilter)); StatsTrendResponse statsTrendResponse = StatsTrendResponse.class.cast(getQueryExecutor().execute(request)); assertNotNull(statsTrendResponse); assertNull(statsTrendResponse.getResult()); assertNotNull(statsTrendResponse.getBuckets()); assertEquals(3, statsTrendResponse.getBuckets() .size()); }
@Test public void testStatsTrendActionWithMultiLevelNesting() throws FoxtrotException, JsonProcessingException { StatsTrendRequest request = new StatsTrendRequest(); request.setTable(TestUtils.TEST_TABLE_NAME); request.setTimestamp("_timestamp"); request.setField("battery"); request.setNesting(Lists.newArrayList("os", "version")); BetweenFilter betweenFilter = new BetweenFilter(); betweenFilter.setFrom(1L); betweenFilter.setTo(System.currentTimeMillis()); betweenFilter.setTemporal(true); betweenFilter.setField("_timestamp"); request.setFilters(Collections.<Filter>singletonList(betweenFilter)); StatsTrendResponse statsTrendResponse = StatsTrendResponse.class.cast(getQueryExecutor().execute(request)); assertNotNull(statsTrendResponse); assertNull(statsTrendResponse.getResult()); assertNotNull(statsTrendResponse.getBuckets()); assertEquals(3, statsTrendResponse.getBuckets() .size()); assertEquals(1, statsTrendResponse.getBuckets() .get(0) .getBuckets() .size()); assertEquals(2, statsTrendResponse.getBuckets() .get(1) .getBuckets() .size()); assertEquals(1, statsTrendResponse.getBuckets() .get(2) .getBuckets() .size()); assertNotNull(statsTrendResponse.getBuckets() .get(0) .getBuckets() .get(0) .getResult() .get(0) .getPercentiles()); }
@Test public void testStatsTrendActionWithMultiLevelNestingSkipPercentile() throws FoxtrotException, JsonProcessingException { StatsTrendRequest request = new StatsTrendRequest(); request.setTable(TestUtils.TEST_TABLE_NAME); request.setTimestamp("_timestamp"); request.setField("battery"); request.setNesting(Lists.newArrayList("os", "version")); request.setFlags(Collections.singleton(AnalyticsRequestFlags.STATS_SKIP_PERCENTILES)); BetweenFilter betweenFilter = new BetweenFilter(); betweenFilter.setFrom(1L); betweenFilter.setTo(System.currentTimeMillis()); betweenFilter.setTemporal(true); betweenFilter.setField("_timestamp"); request.setFilters(Collections.<Filter>singletonList(betweenFilter)); StatsTrendResponse statsTrendResponse = StatsTrendResponse.class.cast(getQueryExecutor().execute(request)); try { System.out.println(Jackson.newObjectMapper() .writerWithDefaultPrettyPrinter() .writeValueAsString(statsTrendResponse)); } catch (JsonProcessingException e) { e.printStackTrace(); } assertNotNull(statsTrendResponse); assertNull(statsTrendResponse.getResult()); assertNotNull(statsTrendResponse.getBuckets()); assertEquals(3, statsTrendResponse.getBuckets() .size()); assertEquals(1, statsTrendResponse.getBuckets() .get(0) .getBuckets() .size()); assertEquals(2, statsTrendResponse.getBuckets() .get(1) .getBuckets() .size()); assertEquals(1, statsTrendResponse.getBuckets() .get(2) .getBuckets() .size()); assertNull(statsTrendResponse.getBuckets() .get(0) .getBuckets() .get(0) .getResult() .get(0) .getPercentiles()); }
@Test public void testStatsTrendActionWithNoFilter() throws FoxtrotException, JsonProcessingException { StatsTrendRequest request = new StatsTrendRequest(); request.setTable(TestUtils.TEST_TABLE_NAME); request.setTimestamp("_timestamp"); request.setField("battery"); request.setNesting(Lists.newArrayList("os", "version")); StatsTrendResponse statsTrendResponse = StatsTrendResponse.class.cast(getQueryExecutor().execute(request)); assertNull(statsTrendResponse); }
@Test(expected = MalformedQueryException.class) public void testStatsTrendActionNullTable() throws FoxtrotException, JsonProcessingException { StatsTrendRequest request = new StatsTrendRequest(); request.setTable(null); request.setTimestamp("_timestamp"); request.setField("battery"); request.setNesting(Lists.newArrayList("os", "version")); getQueryExecutor().execute(request); }
@Test(expected = MalformedQueryException.class) public void testStatsTrendActionNullField() throws FoxtrotException, JsonProcessingException { StatsTrendRequest request = new StatsTrendRequest(); request.setTable(TestUtils.TEST_TABLE_NAME); request.setTimestamp("_timestamp"); request.setField(null); request.setNesting(Lists.newArrayList("os", "version")); getQueryExecutor().execute(request); }
@Test public void testStatsTrendActionTextField() throws FoxtrotException { StatsTrendRequest request = new StatsTrendRequest(); request.setTable(TestUtils.TEST_TABLE_NAME); request.setTimestamp("_timestamp"); request.setField("os"); request.setStats(Collections.singleton(Stat.AVG)); BetweenFilter betweenFilter = new BetweenFilter(); betweenFilter.setFrom(1L); betweenFilter.setTo(System.currentTimeMillis()); betweenFilter.setTemporal(true); betweenFilter.setField("_timestamp"); request.setFilters(Collections.<Filter>singletonList(betweenFilter)); StatsTrendResponse statsTrendResponse = StatsTrendResponse.class.cast(getQueryExecutor().execute(request)); filterNonZeroCounts(statsTrendResponse); assertNotNull(statsTrendResponse); assertNotNull(statsTrendResponse.getResult()); assertEquals(1, statsTrendResponse.getResult() .get(0) .getStats() .size()); assertTrue(statsTrendResponse.getResult() .get(0) .getStats() .containsKey("count")); assertNull(statsTrendResponse.getBuckets()); }
@Test public void testStatsTrendActionTextFieldNested() throws FoxtrotException { StatsTrendRequest request = new StatsTrendRequest(); request.setTable(TestUtils.TEST_TABLE_NAME); request.setTimestamp("_timestamp"); request.setField("os"); request.setNesting(ImmutableList.of("os", "device")); request.setStats(Collections.singleton(Stat.AVG)); BetweenFilter betweenFilter = new BetweenFilter(); betweenFilter.setFrom(1L); betweenFilter.setTo(System.currentTimeMillis()); betweenFilter.setTemporal(true); betweenFilter.setField("_timestamp"); request.setFilters(Collections.singletonList(betweenFilter)); StatsTrendResponse statsTrendResponse = StatsTrendResponse.class.cast(getQueryExecutor().execute(request)); try { System.out.println(Jackson.newObjectMapper() .writerWithDefaultPrettyPrinter() .writeValueAsString(statsTrendResponse)); } catch (JsonProcessingException e) { e.printStackTrace(); } assertNull(statsTrendResponse.getResult()); assertNotNull(statsTrendResponse.getBuckets()); Assert.assertNull(statsTrendResponse.getBuckets().get(0).getResult()); Assert.assertNotNull(statsTrendResponse.getBuckets().get(0).getBuckets()); Assert.assertEquals(1L, statsTrendResponse.getBuckets().get(0).getBuckets().get(0).getResult().get(0).getStats().get("count")); Assert.assertEquals(0L, statsTrendResponse.getBuckets().get(0).getBuckets().get(0).getResult().get(2).getStats().get("count")); Assert.assertEquals(0L, statsTrendResponse.getBuckets().get(0).getBuckets().get(0).getResult().get(12).getStats().get("count")); Assert.assertEquals(1L, statsTrendResponse.getBuckets().get(0).getBuckets().get(0).getResult().get(13).getStats().get("count")); Assert.assertEquals(1L, statsTrendResponse.getBuckets().get(1).getBuckets().get(0).getResult().get(0).getStats().get("count")); Assert.assertEquals(1L, statsTrendResponse.getBuckets().get(1).getBuckets().get(1).getResult().get(0).getStats().get("count")); Assert.assertEquals(1L, statsTrendResponse.getBuckets().get(2).getBuckets().get(0).getResult().get(0).getStats().get("count")); }