use of org.hisp.dhis.analytics.DataQueryGroups in project dhis2-core by dhis2.
the class DataHandler method getAggregatedValueMap.
/**
* Generates a mapping between a dimension key and the aggregated value. The
* dimension key is a concatenation of the identifiers of the dimension
* items separated by "-".
*
* @param params the {@link DataQueryParams}.
* @param tableType the {@link AnalyticsTableType}.
* @param queryGroupers the list of additional query groupers to use for
* query planning, use empty list for none.
* @return a mapping between a dimension key and aggregated values.
*/
private Map<String, Object> getAggregatedValueMap(DataQueryParams params, AnalyticsTableType tableType, List<Function<DataQueryParams, List<DataQueryParams>>> queryGroupers) {
queryValidator.validateMaintenanceMode();
int optimalQueries = getWithin(getProcessNo(), 1, MAX_QUERIES);
int maxLimit = params.isIgnoreLimit() ? 0 : systemSettingManager.getIntSetting(ANALYTICS_MAX_LIMIT);
Timer timer = new Timer().start().disablePrint();
QueryPlannerParams plannerParams = QueryPlannerParams.newBuilder().withOptimalQueries(optimalQueries).withTableType(tableType).withQueryGroupers(queryGroupers).build();
DataQueryGroups queryGroups = queryPlanner.planQuery(params, plannerParams);
timer.getSplitTime("Planned analytics query, got: " + queryGroups.getLargestGroupSize() + " for optimal: " + optimalQueries);
Map<String, Object> map = new HashMap<>();
for (List<DataQueryParams> queries : queryGroups.getSequentialQueries()) {
executeQueries(tableType, maxLimit, map, queries);
}
timer.getTime("Got analytics values");
return map;
}
use of org.hisp.dhis.analytics.DataQueryGroups 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.DataQueryGroups 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;
}
use of org.hisp.dhis.analytics.DataQueryGroups in project dhis2-core by dhis2.
the class QueryPlannerTest method planQueryI.
/**
* Query spans 3 period types. Splits in 3 queries for each period type,
* then splits in 2 queries on data type, then splits in 2 queries on data
* elements to satisfy optimal for a total of 12 queries, because query has
* 2 different aggregation types.
*/
@Test
void planQueryI() {
DataQueryParams params = DataQueryParams.newBuilder().withDataElements(getList(deA, deB, deE, deF)).withOrganisationUnits(getList(ouA, ouB, ouC, ouD, ouE)).withPeriods(getList(createPeriod("2000Q1"), createPeriod("2000Q2"), createPeriod("2000"), createPeriod("200002"), createPeriod("200003"), createPeriod("200004"))).build();
QueryPlannerParams plannerParams = QueryPlannerParams.newBuilder().withOptimalQueries(6).withTableType(ANALYTICS_TABLE_TYPE).build();
DataQueryGroups queryGroups = queryPlanner.planQuery(params, plannerParams);
assertEquals(12, queryGroups.getAllQueries().size());
assertEquals(2, queryGroups.getSequentialQueries().size());
assertEquals(6, queryGroups.getLargestGroupSize());
for (DataQueryParams query : queryGroups.getAllQueries()) {
assertTrue(samePeriodType(query.getPeriods()));
assertDimensionNameNotNull(query);
}
}
use of org.hisp.dhis.analytics.DataQueryGroups in project dhis2-core by dhis2.
the class QueryPlannerTest method planQueryN.
/**
* Query spans 5 QueryModifiers minDate/MaxDate combinations.
*/
@Test
void planQueryN() {
QueryModifiers modsA = QueryModifiers.builder().minDate(parseDate("2022-01-01")).build();
QueryModifiers modsB = QueryModifiers.builder().minDate(parseDate("2022-02-01")).build();
QueryModifiers modsC = QueryModifiers.builder().maxDate(parseDate("2022-12-31")).build();
QueryModifiers modsD = QueryModifiers.builder().minDate(parseDate("2022-01-01")).maxDate(parseDate("2022-12-31")).build();
deC.setQueryMods(modsA);
deD.setQueryMods(modsB);
deG.setQueryMods(modsC);
deH.setQueryMods(modsD);
deI.setQueryMods(modsD);
deC.setAggregationType(AggregationType.SUM);
deD.setAggregationType(AggregationType.SUM);
deI.setAggregationType(AggregationType.SUM);
DataQueryParams params = DataQueryParams.newBuilder().withDataElements(getList(deA, deB, deC, deD, deG, deH, deI)).withPeriods(getList(createPeriod("2022"))).build();
QueryPlannerParams plannerParams = QueryPlannerParams.newBuilder().withOptimalQueries(4).withTableType(ANALYTICS_TABLE_TYPE).build();
DataQueryGroups queryGroups = queryPlanner.planQuery(params, plannerParams);
assertEquals(5, queryGroups.getAllQueries().size());
assertEquals(1, queryGroups.getSequentialQueries().size());
assertEquals(5, queryGroups.getLargestGroupSize());
List<DataQueryParams> group = queryGroups.getAllQueries();
assertQueryMods(group, null, deA, deB);
assertQueryMods(group, modsA, deC);
assertQueryMods(group, modsB, deD);
assertQueryMods(group, modsC, deG);
assertQueryMods(group, modsD, deH, deI);
}
Aggregations