TrendAction extends Action<TrendRequest> { @Override public ActionResponse execute(TrendRequest parameter) throws QueryStoreException { parameter.setTable(ElasticsearchUtils.getValidTableName(parameter.getTable())); if (null == parameter.getFilters()) { parameter.setFilters(Lists.<Filter>newArrayList(new AnyFilter(parameter.getTable()))); } String field = parameter.getField(); if (parameter.getTable() == null) { throw new QueryStoreException(QueryStoreException.ErrorCode.INVALID_REQUEST, "Invalid table name"); } if (null == field || field.isEmpty()) { throw new QueryStoreException(QueryStoreException.ErrorCode.INVALID_REQUEST, "Invalid field name"); } if (null != parameter.getValues() && parameter.getValues().size() != 0) { List<Object> values = (List) parameter.getValues(); Filter filter = new InFilter(field, values); parameter.getFilters().add(filter); } try { AbstractAggregationBuilder aggregationBuilder = buildAggregation(parameter); SearchResponse searchResponse = getConnection().getClient() .prepareSearch(ElasticsearchUtils.getIndices(parameter.getTable(), parameter)) .setQuery(new ElasticSearchQueryGenerator(FilterCombinerType.and).genFilter(parameter.getFilters())) .setSearchType(SearchType.COUNT) .addAggregation(aggregationBuilder) .execute() .get(); Aggregations aggregations = searchResponse.getAggregations(); if (aggregations != null) { return buildResponse(parameter, aggregations); } else { logger.error("Null response for Trend. Request : " + parameter.toString()); return new TrendResponse(Collections.<String, List<TrendResponse.Count>>emptyMap()); } } catch (Exception e) { logger.error("Error running trend action: ", e); throw new QueryStoreException(QueryStoreException.ErrorCode.QUERY_EXECUTION_ERROR, "Error running trend action.", e); } } TrendAction(TrendRequest parameter,
                       TableMetadataManager tableMetadataManager,
                       DataStore dataStore,
                       QueryStore queryStore,
                       ElasticsearchConnection connection,
                       String cacheToken); @Override ActionResponse execute(TrendRequest parameter);  }
@Test(expected = QueryStoreException.class) public void testTrendActionAnyException() throws QueryStoreException, JsonProcessingException { TrendRequest trendRequest = new TrendRequest(); trendRequest.setTable(null); BetweenFilter betweenFilter = new BetweenFilter(); betweenFilter.setFrom(1L); betweenFilter.setTo(System.currentTimeMillis()); betweenFilter.setTemporal(true); betweenFilter.setField("_timestamp"); trendRequest.setField("os"); trendRequest.setFilters(Collections.<Filter>singletonList(betweenFilter)); when(elasticsearchServer.getClient()).thenReturn(null); queryExecutor.execute(trendRequest); }
@Test public void testTrendActionNullField() throws QueryStoreException, JsonProcessingException { TrendRequest trendRequest = new TrendRequest(); trendRequest.setTable(TestUtils.TEST_TABLE_NAME); BetweenFilter betweenFilter = new BetweenFilter(); betweenFilter.setFrom(1L); betweenFilter.setTo(System.currentTimeMillis()); betweenFilter.setTemporal(true); betweenFilter.setField("_timestamp"); trendRequest.setFilters(Collections.<Filter>singletonList(betweenFilter)); trendRequest.setField(null); try { queryExecutor.execute(trendRequest); } catch (Exception e) { assertEquals("Invalid field name", e.getMessage()); return; } fail("Should have thrown exception"); }
@Test public void testTrendActionFieldAll() throws QueryStoreException, JsonProcessingException { TrendRequest trendRequest = new TrendRequest(); trendRequest.setTable(TestUtils.TEST_TABLE_NAME); BetweenFilter betweenFilter = new BetweenFilter(); betweenFilter.setFrom(1L); betweenFilter.setTo(System.currentTimeMillis()); betweenFilter.setTemporal(true); betweenFilter.setField("_timestamp"); trendRequest.setFilters(Collections.<Filter>singletonList(betweenFilter)); trendRequest.setField("all"); trendRequest.setValues(Collections.<String>emptyList()); TrendResponse expectedResponse = new TrendResponse(); expectedResponse.setTrends(new HashMap<String, List<TrendResponse.Count>>()); TrendResponse actualResponse = TrendResponse.class.cast(queryExecutor.execute(trendRequest)); assertEquals(expectedResponse, actualResponse); }
@Test public void testTrendActionFieldWithDot() throws QueryStoreException, JsonProcessingException { Document document = TestUtils.getDocument("G", 1398653118006L, new Object[]{"data.version", 1}, mapper); queryStore.save(TestUtils.TEST_TABLE_NAME, document); elasticsearchServer.getClient().admin().indices() .prepareRefresh(ElasticsearchUtils.getCurrentIndex(TestUtils.TEST_TABLE_NAME, document.getTimestamp())) .setForce(true) .execute() .actionGet(); TrendRequest trendRequest = new TrendRequest(); trendRequest.setTable(TestUtils.TEST_TABLE_NAME); BetweenFilter betweenFilter = new BetweenFilter(); betweenFilter.setFrom(1L); betweenFilter.setTo(System.currentTimeMillis()); betweenFilter.setTemporal(true); betweenFilter.setField("_timestamp"); trendRequest.setFilters(Collections.<Filter>singletonList(betweenFilter)); trendRequest.setField("data.version"); trendRequest.setValues(Collections.<String>emptyList()); TrendResponse expectedResponse = new TrendResponse(); TrendResponse.Count count = new TrendResponse.Count(); count.setPeriod(1398643200000L); count.setCount(1); expectedResponse.setTrends(Collections.<String, List<TrendResponse.Count>>singletonMap("1", Arrays.asList(count))); TrendResponse actualResponse = TrendResponse.class.cast(queryExecutor.execute(trendRequest)); assertEquals(expectedResponse, actualResponse); }
@Test public void testTrendActionEmptyField() throws QueryStoreException, JsonProcessingException { TrendRequest trendRequest = new TrendRequest(); trendRequest.setTable(TestUtils.TEST_TABLE_NAME); BetweenFilter betweenFilter = new BetweenFilter(); betweenFilter.setFrom(1L); betweenFilter.setTo(System.currentTimeMillis()); betweenFilter.setTemporal(true); betweenFilter.setField("_timestamp"); trendRequest.setFilters(Collections.<Filter>singletonList(betweenFilter)); trendRequest.setField(""); trendRequest.setValues(Collections.<String>emptyList()); try { queryExecutor.execute(trendRequest); fail(); } catch (QueryStoreException ex) { assertEquals(QueryStoreException.ErrorCode.INVALID_REQUEST, ex.getErrorCode()); } }
@Test public void testTrendActionFieldWithSpecialCharacters() throws QueryStoreException, JsonProcessingException { TrendRequest trendRequest = new TrendRequest(); trendRequest.setTable(TestUtils.TEST_TABLE_NAME); BetweenFilter betweenFilter = new BetweenFilter(); betweenFilter.setFrom(1L); betweenFilter.setTo(System.currentTimeMillis()); betweenFilter.setTemporal(true); betweenFilter.setField("_timestamp"); trendRequest.setFilters(Collections.<Filter>singletonList(betweenFilter)); trendRequest.setField("!@!41242$"); trendRequest.setValues(Collections.<String>emptyList()); TrendResponse expectedResponse = new TrendResponse(); expectedResponse.setTrends(new HashMap<String, List<TrendResponse.Count>>()); TrendResponse actualResponse = TrendResponse.class.cast(queryExecutor.execute(trendRequest)); assertEquals(expectedResponse, actualResponse); }
@Test public void testTrendActionNullTable() throws QueryStoreException, JsonProcessingException { TrendRequest trendRequest = new TrendRequest(); trendRequest.setTable(null); BetweenFilter betweenFilter = new BetweenFilter(); betweenFilter.setFrom(1L); betweenFilter.setTo(System.currentTimeMillis()); betweenFilter.setTemporal(true); betweenFilter.setField("_timestamp"); trendRequest.setField("os"); trendRequest.setFilters(Collections.<Filter>singletonList(betweenFilter)); try { queryExecutor.execute(trendRequest); fail(); } catch (QueryStoreException ex) { assertEquals(QueryStoreException.ErrorCode.INVALID_REQUEST, ex.getErrorCode()); } }
@Test public void testTrendActionWithField() throws QueryStoreException, JsonProcessingException { TrendRequest trendRequest = new TrendRequest(); trendRequest.setTable(TestUtils.TEST_TABLE_NAME); BetweenFilter betweenFilter = new BetweenFilter(); betweenFilter.setFrom(1L); betweenFilter.setTo(System.currentTimeMillis()); betweenFilter.setTemporal(true); betweenFilter.setField("_timestamp"); trendRequest.setField("os"); trendRequest.setFilters(Collections.<Filter>singletonList(betweenFilter)); TrendResponse expectedResponse = new TrendResponse(); Map<String, List<TrendResponse.Count>> trends = new HashMap<String, List<TrendResponse.Count>>(); List<TrendResponse.Count> counts = Lists.newArrayList(); counts.add(new TrendResponse.Count(1397606400000L, 6)); counts.add(new TrendResponse.Count(1398643200000L, 1)); trends.put("android", counts); counts = Lists.newArrayList(); counts.add(new TrendResponse.Count(1397692800000L, 1)); counts.add(new TrendResponse.Count(1397952000000L, 1)); counts.add(new TrendResponse.Count(1398643200000L, 2)); trends.put("ios", counts); expectedResponse.setTrends(trends); TrendResponse actualResponse = TrendResponse.class.cast(queryExecutor.execute(trendRequest)); assertEquals(expectedResponse, actualResponse); }
@Test public void testTrendActionWithFieldZeroTo() throws QueryStoreException, JsonProcessingException { TrendRequest trendRequest = new TrendRequest(); trendRequest.setTable(TestUtils.TEST_TABLE_NAME); BetweenFilter betweenFilter = new BetweenFilter(); betweenFilter.setFrom(1L); betweenFilter.setTo(System.currentTimeMillis()); betweenFilter.setTemporal(true); betweenFilter.setField("_timestamp"); trendRequest.setField("os"); trendRequest.setFilters(Collections.<Filter>singletonList(betweenFilter)); TrendResponse expectedResponse = new TrendResponse(); Map<String, List<TrendResponse.Count>> trends = new HashMap<String, List<TrendResponse.Count>>(); List<TrendResponse.Count> counts = Lists.newArrayList(); counts.add(new TrendResponse.Count(1397606400000L, 6)); counts.add(new TrendResponse.Count(1398643200000L, 1)); trends.put("android", counts); counts = Lists.newArrayList(); counts.add(new TrendResponse.Count(1397692800000L, 1)); counts.add(new TrendResponse.Count(1397952000000L, 1)); counts.add(new TrendResponse.Count(1398643200000L, 2)); trends.put("ios", counts); expectedResponse.setTrends(trends); TrendResponse actualResponse = TrendResponse.class.cast(queryExecutor.execute(trendRequest)); assertEquals(expectedResponse, actualResponse); }
@Test public void testTrendActionWithFieldZeroFrom() throws QueryStoreException, JsonProcessingException { TrendRequest trendRequest = new TrendRequest(); trendRequest.setTable(TestUtils.TEST_TABLE_NAME); BetweenFilter betweenFilter = new BetweenFilter(); betweenFilter.setFrom(1L); betweenFilter.setTo(System.currentTimeMillis()); betweenFilter.setTemporal(true); betweenFilter.setField("_timestamp"); trendRequest.setField("os"); trendRequest.setFilters(Collections.<Filter>singletonList(betweenFilter)); TrendResponse expectedResponse = new TrendResponse(); Map<String, List<TrendResponse.Count>> trends = new HashMap<String, List<TrendResponse.Count>>(); List<TrendResponse.Count> counts = Lists.newArrayList(); counts.add(new TrendResponse.Count(1397606400000L, 6)); counts.add(new TrendResponse.Count(1398643200000L, 1)); trends.put("android", counts); counts = Lists.newArrayList(); counts.add(new TrendResponse.Count(1397692800000L, 1)); counts.add(new TrendResponse.Count(1397952000000L, 1)); counts.add(new TrendResponse.Count(1398643200000L, 2)); trends.put("ios", counts); expectedResponse.setTrends(trends); TrendResponse actualResponse = TrendResponse.class.cast(queryExecutor.execute(trendRequest)); assertEquals(expectedResponse, actualResponse); }
@Test public void testTrendActionWithFieldWithValues() throws QueryStoreException, JsonProcessingException { TrendRequest trendRequest = new TrendRequest(); trendRequest.setTable(TestUtils.TEST_TABLE_NAME); BetweenFilter betweenFilter = new BetweenFilter(); betweenFilter.setFrom(1L); betweenFilter.setTo(System.currentTimeMillis()); betweenFilter.setTemporal(true); betweenFilter.setField("_timestamp"); trendRequest.setField("os"); trendRequest.setFilters(Lists.<Filter>newArrayList(betweenFilter)); trendRequest.setValues(Arrays.asList("android")); TrendResponse expectedResponse = new TrendResponse(); Map<String, List<TrendResponse.Count>> trends = new HashMap<String, List<TrendResponse.Count>>(); List<TrendResponse.Count> counts = Lists.newArrayList(); counts.add(new TrendResponse.Count(1397606400000L, 6)); counts.add(new TrendResponse.Count(1398643200000L, 1)); trends.put("android", counts); expectedResponse.setTrends(trends); TrendResponse actualResponse = TrendResponse.class.cast(queryExecutor.execute(trendRequest)); assertEquals(expectedResponse, actualResponse); }
@Test public void testTrendActionWithFieldWithFilterWithValues() throws QueryStoreException, JsonProcessingException { TrendRequest trendRequest = new TrendRequest(); trendRequest.setTable(TestUtils.TEST_TABLE_NAME); trendRequest.setField("os"); trendRequest.setValues(Arrays.asList("android")); EqualsFilter equalsFilter = new EqualsFilter(); equalsFilter.setField("version"); equalsFilter.setValue(1); LessThanFilter lessThanFilter = new LessThanFilter(); lessThanFilter.setTemporal(true); lessThanFilter.setField("_timestamp"); lessThanFilter.setValue(System.currentTimeMillis()); trendRequest.setFilters(Lists.newArrayList(equalsFilter, lessThanFilter)); TrendResponse expectedResponse = new TrendResponse(); Map<String, List<TrendResponse.Count>> trends = new HashMap<String, List<TrendResponse.Count>>(); List<TrendResponse.Count> counts = Lists.newArrayList(); counts.add(new TrendResponse.Count(1397606400000L, 2)); trends.put("android", counts); expectedResponse.setTrends(trends); TrendResponse actualResponse = TrendResponse.class.cast(queryExecutor.execute(trendRequest)); assertEquals(expectedResponse, actualResponse); }
@Test public void testTrendActionWithFieldWithFilter() throws QueryStoreException, JsonProcessingException { TrendRequest trendRequest = new TrendRequest(); trendRequest.setTable(TestUtils.TEST_TABLE_NAME); trendRequest.setField("os"); EqualsFilter equalsFilter = new EqualsFilter(); equalsFilter.setField("version"); equalsFilter.setValue(1); LessThanFilter lessThanFilter = new LessThanFilter(); lessThanFilter.setTemporal(true); lessThanFilter.setField("_timestamp"); lessThanFilter.setValue(System.currentTimeMillis()); trendRequest.setFilters(Lists.newArrayList(equalsFilter, lessThanFilter)); TrendResponse expectedResponse = new TrendResponse(); Map<String, List<TrendResponse.Count>> trends = new HashMap<String, List<TrendResponse.Count>>(); List<TrendResponse.Count> counts = Lists.newArrayList(); counts.add(new TrendResponse.Count(1397606400000L, 2)); trends.put("android", counts); counts = Lists.newArrayList(); counts.add(new TrendResponse.Count(1397692800000L, 1)); trends.put("ios", counts); expectedResponse.setTrends(trends); TrendResponse actualResponse = TrendResponse.class.cast(queryExecutor.execute(trendRequest)); assertEquals(expectedResponse, actualResponse); }
@Test public void testTrendActionWithFieldWithFilterWithInterval() throws QueryStoreException, JsonProcessingException { TrendRequest trendRequest = new TrendRequest(); trendRequest.setTable(TestUtils.TEST_TABLE_NAME); trendRequest.setField("os"); trendRequest.setPeriod(Period.days); EqualsFilter equalsFilter = new EqualsFilter(); equalsFilter.setField("version"); equalsFilter.setValue(1); LessThanFilter lessThanFilter = new LessThanFilter(); lessThanFilter.setTemporal(true); lessThanFilter.setField("_timestamp"); lessThanFilter.setValue(System.currentTimeMillis()); trendRequest.setFilters(Lists.newArrayList(equalsFilter, lessThanFilter)); TrendResponse expectedResponse = new TrendResponse(); Map<String, List<TrendResponse.Count>> trends = new HashMap<String, List<TrendResponse.Count>>(); List<TrendResponse.Count> counts = Lists.newArrayList(); counts.add(new TrendResponse.Count(1397606400000L, 2)); trends.put("android", counts); counts = Lists.newArrayList(); counts.add(new TrendResponse.Count(1397692800000L, 1)); trends.put("ios", counts); expectedResponse.setTrends(trends); TrendResponse actualResponse = TrendResponse.class.cast(queryExecutor.execute(trendRequest)); assertEquals(expectedResponse, actualResponse); }