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);
}
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;
}
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);
}
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));
}
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;
}
Aggregations