Search in sources :

Example 6 with AnalyticsAggregationType

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

the class DefaultQueryPlanner method groupByAggregationType.

/**
 * Groups the given query in sub queries based on the aggregation type of
 * its data elements. The aggregation type can be sum, average aggregation
 * or average disaggregation. Sum means that the data elements have sum
 * aggregation operator. Average aggregation means that the data elements
 * have the average aggregation operator and that the period type of the
 * data elements have higher or equal frequency than the aggregation period
 * type. Average disaggregation means that the data elements have the
 * average aggregation operator and that the period type of the data
 * elements have lower frequency than the aggregation period type. Average
 * bool means that the data elements have the average aggregation operator
 * and the bool value type.
 * <p>
 * If no data elements are present, the aggregation type will be determined
 * based on the first data element in the first data element group in the
 * first data element group set in the query.
 * <p>
 * If the aggregation type is already set/overridden in the request, the
 * query will be returned unchanged. If there are no data elements or data
 * element group sets specified the aggregation type will fall back to sum.
 *
 * @param params the {@link DataQueryParams}.
 * @return a list of {@link DataQueryParams}.
 */
private List<DataQueryParams> groupByAggregationType(DataQueryParams params) {
    List<DataQueryParams> queries = new ArrayList<>();
    if (!params.getDataElements().isEmpty()) {
        ListMap<AnalyticsAggregationType, DimensionalItemObject> aggregationTypeDataElementMap = QueryPlannerUtils.getAggregationTypeDataElementMap(params.getDataElements(), params.getAggregationType(), params.getPeriodType());
        for (AnalyticsAggregationType aggregationType : aggregationTypeDataElementMap.keySet()) {
            DataQueryParams query = DataQueryParams.newBuilder(params).withDataElements(aggregationTypeDataElementMap.get(aggregationType)).withAggregationType(aggregationType).build();
            queries.add(query);
        }
    } else if (!params.getDataElementGroupSets().isEmpty()) {
        DataElementGroup deg = params.getFirstDataElementGroup();
        AnalyticsAggregationType aggregationType = ObjectUtils.firstNonNull(params.getAggregationType(), AnalyticsAggregationType.SUM);
        if (deg != null && !deg.getMembers().isEmpty()) {
            PeriodType periodType = PeriodType.getPeriodTypeByName(params.getPeriodType());
            AnalyticsAggregationType degAggType = AnalyticsAggregationType.fromAggregationType(deg.getAggregationType());
            aggregationType = ObjectUtils.firstNonNull(params.getAggregationType(), degAggType, AnalyticsAggregationType.SUM);
            aggregationType = QueryPlannerUtils.getAggregationType(aggregationType, deg.getValueType(), periodType, deg.getPeriodType());
        }
        DataQueryParams query = DataQueryParams.newBuilder(params).withAggregationType(aggregationType).build();
        queries.add(query);
    } else if (filterHasDataElementsOfSameAggregationTypeAndValueType(params)) {
        ListMap<AnalyticsAggregationType, DimensionalItemObject> aggregationTypeDataElementMap = QueryPlannerUtils.getAggregationTypeDataElementMap(params.getFilterOptions(DATA_X_DIM_ID, DataDimensionItemType.DATA_ELEMENT), params.getAggregationType(), params.getPeriodType());
        for (AnalyticsAggregationType aggregationType : aggregationTypeDataElementMap.keySet()) {
            DataQueryParams query = DataQueryParams.newBuilder(params).withAggregationType(aggregationType).build();
            queries.add(query);
        }
    } else {
        DataQueryParams query = DataQueryParams.newBuilder(params).withAggregationType(ObjectUtils.firstNonNull(params.getAggregationType(), AnalyticsAggregationType.SUM)).build();
        queries.add(query);
    }
    logQuerySplit(queries, "aggregation type");
    return queries;
}
Also used : DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) PeriodType(org.hisp.dhis.period.PeriodType) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) ArrayList(java.util.ArrayList) DataElementGroup(org.hisp.dhis.dataelement.DataElementGroup) AnalyticsAggregationType(org.hisp.dhis.analytics.AnalyticsAggregationType)

Example 7 with AnalyticsAggregationType

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

the class QueryPlannerUtilsTest method testGetAggregationType.

@Test
void testGetAggregationType() {
    AnalyticsAggregationType typeA = new AnalyticsAggregationType(AggregationType.SUM, AggregationType.AVERAGE, DataType.NUMERIC, true);
    AnalyticsAggregationType typeB = new AnalyticsAggregationType(AggregationType.AVERAGE, AggregationType.AVERAGE, DataType.NUMERIC, true);
    assertEquals(typeA, QueryPlannerUtils.getAggregationType(new AnalyticsAggregationType(AggregationType.SUM, AggregationType.AVERAGE), ValueType.INTEGER, new QuarterlyPeriodType(), new YearlyPeriodType()));
    assertEquals(typeB, QueryPlannerUtils.getAggregationType(new AnalyticsAggregationType(AggregationType.AVERAGE, AggregationType.AVERAGE), ValueType.INTEGER, new QuarterlyPeriodType(), new YearlyPeriodType()));
}
Also used : QuarterlyPeriodType(org.hisp.dhis.period.QuarterlyPeriodType) YearlyPeriodType(org.hisp.dhis.period.YearlyPeriodType) AnalyticsAggregationType(org.hisp.dhis.analytics.AnalyticsAggregationType) Test(org.junit.jupiter.api.Test)

Example 8 with AnalyticsAggregationType

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

the class QueryPlannerUtils method getAggregationType.

/**
 * Returns the {@link AnalyticsAggregationType} according to the given value
 * type, aggregation type, value type aggregation period type and data
 * period type.
 *
 * @param aggregationType the aggregation type.
 * @param valueType the value type.
 * @param aggregationPeriodType the aggregation period type.
 * @param dataPeriodType the data period type.
 */
public static AnalyticsAggregationType getAggregationType(AnalyticsAggregationType aggregationType, ValueType valueType, PeriodType aggregationPeriodType, PeriodType dataPeriodType) {
    DataType dataType = DataType.fromValueType(valueType);
    boolean disaggregation = isDisaggregation(aggregationType, aggregationPeriodType, dataPeriodType);
    return new AnalyticsAggregationType(aggregationType.getAggregationType(), aggregationType.getPeriodAggregationType(), dataType, disaggregation);
}
Also used : DataType(org.hisp.dhis.analytics.DataType) AnalyticsAggregationType(org.hisp.dhis.analytics.AnalyticsAggregationType)

Example 9 with AnalyticsAggregationType

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

the class AnalyticsManagerTest method testReplaceDataPeriodsWithAggregationPeriods.

@Test
void testReplaceDataPeriodsWithAggregationPeriods() {
    AnalyticsManager analyticsManager = new JdbcAnalyticsManager(queryPlanner, jdbcTemplate, executionPlanStore);
    Period y2012 = createPeriod("2012");
    AnalyticsAggregationType aggregationType = new AnalyticsAggregationType(AggregationType.SUM, AggregationType.AVERAGE, DataType.NUMERIC, true);
    DataQueryParams params = DataQueryParams.newBuilder().withDataElements(getList(createDataElement('A'), createDataElement('B'))).withPeriods(getList(y2012)).withOrganisationUnits(getList(createOrganisationUnit('A'))).withDataPeriodType(new YearlyPeriodType()).withAggregationType(aggregationType).build();
    Map<String, Object> dataValueMap = new HashMap<>();
    dataValueMap.put(BASE_UID + "A-2012-" + BASE_UID + "A", 1d);
    dataValueMap.put(BASE_UID + "B-2012-" + BASE_UID + "A", 1d);
    ListMap<DimensionalItemObject, DimensionalItemObject> dataPeriodAggregationPeriodMap = new ListMap<>();
    dataPeriodAggregationPeriodMap.putValue(y2012, createPeriod("2012Q1"));
    dataPeriodAggregationPeriodMap.putValue(y2012, createPeriod("2012Q2"));
    dataPeriodAggregationPeriodMap.putValue(y2012, createPeriod("2012Q3"));
    dataPeriodAggregationPeriodMap.putValue(y2012, createPeriod("2012Q4"));
    analyticsManager.replaceDataPeriodsWithAggregationPeriods(dataValueMap, params, dataPeriodAggregationPeriodMap);
    assertEquals(8, dataValueMap.size());
    assertTrue(dataValueMap.keySet().contains(BASE_UID + "A-2012Q1-" + BASE_UID + "A"));
    assertTrue(dataValueMap.keySet().contains(BASE_UID + "A-2012Q2-" + BASE_UID + "A"));
    assertTrue(dataValueMap.keySet().contains(BASE_UID + "A-2012Q3-" + BASE_UID + "A"));
    assertTrue(dataValueMap.keySet().contains(BASE_UID + "A-2012Q4-" + BASE_UID + "A"));
    assertTrue(dataValueMap.keySet().contains(BASE_UID + "B-2012Q1-" + BASE_UID + "A"));
    assertTrue(dataValueMap.keySet().contains(BASE_UID + "B-2012Q2-" + BASE_UID + "A"));
    assertTrue(dataValueMap.keySet().contains(BASE_UID + "B-2012Q3-" + BASE_UID + "A"));
    assertTrue(dataValueMap.keySet().contains(BASE_UID + "B-2012Q4-" + BASE_UID + "A"));
}
Also used : HashMap(java.util.HashMap) Period(org.hisp.dhis.period.Period) YearlyPeriodType(org.hisp.dhis.period.YearlyPeriodType) ListMap(org.hisp.dhis.common.ListMap) AnalyticsAggregationType(org.hisp.dhis.analytics.AnalyticsAggregationType) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) AnalyticsManager(org.hisp.dhis.analytics.AnalyticsManager) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest)

Example 10 with AnalyticsAggregationType

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

the class QueryPlannerUtilsTest method testFromAggregationType.

@Test
void testFromAggregationType() {
    assertEquals(new AnalyticsAggregationType(AggregationType.SUM, AggregationType.SUM), AnalyticsAggregationType.fromAggregationType(AggregationType.SUM));
    assertEquals(new AnalyticsAggregationType(AggregationType.SUM, AggregationType.AVERAGE), AnalyticsAggregationType.fromAggregationType(AggregationType.AVERAGE_SUM_ORG_UNIT));
}
Also used : AnalyticsAggregationType(org.hisp.dhis.analytics.AnalyticsAggregationType) Test(org.junit.jupiter.api.Test)

Aggregations

AnalyticsAggregationType (org.hisp.dhis.analytics.AnalyticsAggregationType)10 DataQueryParams (org.hisp.dhis.analytics.DataQueryParams)5 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)5 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 ListMap (org.hisp.dhis.common.ListMap)3 Period (org.hisp.dhis.period.Period)3 PeriodType (org.hisp.dhis.period.PeriodType)3 YearlyPeriodType (org.hisp.dhis.period.YearlyPeriodType)3 Test (org.junit.jupiter.api.Test)3 DataElement (org.hisp.dhis.dataelement.DataElement)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 Date (java.util.Date)1 DhisConvenienceTest (org.hisp.dhis.DhisConvenienceTest)1 AnalyticsManager (org.hisp.dhis.analytics.AnalyticsManager)1 DataType (org.hisp.dhis.analytics.DataType)1 Category (org.hisp.dhis.category.Category)1 CategoryCombo (org.hisp.dhis.category.CategoryCombo)1 CategoryOption (org.hisp.dhis.category.CategoryOption)1 CategoryOptionCombo (org.hisp.dhis.category.CategoryOptionCombo)1