Search in sources :

Example 61 with DataQueryParams

use of org.hisp.dhis.analytics.DataQueryParams 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 62 with DataQueryParams

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

the class DefaultAnalyticsService method addIndicatorValues.

/**
     * Adds indicator values to the given grid based on the given data query
     * parameters.
     *
     * @param params the {@link DataQueryParams}.
     * @param grid the grid.
     */
private void addIndicatorValues(DataQueryParams params, Grid grid) {
    if (!params.getIndicators().isEmpty() && !params.isSkipData()) {
        DataQueryParams dataSourceParams = DataQueryParams.newBuilder(params).retainDataDimension(DataDimensionItemType.INDICATOR).withIncludeNumDen(false).build();
        List<Indicator> indicators = asTypedList(dataSourceParams.getIndicators());
        Period filterPeriod = dataSourceParams.getFilterPeriod();
        Map<String, Double> constantMap = constantService.getConstantMap();
        // -----------------------------------------------------------------
        // Get indicator values
        // -----------------------------------------------------------------
        Map<String, Map<String, Integer>> permutationOrgUnitTargetMap = getOrgUnitTargetMap(dataSourceParams, indicators);
        List<List<DimensionItem>> dimensionItemPermutations = dataSourceParams.getDimensionItemPermutations();
        Map<String, Map<DimensionalItemObject, Double>> permutationDimensionItemValueMap = getPermutationDimensionItemValueMap(dataSourceParams);
        for (Indicator indicator : indicators) {
            for (List<DimensionItem> dimensionItems : dimensionItemPermutations) {
                String permKey = DimensionItem.asItemKey(dimensionItems);
                Map<DimensionalItemObject, Double> valueMap = permutationDimensionItemValueMap.get(permKey);
                if (valueMap == null) {
                    continue;
                }
                Period period = filterPeriod != null ? filterPeriod : (Period) DimensionItem.getPeriodItem(dimensionItems);
                OrganisationUnit unit = (OrganisationUnit) DimensionItem.getOrganisationUnitItem(dimensionItems);
                String ou = unit != null ? unit.getUid() : null;
                Map<String, Integer> orgUnitCountMap = permutationOrgUnitTargetMap != null ? permutationOrgUnitTargetMap.get(ou) : null;
                IndicatorValue value = expressionService.getIndicatorValueObject(indicator, period, valueMap, constantMap, orgUnitCountMap);
                if (value != null) {
                    List<DimensionItem> row = new ArrayList<>(dimensionItems);
                    row.add(DX_INDEX, new DimensionItem(DATA_X_DIM_ID, indicator));
                    grid.addRow().addValues(DimensionItem.getItemIdentifiers(row)).addValue(AnalyticsUtils.getRoundedValue(dataSourceParams, indicator.getDecimals(), value.getValue()));
                    if (params.isIncludeNumDen()) {
                        grid.addValue(AnalyticsUtils.getRoundedValue(dataSourceParams, indicator.getDecimals(), value.getNumeratorValue())).addValue(AnalyticsUtils.getRoundedValue(dataSourceParams, indicator.getDecimals(), value.getDenominatorValue())).addValue(AnalyticsUtils.getRoundedValue(dataSourceParams, indicator.getDecimals(), value.getFactorAnnualizedValue()));
                    }
                }
            }
        }
    }
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) IndicatorValue(org.hisp.dhis.indicator.IndicatorValue) DimensionItem(org.hisp.dhis.analytics.DimensionItem) ArrayList(java.util.ArrayList) Period(org.hisp.dhis.period.Period) PeriodType.getPeriodTypeFromIsoString(org.hisp.dhis.period.PeriodType.getPeriodTypeFromIsoString) Indicator(org.hisp.dhis.indicator.Indicator) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) List(java.util.List) DimensionalObjectUtils.asTypedList(org.hisp.dhis.common.DimensionalObjectUtils.asTypedList) ArrayList(java.util.ArrayList) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) OrganisationUnit.getParentGraphMap(org.hisp.dhis.organisationunit.OrganisationUnit.getParentGraphMap) HashMap(java.util.HashMap) OrganisationUnit.getParentNameGraphMap(org.hisp.dhis.organisationunit.OrganisationUnit.getParentNameGraphMap)

Example 63 with DataQueryParams

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

the class DefaultAnalyticsService method getOrgUnitTargetMap.

// -------------------------------------------------------------------------
// Supportive methods
// -------------------------------------------------------------------------
/**
     * Generates a mapping of permutations keys (organisation unit id or null)
     * and mappings of organisation unit group and counts.
     *
     * @param params the {@link DataQueryParams}.
     * @param indicators the indicators for which formulas to scan for organisation
     *        unit groups.
     * @return a map of maps.
     */
private Map<String, Map<String, Integer>> getOrgUnitTargetMap(DataQueryParams params, Collection<Indicator> indicators) {
    Set<OrganisationUnitGroup> orgUnitGroups = expressionService.getOrganisationUnitGroupsInIndicators(indicators);
    if (orgUnitGroups.isEmpty()) {
        return null;
    }
    DataQueryParams orgUnitTargetParams = DataQueryParams.newBuilder(params).pruneToDimensionType(DimensionType.ORGANISATION_UNIT).addDimension(new BaseDimensionalObject(DimensionalObject.ORGUNIT_GROUP_DIM_ID, null, new ArrayList<DimensionalItemObject>(orgUnitGroups))).withSkipPartitioning(true).build();
    Map<String, Double> orgUnitCountMap = getAggregatedOrganisationUnitTargetMap(orgUnitTargetParams);
    return DataQueryParams.getPermutationOrgUnitGroupCountMap(orgUnitCountMap);
}
Also used : OrganisationUnitGroup(org.hisp.dhis.organisationunit.OrganisationUnitGroup) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) ArrayList(java.util.ArrayList) PeriodType.getPeriodTypeFromIsoString(org.hisp.dhis.period.PeriodType.getPeriodTypeFromIsoString)

Example 64 with DataQueryParams

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

the class DefaultQueryPlanner method groupByDataPeriodType.

/**
     * Groups the given query in sub queries based on the period type of its
     * data elements. Sets the data period type on each query. This only applies
     * if the aggregation type of the query involves disaggregation.
     * 
     * @param params the data query parameters.
     * @return a list of {@link DataQueryParams}.
     */
private List<DataQueryParams> groupByDataPeriodType(DataQueryParams params) {
    List<DataQueryParams> queries = new ArrayList<>();
    if (params.getDataElements().isEmpty() || !params.isDisaggregation()) {
        queries.add(DataQueryParams.newBuilder(params).build());
        return queries;
    }
    ListMap<PeriodType, DimensionalItemObject> periodTypeDataElementMap = QueryPlannerUtils.getPeriodTypeDataElementMap(params.getDataElements());
    for (PeriodType periodType : periodTypeDataElementMap.keySet()) {
        DataQueryParams query = DataQueryParams.newBuilder(params).withDataElements(periodTypeDataElementMap.get(periodType)).withDataPeriodType(periodType).build();
        queries.add(query);
    }
    if (queries.size() > 1) {
        log.debug(String.format("Split on data period 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)

Example 65 with DataQueryParams

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

the class DefaultQueryPlanner method planQuery.

@Override
public DataQueryGroups planQuery(DataQueryParams params, QueryPlannerParams plannerParams) {
    validate(params);
    // ---------------------------------------------------------------------
    // Group queries which can be executed together
    // ---------------------------------------------------------------------
    final List<DataQueryParams> queries = new ArrayList<>(groupByPartition(params, plannerParams));
    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 -> groupByAggregationType(q)).add(q -> groupByDaysInPeriod(q)).add(q -> groupByDataPeriodType(q)).addAll(plannerParams.getQueryGroupers()).build();
    for (Function<DataQueryParams, List<DataQueryParams>> grouper : groupers) {
        List<DataQueryParams> currentQueries = Lists.newArrayList(queries);
        queries.clear();
        for (DataQueryParams query : currentQueries) {
            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 : 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) Autowired(org.springframework.beans.factory.annotation.Autowired) COMPLETENESS_DIMENSION_TYPES(org.hisp.dhis.analytics.DataQueryParams.COMPLETENESS_DIMENSION_TYPES) ProgramDataElementDimensionItem(org.hisp.dhis.program.ProgramDataElementDimensionItem) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) Function(java.util.function.Function) PartitionManager(org.hisp.dhis.analytics.partition.PartitionManager) PartitionUtils(org.hisp.dhis.analytics.table.PartitionUtils) 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) DataQueryGroups(org.hisp.dhis.analytics.DataQueryGroups) AggregatableDataElementFilter(org.hisp.dhis.system.filter.AggregatableDataElementFilter) SystemSettingManager(org.hisp.dhis.setting.SystemSettingManager) Period(org.hisp.dhis.period.Period) Partitions(org.hisp.dhis.analytics.Partitions) QueryPlanner(org.hisp.dhis.analytics.QueryPlanner) MaintenanceModeException(org.hisp.dhis.common.MaintenanceModeException) SUM(org.hisp.dhis.analytics.AggregationType.SUM) AggregationType(org.hisp.dhis.analytics.AggregationType) FilterUtils(org.hisp.dhis.commons.filter.FilterUtils) Set(java.util.Set) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) DimensionType(org.hisp.dhis.common.DimensionType) OutputFormat(org.hisp.dhis.analytics.OutputFormat) ObjectUtils(org.hisp.dhis.util.ObjectUtils) List(java.util.List) QueryPlannerParams(org.hisp.dhis.analytics.QueryPlannerParams) IdentifiableObjectUtils.getUids(org.hisp.dhis.common.IdentifiableObjectUtils.getUids) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) DimensionalObject(org.hisp.dhis.common.DimensionalObject) PeriodType(org.hisp.dhis.period.PeriodType) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) SettingKey(org.hisp.dhis.setting.SettingKey) DataElementGroup(org.hisp.dhis.dataelement.DataElementGroup) MathUtils(org.hisp.dhis.system.util.MathUtils) DimensionalObjectUtils.asTypedList(org.hisp.dhis.common.DimensionalObjectUtils.asTypedList) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) Function(java.util.function.Function) 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) DataQueryGroups(org.hisp.dhis.analytics.DataQueryGroups)

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