Search in sources :

Example 16 with ListMap

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

the class QueryPlannerUtils method getMinMaxDateDateElementMap.

/**
 * Creates a mapping between minDate/maxDate query modifiers and data
 * elements for the given data elements.
 *
 * @param dataElements list of data elements.
 */
public static ListMap<QueryModifiers, DimensionalItemObject> getMinMaxDateDateElementMap(List<DimensionalItemObject> dataElements) {
    ListMap<QueryModifiers, DimensionalItemObject> map = new ListMap<>();
    for (DimensionalItemObject element : dataElements) {
        QueryModifiers queryMods = element.getQueryMods();
        // Get QueryModifiers but only with min and max date, nothing else
        QueryModifiers minMaxDateModifiers = (queryMods == null) ? QueryModifiers.builder().build() : QueryModifiers.builder().minDate(queryMods.getMinDate()).maxDate(queryMods.getMaxDate()).build();
        map.putValue(minMaxDateModifiers, element);
    }
    return map;
}
Also used : QueryModifiers(org.hisp.dhis.common.QueryModifiers) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) ListMap(org.hisp.dhis.common.ListMap)

Example 17 with ListMap

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

the class DefaultQueryPlanner method groupByOrgUnitLevel.

@Override
public List<DataQueryParams> groupByOrgUnitLevel(DataQueryParams params) {
    List<DataQueryParams> queries = new ArrayList<>();
    if (!params.getOrganisationUnits().isEmpty()) {
        ListMap<Integer, DimensionalItemObject> levelOrgUnitMap = QueryPlannerUtils.getLevelOrgUnitMap(params.getOrganisationUnits());
        for (Integer level : levelOrgUnitMap.keySet()) {
            DataQueryParams query = DataQueryParams.newBuilder(params).addOrSetDimensionOptions(ORGUNIT_DIM_ID, DimensionType.ORGANISATION_UNIT, LEVEL_PREFIX + level, levelOrgUnitMap.get(level)).build();
            queries.add(query);
        }
    } else if (!params.getFilterOrganisationUnits().isEmpty()) {
        ListMap<Integer, DimensionalItemObject> levelOrgUnitMap = QueryPlannerUtils.getLevelOrgUnitMap(params.getFilterOrganisationUnits());
        DimensionalObject filter = params.getFilter(ORGUNIT_DIM_ID);
        DataQueryParams.Builder query = DataQueryParams.newBuilder(params).removeFilter(ORGUNIT_DIM_ID);
        for (Integer level : levelOrgUnitMap.keySet()) {
            query.addFilter(new BaseDimensionalObject(filter.getDimension(), filter.getDimensionType(), LEVEL_PREFIX + level, filter.getDimensionDisplayName(), levelOrgUnitMap.get(level)));
        }
        queries.add(query.build());
    } else {
        queries.add(DataQueryParams.newBuilder(params).build());
        return queries;
    }
    logQuerySplit(queries, "organisation unit level");
    return queries;
}
Also used : DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) ArrayList(java.util.ArrayList) ListMap(org.hisp.dhis.common.ListMap) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) DimensionalObject(org.hisp.dhis.common.DimensionalObject)

Example 18 with ListMap

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

the class AnalyticsManagerTest method testReplaceDataPeriodsWithAggregationPeriods.

@Test
void testReplaceDataPeriodsWithAggregationPeriods() {
    AnalyticsManager analyticsManager = new JdbcAnalyticsManager(queryPlanner, jdbcTemplate, executionPlanStore);
    Period y2012 = createPeriod("2012");
    AnalyticsAggregationType aggregationType = new AnalyticsAggregationType(AggregationType.SUM, AggregationType.AVERAGE, DataType.NUMERIC, true);
    DataQueryParams params = DataQueryParams.newBuilder().withDataElements(getList(createDataElement('A'), createDataElement('B'))).withPeriods(getList(y2012)).withOrganisationUnits(getList(createOrganisationUnit('A'))).withDataPeriodType(new YearlyPeriodType()).withAggregationType(aggregationType).build();
    Map<String, Object> dataValueMap = new HashMap<>();
    dataValueMap.put(BASE_UID + "A-2012-" + BASE_UID + "A", 1d);
    dataValueMap.put(BASE_UID + "B-2012-" + BASE_UID + "A", 1d);
    ListMap<DimensionalItemObject, DimensionalItemObject> dataPeriodAggregationPeriodMap = new ListMap<>();
    dataPeriodAggregationPeriodMap.putValue(y2012, createPeriod("2012Q1"));
    dataPeriodAggregationPeriodMap.putValue(y2012, createPeriod("2012Q2"));
    dataPeriodAggregationPeriodMap.putValue(y2012, createPeriod("2012Q3"));
    dataPeriodAggregationPeriodMap.putValue(y2012, createPeriod("2012Q4"));
    analyticsManager.replaceDataPeriodsWithAggregationPeriods(dataValueMap, params, dataPeriodAggregationPeriodMap);
    assertEquals(8, dataValueMap.size());
    assertTrue(dataValueMap.keySet().contains(BASE_UID + "A-2012Q1-" + BASE_UID + "A"));
    assertTrue(dataValueMap.keySet().contains(BASE_UID + "A-2012Q2-" + BASE_UID + "A"));
    assertTrue(dataValueMap.keySet().contains(BASE_UID + "A-2012Q3-" + BASE_UID + "A"));
    assertTrue(dataValueMap.keySet().contains(BASE_UID + "A-2012Q4-" + BASE_UID + "A"));
    assertTrue(dataValueMap.keySet().contains(BASE_UID + "B-2012Q1-" + BASE_UID + "A"));
    assertTrue(dataValueMap.keySet().contains(BASE_UID + "B-2012Q2-" + BASE_UID + "A"));
    assertTrue(dataValueMap.keySet().contains(BASE_UID + "B-2012Q3-" + BASE_UID + "A"));
    assertTrue(dataValueMap.keySet().contains(BASE_UID + "B-2012Q4-" + BASE_UID + "A"));
}
Also used : HashMap(java.util.HashMap) Period(org.hisp.dhis.period.Period) YearlyPeriodType(org.hisp.dhis.period.YearlyPeriodType) ListMap(org.hisp.dhis.common.ListMap) AnalyticsAggregationType(org.hisp.dhis.analytics.AnalyticsAggregationType) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) AnalyticsManager(org.hisp.dhis.analytics.AnalyticsManager) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest)

Example 19 with ListMap

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

the class DefaultCsvImportService method setOptionSetsFromCsv.

/**
 * Option set format:
 * <p>
 * <ul>
 * <li>option set name</li>
 * <li>option set uid</li>
 * <li>option set code</li>
 * <li>option name</li>
 * <li>option uid</li>
 * <li>option code</li>
 * </ul>
 */
private void setOptionSetsFromCsv(CsvReader reader, Metadata metadata) throws IOException {
    ListMap<String, Option> nameOptionMap = new ListMap<>();
    Map<String, OptionSet> nameOptionSetMap = new HashMap<>();
    List<Option> options = new ArrayList<>();
    while (reader.readRecord()) {
        String[] values = reader.getValues();
        if (values != null && values.length > 0) {
            OptionSet optionSet = new OptionSet();
            setIdentifiableObject(optionSet, values);
            optionSet.setAutoFields();
            optionSet.setValueType(ValueType.TEXT);
            Option option = new Option();
            option.setName(getSafe(values, 3, 230));
            option.setUid(getSafe(values, 4, CodeGenerator.generateUid(), 11));
            option.setCode(getSafe(values, 5, 50));
            option.setAutoFields();
            if (optionSet.getName() == null || option.getCode() == null) {
                continue;
            }
            nameOptionSetMap.put(optionSet.getName(), optionSet);
            nameOptionMap.putValue(optionSet.getName(), option);
            options.add(option);
        }
    }
    for (Entry<String, OptionSet> optionSetEntry : nameOptionSetMap.entrySet()) {
        optionSetEntry.getValue().setOptions(new ArrayList<>(nameOptionMap.get(optionSetEntry.getKey())));
    }
    metadata.setOptions(options);
    metadata.setOptionSets(new ArrayList<>(nameOptionSetMap.values()));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CategoryOption(org.hisp.dhis.category.CategoryOption) Option(org.hisp.dhis.option.Option) OptionSet(org.hisp.dhis.option.OptionSet) ListMap(org.hisp.dhis.common.ListMap)

Example 20 with ListMap

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

Aggregations

ListMap (org.hisp.dhis.common.ListMap)22 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)14 DataElement (org.hisp.dhis.dataelement.DataElement)8 Period (org.hisp.dhis.period.Period)7 PeriodType (org.hisp.dhis.period.PeriodType)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)5 DataQueryParams (org.hisp.dhis.analytics.DataQueryParams)5 AnalyticsAggregationType (org.hisp.dhis.analytics.AnalyticsAggregationType)3 HashSet (java.util.HashSet)2 List (java.util.List)2 Map (java.util.Map)2 AllArgsConstructor (lombok.AllArgsConstructor)2 StringUtils (org.apache.commons.lang3.StringUtils)2 AggregationType (org.hisp.dhis.analytics.AggregationType)2 DataType (org.hisp.dhis.analytics.DataType)2 CategoryCombo (org.hisp.dhis.category.CategoryCombo)2 CategoryOption (org.hisp.dhis.category.CategoryOption)2 CategoryService (org.hisp.dhis.category.CategoryService)2 BaseDimensionalObject (org.hisp.dhis.common.BaseDimensionalObject)2