Search in sources :

Example 1 with DimensionItemObjectValue

use of org.hisp.dhis.common.DimensionItemObjectValue in project dhis2-core by dhis2.

the class DataHandler method getIndicatorValue.

/**
 * Based on the given Indicator plus additional parameters, this method will
 * find the respective IndicatorValue.
 *
 * @param filterPeriods the filter periods.
 *        See @{{@link ConstantService#getConstantMap()}}.
 * @param permutationOrgUnitTargetMap the org unit permutation map. See
 *        {@link #getOrgUnitTargetMap(DataQueryParams, Collection)}.
 * @param itemMap Every dimensional item to process.
 * @param permutationDimensionItemValueMap the dimension item permutation
 *        map. See
 *        {@link #getPermutationDimensionItemValueMap(DataQueryParams,
 *        List<DimensionalItemObject>)}.
 * @param indicator the input Indicator where the IndicatorValue will be
 *        based.
 * @param dimensionItems the dimensional items permutation map. See
 *        {@link DataQueryParams#getDimensionItemPermutations()}.
 * @return the IndicatorValue
 */
private IndicatorValue getIndicatorValue(List<Period> filterPeriods, Map<DimensionalItemId, DimensionalItemObject> itemMap, Map<String, Map<String, Integer>> permutationOrgUnitTargetMap, Map<String, List<DimensionItemObjectValue>> permutationDimensionItemValueMap, Indicator indicator, List<DimensionItem> dimensionItems) {
    String permKey = asItemKey(dimensionItems);
    final List<DimensionItemObjectValue> values = permutationDimensionItemValueMap.getOrDefault(permKey, new ArrayList<>());
    List<Period> periods = !filterPeriods.isEmpty() ? filterPeriods : singletonList((Period) getPeriodItem(dimensionItems));
    OrganisationUnit unit = (OrganisationUnit) getOrganisationUnitItem(dimensionItems);
    String ou = unit != null ? unit.getUid() : null;
    Map<String, Integer> orgUnitCountMap = permutationOrgUnitTargetMap != null ? permutationOrgUnitTargetMap.get(ou) : null;
    return expressionService.getIndicatorValueObject(indicator, periods, itemMap, convertToDimItemValueMap(values), orgUnitCountMap);
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DimensionItemObjectValue(org.hisp.dhis.common.DimensionItemObjectValue) Period(org.hisp.dhis.period.Period) AnalyticsUtils.hasPeriod(org.hisp.dhis.analytics.util.AnalyticsUtils.hasPeriod) PeriodType.getPeriodTypeFromIsoString(org.hisp.dhis.period.PeriodType.getPeriodTypeFromIsoString)

Example 2 with DimensionItemObjectValue

use of org.hisp.dhis.common.DimensionItemObjectValue in project dhis2-core by dhis2.

the class DataHandler 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. In case of circular references between
 * Indicators, an exception is thrown.
 *
 * @param params the {@link DataQueryParams}.
 * @param items the list of {@link DimensionalItemObject}.
 * @return a dimensional items to aggregate values map.
 */
private MultiValuedMap<String, DimensionItemObjectValue> getAggregatedDataValueMap(DataQueryParams params, List<DimensionalItemObject> items) {
    if (items.isEmpty()) {
        return new ArrayListValuedHashMap<>();
    }
    DimensionalObject dimension = new BaseDimensionalObject(DATA_X_DIM_ID, DATA_X, null, DISPLAY_NAME_DATA_X, items);
    DataQueryParams dataSourceParams = newBuilder(params).replaceDimension(dimension).withMeasureCriteria(new HashMap<>()).withIncludeNumDen(false).withSkipHeaders(true).withOutputFormat(ANALYTICS).withSkipMeta(true).build();
    Grid grid = dataAggregator.getAggregatedDataValueGrid(dataSourceParams);
    MultiValuedMap<String, DimensionItemObjectValue> result = new ArrayListValuedHashMap<>();
    if (isEmpty(grid.getRows())) {
        return result;
    }
    // Derive the Grid indexes for data, value and period based on the first
    // row of the Grid
    final int dataIndex = getGridIndexByDimensionItem(grid.getRow(0), items, 0);
    final int periodIndex = getGridIndexByDimensionItem(grid.getRow(0), params.getPeriods(), 1);
    final int valueIndex = grid.getWidth() - 1;
    final List<DimensionalItemObject> basePeriods = params.getPeriods();
    for (List<Object> row : grid.getRows()) {
        for (DimensionalItemObject dimensionalItem : findDimensionalItems((String) row.get(dataIndex), items)) {
            if (hasPeriod(row, periodIndex)) {
                addItemBasedOnPeriodOffset(result, periodIndex, valueIndex, row, dimensionalItem, basePeriods);
            } else {
                result.put(join(remove(row.toArray(new Object[0]), valueIndex), DIMENSION_SEP), new DimensionItemObjectValue(dimensionalItem, ((Number) row.get(valueIndex)).doubleValue()));
            }
        }
    }
    return result;
}
Also used : HashMap(java.util.HashMap) ArrayListValuedHashMap(org.apache.commons.collections4.multimap.ArrayListValuedHashMap) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) Grid(org.hisp.dhis.common.Grid) DimensionItemObjectValue(org.hisp.dhis.common.DimensionItemObjectValue) PeriodType.getPeriodTypeFromIsoString(org.hisp.dhis.period.PeriodType.getPeriodTypeFromIsoString) DimensionalObject(org.hisp.dhis.common.DimensionalObject) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) EventQueryParams.fromDataQueryParams(org.hisp.dhis.analytics.event.EventQueryParams.fromDataQueryParams) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) AnalyticsUtils.getRoundedValueObject(org.hisp.dhis.analytics.util.AnalyticsUtils.getRoundedValueObject) DimensionalObject(org.hisp.dhis.common.DimensionalObject) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) ArrayListValuedHashMap(org.apache.commons.collections4.multimap.ArrayListValuedHashMap)

Example 3 with DimensionItemObjectValue

use of org.hisp.dhis.common.DimensionItemObjectValue in project dhis2-core by dhis2.

the class QueryPlannerTest method testGetPermutationDimensionalItemValueMapCocEnabled.

@Test
void testGetPermutationDimensionalItemValueMapCocEnabled() {
    MultiValuedMap<String, DimensionItemObjectValue> aggregatedDataMap = new ArrayListValuedHashMap<>();
    aggregatedDataMap.put(makeKey(deA, coc, ouA, "2000Q1"), new DimensionItemObjectValue(deA, 1d));
    aggregatedDataMap.put(makeKey(deA, coc, ouA, "2000Q2"), new DimensionItemObjectValue(deA, 2d));
    aggregatedDataMap.put(makeKey(deA, coc, ouB, "2000Q1"), new DimensionItemObjectValue(deA, 3d));
    aggregatedDataMap.put(makeKey(deA, coc, ouB, "2000Q2"), new DimensionItemObjectValue(deA, 4d));
    aggregatedDataMap.put(makeKey(deB, coc, ouA, "2000Q1"), new DimensionItemObjectValue(deB, 5d));
    aggregatedDataMap.put(makeKey(deB, coc, ouA, "2000Q2"), new DimensionItemObjectValue(deB, 6d));
    aggregatedDataMap.put(makeKey(deB, coc, ouB, "2000Q1"), new DimensionItemObjectValue(deB, 7d));
    aggregatedDataMap.put(makeKey(deB, coc, ouB, "2000Q2"), new DimensionItemObjectValue(deB, 8d));
    // Method under test //
    Map<String, List<DimensionItemObjectValue>> permutationMap = DataQueryParams.getPermutationDimensionalItemValueMap(aggregatedDataMap);
    assertNotNull(permutationMap);
    String ouAQ1Key = ouA.getUid() + DIMENSION_SEP + "2000Q1";
    String ouAQ2Key = ouA.getUid() + DIMENSION_SEP + "2000Q2";
    String ouBQ1Key = ouB.getUid() + DIMENSION_SEP + "2000Q1";
    String ouBQ2Key = ouB.getUid() + DIMENSION_SEP + "2000Q2";
    List<DimensionItemObjectValue> ouAQ1 = permutationMap.get(ouAQ1Key);
    List<DimensionItemObjectValue> ouAQ2 = permutationMap.get(ouAQ2Key);
    List<DimensionItemObjectValue> ouBQ1 = permutationMap.get(ouBQ1Key);
    List<DimensionItemObjectValue> ouBQ2 = permutationMap.get(ouBQ2Key);
    assertEquals(2, ouAQ1.size());
    assertEquals(2, ouAQ2.size());
    assertEquals(2, ouBQ1.size());
    assertEquals(2, ouBQ2.size());
    List<DimensionItemObjectValue> ouAQ1Expected = new ArrayList<>();
    ouAQ1Expected.add(new DimensionItemObjectValue(deA, 1d));
    ouAQ1Expected.add(new DimensionItemObjectValue(deB, 5d));
    List<DimensionItemObjectValue> ouAQ2Expected = new ArrayList<>();
    ouAQ2Expected.add(new DimensionItemObjectValue(deA, 2d));
    ouAQ2Expected.add(new DimensionItemObjectValue(deB, 6d));
    List<DimensionItemObjectValue> ouBQ1Expected = new ArrayList<>();
    ouBQ1Expected.add(new DimensionItemObjectValue(deA, 3d));
    ouBQ1Expected.add(new DimensionItemObjectValue(deB, 7d));
    List<DimensionItemObjectValue> ouBQ2Expected = new ArrayList<>();
    ouBQ2Expected.add(new DimensionItemObjectValue(deA, 4d));
    ouBQ2Expected.add(new DimensionItemObjectValue(deB, 8d));
    assertCollectionsMatch(ouAQ1Expected, ouAQ1);
    assertCollectionsMatch(ouAQ2Expected, ouAQ2);
    assertCollectionsMatch(ouBQ1Expected, ouBQ1);
    assertCollectionsMatch(ouBQ2Expected, ouBQ2);
}
Also used : ArrayList(java.util.ArrayList) DimensionItemObjectValue(org.hisp.dhis.common.DimensionItemObjectValue) List(java.util.List) ArrayList(java.util.ArrayList) DimensionalObjectUtils.getList(org.hisp.dhis.common.DimensionalObjectUtils.getList) ArrayListValuedHashMap(org.apache.commons.collections4.multimap.ArrayListValuedHashMap) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 4 with DimensionItemObjectValue

use of org.hisp.dhis.common.DimensionItemObjectValue in project dhis2-core by dhis2.

the class DataHandler method addItemBasedOnPeriodOffset.

/**
 * Calculate the dimensional item offset and adds to the give result map.
 *
 * @param result the map where the values will be added to.
 * @param periodIndex the current grid row period index.
 * @param valueIndex the current grid row value index.
 * @param row the current grid row.
 * @param dimensionalItemObject a dimensional item for the current grid row,
 *        see
 *        {@link org.hisp.dhis.analytics.util.AnalyticsUtils#findDimensionalItems(String, List)}
 * @param basePeriods the periods from the parameters.
 *
 * @return the DimensionalItemObject
 */
private void addItemBasedOnPeriodOffset(MultiValuedMap<String, DimensionItemObjectValue> result, int periodIndex, int valueIndex, List<Object> row, DimensionalItemObject dimensionalItemObject, List<DimensionalItemObject> basePeriods) {
    if (row.get(valueIndex) == null) {
        return;
    }
    int periodOffset = (dimensionalItemObject.getQueryMods() == null) ? 0 : dimensionalItemObject.getQueryMods().getPeriodOffset();
    final List<Object> adjustedRow = (periodOffset != 0) ? getPeriodOffsetRow(row, periodIndex, periodOffset) : row;
    if (!isPeriodInPeriods((String) adjustedRow.get(periodIndex), basePeriods)) {
        return;
    }
    // Key is composed of [uid-period]
    final String key = join(remove(adjustedRow.toArray(new Object[0]), valueIndex), DIMENSION_SEP);
    final Double value = ((Number) adjustedRow.get(valueIndex)).doubleValue();
    result.put(key, new DimensionItemObjectValue(dimensionalItemObject, value));
}
Also used : DimensionItemObjectValue(org.hisp.dhis.common.DimensionItemObjectValue) AnalyticsUtils.getRoundedValueObject(org.hisp.dhis.analytics.util.AnalyticsUtils.getRoundedValueObject) DimensionalObject(org.hisp.dhis.common.DimensionalObject) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) PeriodType.getPeriodTypeFromIsoString(org.hisp.dhis.period.PeriodType.getPeriodTypeFromIsoString)

Example 5 with DimensionItemObjectValue

use of org.hisp.dhis.common.DimensionItemObjectValue in project dhis2-core by dhis2.

the class DataQueryParams method getPermutationDimensionalItemValueMap.

// -------------------------------------------------------------------------
// Static methods
// -------------------------------------------------------------------------
/**
 * Creates a mapping of permutation keys and mappings of data element
 * operands and values based on the given mapping of dimension option keys
 * and aggregated values. The data element dimension will be at index 0.
 *
 * @param aggregatedDataMap the aggregated data map.
 * @return a mapping of permutation keys and mappings of data element
 *         operands and values.
 */
public static Map<String, List<DimensionItemObjectValue>> getPermutationDimensionalItemValueMap(MultiValuedMap<String, DimensionItemObjectValue> aggregatedDataMap) {
    Map<String, List<DimensionItemObjectValue>> permutationMap = new HashMap<>();
    for (String key : aggregatedDataMap.keySet()) {
        // Remove DimensionalItemObject uid from key
        List<String> keys = Lists.newArrayList(key.split(DIMENSION_SEP));
        keys.remove(DX_INDEX);
        final Collection<DimensionItemObjectValue> dimensionItemObjectValues = aggregatedDataMap.get(key);
        // Generate final permutation key
        final String permKey = StringUtils.join(keys, DIMENSION_SEP);
        for (DimensionItemObjectValue dimWithValue : dimensionItemObjectValues) {
            if (!permutationMap.containsKey(permKey)) {
                permutationMap.put(permKey, Lists.newArrayList(dimWithValue));
            } else {
                permutationMap.get(permKey).add(dimWithValue);
            }
        }
    }
    return permutationMap;
}
Also used : HashMap(java.util.HashMap) DimensionItemObjectValue(org.hisp.dhis.common.DimensionItemObjectValue) Collections.singletonList(java.util.Collections.singletonList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) DimensionalObjectUtils.asList(org.hisp.dhis.common.DimensionalObjectUtils.asList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) DimensionalObjectUtils.getList(org.hisp.dhis.common.DimensionalObjectUtils.getList)

Aggregations

DimensionItemObjectValue (org.hisp.dhis.common.DimensionItemObjectValue)8 ArrayList (java.util.ArrayList)5 List (java.util.List)5 ArrayListValuedHashMap (org.apache.commons.collections4.multimap.ArrayListValuedHashMap)5 DimensionalObjectUtils.getList (org.hisp.dhis.common.DimensionalObjectUtils.getList)5 DhisSpringTest (org.hisp.dhis.DhisSpringTest)4 Test (org.junit.jupiter.api.Test)4 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)3 DimensionalObject (org.hisp.dhis.common.DimensionalObject)3 HashMap (java.util.HashMap)2 DataQueryParams (org.hisp.dhis.analytics.DataQueryParams)2 AnalyticsUtils.getRoundedValueObject (org.hisp.dhis.analytics.util.AnalyticsUtils.getRoundedValueObject)2 BaseDimensionalObject (org.hisp.dhis.common.BaseDimensionalObject)2 PeriodType.getPeriodTypeFromIsoString (org.hisp.dhis.period.PeriodType.getPeriodTypeFromIsoString)2 ImmutableList (com.google.common.collect.ImmutableList)1 Lists (com.google.common.collect.Lists)1 Sets (com.google.common.collect.Sets)1 Arrays (java.util.Arrays)1 Collections.emptyList (java.util.Collections.emptyList)1 Collections.singletonList (java.util.Collections.singletonList)1