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