Search in sources :

Example 16 with DataQueryGroups

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;
}
Also used : DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) EventQueryParams.fromDataQueryParams(org.hisp.dhis.analytics.event.EventQueryParams.fromDataQueryParams) Timer(org.hisp.dhis.util.Timer) HashMap(java.util.HashMap) ArrayListValuedHashMap(org.apache.commons.collections4.multimap.ArrayListValuedHashMap) DataQueryGroups(org.hisp.dhis.analytics.DataQueryGroups) AnalyticsUtils.getRoundedValueObject(org.hisp.dhis.analytics.util.AnalyticsUtils.getRoundedValueObject) DimensionalObject(org.hisp.dhis.common.DimensionalObject) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) PeriodType.getPeriodTypeFromIsoString(org.hisp.dhis.period.PeriodType.getPeriodTypeFromIsoString) QueryPlannerParams(org.hisp.dhis.analytics.QueryPlannerParams)

Example 17 with DataQueryGroups

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());
    }
}
Also used : DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) Function(java.util.function.Function) DataQueryGroups(org.hisp.dhis.analytics.DataQueryGroups) QueryPlannerParams(org.hisp.dhis.analytics.QueryPlannerParams) DimensionalObject(org.hisp.dhis.common.DimensionalObject) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 18 with DataQueryGroups

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;
}
Also used : ORGUNIT_DIM_ID(org.hisp.dhis.common.DimensionalObject.ORGUNIT_DIM_ID) ListMap(org.hisp.dhis.common.ListMap) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) LEVEL_PREFIX(org.hisp.dhis.analytics.DataQueryParams.LEVEL_PREFIX) DataType(org.hisp.dhis.analytics.DataType) DateUtils.getLatest(org.hisp.dhis.util.DateUtils.getLatest) Function(java.util.function.Function) PartitionManager(org.hisp.dhis.analytics.partition.PartitionManager) PartitionUtils(org.hisp.dhis.analytics.table.PartitionUtils) PeriodOffsetUtils(org.hisp.dhis.analytics.util.PeriodOffsetUtils) ArrayList(java.util.ArrayList) DataElement(org.hisp.dhis.dataelement.DataElement) PaginatedList(org.hisp.dhis.commons.collection.PaginatedList) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) DateUtils.getEarliest(org.hisp.dhis.util.DateUtils.getEarliest) AnalyticsAggregationType(org.hisp.dhis.analytics.AnalyticsAggregationType) DATA_X_DIM_ID(org.hisp.dhis.common.DimensionalObject.DATA_X_DIM_ID) DataQueryGroups(org.hisp.dhis.analytics.DataQueryGroups) ErrorCode(org.hisp.dhis.feedback.ErrorCode) AnalyticsTableType(org.hisp.dhis.analytics.AnalyticsTableType) Period(org.hisp.dhis.period.Period) Partitions(org.hisp.dhis.analytics.Partitions) DataDimensionItemType(org.hisp.dhis.common.DataDimensionItemType) QueryPlanner(org.hisp.dhis.analytics.QueryPlanner) PERIOD_DIM_ID(org.hisp.dhis.common.DimensionalObject.PERIOD_DIM_ID) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) AggregationType(org.hisp.dhis.analytics.AggregationType) AnalyticsUtils.throwIllegalQueryEx(org.hisp.dhis.analytics.util.AnalyticsUtils.throwIllegalQueryEx) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) DimensionType(org.hisp.dhis.common.DimensionType) QueryModifiers(org.hisp.dhis.common.QueryModifiers) ObjectUtils(org.hisp.dhis.util.ObjectUtils) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) QueryPlannerParams(org.hisp.dhis.analytics.QueryPlannerParams) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) DimensionalObject(org.hisp.dhis.common.DimensionalObject) PeriodType(org.hisp.dhis.period.PeriodType) DataElementGroup(org.hisp.dhis.dataelement.DataElementGroup) MathUtils(org.hisp.dhis.system.util.MathUtils) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) Function(java.util.function.Function) ArrayList(java.util.ArrayList) PaginatedList(org.hisp.dhis.commons.collection.PaginatedList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) DataQueryGroups(org.hisp.dhis.analytics.DataQueryGroups)

Example 19 with DataQueryGroups

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);
    }
}
Also used : DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) DataQueryGroups(org.hisp.dhis.analytics.DataQueryGroups) QueryPlannerParams(org.hisp.dhis.analytics.QueryPlannerParams) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 20 with DataQueryGroups

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);
}
Also used : QueryModifiers(org.hisp.dhis.common.QueryModifiers) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) DataQueryGroups(org.hisp.dhis.analytics.DataQueryGroups) QueryPlannerParams(org.hisp.dhis.analytics.QueryPlannerParams) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Aggregations

DataQueryGroups (org.hisp.dhis.analytics.DataQueryGroups)31 DataQueryParams (org.hisp.dhis.analytics.DataQueryParams)31 Test (org.junit.jupiter.api.Test)25 QueryPlannerParams (org.hisp.dhis.analytics.QueryPlannerParams)24 DhisSpringTest (org.hisp.dhis.DhisSpringTest)20 BaseDimensionalObject (org.hisp.dhis.common.BaseDimensionalObject)11 Function (java.util.function.Function)5 CategoryCombo (org.hisp.dhis.category.CategoryCombo)5 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)5 DimensionalObject (org.hisp.dhis.common.DimensionalObject)5 ArrayList (java.util.ArrayList)4 DataElement (org.hisp.dhis.dataelement.DataElement)3 HashMap (java.util.HashMap)2 QueryModifiers (org.hisp.dhis.common.QueryModifiers)2 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)2 MonthlyPeriodType (org.hisp.dhis.period.MonthlyPeriodType)2 PeriodType.getPeriodTypeFromIsoString (org.hisp.dhis.period.PeriodType.getPeriodTypeFromIsoString)2 Timer (org.hisp.dhis.util.Timer)2 DateTime (org.joda.time.DateTime)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1