Search in sources :

Example 11 with DataElementGroup

use of org.hisp.dhis.dataelement.DataElementGroup 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 12 with DataElementGroup

use of org.hisp.dhis.dataelement.DataElementGroup in project dhis2-core by dhis2.

the class PreheatServiceTest method testCollectReferenceUidDEG2.

@Test
void testCollectReferenceUidDEG2() {
    DataElementGroup deg1 = createDataElementGroup('A');
    DataElementGroup deg2 = createDataElementGroup('B');
    DataElement de1 = createDataElement('A');
    DataElement de2 = createDataElement('B');
    DataElement de3 = createDataElement('C');
    deg1.addDataElement(de1);
    deg1.addDataElement(de2);
    deg2.addDataElement(de3);
    Map<Class<? extends IdentifiableObject>, Set<String>> references = preheatService.collectReferences(Lists.newArrayList(deg1, deg2)).get(PreheatIdentifier.UID);
    assertTrue(references.containsKey(DataElement.class));
    assertEquals(3, references.get(DataElement.class).size());
    assertTrue(references.get(DataElement.class).contains(de1.getUid()));
    assertTrue(references.get(DataElement.class).contains(de2.getUid()));
    assertTrue(references.get(DataElement.class).contains(de3.getUid()));
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) DataSet(org.hisp.dhis.dataset.DataSet) LegendSet(org.hisp.dhis.legend.LegendSet) Set(java.util.Set) OptionSet(org.hisp.dhis.option.OptionSet) DataElementGroup(org.hisp.dhis.dataelement.DataElementGroup) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 13 with DataElementGroup

use of org.hisp.dhis.dataelement.DataElementGroup in project dhis2-core by dhis2.

the class PreheatServiceTest method testCollectNoObjectsDEG.

@Test
void testCollectNoObjectsDEG() {
    DataElementGroup dataElementGroup = createDataElementGroup('A');
    Map<Class<? extends IdentifiableObject>, Set<String>> references = preheatService.collectReferences(dataElementGroup).get(PreheatIdentifier.UID);
    assertFalse(references.containsKey(DataElement.class));
    assertFalse(references.containsKey(User.class));
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) DataSet(org.hisp.dhis.dataset.DataSet) LegendSet(org.hisp.dhis.legend.LegendSet) Set(java.util.Set) OptionSet(org.hisp.dhis.option.OptionSet) User(org.hisp.dhis.user.User) DataElementGroup(org.hisp.dhis.dataelement.DataElementGroup) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 14 with DataElementGroup

use of org.hisp.dhis.dataelement.DataElementGroup in project dhis2-core by dhis2.

the class PreheatServiceTest method testCollectReferenceCodeDEG2.

@Test
void testCollectReferenceCodeDEG2() {
    DataElementGroup deg1 = createDataElementGroup('A');
    DataElementGroup deg2 = createDataElementGroup('B');
    DataElement de1 = createDataElement('A');
    DataElement de2 = createDataElement('B');
    DataElement de3 = createDataElement('C');
    deg1.addDataElement(de1);
    deg1.addDataElement(de2);
    deg2.addDataElement(de3);
    Map<Class<? extends IdentifiableObject>, Set<String>> references = preheatService.collectReferences(Lists.newArrayList(deg1, deg2)).get(PreheatIdentifier.CODE);
    assertTrue(references.containsKey(DataElement.class));
    assertEquals(3, references.get(DataElement.class).size());
    assertTrue(references.get(DataElement.class).contains(de1.getCode()));
    assertTrue(references.get(DataElement.class).contains(de2.getCode()));
    assertTrue(references.get(DataElement.class).contains(de3.getCode()));
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) DataSet(org.hisp.dhis.dataset.DataSet) LegendSet(org.hisp.dhis.legend.LegendSet) Set(java.util.Set) OptionSet(org.hisp.dhis.option.OptionSet) DataElementGroup(org.hisp.dhis.dataelement.DataElementGroup) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 15 with DataElementGroup

use of org.hisp.dhis.dataelement.DataElementGroup in project dhis2-core by dhis2.

the class PreheatServiceTest method testPreheatReferenceConnectUID.

@Test
void testPreheatReferenceConnectUID() {
    DataElementGroup dataElementGroup = fromJson("preheat/degAUidRef.json", DataElementGroup.class);
    defaultSetup();
    List<User> allUsers = userService.getAllUsers();
    PreheatParams params = new PreheatParams();
    params.setPreheatMode(PreheatMode.REFERENCE);
    params.getObjects().put(DataElementGroup.class, Lists.newArrayList(dataElementGroup));
    preheatService.validate(params);
    Preheat preheat = preheatService.preheat(params);
    preheatService.connectReferences(dataElementGroup, preheat, PreheatIdentifier.UID);
    List<DataElement> members = new ArrayList<>(dataElementGroup.getMembers());
    assertContains(members, "DataElementA", "DataElementCodeA");
    assertContains(members, "DataElementB", "DataElementCodeB");
    assertContains(members, "DataElementC", "DataElementCodeC");
    User createdBy = dataElementGroup.getCreatedBy();
    assertEquals("FirstNameA", createdBy.getFirstName());
    assertEquals("SurnameA", createdBy.getSurname());
    assertEquals("UserCodeA", createdBy.getCode());
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) User(org.hisp.dhis.user.User) DataElementGroup(org.hisp.dhis.dataelement.DataElementGroup) ArrayList(java.util.ArrayList) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Aggregations

DataElementGroup (org.hisp.dhis.dataelement.DataElementGroup)55 DataElement (org.hisp.dhis.dataelement.DataElement)31 Test (org.junit.jupiter.api.Test)29 TransactionalIntegrationTest (org.hisp.dhis.TransactionalIntegrationTest)17 ArrayList (java.util.ArrayList)15 User (org.hisp.dhis.user.User)15 DataSet (org.hisp.dhis.dataset.DataSet)9 List (java.util.List)8 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)8 Set (java.util.Set)7 DhisSpringTest (org.hisp.dhis.DhisSpringTest)7 DataQueryParams (org.hisp.dhis.analytics.DataQueryParams)6 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)6 DataElementGroupSet (org.hisp.dhis.dataelement.DataElementGroupSet)6 DimensionalObject (org.hisp.dhis.common.DimensionalObject)5 LegendSet (org.hisp.dhis.legend.LegendSet)5 OptionSet (org.hisp.dhis.option.OptionSet)5 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)5 PeriodType (org.hisp.dhis.period.PeriodType)5 DimensionItemKeywords (org.hisp.dhis.common.DimensionItemKeywords)4