Search in sources :

Example 1 with QueryPlannerParams

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

the class QueryPlannerTest method planQueryG.

/**
     * Expected to fail because of no periods specified.
     */
@Test(expected = IllegalQueryException.class)
public void planQueryG() {
    DataQueryParams params = DataQueryParams.newBuilder().withDataElements(getList(deA, deB, deC)).withOrganisationUnits(getList(ouA, ouB, ouC, ouD, ouE)).build();
    QueryPlannerParams plannerParams = QueryPlannerParams.newBuilder().withOptimalQueries(4).withTableName(ANALYTICS_TABLE_NAME).build();
    queryPlanner.planQuery(params, plannerParams);
}
Also used : DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) QueryPlannerParams(org.hisp.dhis.analytics.QueryPlannerParams) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 2 with QueryPlannerParams

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

the class QueryPlannerTest method planQueryStartEndDateQueryGrouperB.

/**
     * 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 planQueryStartEndDateQueryGrouperB() {
    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.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(2, queries.size());
    assertEquals(1, queryGroups.getSequentialQueries().size());
    assertEquals(2, queryGroups.getLargestGroupSize());
    for (DataQueryParams query : queries) {
        assertNotNull(query.getStartDate());
        assertNotNull(query.getEndDate());
        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.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 3 with QueryPlannerParams

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

the class DefaultAnalyticsService 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 tableName the table name to use for the query.
     * @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, String tableName, List<Function<DataQueryParams, List<DataQueryParams>>> queryGroupers) {
    queryPlanner.validateMaintenanceMode();
    int optimalQueries = MathUtils.getWithin(getProcessNo(), 1, MAX_QUERIES);
    int maxLimit = params.isIgnoreLimit() ? 0 : (Integer) systemSettingManager.getSystemSetting(SettingKey.ANALYTICS_MAX_LIMIT);
    Timer timer = new Timer().start().disablePrint();
    QueryPlannerParams plannerParams = QueryPlannerParams.newBuilder().withOptimalQueries(optimalQueries).withTableName(tableName).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()) {
        List<Future<Map<String, Object>>> futures = new ArrayList<>();
        for (DataQueryParams query : queries) {
            futures.add(analyticsManager.getAggregatedDataValues(query, maxLimit));
        }
        for (Future<Map<String, Object>> future : futures) {
            try {
                Map<String, Object> taskValues = future.get();
                if (taskValues != null) {
                    map.putAll(taskValues);
                }
            } catch (Exception ex) {
                log.error(DebugUtils.getStackTrace(ex));
                log.error(DebugUtils.getStackTrace(ex.getCause()));
                throw new RuntimeException("Error during execution of aggregation query task", ex);
            }
        }
    }
    timer.getTime("Got analytics values");
    return map;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PeriodType.getPeriodTypeFromIsoString(org.hisp.dhis.period.PeriodType.getPeriodTypeFromIsoString) ProcessingHint(org.hisp.dhis.analytics.ProcessingHint) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) Timer(org.hisp.dhis.util.Timer) Future(java.util.concurrent.Future) DataQueryGroups(org.hisp.dhis.analytics.DataQueryGroups) DimensionalObject(org.hisp.dhis.common.DimensionalObject) AnalyticalObject(org.hisp.dhis.common.AnalyticalObject) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) OrganisationUnit.getParentGraphMap(org.hisp.dhis.organisationunit.OrganisationUnit.getParentGraphMap) HashMap(java.util.HashMap) OrganisationUnit.getParentNameGraphMap(org.hisp.dhis.organisationunit.OrganisationUnit.getParentNameGraphMap) QueryPlannerParams(org.hisp.dhis.analytics.QueryPlannerParams)

Example 4 with QueryPlannerParams

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

the class DefaultEventQueryPlanner method groupByPartition.

// -------------------------------------------------------------------------
// Supportive methods
// -------------------------------------------------------------------------
/**
     * Group by partition.
     * 
     * @param params the event query parameters.
     * @param validPartitions the set of valid partitions.
     * @return a list of {@link EventQueryParams}.
     */
private List<EventQueryParams> groupByPartition(EventQueryParams params, Set<String> validPartitions) {
    String tableSuffix = PartitionUtils.SEP + params.getProgram().getUid();
    if (params.hasEnrollmentProgramIndicatorDimension()) {
        List<EventQueryParams> indicatorQueries = new ArrayList<>();
        EventQueryParams query = new EventQueryParams.Builder(params).withPartitions(PartitionUtils.getPartitions(AnalyticsTableType.ENROLLMENT.getTableName(), tableSuffix, validPartitions)).build();
        if (query.getPartitions().hasAny()) {
            indicatorQueries.add(query);
        }
        return indicatorQueries;
    } else if (params.hasStartEndDate()) {
        List<EventQueryParams> queries = new ArrayList<>();
        Period queryPeriod = new Period();
        queryPeriod.setStartDate(params.getStartDate());
        queryPeriod.setEndDate(params.getEndDate());
        EventQueryParams query = new EventQueryParams.Builder(params).withPartitions(PartitionUtils.getPartitions(queryPeriod, AnalyticsTableType.EVENT.getTableName(), tableSuffix, validPartitions)).build();
        if (query.getPartitions().hasAny()) {
            queries.add(query);
        }
        return queries;
    } else // Aggregate only
    {
        QueryPlannerParams plannerParams = QueryPlannerParams.newBuilder().withTableName(AnalyticsTableType.EVENT.getTableName()).withTableSuffix(tableSuffix).build();
        return QueryPlannerUtils.convert(queryPlanner.groupByPartition(params, plannerParams));
    }
}
Also used : EventQueryParams(org.hisp.dhis.analytics.event.EventQueryParams) ArrayList(java.util.ArrayList) Period(org.hisp.dhis.period.Period) ArrayList(java.util.ArrayList) List(java.util.List) QueryPlannerParams(org.hisp.dhis.analytics.QueryPlannerParams)

Example 5 with QueryPlannerParams

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

the class QueryPlannerTest method planQueryK.

/**
 * Splits in 4 queries on data sets to satisfy optimal for a total of 4
 * queries.
 */
@Test
void planQueryK() {
    DataQueryParams params = DataQueryParams.newBuilder().withReportingRates(getList(rrA, rrB, rrC, rrD)).withOrganisationUnits(getList(ouA, ouB, ouC, ouD, ouE)).withPeriods(getList(createPeriod("2000Q1"), createPeriod("2000Q2"), createPeriod("2000Q3"), createPeriod("2000Q4"), createPeriod("2001Q1"), createPeriod("2001Q2"))).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) {
        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