Search in sources :

Example 16 with DataQueryParams

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

the class DefaultQueryPlanner method splitByDimension.

// -------------------------------------------------------------------------
// Supportive methods
// -------------------------------------------------------------------------
/**
     * Splits the given list of queries in sub queries on the given dimension.
     */
private DataQueryGroups splitByDimension(DataQueryGroups queryGroups, String dimension, int optimalQueries) {
    int optimalForSubQuery = MathUtils.divideToFloor(optimalQueries, queryGroups.getLargestGroupSize());
    List<DataQueryParams> subQueries = new ArrayList<>();
    for (DataQueryParams query : queryGroups.getAllQueries()) {
        DimensionalObject dim = query.getDimension(dimension);
        List<DimensionalItemObject> values = null;
        if (dim == null || (values = dim.getItems()) == null || values.isEmpty()) {
            subQueries.add(DataQueryParams.newBuilder(query).build());
            continue;
        }
        List<List<DimensionalItemObject>> valuePages = new PaginatedList<>(values).setNumberOfPages(optimalForSubQuery).getPages();
        for (List<DimensionalItemObject> valuePage : valuePages) {
            DataQueryParams subQuery = DataQueryParams.newBuilder(query).withDimensionOptions(dim.getDimension(), valuePage).build();
            subQueries.add(subQuery);
        }
    }
    if (subQueries.size() > queryGroups.getAllQueries().size()) {
        log.debug(String.format("Split on dimension %s: %d", dimension, (subQueries.size() / queryGroups.getAllQueries().size())));
    }
    return DataQueryGroups.newBuilder().withQueries(subQueries).build();
}
Also used : DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) PaginatedList(org.hisp.dhis.commons.collection.PaginatedList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) DimensionalObjectUtils.asTypedList(org.hisp.dhis.common.DimensionalObjectUtils.asTypedList) PaginatedList(org.hisp.dhis.commons.collection.PaginatedList) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) DimensionalObject(org.hisp.dhis.common.DimensionalObject)

Example 17 with DataQueryParams

use of org.hisp.dhis.analytics.DataQueryParams 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 data query parameters.
     * @return a list of {@link DataQueryParams}.
     */
private List<DataQueryParams> groupByAggregationType(DataQueryParams params) {
    List<DataQueryParams> queries = new ArrayList<>();
    if (!params.getDataElements().isEmpty()) {
        ListMap<AggregationType, DimensionalItemObject> aggregationTypeDataElementMap = QueryPlannerUtils.getAggregationTypeDataElementMap(params);
        for (AggregationType aggregationType : aggregationTypeDataElementMap.keySet()) {
            DataQueryParams query = DataQueryParams.newBuilder(params).withDataElements(aggregationTypeDataElementMap.get(aggregationType)).withAggregationType(aggregationType).build();
            queries.add(query);
        }
    } else if (!params.getDataElementGroupSets().isEmpty()) {
        DimensionalObject degs = params.getDataElementGroupSets().get(0);
        DataElementGroup deg = (DataElementGroup) (degs.hasItems() ? degs.getItems().get(0) : null);
        AggregationType aggregationType = ObjectUtils.firstNonNull(params.getAggregationType(), SUM);
        if (deg != null && !deg.getMembers().isEmpty()) {
            PeriodType periodType = PeriodType.getPeriodTypeByName(params.getPeriodType());
            aggregationType = ObjectUtils.firstNonNull(params.getAggregationType(), deg.getAggregationType());
            aggregationType = QueryPlannerUtils.getAggregationType(deg.getValueType(), aggregationType, periodType, deg.getPeriodType());
        }
        DataQueryParams query = DataQueryParams.newBuilder(params).withAggregationType(aggregationType).build();
        queries.add(query);
    } else {
        DataQueryParams query = DataQueryParams.newBuilder(params).withAggregationType(ObjectUtils.firstNonNull(params.getAggregationType(), SUM)).build();
        queries.add(query);
    }
    if (queries.size() > 1) {
        log.debug(String.format("Split on aggregation type: %d", queries.size()));
    }
    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) AggregationType(org.hisp.dhis.analytics.AggregationType) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) DimensionalObject(org.hisp.dhis.common.DimensionalObject)

Example 18 with DataQueryParams

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

the class DataValidationTask method getEventMapForSlidingWindow.

private MapMap<String, DimensionalItemObject, Double> getEventMapForSlidingWindow(Set<DimensionalItemObject> dimensionItems, Period period, OrganisationUnit organisationUnit) {
    MapMap<String, DimensionalItemObject, Double> map = new MapMap<>();
    if (dimensionItems.isEmpty() || period == null || organisationUnit == null) {
        return map;
    }
    // We want to position the sliding window over the most recent data. To achieve this, we need to satisfy the
    // following criteria:
    //
    // 1. Window end should not be later than the current date
    // 2. Window end should not be later than the period.endDate
    // Criteria 1
    Calendar endDate = Calendar.getInstance();
    Calendar startDate = Calendar.getInstance();
    // Criteria 2
    if (endDate.getTime().after(period.getEndDate())) {
        endDate.setTime(period.getEndDate());
    }
    // The window size is based on the frequencyOrder of the period's periodType:
    startDate.setTime(endDate.getTime());
    startDate.add(Calendar.DATE, (-1 * period.frequencyOrder()));
    DataQueryParams params = DataQueryParams.newBuilder().withDataDimensionItems(Lists.newArrayList(dimensionItems)).withAttributeOptionCombos(Lists.newArrayList()).withStartDate(startDate.getTime()).withEndDate(endDate.getTime()).withFilterOrganisationUnits(Lists.newArrayList(organisationUnit)).build();
    return getEventData(params);
}
Also used : DataQueryParams(org.hisp.dhis.analytics.DataQueryParams)

Example 19 with DataQueryParams

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

the class DataValidationTask method getEventMap.

/**
     * Returns aggregated event data for the given parameters.
     *
     * @param dimensionItems   the data dimension items.
     * @param period           the period.
     * @param organisationUnit the organisation unit.
     * @return a map mapping of attribute option combo identifier to data element operand
     * and value.
     */
private MapMap<String, DimensionalItemObject, Double> getEventMap(Set<DimensionalItemObject> dimensionItems, Period period, OrganisationUnit organisationUnit) {
    MapMap<String, DimensionalItemObject, Double> map = new MapMap<>();
    if (dimensionItems.isEmpty() || period == null || organisationUnit == null) {
        return map;
    }
    DataQueryParams params = DataQueryParams.newBuilder().withDataDimensionItems(Lists.newArrayList(dimensionItems)).withAttributeOptionCombos(Lists.newArrayList()).withFilterPeriods(Lists.newArrayList(period)).withFilterOrganisationUnits(Lists.newArrayList(organisationUnit)).build();
    return getEventData(params);
}
Also used : DataQueryParams(org.hisp.dhis.analytics.DataQueryParams)

Example 20 with DataQueryParams

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

the class DataQueryServiceTest method testGetFromUrlD.

@Test
public void testGetFromUrlD() {
    Set<String> dimensionParams = new HashSet<>();
    dimensionParams.add("dx:" + deA.getDimensionItem() + ";" + deB.getDimensionItem() + ";" + patA.getDimensionItem() + ";" + patB.getDimensionItem());
    Set<String> filterParams = new HashSet<>();
    filterParams.add("ou:" + ouA.getDimensionItem());
    DataQueryParams params = dataQueryService.getFromUrl(dimensionParams, filterParams, null, null, null, null, null, false, false, false, false, false, false, false, false, false, false, null, null, null, false, null, null, null, false, null);
    assertEquals(2, params.getDataElements().size());
    assertEquals(2, params.getProgramAttributes().size());
    assertEquals(1, params.getFilterOrganisationUnits().size());
}
Also used : DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Aggregations

DataQueryParams (org.hisp.dhis.analytics.DataQueryParams)81 Test (org.junit.Test)52 DhisSpringTest (org.hisp.dhis.DhisSpringTest)51 BaseDimensionalObject (org.hisp.dhis.common.BaseDimensionalObject)24 QueryPlannerParams (org.hisp.dhis.analytics.QueryPlannerParams)22 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)19 DataQueryGroups (org.hisp.dhis.analytics.DataQueryGroups)17 ArrayList (java.util.ArrayList)16 DimensionalObject (org.hisp.dhis.common.DimensionalObject)15 HashSet (java.util.HashSet)14 LinkedHashSet (java.util.LinkedHashSet)13 HashMap (java.util.HashMap)7 PeriodType.getPeriodTypeFromIsoString (org.hisp.dhis.period.PeriodType.getPeriodTypeFromIsoString)7 ImmutableMap (com.google.common.collect.ImmutableMap)6 List (java.util.List)6 Map (java.util.Map)6 OrganisationUnit.getParentGraphMap (org.hisp.dhis.organisationunit.OrganisationUnit.getParentGraphMap)6 OrganisationUnit.getParentNameGraphMap (org.hisp.dhis.organisationunit.OrganisationUnit.getParentNameGraphMap)6 EventQueryParams (org.hisp.dhis.analytics.event.EventQueryParams)5 Grid (org.hisp.dhis.common.Grid)5