Search in sources :

Example 1 with ListMap

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

the class DefaultPredictorService method getSamplePeriodsMap.

/**
     * Creates a map relating each output period to a list of sample periods
     * from which the sample data is to be drawn.
     *
     * @param outputPeriods the output periods
     * @param predictor the predictor
     * @return map from output periods to sample periods
     */
private ListMap<Period, Period> getSamplePeriodsMap(List<Period> outputPeriods, Predictor predictor) {
    int sequentialCount = predictor.getSequentialSampleCount();
    int annualCount = predictor.getAnnualSampleCount();
    int skipCount = firstNonNull(predictor.getSequentialSkipCount(), 0);
    PeriodType periodType = predictor.getPeriodType();
    ListMap<Period, Period> samplePeriodsMap = new ListMap<Period, Period>();
    for (Period outputPeriod : outputPeriods) {
        samplePeriodsMap.put(outputPeriod, new ArrayList<Period>());
        Period p = periodType.getPreviousPeriod(outputPeriod, skipCount);
        for (int i = skipCount; i < sequentialCount; i++) {
            p = periodType.getPreviousPeriod(p);
            addPeriod(samplePeriodsMap, outputPeriod, p);
        }
        for (int year = 1; year <= annualCount; year++) {
            Period pPrev = periodType.getPreviousYearsPeriod(outputPeriod, year);
            Period pNext = pPrev;
            addPeriod(samplePeriodsMap, outputPeriod, pPrev);
            for (int i = 0; i < sequentialCount; i++) {
                pPrev = periodType.getPreviousPeriod(pPrev);
                pNext = periodType.getNextPeriod(pNext);
                addPeriod(samplePeriodsMap, outputPeriod, pPrev);
                addPeriod(samplePeriodsMap, outputPeriod, pNext);
            }
        }
    }
    return samplePeriodsMap;
}
Also used : PeriodType(org.hisp.dhis.period.PeriodType) Period(org.hisp.dhis.period.Period) ListMap(org.hisp.dhis.common.ListMap)

Example 2 with ListMap

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

the class DefaultQueryPlanner method groupByPartition.

// -------------------------------------------------------------------------
// Supportive - group by methods
// -------------------------------------------------------------------------
@Override
public List<DataQueryParams> groupByPartition(DataQueryParams params, QueryPlannerParams plannerParams) {
    Set<String> validPartitions = partitionManager.getDataValueAnalyticsPartitions();
    String tableName = plannerParams.getTableName();
    String tableSuffix = plannerParams.getTableSuffix();
    List<DataQueryParams> queries = new ArrayList<>();
    if (params.isSkipPartitioning()) {
        DataQueryParams query = DataQueryParams.newBuilder(params).withPartitions(new Partitions().add(tableName)).build();
        queries.add(query);
    } else if (!params.getPeriods().isEmpty()) {
        ListMap<Partitions, DimensionalItemObject> partitionPeriodMap = PartitionUtils.getPartitionPeriodMap(params.getPeriods(), tableName, tableSuffix, validPartitions);
        for (Partitions partitions : partitionPeriodMap.keySet()) {
            if (partitions.hasAny()) {
                DataQueryParams query = DataQueryParams.newBuilder(params).withPeriods(partitionPeriodMap.get(partitions)).withPartitions(partitions).build();
                queries.add(query);
            }
        }
    } else if (!params.getFilterPeriods().isEmpty()) {
        Partitions partitions = PartitionUtils.getPartitions(params.getFilterPeriods(), tableName, tableSuffix, validPartitions);
        if (partitions.hasAny()) {
            DataQueryParams query = DataQueryParams.newBuilder(params).withPartitions(partitions).build();
            queries.add(query);
        }
    } else if (params.hasStartEndDate()) {
        Partitions partitions = PartitionUtils.getPartitions(params.getStartDate(), params.getEndDate(), tableName, tableSuffix, validPartitions);
        if (partitions.hasAny()) {
            DataQueryParams query = DataQueryParams.newBuilder(params).withPartitions(partitions).build();
            queries.add(query);
        }
    } else {
        throw new IllegalQueryException("Query does not contain any period dimension items");
    }
    if (queries.size() > 1) {
        log.debug(String.format("Split on partition: %d", queries.size()));
    }
    return queries;
}
Also used : DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) Partitions(org.hisp.dhis.analytics.Partitions) ArrayList(java.util.ArrayList) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) ListMap(org.hisp.dhis.common.ListMap)

Example 3 with ListMap

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

the class QueryPlannerUtils method getAggregationTypeDataElementMap.

/**
     * Creates a mapping between the aggregation type and data element for the
     * given data elements and period type.
     * 
     * @param params the data query parameters.
     */
public static ListMap<AggregationType, DimensionalItemObject> getAggregationTypeDataElementMap(DataQueryParams params) {
    List<DimensionalItemObject> dataElements = params.getDataElements();
    PeriodType aggregationPeriodType = PeriodType.getPeriodTypeByName(params.getPeriodType());
    ListMap<AggregationType, DimensionalItemObject> map = new ListMap<>();
    for (DimensionalItemObject element : dataElements) {
        DataElement de = (DataElement) element;
        AggregationType type = ObjectUtils.firstNonNull(params.getAggregationType(), de.getAggregationType());
        AggregationType aggregationType = getAggregationType(de.getValueType(), type, aggregationPeriodType, de.getPeriodType());
        map.putValue(aggregationType, de);
    }
    return map;
}
Also used : PeriodType(org.hisp.dhis.period.PeriodType) DataElement(org.hisp.dhis.dataelement.DataElement) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) AggregationType(org.hisp.dhis.analytics.AggregationType) ListMap(org.hisp.dhis.common.ListMap)

Example 4 with ListMap

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

the class AnalyticsManagerTest method testWeightedAverage.

@ParameterizedTest
@MethodSource("data")
public void testWeightedAverage(String financialYear, Double weightedAverage) {
    analyticsManager = new JdbcAnalyticsManager(queryPlanner, jdbcTemplate, executionPlanStore);
    AnalyticsAggregationType aggregationType = new AnalyticsAggregationType(AggregationType.SUM, AggregationType.AVERAGE, DataType.NUMERIC, true);
    Period y2017 = createPeriod("2017");
    Period y2018 = createPeriod("2018");
    Period finYear2017 = createPeriod(financialYear);
    Map<String, Object> dataValueMap = new HashMap<>();
    dataValueMap.put(BASE_UID + "-2018", 1.0);
    dataValueMap.put(BASE_UID + "-2017", 154.0);
    ListMap<DimensionalItemObject, DimensionalItemObject> dataPeriodAggregationPeriodMap = new ListMap<>();
    dataPeriodAggregationPeriodMap.putValue(y2017, finYear2017);
    dataPeriodAggregationPeriodMap.putValue(y2018, finYear2017);
    DataQueryParams params = DataQueryParams.newBuilder().withDataElements(getList(createDataElement('A'), createDataElement('B'))).withPeriods(getList(y2017, y2018)).withDataPeriodType(new YearlyPeriodType()).withAggregationType(aggregationType).build();
    analyticsManager.replaceDataPeriodsWithAggregationPeriods(dataValueMap, params, dataPeriodAggregationPeriodMap);
    assertEquals(1, dataValueMap.size());
    assertThat(dataValueMap.get(BASE_UID + "-" + finYear2017.getIsoDate()), is(weightedAverage));
}
Also used : DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) HashMap(java.util.HashMap) Period(org.hisp.dhis.period.Period) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) YearlyPeriodType(org.hisp.dhis.period.YearlyPeriodType) ListMap(org.hisp.dhis.common.ListMap) AnalyticsAggregationType(org.hisp.dhis.analytics.AnalyticsAggregationType) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 5 with ListMap

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

the class DefaultCsvImportService method setOptionGroupsFromCsv.

/**
 * Option group format:
 * <p>
 * <ul>
 * <li>option group name</li>
 * <li>option group uid</li>
 * <li>option group code</li>
 * <li>option group short name</li>
 * <li>option set uid</li>
 * <li>option uid</li>
 * </ul>
 */
private void setOptionGroupsFromCsv(CsvReader reader, Metadata metadata) throws IOException {
    ListMap<String, Option> nameOptionMap = new ListMap<>();
    Map<String, OptionGroup> nameOptionGroupMap = new HashMap<>();
    Map<String, OptionSet> mapOptionSet = new HashMap<>();
    while (reader.readRecord()) {
        String[] values = reader.getValues();
        if (values != null && values.length > 0) {
            OptionGroup optionGroup = new OptionGroup();
            setIdentifiableObject(optionGroup, values);
            optionGroup.setShortName(getSafe(values, 3, 50));
            optionGroup.setAutoFields();
            if (optionGroup.getName() == null || optionGroup.getShortName() == null) {
                continue;
            }
            OptionSet optionSet = new OptionSet();
            optionSet.setUid(getSafe(values, 4, 11));
            if (optionSet.getUid() == null) {
                continue;
            }
            OptionSet persistedOptionSet = optionSet.getUid() != null ? mapOptionSet.computeIfAbsent(optionSet.getUid(), key -> optionService.getOptionSet(optionSet.getUid())) : mapOptionSet.computeIfAbsent(optionSet.getCode(), key -> optionService.getOptionSetByCode(optionSet.getCode()));
            if (persistedOptionSet == null) {
                continue;
            }
            optionGroup.setOptionSet(optionSet);
            Option option = new Option();
            option.setUid(getSafe(values, 5, 11));
            option.setCode(getSafe(values, 6, 50));
            if (option.getCode() == null && option.getUid() == null) {
                continue;
            }
            Optional<Option> isOptionExisted = persistedOptionSet.getOptions().stream().filter(persistedOption -> {
                if (option.getUid() != null) {
                    return persistedOption.getUid().equals(option.getUid());
                } else {
                    return persistedOption.getCode().equals(option.getCode());
                }
            }).findFirst();
            if (!isOptionExisted.isPresent()) {
                continue;
            }
            nameOptionGroupMap.put(optionGroup.getName(), optionGroup);
            nameOptionMap.putValue(optionGroup.getName(), isOptionExisted.get());
        }
    }
    // Read option groups from map and set in meta data
    for (Entry<String, OptionGroup> optionGroupEntry : nameOptionGroupMap.entrySet()) {
        optionGroupEntry.getValue().setMembers(new HashSet<>(nameOptionMap.get(optionGroupEntry.getKey())));
    }
    metadata.setOptionGroups(new ArrayList<>(nameOptionGroupMap.values()));
}
Also used : CategoryService(org.hisp.dhis.category.CategoryService) Importance(org.hisp.dhis.validation.Importance) CategoryOption(org.hisp.dhis.category.CategoryOption) ValueType(org.hisp.dhis.common.ValueType) StringUtils(org.apache.commons.lang3.StringUtils) OptionGroup(org.hisp.dhis.option.OptionGroup) MonthlyPeriodType(org.hisp.dhis.period.MonthlyPeriodType) Metadata(org.hisp.dhis.dxf2.metadata.Metadata) Map(java.util.Map) IndicatorGroup(org.hisp.dhis.indicator.IndicatorGroup) OrganisationUnitGroupService(org.hisp.dhis.organisationunit.OrganisationUnitGroupService) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) OrganisationUnitGroup(org.hisp.dhis.organisationunit.OrganisationUnitGroup) OptionGroupSet(org.hisp.dhis.option.OptionGroupSet) Category(org.hisp.dhis.category.Category) Operator(org.hisp.dhis.expression.Operator) DataElementGroupService(org.hisp.dhis.dataelement.DataElementGroupService) List(java.util.List) IndicatorGroupService(org.hisp.dhis.indicator.IndicatorGroupService) Entry(java.util.Map.Entry) CsvReader(com.csvreader.CsvReader) Optional(java.util.Optional) CategoryCombo(org.hisp.dhis.category.CategoryCombo) DataDimensionType(org.hisp.dhis.common.DataDimensionType) ListMap(org.hisp.dhis.common.ListMap) DateUtils.getMediumDate(org.hisp.dhis.util.DateUtils.getMediumDate) HashMap(java.util.HashMap) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) DataElement(org.hisp.dhis.dataelement.DataElement) MissingValueStrategy(org.hisp.dhis.expression.MissingValueStrategy) Service(org.springframework.stereotype.Service) Indicator(org.hisp.dhis.indicator.Indicator) CategoryOptionGroup(org.hisp.dhis.category.CategoryOptionGroup) OptionService(org.hisp.dhis.option.OptionService) CsvUtils(org.hisp.dhis.system.util.CsvUtils) DataElementDomain(org.hisp.dhis.dataelement.DataElementDomain) AggregationType(org.hisp.dhis.analytics.AggregationType) IOException(java.io.IOException) ValidationRule(org.hisp.dhis.validation.ValidationRule) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Option(org.hisp.dhis.option.Option) FeatureType(org.hisp.dhis.organisationunit.FeatureType) OptionSet(org.hisp.dhis.option.OptionSet) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) PeriodType(org.hisp.dhis.period.PeriodType) CodeGenerator(org.hisp.dhis.common.CodeGenerator) Expression(org.hisp.dhis.expression.Expression) AllArgsConstructor(lombok.AllArgsConstructor) DataElementGroup(org.hisp.dhis.dataelement.DataElementGroup) InputStream(java.io.InputStream) HashMap(java.util.HashMap) ListMap(org.hisp.dhis.common.ListMap) OptionGroup(org.hisp.dhis.option.OptionGroup) CategoryOptionGroup(org.hisp.dhis.category.CategoryOptionGroup) CategoryOption(org.hisp.dhis.category.CategoryOption) Option(org.hisp.dhis.option.Option) OptionSet(org.hisp.dhis.option.OptionSet)

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