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