use of org.hisp.dhis.analytics.QueryPlannerParams in project dhis2-core by dhis2.
the class QueryPlannerTest method planQueryStartEndDateQueryGrouperA.
/**
* Splits in 4 queries for each period to satisfy optimal for a total
* of 4 queries, because all queries have different periods.
*/
@Test
public void planQueryStartEndDateQueryGrouperA() {
DataQueryParams params = DataQueryParams.newBuilder().withDataElements(getList(deA, deB)).withOrganisationUnits(getList(ouA)).withPeriods(getList(createPeriod("200101"), createPeriod("200102"), createPeriod("200103"), createPeriod("200104"))).build();
List<Function<DataQueryParams, List<DataQueryParams>>> queryGroupers = Lists.newArrayList();
queryGroupers.add(q -> queryPlanner.groupByStartEndDate(q));
QueryPlannerParams plannerParams = QueryPlannerParams.newBuilder().withOptimalQueries(4).withTableName(ANALYTICS_TABLE_NAME).withQueryGroupers(queryGroupers).build();
DataQueryGroups queryGroups = queryPlanner.planQuery(params, plannerParams);
List<DataQueryParams> queries = queryGroups.getAllQueries();
assertEquals(4, queries.size());
assertEquals(1, queryGroups.getSequentialQueries().size());
assertEquals(4, queryGroups.getLargestGroupSize());
for (DataQueryParams query : queries) {
assertNotNull(query.getStartDate());
assertNotNull(query.getEndDate());
assertDimensionNameNotNull(query);
DimensionalObject periodDim = query.getDimension(PERIOD_DIM_ID);
assertNotNull(periodDim.getDimensionName());
assertTrue(periodDim.isFixed());
}
}
use of org.hisp.dhis.analytics.QueryPlannerParams in project dhis2-core by dhis2.
the class QueryPlannerTest method testGroupByPartition.
@Test
public void testGroupByPartition() {
DataQueryParams params = DataQueryParams.newBuilder().withStartDate(getDate(2014, 4, 1)).withEndDate(getDate(2016, 8, 1)).build();
assertTrue(params.hasStartEndDate());
QueryPlannerParams plannerParams = QueryPlannerParams.newBuilder().withTableName(ANALYTICS_TABLE_NAME).build();
List<DataQueryParams> queries = queryPlanner.groupByPartition(params, plannerParams);
assertEquals(1, queries.size());
DataQueryParams query = queries.get(0);
List<String> partitions = query.getPartitions().getPartitions();
assertNotNull(partitions);
assertEquals(3, partitions.size());
assertEquals(ANALYTICS_TABLE_NAME + "_2014", partitions.get(0));
assertEquals(ANALYTICS_TABLE_NAME + "_2015", partitions.get(1));
assertEquals(ANALYTICS_TABLE_NAME + "_2016", partitions.get(2));
}
use of org.hisp.dhis.analytics.QueryPlannerParams in project dhis2-core by dhis2.
the class DefaultAnalyticsService method addRawData.
/**
* Adds raw data to the grid for the given data query parameters.
*
* @param params the {@link DataQueryParams}.
* @param grid the grid.
*/
private void addRawData(DataQueryParams params, Grid grid) {
if (!params.isSkipData()) {
QueryPlannerParams plannerParams = QueryPlannerParams.newBuilder().withTableName(AnalyticsTableType.DATA_VALUE.getTableName()).build();
List<DataQueryParams> queries = queryPlanner.groupByPartition(params, plannerParams);
queries.forEach(query -> rawAnalyticsManager.getRawDataValues(query, grid));
}
}
use of org.hisp.dhis.analytics.QueryPlannerParams in project dhis2-core by dhis2.
the class QueryPlannerTest method planQueryJ.
/**
* No periods specified, illegal query.
*/
@Test(expected = IllegalQueryException.class)
public void planQueryJ() {
DataQueryParams params = DataQueryParams.newBuilder().withDataElements(getList(deA, deB, deC, deD)).withOrganisationUnits(getList(ouA, ouB, ouC, ouD, ouE)).build();
QueryPlannerParams plannerParams = QueryPlannerParams.newBuilder().withOptimalQueries(4).withTableName(ANALYTICS_TABLE_NAME).build();
queryPlanner.planQuery(params, plannerParams);
}
use of org.hisp.dhis.analytics.QueryPlannerParams in project dhis2-core by dhis2.
the class DefaultQueryPlanner method planQuery.
// -------------------------------------------------------------------------
// QueryPlanner implementation
// -------------------------------------------------------------------------
@Override
public DataQueryGroups planQuery(DataQueryParams params, QueryPlannerParams plannerParams) {
params = PeriodOffsetUtils.addShiftedPeriods(params);
// ---------------------------------------------------------------------
// Group queries which can be executed together
// ---------------------------------------------------------------------
params = withTableNameAndPartitions(params, plannerParams);
partitionManager.filterNonExistingPartitions(params.getPartitions(), plannerParams.getTableName());
final List<DataQueryParams> queries = Lists.newArrayList(params);
List<Function<DataQueryParams, List<DataQueryParams>>> groupers = new ImmutableList.Builder<Function<DataQueryParams, List<DataQueryParams>>>().add(q -> groupByOrgUnitLevel(q)).add(q -> groupByPeriodType(q)).add(q -> groupByDataType(q)).add(q -> groupByMinMaxDate(q)).add(q -> groupByAggregationType(q)).add(q -> groupByDaysInPeriod(q)).add(q -> groupByDataPeriodType(q)).add(q -> groupByPeriod(q)).addAll(plannerParams.getQueryGroupers()).build();
for (Function<DataQueryParams, List<DataQueryParams>> grouper : groupers) {
List<DataQueryParams> currentQueries = Lists.newArrayList(queries);
queries.clear();
currentQueries.forEach(query -> queries.addAll(grouper.apply(query)));
}
// ---------------------------------------------------------------------
// Split queries until optimal number
// ---------------------------------------------------------------------
DataQueryGroups queryGroups = DataQueryGroups.newBuilder().withQueries(queries).build();
if (queryGroups.isOptimal(plannerParams.getOptimalQueries())) {
return queryGroups;
}
List<String> splitDimensions = Lists.newArrayList(DATA_X_DIM_ID, ORGUNIT_DIM_ID);
for (String dim : splitDimensions) {
queryGroups = splitByDimension(queryGroups, dim, plannerParams.getOptimalQueries());
if (queryGroups.isOptimal(plannerParams.getOptimalQueries())) {
break;
}
}
return queryGroups;
}
Aggregations