use of org.hisp.dhis.analytics.DataQueryParams in project dhis2-core by dhis2.
the class DefaultAnalyticsService method addRawData.
/**
* Adds raw data to the grid for the given data query parameters.
*
* @param params the {@link DataQueryParams}.
* @param grid the grid.
*/
private void addRawData(DataQueryParams params, Grid grid) {
if (!params.isSkipData()) {
QueryPlannerParams plannerParams = QueryPlannerParams.newBuilder().withTableName(AnalyticsTableType.DATA_VALUE.getTableName()).build();
List<DataQueryParams> queries = queryPlanner.groupByPartition(params, plannerParams);
queries.forEach(query -> rawAnalyticsManager.getRawDataValues(query, grid));
}
}
use of org.hisp.dhis.analytics.DataQueryParams 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.analytics.DataQueryParams in project dhis2-core by dhis2.
the class DefaultAnalyticsService method getOrgUnitTargetMap.
// -------------------------------------------------------------------------
// Supportive methods
// -------------------------------------------------------------------------
/**
* Generates a mapping of permutations keys (organisation unit id or null)
* and mappings of organisation unit group and counts.
*
* @param params the {@link DataQueryParams}.
* @param indicators the indicators for which formulas to scan for organisation
* unit groups.
* @return a map of maps.
*/
private Map<String, Map<String, Integer>> getOrgUnitTargetMap(DataQueryParams params, Collection<Indicator> indicators) {
Set<OrganisationUnitGroup> orgUnitGroups = expressionService.getOrganisationUnitGroupsInIndicators(indicators);
if (orgUnitGroups.isEmpty()) {
return null;
}
DataQueryParams orgUnitTargetParams = DataQueryParams.newBuilder(params).pruneToDimensionType(DimensionType.ORGANISATION_UNIT).addDimension(new BaseDimensionalObject(DimensionalObject.ORGUNIT_GROUP_DIM_ID, null, new ArrayList<DimensionalItemObject>(orgUnitGroups))).withSkipPartitioning(true).build();
Map<String, Double> orgUnitCountMap = getAggregatedOrganisationUnitTargetMap(orgUnitTargetParams);
return DataQueryParams.getPermutationOrgUnitGroupCountMap(orgUnitCountMap);
}
use of org.hisp.dhis.analytics.DataQueryParams in project dhis2-core by dhis2.
the class DefaultQueryPlanner method groupByDataPeriodType.
/**
* Groups the given query in sub queries based on the period type of its
* data elements. Sets the data period type on each query. This only applies
* if the aggregation type of the query involves disaggregation.
*
* @param params the data query parameters.
* @return a list of {@link DataQueryParams}.
*/
private List<DataQueryParams> groupByDataPeriodType(DataQueryParams params) {
List<DataQueryParams> queries = new ArrayList<>();
if (params.getDataElements().isEmpty() || !params.isDisaggregation()) {
queries.add(DataQueryParams.newBuilder(params).build());
return queries;
}
ListMap<PeriodType, DimensionalItemObject> periodTypeDataElementMap = QueryPlannerUtils.getPeriodTypeDataElementMap(params.getDataElements());
for (PeriodType periodType : periodTypeDataElementMap.keySet()) {
DataQueryParams query = DataQueryParams.newBuilder(params).withDataElements(periodTypeDataElementMap.get(periodType)).withDataPeriodType(periodType).build();
queries.add(query);
}
if (queries.size() > 1) {
log.debug(String.format("Split on data period type: %d", queries.size()));
}
return queries;
}
use of org.hisp.dhis.analytics.DataQueryParams in project dhis2-core by dhis2.
the class DefaultQueryPlanner method planQuery.
@Override
public DataQueryGroups planQuery(DataQueryParams params, QueryPlannerParams plannerParams) {
validate(params);
// ---------------------------------------------------------------------
// Group queries which can be executed together
// ---------------------------------------------------------------------
final List<DataQueryParams> queries = new ArrayList<>(groupByPartition(params, plannerParams));
List<Function<DataQueryParams, List<DataQueryParams>>> groupers = new ImmutableList.Builder<Function<DataQueryParams, List<DataQueryParams>>>().add(q -> groupByOrgUnitLevel(q)).add(q -> groupByPeriodType(q)).add(q -> groupByDataType(q)).add(q -> groupByAggregationType(q)).add(q -> groupByDaysInPeriod(q)).add(q -> groupByDataPeriodType(q)).addAll(plannerParams.getQueryGroupers()).build();
for (Function<DataQueryParams, List<DataQueryParams>> grouper : groupers) {
List<DataQueryParams> currentQueries = Lists.newArrayList(queries);
queries.clear();
for (DataQueryParams query : currentQueries) {
queries.addAll(grouper.apply(query));
}
}
// ---------------------------------------------------------------------
// Split queries until optimal number
// ---------------------------------------------------------------------
DataQueryGroups queryGroups = DataQueryGroups.newBuilder().withQueries(queries).build();
if (queryGroups.isOptimal(plannerParams.getOptimalQueries())) {
return queryGroups;
}
List<String> splitDimensions = Lists.newArrayList(DATA_X_DIM_ID, ORGUNIT_DIM_ID);
for (String dim : splitDimensions) {
queryGroups = splitByDimension(queryGroups, dim, plannerParams.getOptimalQueries());
if (queryGroups.isOptimal(plannerParams.getOptimalQueries())) {
break;
}
}
return queryGroups;
}
Aggregations