Search in sources :

Example 31 with DimensionalItemId

use of org.hisp.dhis.common.DimensionalItemId 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 32 with DimensionalItemId

use of org.hisp.dhis.common.DimensionalItemId 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 33 with DimensionalItemId

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

the class DefaultPredictionService method predict.

@Override
public void predict(Predictor predictor, Date startDate, Date endDate, PredictionSummary predictionSummary) {
    Expression generator = predictor.getGenerator();
    Expression skipTest = predictor.getSampleSkipTest();
    DataElement outputDataElement = predictor.getOutput();
    DataType expressionDataType = DataType.fromValueType(outputDataElement.getValueType());
    Map<DimensionalItemId, DimensionalItemObject> outputPeriodItemMap = new HashMap<>();
    Map<DimensionalItemId, DimensionalItemObject> sampledItemMap = new HashMap<>();
    expressionService.getExpressionDimensionalItemMaps(generator.getExpression(), PREDICTOR_EXPRESSION, expressionDataType, outputPeriodItemMap, sampledItemMap);
    Set<String> orgUnitGroupIds = expressionService.getExpressionOrgUnitGroupIds(generator.getExpression(), PREDICTOR_EXPRESSION);
    if (skipTest != null) {
        expressionService.getExpressionDimensionalItemMaps(skipTest.getExpression(), PREDICTOR_SKIP_TEST, DataType.BOOLEAN, sampledItemMap, sampledItemMap);
        orgUnitGroupIds.addAll(expressionService.getExpressionOrgUnitGroupIds(skipTest.getExpression(), PREDICTOR_SKIP_TEST));
    }
    Map<String, OrganisationUnitGroup> orgUnitGroupMap = orgUnitGroupIds.stream().map(organisationUnitGroupService::getOrganisationUnitGroup).filter(Objects::nonNull).collect(Collectors.toMap(OrganisationUnitGroup::getUid, g -> g));
    Map<DimensionalItemId, DimensionalItemObject> itemMap = new HashMap<>(outputPeriodItemMap);
    itemMap.putAll(sampledItemMap);
    Set<DimensionalItemObject> outputPeriodItems = new HashSet<>(outputPeriodItemMap.values());
    Set<DimensionalItemObject> sampledItems = new HashSet<>(sampledItemMap.values());
    Set<DimensionalItemObject> items = new HashSet<>(itemMap.values());
    List<Period> outputPeriods = getPeriodsBetweenDates(predictor.getPeriodType(), startDate, endDate);
    Set<Period> existingOutputPeriods = getExistingPeriods(outputPeriods);
    ListMap<Period, Period> samplePeriodsMap = getSamplePeriodsMap(outputPeriods, predictor);
    Set<Period> allSamplePeriods = samplePeriodsMap.uniqueValues();
    Set<Period> analyticsQueryPeriods = getAnalyticsQueryPeriods(sampledItems, allSamplePeriods, outputPeriodItems, existingOutputPeriods);
    Set<Period> dataValueQueryPeriods = getDataValueQueryPeriods(analyticsQueryPeriods, existingOutputPeriods);
    outputPeriods = periodService.reloadPeriods(outputPeriods);
    CategoryOptionCombo defaultCategoryOptionCombo = categoryService.getDefaultCategoryOptionCombo();
    CategoryOptionCombo outputOptionCombo = predictor.getOutputCombo() == null ? defaultCategoryOptionCombo : predictor.getOutputCombo();
    DataElementOperand outputDataElementOperand = new DataElementOperand(outputDataElement, outputOptionCombo);
    Date now = new Date();
    boolean requireData = generator.getMissingValueStrategy() != NEVER_SKIP && (!items.isEmpty());
    DimensionalItemObject forwardReference = addOuputToItems(outputDataElementOperand, items);
    Set<OrganisationUnit> currentUserOrgUnits = new HashSet<>();
    String storedBy = "system-process";
    User currentUser = currentUserService.getCurrentUser();
    if (currentUser != null) {
        currentUserOrgUnits = currentUser.getOrganisationUnits();
        storedBy = currentUser.getUsername();
    }
    PredictionDataConsolidator consolidator = new PredictionDataConsolidator(items, predictor.getOrganisationUnitDescendants().equals(DESCENDANTS), new PredictionDataValueFetcher(dataValueService, categoryService), new PredictionAnalyticsDataFetcher(analyticsService, categoryService));
    PredictionWriter predictionWriter = new PredictionWriter(dataValueService, batchHandlerFactory);
    predictionWriter.init(existingOutputPeriods, predictionSummary);
    predictionSummary.incrementPredictors();
    for (OrganisationUnitLevel orgUnitLevel : predictor.getOrganisationUnitLevels()) {
        List<OrganisationUnit> orgUnits = organisationUnitService.getOrganisationUnitsAtOrgUnitLevels(Lists.newArrayList(orgUnitLevel), currentUserOrgUnits);
        consolidator.init(currentUserOrgUnits, orgUnitLevel.getLevel(), orgUnits, dataValueQueryPeriods, analyticsQueryPeriods, existingOutputPeriods, outputDataElementOperand);
        PredictionData data;
        while ((data = consolidator.getData()) != null) {
            List<DataValue> predictions = new ArrayList<>();
            List<PredictionContext> contexts = PredictionContextGenerator.getContexts(outputPeriods, data.getValues(), defaultCategoryOptionCombo);
            for (PredictionContext c : contexts) {
                List<Period> samplePeriods = new ArrayList<>(samplePeriodsMap.get(c.getOutputPeriod()));
                samplePeriods.removeAll(getSkippedPeriods(allSamplePeriods, itemMap, c.getPeriodValueMap(), skipTest, orgUnitGroupMap, data.getOrgUnit()));
                if (requireData && !dataIsPresent(outputPeriodItems, c.getValueMap(), sampledItems, samplePeriods, c.getPeriodValueMap())) {
                    continue;
                }
                Object value = expressionService.getExpressionValue(ExpressionParams.builder().expression(predictor.getGenerator().getExpression()).parseType(PREDICTOR_EXPRESSION).dataType(expressionDataType).itemMap(itemMap).valueMap(c.getValueMap()).orgUnitGroupMap(orgUnitGroupMap).days(c.getOutputPeriod().getDaysInPeriod()).missingValueStrategy(generator.getMissingValueStrategy()).orgUnit(data.getOrgUnit()).samplePeriods(samplePeriods).periodValueMap(c.getPeriodValueMap()).build());
                if (value != null || generator.getMissingValueStrategy() == NEVER_SKIP) {
                    String valueString = formatPrediction(value, outputDataElement);
                    if (valueString != null) {
                        DataValue prediction = new DataValue(outputDataElement, c.getOutputPeriod(), data.getOrgUnit(), outputOptionCombo, c.getAttributeOptionCombo(), valueString, storedBy, now, null);
                        carryPredictionForward(prediction, contexts, forwardReference);
                        predictions.add(prediction);
                    }
                }
            }
            predictionWriter.write(predictions, data.getOldPredictions());
        }
    }
    predictionWriter.flush();
}
Also used : CategoryService(org.hisp.dhis.category.CategoryService) Date(java.util.Date) DataType(org.hisp.dhis.analytics.DataType) PeriodService(org.hisp.dhis.period.PeriodService) PREDICTOR_SKIP_TEST(org.hisp.dhis.expression.ParseType.PREDICTOR_SKIP_TEST) OrganisationUnitService(org.hisp.dhis.organisationunit.OrganisationUnitService) StringUtils(org.apache.commons.lang3.StringUtils) PredictorJobParameters(org.hisp.dhis.scheduling.parameters.PredictorJobParameters) CurrentUserServiceTarget(org.hisp.dhis.user.CurrentUserServiceTarget) Map(java.util.Map) ExpressionParams(org.hisp.dhis.expression.ExpressionParams) JobConfiguration(org.hisp.dhis.scheduling.JobConfiguration) Period(org.hisp.dhis.period.Period) OrganisationUnitGroupService(org.hisp.dhis.organisationunit.OrganisationUnitGroupService) DimensionalItemId(org.hisp.dhis.common.DimensionalItemId) OrganisationUnitGroup(org.hisp.dhis.organisationunit.OrganisationUnitGroup) AnalyticsServiceTarget(org.hisp.dhis.analytics.AnalyticsServiceTarget) Set(java.util.Set) PREDICTOR_EXPRESSION(org.hisp.dhis.expression.ParseType.PREDICTOR_EXPRESSION) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) MoreObjects.firstNonNull(com.google.common.base.MoreObjects.firstNonNull) DESCENDANTS(org.hisp.dhis.common.OrganisationUnitDescendants.DESCENDANTS) BatchHandlerFactory(org.hisp.quick.BatchHandlerFactory) ListMap(org.hisp.dhis.common.ListMap) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) HashMap(java.util.HashMap) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) DataElement(org.hisp.dhis.dataelement.DataElement) Notifier(org.hisp.dhis.system.notification.Notifier) NEVER_SKIP(org.hisp.dhis.expression.MissingValueStrategy.NEVER_SKIP) ExpressionService(org.hisp.dhis.expression.ExpressionService) PredictionFormatter.formatPrediction(org.hisp.dhis.predictor.PredictionFormatter.formatPrediction) DataValueService(org.hisp.dhis.datavalue.DataValueService) Lists(com.google.common.collect.Lists) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) Service(org.springframework.stereotype.Service) User(org.hisp.dhis.user.User) DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) DebugUtils(org.hisp.dhis.commons.util.DebugUtils) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) NotificationLevel(org.hisp.dhis.system.notification.NotificationLevel) CurrentUserService(org.hisp.dhis.user.CurrentUserService) ERROR(org.hisp.dhis.system.notification.NotificationLevel.ERROR) DataValue(org.hisp.dhis.datavalue.DataValue) PeriodType(org.hisp.dhis.period.PeriodType) Expression(org.hisp.dhis.expression.Expression) AllArgsConstructor(lombok.AllArgsConstructor) OrganisationUnitLevel(org.hisp.dhis.organisationunit.OrganisationUnitLevel) AnalyticsService(org.hisp.dhis.analytics.AnalyticsService) DateUtils(org.hisp.dhis.util.DateUtils) MapMap(org.hisp.dhis.common.MapMap) Transactional(org.springframework.transaction.annotation.Transactional) DimensionalItemId(org.hisp.dhis.common.DimensionalItemId) User(org.hisp.dhis.user.User) HashMap(java.util.HashMap) DataValue(org.hisp.dhis.datavalue.DataValue) ArrayList(java.util.ArrayList) DataElement(org.hisp.dhis.dataelement.DataElement) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) DataType(org.hisp.dhis.analytics.DataType) HashSet(java.util.HashSet) DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Period(org.hisp.dhis.period.Period) Date(java.util.Date) OrganisationUnitGroup(org.hisp.dhis.organisationunit.OrganisationUnitGroup) Expression(org.hisp.dhis.expression.Expression) OrganisationUnitLevel(org.hisp.dhis.organisationunit.OrganisationUnitLevel) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo)

Example 34 with DimensionalItemId

use of org.hisp.dhis.common.DimensionalItemId 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)

Example 35 with DimensionalItemId

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

the class DataElementGroupResolverTest method verifyExpressionIsNotResolvedWhenExpressionIsNotValid.

@Test
void verifyExpressionIsNotResolvedWhenExpressionIsNotValid() {
    // arrange
    dimensionalItemId = new DimensionalItemId(DimensionItemType.DATA_ELEMENT_OPERAND, uid1, uid2, uid3, createIndicatorExpression());
    String expression = "lsdjflakjdflkajdslfhaglakujdhfg";
    when(expressionService.getExpressionDimensionalItemIds(expression, INDICATOR_EXPRESSION)).thenReturn(Sets.newHashSet(dimensionalItemId));
    // act
    String resolvedExpression = resolver.resolve(expression);
    // assert
    assertEquals(expression, resolvedExpression);
}
Also used : DimensionalItemId(org.hisp.dhis.common.DimensionalItemId) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Aggregations

DimensionalItemId (org.hisp.dhis.common.DimensionalItemId)46 Test (org.junit.jupiter.api.Test)32 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)21 DhisSpringTest (org.hisp.dhis.DhisSpringTest)13 Indicator (org.hisp.dhis.indicator.Indicator)12 ProgramIndicator (org.hisp.dhis.program.ProgramIndicator)12 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)12 HashMap (java.util.HashMap)11 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)9 DataElementOperand (org.hisp.dhis.dataelement.DataElementOperand)9 IndicatorValue (org.hisp.dhis.indicator.IndicatorValue)8 IndicatorType (org.hisp.dhis.indicator.IndicatorType)6 DataElement (org.hisp.dhis.dataelement.DataElement)5 List (java.util.List)4 BaseDimensionalItemObject (org.hisp.dhis.common.BaseDimensionalItemObject)4 Period (org.hisp.dhis.period.Period)4 ArrayList (java.util.ArrayList)3 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3 AllArgsConstructor (lombok.AllArgsConstructor)3