Search in sources :

Example 6 with QueryPlannerParams

use of org.hisp.dhis.analytics.QueryPlannerParams 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 7 with QueryPlannerParams

use of org.hisp.dhis.analytics.QueryPlannerParams in project dhis2-core by dhis2.

the class QueryPlannerTest method planQueryForFirstOrLastAggregationType.

private void planQueryForFirstOrLastAggregationType(AnalyticsAggregationType analyticsAggregationType) {
    DataQueryParams params = DataQueryParams.newBuilder().withDataElements(getList(deA)).withOrganisationUnits(getList(ouA)).withPeriods(getList(createPeriod("200101"), createPeriod("200102"), createPeriod("200103"), createPeriod("200104"))).withAggregationType(analyticsAggregationType).build();
    QueryPlannerParams plannerParams = QueryPlannerParams.newBuilder().withOptimalQueries(4).withTableType(ANALYTICS_TABLE_TYPE).build();
    DataQueryGroups queryGroups = queryPlanner.planQuery(params, plannerParams);
    List<DataQueryParams> queries = queryGroups.getAllQueries();
    assertEquals(4, queries.size());
    for (DataQueryParams query : queries) {
        assertEquals(1, query.getPeriods().size());
        assertNotNull(query.getDimension(PERIOD_DIM_ID));
        assertEquals(MonthlyPeriodType.NAME.toLowerCase(), query.getDimension(PERIOD_DIM_ID).getDimensionName());
    }
}
Also used : DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) DataQueryGroups(org.hisp.dhis.analytics.DataQueryGroups) QueryPlannerParams(org.hisp.dhis.analytics.QueryPlannerParams)

Example 8 with QueryPlannerParams

use of org.hisp.dhis.analytics.QueryPlannerParams in project dhis2-core by dhis2.

the class QueryPlannerTest method planQueryStartEndDateRestrictionQueryGrouperB.

/**
 * Splits in 4 queries for each period to satisfy optimal for a total of 4
 * queries, because all queries have different periods.
 */
@Test
void planQueryStartEndDateRestrictionQueryGrouperB() {
    DataQueryParams params = DataQueryParams.newBuilder().withDataElements(getList(deA, deB)).withOrganisationUnits(getList(ouA)).withFilterPeriods(getList(createPeriod("200101"), createPeriod("200102"), createPeriod("200103"), createPeriod("200104"))).build();
    List<Function<DataQueryParams, List<DataQueryParams>>> queryGroupers = Lists.newArrayList();
    queryGroupers.add(q -> queryPlanner.groupByStartEndDateRestriction(q));
    QueryPlannerParams plannerParams = QueryPlannerParams.newBuilder().withOptimalQueries(4).withTableType(ANALYTICS_TABLE_TYPE).withQueryGroupers(queryGroupers).build();
    DataQueryGroups queryGroups = queryPlanner.planQuery(params, plannerParams);
    List<DataQueryParams> queries = queryGroups.getAllQueries();
    assertEquals(2, queries.size());
    assertEquals(1, queryGroups.getSequentialQueries().size());
    assertEquals(2, queryGroups.getLargestGroupSize());
    for (DataQueryParams query : queries) {
        assertNull(query.getStartDate());
        assertNull(query.getEndDate());
        assertNotNull(query.getStartDateRestriction());
        assertNotNull(query.getEndDateRestriction());
        assertDimensionNameNotNull(query);
        assertNull(query.getFilter(PERIOD_DIM_ID));
    }
}
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) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 9 with QueryPlannerParams

use of org.hisp.dhis.analytics.QueryPlannerParams in project dhis2-core by dhis2.

the class QueryPlannerTest method planQueryD.

/**
 * Query spans 2 aggregation types. Splits on 2 aggregation types, then
 * splits one query on 3 days in period to satisfy optimal for a total of 4
 * queries.
 */
@Test
void planQueryD() {
    DataQueryParams params = DataQueryParams.newBuilder().withDataElements(getList(deA, deB, deC)).withOrganisationUnits(getList(ouA)).withPeriods(getList(createPeriod("200001"), createPeriod("200002"), createPeriod("200003"), createPeriod("200004"), createPeriod("200005"), createPeriod("200006"), createPeriod("200007"), createPeriod("200008"), createPeriod("200009"))).build();
    QueryPlannerParams plannerParams = QueryPlannerParams.newBuilder().withOptimalQueries(4).withTableType(ANALYTICS_TABLE_TYPE).build();
    DataQueryGroups queryGroups = queryPlanner.planQuery(params, plannerParams);
    assertEquals(4, queryGroups.getAllQueries().size());
    assertEquals(2, queryGroups.getSequentialQueries().size());
    assertEquals(3, 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 10 with QueryPlannerParams

use of org.hisp.dhis.analytics.QueryPlannerParams in project dhis2-core by dhis2.

the class QueryPlannerTest method planQueryA.

/**
 * Query spans two period types and two aggregation types. Splits in 2
 * queries for each period type, then splits in 4 queries on data elements
 * to satisfy optimal of 4 queries per query group.
 */
@Test
void planQueryA() {
    DataQueryParams params = DataQueryParams.newBuilder().withDataElements(getList(deA, deB, deC, deD)).withOrganisationUnits(getList(ouA, ouB, ouC, ouD, ouE)).withPeriods(getList(createPeriod("200101"), createPeriod("200103"), createPeriod("200105"), createPeriod("200107"), createPeriod("2002Q3"), createPeriod("2002Q4"))).build();
    QueryPlannerParams plannerParams = QueryPlannerParams.newBuilder().withOptimalQueries(4).withTableType(ANALYTICS_TABLE_TYPE).build();
    DataQueryGroups queryGroups = queryPlanner.planQuery(params, plannerParams);
    assertEquals(8, queryGroups.getAllQueries().size());
    assertEquals(2, queryGroups.getSequentialQueries().size());
    assertEquals(4, 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)

Aggregations

QueryPlannerParams (org.hisp.dhis.analytics.QueryPlannerParams)31 DataQueryParams (org.hisp.dhis.analytics.DataQueryParams)29 DhisSpringTest (org.hisp.dhis.DhisSpringTest)24 DataQueryGroups (org.hisp.dhis.analytics.DataQueryGroups)24 Test (org.junit.jupiter.api.Test)19 Function (java.util.function.Function)5 DimensionalObject (org.hisp.dhis.common.DimensionalObject)5 Test (org.junit.Test)5 BaseDimensionalObject (org.hisp.dhis.common.BaseDimensionalObject)4 ArrayList (java.util.ArrayList)3 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)3 DataElement (org.hisp.dhis.dataelement.DataElement)3 HashMap (java.util.HashMap)2 List (java.util.List)2 Partitions (org.hisp.dhis.analytics.Partitions)2 QueryModifiers (org.hisp.dhis.common.QueryModifiers)2 Period (org.hisp.dhis.period.Period)2 PeriodType.getPeriodTypeFromIsoString (org.hisp.dhis.period.PeriodType.getPeriodTypeFromIsoString)2 Timer (org.hisp.dhis.util.Timer)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1