Search in sources :

Example 16 with QueryPlannerParams

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());
    }
}
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 17 with QueryPlannerParams

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

Example 18 with QueryPlannerParams

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));
    }
}
Also used : DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) QueryPlannerParams(org.hisp.dhis.analytics.QueryPlannerParams)

Example 19 with QueryPlannerParams

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

Example 20 with QueryPlannerParams

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;
}
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)

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