HistogramAction extends Action<HistogramRequest> { @Override public ActionResponse execute(HistogramRequest parameter) throws QueryStoreException { parameter.setTable(ElasticsearchUtils.getValidTableName(parameter.getTable())); if (null == parameter.getFilters()) { parameter.setFilters(Lists.<Filter>newArrayList(new AnyFilter(parameter.getTable()))); } if (parameter.getField() == null || parameter.getField().trim().isEmpty()) { throw new QueryStoreException(QueryStoreException.ErrorCode.INVALID_REQUEST, "Illegal Nesting Parameters"); } try { DateHistogram.Interval interval = null; switch (parameter.getPeriod()) { case seconds: interval = DateHistogram.Interval.SECOND; break; case minutes: interval = DateHistogram.Interval.MINUTE; break; case hours: interval = DateHistogram.Interval.HOUR; break; case days: interval = DateHistogram.Interval.DAY; break; } String dateHistogramKey = Utils.sanitizeFieldForAggregation(parameter.getField()); SearchResponse response = getConnection().getClient().prepareSearch( ElasticsearchUtils.getIndices(parameter.getTable(), parameter)) .setTypes(ElasticsearchUtils.TYPE_NAME) .setQuery(new ElasticSearchQueryGenerator(FilterCombinerType.and) .genFilter(parameter.getFilters())) .setSize(0) .setSearchType(SearchType.COUNT) .addAggregation(AggregationBuilders.dateHistogram(dateHistogramKey) .field(parameter.getField()) .interval(interval)) .execute() .actionGet(); Aggregations aggregations = response.getAggregations(); if (aggregations == null) { logger.error("Null response for Histogram. Request : " + parameter.toString()); return new HistogramResponse(Collections.<HistogramResponse.Count>emptyList()); } DateHistogram dateHistogram = aggregations.get(dateHistogramKey); Collection<? extends DateHistogram.Bucket> buckets = dateHistogram.getBuckets(); List<HistogramResponse.Count> counts = new ArrayList<HistogramResponse.Count>(buckets.size()); for (DateHistogram.Bucket bucket : buckets) { HistogramResponse.Count count = new HistogramResponse.Count( bucket.getKeyAsNumber(), bucket.getDocCount()); counts.add(count); } return new HistogramResponse(counts); } catch (QueryStoreException ex) { throw ex; } catch (Exception e) { throw new QueryStoreException(QueryStoreException.ErrorCode.HISTOGRAM_GENERATION_ERROR, "Malformed query", e); } } HistogramAction(HistogramRequest parameter,
                           TableMetadataManager tableMetadataManager,
                           DataStore dataStore,
                           QueryStore queryStore,
                           ElasticsearchConnection connection,
                           String cacheToken); @Override ActionResponse execute(HistogramRequest parameter);  }
@Test(expected = QueryStoreException.class) public void testHistogramActionAnyException() throws QueryStoreException, JsonProcessingException { HistogramRequest histogramRequest = new HistogramRequest(); histogramRequest.setTable(TestUtils.TEST_TABLE_NAME); histogramRequest.setPeriod(Period.minutes); when(elasticsearchServer.getClient()).thenReturn(null); queryExecutor.execute(histogramRequest); }
@Test public void testHistogramActionIntervalMinuteNoFilter() throws QueryStoreException, JsonProcessingException { HistogramRequest histogramRequest = new HistogramRequest(); histogramRequest.setTable(TestUtils.TEST_TABLE_NAME); histogramRequest.setPeriod(Period.minutes); LessThanFilter lessThanFilter = new LessThanFilter(); lessThanFilter.setTemporal(true); lessThanFilter.setField("_timestamp"); lessThanFilter.setValue(System.currentTimeMillis()); histogramRequest.setFilters(Lists.<Filter>newArrayList(lessThanFilter)); HistogramResponse response = HistogramResponse.class.cast(queryExecutor.execute(histogramRequest)); List<HistogramResponse.Count> counts = new ArrayList<HistogramResponse.Count>(); counts.add(new HistogramResponse.Count(1397651100000L, 2)); counts.add(new HistogramResponse.Count(1397658060000L, 3)); counts.add(new HistogramResponse.Count(1397658180000L, 1)); counts.add(new HistogramResponse.Count(1397758200000L, 1)); counts.add(new HistogramResponse.Count(1397958060000L, 1)); counts.add(new HistogramResponse.Count(1398653100000L, 2)); counts.add(new HistogramResponse.Count(1398658200000L, 1)); assertTrue(response.getCounts().equals(counts)); }
@Test public void testHistogramActionIntervalMinuteWithFilter() throws QueryStoreException, JsonProcessingException { HistogramRequest histogramRequest = new HistogramRequest(); histogramRequest.setTable(TestUtils.TEST_TABLE_NAME); histogramRequest.setPeriod(Period.minutes); GreaterThanFilter greaterThanFilter = new GreaterThanFilter(); greaterThanFilter.setField("battery"); greaterThanFilter.setValue(48); LessThanFilter lessThanFilter = new LessThanFilter(); lessThanFilter.setTemporal(true); lessThanFilter.setField("_timestamp"); lessThanFilter.setValue(System.currentTimeMillis()); histogramRequest.setFilters(Lists.<Filter>newArrayList(greaterThanFilter, lessThanFilter)); HistogramResponse response = HistogramResponse.class.cast(queryExecutor.execute(histogramRequest)); List<HistogramResponse.Count> counts = new ArrayList<HistogramResponse.Count>(); counts.add(new HistogramResponse.Count(1397651100000L, 1)); counts.add(new HistogramResponse.Count(1397658060000L, 2)); counts.add(new HistogramResponse.Count(1397658180000L, 1)); counts.add(new HistogramResponse.Count(1397958060000L, 1)); counts.add(new HistogramResponse.Count(1398658200000L, 1)); assertTrue(response.getCounts().equals(counts)); }
@Test public void testHistogramActionIntervalHourNoFilter() throws QueryStoreException, JsonProcessingException { HistogramRequest histogramRequest = new HistogramRequest(); histogramRequest.setTable(TestUtils.TEST_TABLE_NAME); histogramRequest.setPeriod(Period.hours); LessThanFilter lessThanFilter = new LessThanFilter(); lessThanFilter.setTemporal(true); lessThanFilter.setField("_timestamp"); lessThanFilter.setValue(System.currentTimeMillis()); histogramRequest.setFilters(Lists.<Filter>newArrayList(lessThanFilter)); HistogramResponse response = HistogramResponse.class.cast(queryExecutor.execute(histogramRequest)); List<HistogramResponse.Count> counts = new ArrayList<HistogramResponse.Count>(); counts.add(new HistogramResponse.Count(1397649600000L, 2)); counts.add(new HistogramResponse.Count(1397656800000L, 4)); counts.add(new HistogramResponse.Count(1397757600000L, 1)); counts.add(new HistogramResponse.Count(1397955600000L, 1)); counts.add(new HistogramResponse.Count(1398650400000L, 2)); counts.add(new HistogramResponse.Count(1398657600000L, 1)); assertTrue(response.getCounts().equals(counts)); }
@Test public void testHistogramActionIntervalHourWithFilter() throws QueryStoreException, JsonProcessingException { HistogramRequest histogramRequest = new HistogramRequest(); histogramRequest.setTable(TestUtils.TEST_TABLE_NAME); histogramRequest.setPeriod(Period.hours); GreaterThanFilter greaterThanFilter = new GreaterThanFilter(); greaterThanFilter.setField("battery"); greaterThanFilter.setValue(48); LessThanFilter lessThanFilter = new LessThanFilter(); lessThanFilter.setTemporal(true); lessThanFilter.setField("_timestamp"); lessThanFilter.setValue(System.currentTimeMillis()); histogramRequest.setFilters(Lists.<Filter>newArrayList(greaterThanFilter, lessThanFilter)); HistogramResponse response = HistogramResponse.class.cast(queryExecutor.execute(histogramRequest)); List<HistogramResponse.Count> counts = new ArrayList<HistogramResponse.Count>(); counts.add(new HistogramResponse.Count(1397649600000L, 1)); counts.add(new HistogramResponse.Count(1397656800000L, 3)); counts.add(new HistogramResponse.Count(1397955600000L, 1)); counts.add(new HistogramResponse.Count(1398657600000L, 1)); assertTrue(response.getCounts().equals(counts)); }
@Test public void testHistogramActionIntervalDayNoFilter() throws QueryStoreException, JsonProcessingException { HistogramRequest histogramRequest = new HistogramRequest(); histogramRequest.setTable(TestUtils.TEST_TABLE_NAME); histogramRequest.setPeriod(Period.days); LessThanFilter lessThanFilter = new LessThanFilter(); lessThanFilter.setTemporal(true); lessThanFilter.setField("_timestamp"); lessThanFilter.setValue(System.currentTimeMillis()); histogramRequest.setFilters(Lists.<Filter>newArrayList(lessThanFilter)); HistogramResponse response = HistogramResponse.class.cast(queryExecutor.execute(histogramRequest)); List<HistogramResponse.Count> counts = new ArrayList<HistogramResponse.Count>(); counts.add(new HistogramResponse.Count(1397606400000L, 6)); counts.add(new HistogramResponse.Count(1397692800000L, 1)); counts.add(new HistogramResponse.Count(1397952000000L, 1)); counts.add(new HistogramResponse.Count(1398643200000L, 3)); assertTrue(response.getCounts().equals(counts)); }
@Test public void testHistogramActionIntervalDayWithFilter() throws QueryStoreException, JsonProcessingException { HistogramRequest histogramRequest = new HistogramRequest(); histogramRequest.setTable(TestUtils.TEST_TABLE_NAME); histogramRequest.setPeriod(Period.days); GreaterThanFilter greaterThanFilter = new GreaterThanFilter(); greaterThanFilter.setField("battery"); greaterThanFilter.setValue(48); LessThanFilter lessThanFilter = new LessThanFilter(); lessThanFilter.setTemporal(true); lessThanFilter.setField("_timestamp"); lessThanFilter.setValue(System.currentTimeMillis()); histogramRequest.setFilters(Lists.<Filter>newArrayList(greaterThanFilter, lessThanFilter)); HistogramResponse response = HistogramResponse.class.cast(queryExecutor.execute(histogramRequest)); List<HistogramResponse.Count> counts = new ArrayList<HistogramResponse.Count>(); counts.add(new HistogramResponse.Count(1397606400000L, 4)); counts.add(new HistogramResponse.Count(1397952000000L, 1)); counts.add(new HistogramResponse.Count(1398643200000L, 1)); assertTrue(response.getCounts().equals(counts)); }