Search in sources :

Example 6 with IndicatorValue

use of org.hisp.dhis.indicator.IndicatorValue in project dhis2-core by dhis2.

the class ExpressionServiceTest method testGetIndicatorValueObject.

@Test
public void testGetIndicatorValueObject() {
    IndicatorType indicatorType = new IndicatorType("A", 100, false);
    Indicator indicatorA = createIndicator('A', indicatorType);
    indicatorA.setNumerator(expressionE);
    indicatorA.setDenominator(expressionF);
    Indicator indicatorB = createIndicator('B', indicatorType);
    indicatorB.setNumerator(expressionN);
    indicatorB.setDenominator(expressionF);
    Map<DataElementOperand, Double> valueMap = new HashMap<>();
    valueMap.put(new DataElementOperand(deA, coc), 12d);
    valueMap.put(new DataElementOperand(deB, coc), 34d);
    valueMap.put(new DataElementOperand(deA, cocA, cocB), 46d);
    valueMap.put(new DataElementOperand(deB, cocA), 10d);
    Map<String, Double> constantMap = new HashMap<>();
    constantMap.put(constantA.getUid(), 2.0);
    IndicatorValue value = expressionService.getIndicatorValueObject(indicatorA, period, valueMap, constantMap, null);
    assertEquals(24d, value.getNumeratorValue(), DELTA);
    assertEquals(12d, value.getDenominatorValue(), DELTA);
    assertEquals(100, value.getFactor());
    assertEquals(200d, value.getValue(), DELTA);
    value = expressionService.getIndicatorValueObject(indicatorB, period, valueMap, constantMap, null);
    assertEquals(36d, value.getNumeratorValue(), DELTA);
    assertEquals(12d, value.getDenominatorValue(), DELTA);
    assertEquals(100, value.getFactor());
    assertEquals(300d, value.getValue(), DELTA);
}
Also used : IndicatorType(org.hisp.dhis.indicator.IndicatorType) IndicatorValue(org.hisp.dhis.indicator.IndicatorValue) Indicator(org.hisp.dhis.indicator.Indicator) ProgramIndicator(org.hisp.dhis.program.ProgramIndicator) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 7 with IndicatorValue

use of org.hisp.dhis.indicator.IndicatorValue 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 8 with IndicatorValue

use of org.hisp.dhis.indicator.IndicatorValue in project dhis2-core by dhis2.

the class ExpressionService2Test method testGetIndicatorDimensionalItemMap2.

@Test
void testGetIndicatorDimensionalItemMap2() {
    Set<DimensionalItemId> itemIds = Sets.newHashSet(getId(opA));
    Map<DimensionalItemId, DimensionalItemObject> expectedItemMap = ImmutableMap.of(getId(opA), opA);
    when(dimensionService.getDataDimensionalItemObjectMap(itemIds)).thenReturn(expectedItemMap);
    mockConstantService();
    IndicatorType indicatorType = new IndicatorType("A", 100, false);
    Indicator indicatorA = createIndicator('A', indicatorType);
    indicatorA.setNumerator(expressionE);
    indicatorA.setDenominator(expressionF);
    List<Indicator> indicators = Arrays.asList(indicatorA);
    Map<DimensionalItemId, DimensionalItemObject> itemMap = target.getIndicatorDimensionalItemMap(indicators);
    Map<DimensionalItemObject, Object> valueMap = new HashMap<>();
    valueMap.put(new DataElementOperand(deA, coc), 12d);
    IndicatorValue value = target.getIndicatorValueObject(indicatorA, Collections.singletonList(period), itemMap, valueMap, null);
    assertNotNull(value);
    assertEquals(24d, value.getNumeratorValue(), DELTA);
    assertEquals(12d, value.getDenominatorValue(), DELTA);
    assertEquals(100, value.getMultiplier());
    assertEquals(1, value.getDivisor());
    assertEquals(100d, value.getFactor(), DELTA);
    assertEquals(200d, value.getValue(), DELTA);
}
Also used : DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) DimensionalItemId(org.hisp.dhis.common.DimensionalItemId) IndicatorType(org.hisp.dhis.indicator.IndicatorType) IndicatorValue(org.hisp.dhis.indicator.IndicatorValue) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) HashMap(java.util.HashMap) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) ProgramIndicator(org.hisp.dhis.program.ProgramIndicator) Indicator(org.hisp.dhis.indicator.Indicator) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 9 with IndicatorValue

use of org.hisp.dhis.indicator.IndicatorValue in project dhis2-core by dhis2.

the class ExpressionService2Test method testAnnualizedIndicatorValueWhenHavingNullPeriods.

@Test
void testAnnualizedIndicatorValueWhenHavingNullPeriods() {
    Set<DimensionalItemId> itemIds = Sets.newHashSet(getId(opA));
    Map<DimensionalItemId, DimensionalItemObject> expectedItemMap = ImmutableMap.of(getId(opA), opA);
    when(dimensionService.getDataDimensionalItemObjectMap(itemIds)).thenReturn(expectedItemMap);
    mockConstantService();
    IndicatorType indicatorType = new IndicatorType("A", 100, false);
    Indicator indicatorA = createIndicator('A', indicatorType);
    indicatorA.setAnnualized(true);
    indicatorA.setNumerator(expressionE);
    indicatorA.setDenominator(expressionF);
    Map<DimensionalItemObject, Object> valueMap = new HashMap<>();
    valueMap.put(new DataElementOperand(deA, coc), 12d);
    valueMap.put(new DataElementOperand(deB, coc), 34d);
    valueMap.put(new DataElementOperand(deA, cocA, cocB), 46d);
    valueMap.put(new DataElementOperand(deB, cocA), 10d);
    Map<DimensionalItemId, DimensionalItemObject> itemMap = target.getIndicatorDimensionalItemMap(Arrays.asList(indicatorA));
    IndicatorValue value = target.getIndicatorValueObject(indicatorA, null, itemMap, valueMap, null);
    assertNotNull(value);
    assertEquals(24d, value.getNumeratorValue(), DELTA);
    assertEquals(12d, value.getDenominatorValue(), DELTA);
    assertEquals(100, value.getMultiplier());
    assertEquals(1, value.getDivisor());
    assertEquals(100.0d, Precision.round(value.getFactor(), 2), DELTA);
    assertEquals(200.0d, Precision.round(value.getValue(), 2), DELTA);
}
Also used : DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) DimensionalItemId(org.hisp.dhis.common.DimensionalItemId) IndicatorType(org.hisp.dhis.indicator.IndicatorType) IndicatorValue(org.hisp.dhis.indicator.IndicatorValue) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) HashMap(java.util.HashMap) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) ProgramIndicator(org.hisp.dhis.program.ProgramIndicator) Indicator(org.hisp.dhis.indicator.Indicator) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 10 with IndicatorValue

use of org.hisp.dhis.indicator.IndicatorValue in project dhis2-core by dhis2.

the class DataHandler 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.
 */
void addIndicatorValues(DataQueryParams params, Grid grid) {
    if (!params.getIndicators().isEmpty() && !params.isSkipData()) {
        DataQueryParams dataSourceParams = newBuilder(params).retainDataDimension(INDICATOR).withIncludeNumDen(false).build();
        List<Indicator> indicators = resolveIndicatorExpressions(dataSourceParams);
        // Try to get filters periods from dimension (pe), or else fall back
        // to "startDate/endDate" periods
        List<Period> filterPeriods = isNotEmpty(dataSourceParams.getTypedFilterPeriods()) ? dataSourceParams.getTypedFilterPeriods() : dataSourceParams.getStartEndDatesToSingleList();
        // -----------------------------------------------------------------
        // Get indicator values
        // -----------------------------------------------------------------
        Map<String, Map<String, Integer>> permutationOrgUnitTargetMap = getOrgUnitTargetMap(dataSourceParams, indicators);
        List<List<DimensionItem>> dimensionItemPermutations = dataSourceParams.getDimensionItemPermutations();
        Map<DimensionalItemId, DimensionalItemObject> itemMap = expressionService.getIndicatorDimensionalItemMap(indicators);
        Map<String, List<DimensionItemObjectValue>> permutationDimensionItemValueMap = getPermutationDimensionItemValueMap(params, new ArrayList<>(itemMap.values()));
        handleEmptyDimensionItemPermutations(dimensionItemPermutations);
        for (Indicator indicator : indicators) {
            for (List<DimensionItem> dimensionItems : dimensionItemPermutations) {
                IndicatorValue value = getIndicatorValue(filterPeriods, itemMap, permutationOrgUnitTargetMap, permutationDimensionItemValueMap, indicator, dimensionItems);
                addIndicatorValuesToGrid(params, grid, dataSourceParams, indicator, dimensionItems, value);
            }
        }
    }
}
Also used : DimensionalItemId(org.hisp.dhis.common.DimensionalItemId) IndicatorValue(org.hisp.dhis.indicator.IndicatorValue) DimensionalObjectUtils.getDimensionItem(org.hisp.dhis.common.DimensionalObjectUtils.getDimensionItem) DimensionItem(org.hisp.dhis.analytics.DimensionItem) GridUtils.getGridIndexByDimensionItem(org.hisp.dhis.system.grid.GridUtils.getGridIndexByDimensionItem) Period(org.hisp.dhis.period.Period) AnalyticsUtils.hasPeriod(org.hisp.dhis.analytics.util.AnalyticsUtils.hasPeriod) PeriodType.getPeriodTypeFromIsoString(org.hisp.dhis.period.PeriodType.getPeriodTypeFromIsoString) Indicator(org.hisp.dhis.indicator.Indicator) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) EventQueryParams.fromDataQueryParams(org.hisp.dhis.analytics.event.EventQueryParams.fromDataQueryParams) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) Collections.singletonList(java.util.Collections.singletonList) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) DimensionalObjectUtils.asTypedList(org.hisp.dhis.common.DimensionalObjectUtils.asTypedList) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) DataQueryParams.getPermutationDimensionalItemValueMap(org.hisp.dhis.analytics.DataQueryParams.getPermutationDimensionalItemValueMap) DataQueryParams.getPermutationOrgUnitGroupCountMap(org.hisp.dhis.analytics.DataQueryParams.getPermutationOrgUnitGroupCountMap) MultiValuedMap(org.apache.commons.collections4.MultiValuedMap) AnalyticsUtils.getDoubleMap(org.hisp.dhis.analytics.util.AnalyticsUtils.getDoubleMap) DimensionalObjectUtils.convertToDimItemValueMap(org.hisp.dhis.common.DimensionalObjectUtils.convertToDimItemValueMap) HashMap(java.util.HashMap) ArrayListValuedHashMap(org.apache.commons.collections4.multimap.ArrayListValuedHashMap)

Aggregations

IndicatorValue (org.hisp.dhis.indicator.IndicatorValue)11 Indicator (org.hisp.dhis.indicator.Indicator)10 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)9 HashMap (java.util.HashMap)8 DhisSpringTest (org.hisp.dhis.DhisSpringTest)8 DimensionalItemId (org.hisp.dhis.common.DimensionalItemId)8 ProgramIndicator (org.hisp.dhis.program.ProgramIndicator)8 IndicatorType (org.hisp.dhis.indicator.IndicatorType)7 Test (org.junit.jupiter.api.Test)7 DataElementOperand (org.hisp.dhis.dataelement.DataElementOperand)6 Period (org.hisp.dhis.period.Period)4 ArrayList (java.util.ArrayList)3 List (java.util.List)2 Map (java.util.Map)2 DataQueryParams (org.hisp.dhis.analytics.DataQueryParams)2 DimensionItem (org.hisp.dhis.analytics.DimensionItem)2 DimensionalObjectUtils.asTypedList (org.hisp.dhis.common.DimensionalObjectUtils.asTypedList)2 PeriodType.getPeriodTypeFromIsoString (org.hisp.dhis.period.PeriodType.getPeriodTypeFromIsoString)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1