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;
}
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()));
}
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);
}
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"));
}
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));
}
Aggregations