Search in sources :

Example 21 with DataQueryParams

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

the class DefaultAnalyticsService method addDataElementValues.

/**
     * Adds data element values to the given grid based on the given data query
     * parameters.
     *
     * @param params the {@link DataQueryParams}.
     * @param grid the grid.
     */
private void addDataElementValues(DataQueryParams params, Grid grid) {
    if (!params.getAllDataElements().isEmpty() && !params.isSkipData()) {
        DataQueryParams dataSourceParams = DataQueryParams.newBuilder(params).retainDataDimension(DataDimensionItemType.DATA_ELEMENT).withIncludeNumDen(false).build();
        Map<String, Object> aggregatedDataMap = getAggregatedDataValueMapObjectTyped(dataSourceParams);
        for (Map.Entry<String, Object> entry : aggregatedDataMap.entrySet()) {
            Object value = AnalyticsUtils.getRoundedValueObject(params, entry.getValue());
            grid.addRow().addValues(entry.getKey().split(DIMENSION_SEP)).addValue(value);
            if (params.isIncludeNumDen()) {
                grid.addNullValues(3);
            }
        }
    }
}
Also used : DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) DimensionalObject(org.hisp.dhis.common.DimensionalObject) AnalyticalObject(org.hisp.dhis.common.AnalyticalObject) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) PeriodType.getPeriodTypeFromIsoString(org.hisp.dhis.period.PeriodType.getPeriodTypeFromIsoString) 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 22 with DataQueryParams

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

the class DefaultAnalyticsService method getAggregatedDataValueMap.

/**
     * Returns a mapping between dimension items and values for the given data
     * query and list of indicators. The dimensional items part of the indicator
     * numerators and denominators are used as dimensional item for the aggregated
     * values being retrieved.
     *
     * @param params the {@link DataQueryParams}.
     * @param indicators the list of indicators.
     * @return a dimensional items to aggregate values map.
     */
private Map<String, Double> getAggregatedDataValueMap(DataQueryParams params, List<Indicator> indicators) {
    List<DimensionalItemObject> items = Lists.newArrayList(expressionService.getDimensionalItemObjectsInIndicators(indicators));
    items = DimensionalObjectUtils.replaceOperandTotalsWithDataElements(items);
    DimensionalObject dimension = new BaseDimensionalObject(DimensionalObject.DATA_X_DIM_ID, DimensionType.DATA_X, null, DISPLAY_NAME_DATA_X, items);
    DataQueryParams dataSourceParams = DataQueryParams.newBuilder(params).replaceDimension(dimension).withIncludeNumDen(false).withSkipHeaders(true).withSkipMeta(true).build();
    Grid grid = getAggregatedDataValueGridInternal(dataSourceParams);
    return grid.getAsMap(grid.getWidth() - 1, DimensionalObject.DIMENSION_SEP);
}
Also used : DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) ListGrid(org.hisp.dhis.system.grid.ListGrid) Grid(org.hisp.dhis.common.Grid) DimensionalObject(org.hisp.dhis.common.DimensionalObject) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject)

Example 23 with DataQueryParams

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

the class DefaultAnalyticsService method getAggregatedValueMap.

/**
     * Generates a mapping between a dimension key and the aggregated value. The
     * dimension key is a concatenation of the identifiers of the dimension items
     * separated by "-".
     *
     * @param params the {@link DataQueryParams}.
     * @param tableName the table name to use for the query.
     * @param queryGroupers the list of additional query groupers to use for
     *        query planning, use empty list for none.
     * @return a mapping between a dimension key and aggregated values.
     */
private Map<String, Object> getAggregatedValueMap(DataQueryParams params, String tableName, List<Function<DataQueryParams, List<DataQueryParams>>> queryGroupers) {
    queryPlanner.validateMaintenanceMode();
    int optimalQueries = MathUtils.getWithin(getProcessNo(), 1, MAX_QUERIES);
    int maxLimit = params.isIgnoreLimit() ? 0 : (Integer) systemSettingManager.getSystemSetting(SettingKey.ANALYTICS_MAX_LIMIT);
    Timer timer = new Timer().start().disablePrint();
    QueryPlannerParams plannerParams = QueryPlannerParams.newBuilder().withOptimalQueries(optimalQueries).withTableName(tableName).withQueryGroupers(queryGroupers).build();
    DataQueryGroups queryGroups = queryPlanner.planQuery(params, plannerParams);
    timer.getSplitTime("Planned analytics query, got: " + queryGroups.getLargestGroupSize() + " for optimal: " + optimalQueries);
    Map<String, Object> map = new HashMap<>();
    for (List<DataQueryParams> queries : queryGroups.getSequentialQueries()) {
        List<Future<Map<String, Object>>> futures = new ArrayList<>();
        for (DataQueryParams query : queries) {
            futures.add(analyticsManager.getAggregatedDataValues(query, maxLimit));
        }
        for (Future<Map<String, Object>> future : futures) {
            try {
                Map<String, Object> taskValues = future.get();
                if (taskValues != null) {
                    map.putAll(taskValues);
                }
            } catch (Exception ex) {
                log.error(DebugUtils.getStackTrace(ex));
                log.error(DebugUtils.getStackTrace(ex.getCause()));
                throw new RuntimeException("Error during execution of aggregation query task", ex);
            }
        }
    }
    timer.getTime("Got analytics values");
    return map;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PeriodType.getPeriodTypeFromIsoString(org.hisp.dhis.period.PeriodType.getPeriodTypeFromIsoString) ProcessingHint(org.hisp.dhis.analytics.ProcessingHint) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) Timer(org.hisp.dhis.util.Timer) Future(java.util.concurrent.Future) DataQueryGroups(org.hisp.dhis.analytics.DataQueryGroups) DimensionalObject(org.hisp.dhis.common.DimensionalObject) AnalyticalObject(org.hisp.dhis.common.AnalyticalObject) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) 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) QueryPlannerParams(org.hisp.dhis.analytics.QueryPlannerParams)

Example 24 with DataQueryParams

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

the class DefaultAnalyticsService method addMetaData.

/**
     * Adds meta data values to the given grid based on the given data query
     * parameters.
     *
     * @param params the {@link DataQueryParams}.
     * @param grid the grid.
     */
private void addMetaData(DataQueryParams params, Grid grid) {
    if (!params.isSkipMeta()) {
        Map<String, Object> metaData = new HashMap<>();
        Map<String, Object> internalMetaData = new HashMap<>();
        // -----------------------------------------------------------------
        // Items / names element
        // -----------------------------------------------------------------
        Map<String, String> cocNameMap = AnalyticsUtils.getCocNameMap(params);
        if (params.getApiVersion().ge(DhisApiVersion.V26)) {
            metaData.put(AnalyticsMetaDataKey.ITEMS.getKey(), AnalyticsUtils.getDimensionMetadataItemMap(params));
        } else {
            Map<String, String> uidNameMap = AnalyticsUtils.getDimensionItemNameMap(params);
            uidNameMap.putAll(cocNameMap);
            uidNameMap.put(DATA_X_DIM_ID, DISPLAY_NAME_DATA_X);
            metaData.put(AnalyticsMetaDataKey.NAMES.getKey(), uidNameMap);
        }
        // -----------------------------------------------------------------
        // Item order elements
        // -----------------------------------------------------------------
        Map<String, Object> dimensionItems = new HashMap<>();
        Calendar calendar = PeriodType.getCalendar();
        List<String> periodUids = calendar.isIso8601() ? getDimensionalItemIds(params.getDimensionOrFilterItems(PERIOD_DIM_ID)) : getLocalPeriodIdentifiers(params.getDimensionOrFilterItems(PERIOD_DIM_ID), calendar);
        dimensionItems.put(PERIOD_DIM_ID, periodUids);
        dimensionItems.put(CATEGORYOPTIONCOMBO_DIM_ID, cocNameMap.keySet());
        for (DimensionalObject dim : params.getDimensionsAndFilters()) {
            if (!dimensionItems.keySet().contains(dim.getDimension())) {
                dimensionItems.put(dim.getDimension(), getDimensionalItemIds(dim.getItems()));
            }
        }
        if (params.getApiVersion().ge(DhisApiVersion.V26)) {
            metaData.put(AnalyticsMetaDataKey.DIMENSIONS.getKey(), dimensionItems);
        } else {
            metaData.putAll(dimensionItems);
        }
        // -----------------------------------------------------------------
        // Organisation unit hierarchy
        // -----------------------------------------------------------------
        User user = securityManager.getCurrentUser(params);
        List<OrganisationUnit> organisationUnits = asTypedList(params.getDimensionOrFilterItems(ORGUNIT_DIM_ID));
        Collection<OrganisationUnit> roots = user != null ? user.getOrganisationUnits() : null;
        if (params.isHierarchyMeta()) {
            metaData.put(AnalyticsMetaDataKey.ORG_UNIT_HIERARCHY.getKey(), getParentGraphMap(organisationUnits, roots));
        }
        if (params.isShowHierarchy()) {
            Map<Object, List<?>> ancestorMap = organisationUnits.stream().collect(Collectors.toMap(OrganisationUnit::getUid, ou -> ou.getAncestorNames(roots, true)));
            internalMetaData.put(AnalyticsMetaDataKey.ORG_UNIT_ANCESTORS.getKey(), ancestorMap);
            metaData.put(AnalyticsMetaDataKey.ORG_UNIT_NAME_HIERARCHY.getKey(), getParentNameGraphMap(organisationUnits, roots, true));
        }
        grid.setMetaData(ImmutableMap.copyOf(metaData));
        grid.setInternalMetaData(ImmutableMap.copyOf(internalMetaData));
    }
}
Also used : ListGrid(org.hisp.dhis.system.grid.ListGrid) EventAnalyticsService(org.hisp.dhis.analytics.event.EventAnalyticsService) ValueType(org.hisp.dhis.common.ValueType) Autowired(org.springframework.beans.factory.annotation.Autowired) AnalyticsSecurityManager(org.hisp.dhis.analytics.AnalyticsSecurityManager) CombinationGenerator(org.hisp.dhis.common.CombinationGenerator) Future(java.util.concurrent.Future) Calendar(org.hisp.dhis.calendar.Calendar) Map(java.util.Map) IndicatorValue(org.hisp.dhis.indicator.IndicatorValue) ReportingRateMetric(org.hisp.dhis.common.ReportingRateMetric) Period(org.hisp.dhis.period.Period) ImmutableMap(com.google.common.collect.ImmutableMap) EventQueryParams(org.hisp.dhis.analytics.event.EventQueryParams) ListUtils(org.hisp.dhis.commons.collection.ListUtils) OrganisationUnitGroup(org.hisp.dhis.organisationunit.OrganisationUnitGroup) Collection(java.util.Collection) IRT2D(org.hisp.dhis.reporttable.ReportTable.IRT2D) Set(java.util.Set) AnalyticsTableType(org.hisp.dhis.analytics.table.AnalyticsTableType) RawAnalyticsManager(org.hisp.dhis.analytics.RawAnalyticsManager) DimensionType(org.hisp.dhis.common.DimensionType) Collectors(java.util.stream.Collectors) DataValueSet(org.hisp.dhis.dxf2.datavalueset.DataValueSet) OrganisationUnit.getParentGraphMap(org.hisp.dhis.organisationunit.OrganisationUnit.getParentGraphMap) List(java.util.List) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) DimensionalObject(org.hisp.dhis.common.DimensionalObject) ProcessingHint(org.hisp.dhis.analytics.ProcessingHint) LogFactory(org.apache.commons.logging.LogFactory) DimensionItem(org.hisp.dhis.analytics.DimensionItem) MathUtils(org.hisp.dhis.system.util.MathUtils) DimensionalObjectUtils.asTypedList(org.hisp.dhis.common.DimensionalObjectUtils.asTypedList) AnalyticalObject(org.hisp.dhis.common.AnalyticalObject) DhisApiVersion(org.hisp.dhis.common.DhisApiVersion) ReportTable.addListIfEmpty(org.hisp.dhis.reporttable.ReportTable.addListIfEmpty) AnalyticsMetaDataKey(org.hisp.dhis.analytics.AnalyticsMetaDataKey) DataQueryService(org.hisp.dhis.analytics.DataQueryService) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) IdentifiableObjectUtils(org.hisp.dhis.common.IdentifiableObjectUtils) PeriodType.getPeriodTypeFromIsoString(org.hisp.dhis.period.PeriodType.getPeriodTypeFromIsoString) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) ExpressionService(org.hisp.dhis.expression.ExpressionService) Lists(com.google.common.collect.Lists) ReportTable(org.hisp.dhis.reporttable.ReportTable) GridHeader(org.hisp.dhis.common.GridHeader) DataQueryGroups(org.hisp.dhis.analytics.DataQueryGroups) Indicator(org.hisp.dhis.indicator.Indicator) User(org.hisp.dhis.user.User) DimensionalObjectUtils.getDimensionalItemIds(org.hisp.dhis.common.DimensionalObjectUtils.getDimensionalItemIds) SystemSettingManager(org.hisp.dhis.setting.SystemSettingManager) OrganisationUnit.getParentNameGraphMap(org.hisp.dhis.organisationunit.OrganisationUnit.getParentNameGraphMap) DataDimensionItemType(org.hisp.dhis.common.DataDimensionItemType) DimensionalObjectUtils(org.hisp.dhis.common.DimensionalObjectUtils) DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) Timer(org.hisp.dhis.util.Timer) QueryPlanner(org.hisp.dhis.analytics.QueryPlanner) AggregationType(org.hisp.dhis.analytics.AggregationType) ConstantService(org.hisp.dhis.constant.ConstantService) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) Grid(org.hisp.dhis.common.Grid) OutputFormat(org.hisp.dhis.analytics.OutputFormat) DebugUtils(org.hisp.dhis.commons.util.DebugUtils) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) SystemUtils(org.hisp.dhis.commons.util.SystemUtils) QueryPlannerParams(org.hisp.dhis.analytics.QueryPlannerParams) AnalyticsUtils(org.hisp.dhis.analytics.AnalyticsUtils) PeriodType(org.hisp.dhis.period.PeriodType) Log(org.apache.commons.logging.Log) IdentifiableObjectUtils.getLocalPeriodIdentifiers(org.hisp.dhis.common.IdentifiableObjectUtils.getLocalPeriodIdentifiers) SettingKey(org.hisp.dhis.setting.SettingKey) AnalyticsManager(org.hisp.dhis.analytics.AnalyticsManager) AnalyticsService(org.hisp.dhis.analytics.AnalyticsService) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) User(org.hisp.dhis.user.User) HashMap(java.util.HashMap) Calendar(org.hisp.dhis.calendar.Calendar) PeriodType.getPeriodTypeFromIsoString(org.hisp.dhis.period.PeriodType.getPeriodTypeFromIsoString) DimensionalObject(org.hisp.dhis.common.DimensionalObject) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) DimensionalObject(org.hisp.dhis.common.DimensionalObject) AnalyticalObject(org.hisp.dhis.common.AnalyticalObject) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) List(java.util.List) DimensionalObjectUtils.asTypedList(org.hisp.dhis.common.DimensionalObjectUtils.asTypedList) ArrayList(java.util.ArrayList)

Example 25 with DataQueryParams

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

the class DataQueryServiceTest method testGetFromAnalyticalObjectC.

@Test
public void testGetFromAnalyticalObjectC() {
    Chart chart = new Chart();
    chart.setSeries(DimensionalObject.DATA_X_DIM_ID);
    chart.setCategory(ouGroupSetA.getUid());
    chart.getFilterDimensions().add(DimensionalObject.PERIOD_DIM_ID);
    chart.addDataDimensionItem(deA);
    chart.addDataDimensionItem(pdA);
    chart.addDataDimensionItem(pdB);
    OrganisationUnitGroupSetDimension ouGroupSetDim = new OrganisationUnitGroupSetDimension();
    ouGroupSetDim.setDimension(ouGroupSetA);
    ouGroupSetDim.setItems(Lists.newArrayList(ouGroupA, ouGroupB, ouGroupC));
    chart.getOrganisationUnitGroupSetDimensions().add(ouGroupSetDim);
    chart.getPeriods().add(PeriodType.getPeriodFromIsoString("2012"));
    DataQueryParams params = dataQueryService.getFromAnalyticalObject(chart);
    assertNotNull(params);
    assertEquals(1, params.getDataElements().size());
    assertEquals(2, params.getProgramDataElements().size());
    assertEquals(1, params.getFilterPeriods().size());
    assertEquals(2, params.getDimensions().size());
    assertEquals(1, params.getFilters().size());
    assertEquals(3, params.getDimensionOptions(ouGroupSetA.getUid()).size());
}
Also used : DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) Chart(org.hisp.dhis.chart.Chart) 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