use of org.hisp.dhis.analytics.DataQueryParams in project dhis2-core by dhis2.
the class DefaultAnalyticsService method addDataElementValues.
/**
* Adds data element values to the given grid based on the given data query
* parameters.
*
* @param params the {@link DataQueryParams}.
* @param grid the grid.
*/
private void addDataElementValues(DataQueryParams params, Grid grid) {
if (!params.getAllDataElements().isEmpty() && !params.isSkipData()) {
DataQueryParams dataSourceParams = DataQueryParams.newBuilder(params).retainDataDimension(DataDimensionItemType.DATA_ELEMENT).withIncludeNumDen(false).build();
Map<String, Object> aggregatedDataMap = getAggregatedDataValueMapObjectTyped(dataSourceParams);
for (Map.Entry<String, Object> entry : aggregatedDataMap.entrySet()) {
Object value = AnalyticsUtils.getRoundedValueObject(params, entry.getValue());
grid.addRow().addValues(entry.getKey().split(DIMENSION_SEP)).addValue(value);
if (params.isIncludeNumDen()) {
grid.addNullValues(3);
}
}
}
}
use of org.hisp.dhis.analytics.DataQueryParams in project dhis2-core by dhis2.
the class DefaultAnalyticsService method getAggregatedDataValueMap.
/**
* Returns a mapping between dimension items and values for the given data
* query and list of indicators. The dimensional items part of the indicator
* numerators and denominators are used as dimensional item for the aggregated
* values being retrieved.
*
* @param params the {@link DataQueryParams}.
* @param indicators the list of indicators.
* @return a dimensional items to aggregate values map.
*/
private Map<String, Double> getAggregatedDataValueMap(DataQueryParams params, List<Indicator> indicators) {
List<DimensionalItemObject> items = Lists.newArrayList(expressionService.getDimensionalItemObjectsInIndicators(indicators));
items = DimensionalObjectUtils.replaceOperandTotalsWithDataElements(items);
DimensionalObject dimension = new BaseDimensionalObject(DimensionalObject.DATA_X_DIM_ID, DimensionType.DATA_X, null, DISPLAY_NAME_DATA_X, items);
DataQueryParams dataSourceParams = DataQueryParams.newBuilder(params).replaceDimension(dimension).withIncludeNumDen(false).withSkipHeaders(true).withSkipMeta(true).build();
Grid grid = getAggregatedDataValueGridInternal(dataSourceParams);
return grid.getAsMap(grid.getWidth() - 1, DimensionalObject.DIMENSION_SEP);
}
use of org.hisp.dhis.analytics.DataQueryParams in project dhis2-core by dhis2.
the class DefaultAnalyticsService method getAggregatedValueMap.
/**
* Generates a mapping between a dimension key and the aggregated value. The
* dimension key is a concatenation of the identifiers of the dimension items
* separated by "-".
*
* @param params the {@link DataQueryParams}.
* @param tableName the table name to use for the query.
* @param queryGroupers the list of additional query groupers to use for
* query planning, use empty list for none.
* @return a mapping between a dimension key and aggregated values.
*/
private Map<String, Object> getAggregatedValueMap(DataQueryParams params, String tableName, List<Function<DataQueryParams, List<DataQueryParams>>> queryGroupers) {
queryPlanner.validateMaintenanceMode();
int optimalQueries = MathUtils.getWithin(getProcessNo(), 1, MAX_QUERIES);
int maxLimit = params.isIgnoreLimit() ? 0 : (Integer) systemSettingManager.getSystemSetting(SettingKey.ANALYTICS_MAX_LIMIT);
Timer timer = new Timer().start().disablePrint();
QueryPlannerParams plannerParams = QueryPlannerParams.newBuilder().withOptimalQueries(optimalQueries).withTableName(tableName).withQueryGroupers(queryGroupers).build();
DataQueryGroups queryGroups = queryPlanner.planQuery(params, plannerParams);
timer.getSplitTime("Planned analytics query, got: " + queryGroups.getLargestGroupSize() + " for optimal: " + optimalQueries);
Map<String, Object> map = new HashMap<>();
for (List<DataQueryParams> queries : queryGroups.getSequentialQueries()) {
List<Future<Map<String, Object>>> futures = new ArrayList<>();
for (DataQueryParams query : queries) {
futures.add(analyticsManager.getAggregatedDataValues(query, maxLimit));
}
for (Future<Map<String, Object>> future : futures) {
try {
Map<String, Object> taskValues = future.get();
if (taskValues != null) {
map.putAll(taskValues);
}
} catch (Exception ex) {
log.error(DebugUtils.getStackTrace(ex));
log.error(DebugUtils.getStackTrace(ex.getCause()));
throw new RuntimeException("Error during execution of aggregation query task", ex);
}
}
}
timer.getTime("Got analytics values");
return map;
}
use of org.hisp.dhis.analytics.DataQueryParams in project dhis2-core by dhis2.
the class DefaultAnalyticsService method addMetaData.
/**
* Adds meta data values to the given grid based on the given data query
* parameters.
*
* @param params the {@link DataQueryParams}.
* @param grid the grid.
*/
private void addMetaData(DataQueryParams params, Grid grid) {
if (!params.isSkipMeta()) {
Map<String, Object> metaData = new HashMap<>();
Map<String, Object> internalMetaData = new HashMap<>();
// -----------------------------------------------------------------
// Items / names element
// -----------------------------------------------------------------
Map<String, String> cocNameMap = AnalyticsUtils.getCocNameMap(params);
if (params.getApiVersion().ge(DhisApiVersion.V26)) {
metaData.put(AnalyticsMetaDataKey.ITEMS.getKey(), AnalyticsUtils.getDimensionMetadataItemMap(params));
} else {
Map<String, String> uidNameMap = AnalyticsUtils.getDimensionItemNameMap(params);
uidNameMap.putAll(cocNameMap);
uidNameMap.put(DATA_X_DIM_ID, DISPLAY_NAME_DATA_X);
metaData.put(AnalyticsMetaDataKey.NAMES.getKey(), uidNameMap);
}
// -----------------------------------------------------------------
// Item order elements
// -----------------------------------------------------------------
Map<String, Object> dimensionItems = new HashMap<>();
Calendar calendar = PeriodType.getCalendar();
List<String> periodUids = calendar.isIso8601() ? getDimensionalItemIds(params.getDimensionOrFilterItems(PERIOD_DIM_ID)) : getLocalPeriodIdentifiers(params.getDimensionOrFilterItems(PERIOD_DIM_ID), calendar);
dimensionItems.put(PERIOD_DIM_ID, periodUids);
dimensionItems.put(CATEGORYOPTIONCOMBO_DIM_ID, cocNameMap.keySet());
for (DimensionalObject dim : params.getDimensionsAndFilters()) {
if (!dimensionItems.keySet().contains(dim.getDimension())) {
dimensionItems.put(dim.getDimension(), getDimensionalItemIds(dim.getItems()));
}
}
if (params.getApiVersion().ge(DhisApiVersion.V26)) {
metaData.put(AnalyticsMetaDataKey.DIMENSIONS.getKey(), dimensionItems);
} else {
metaData.putAll(dimensionItems);
}
// -----------------------------------------------------------------
// Organisation unit hierarchy
// -----------------------------------------------------------------
User user = securityManager.getCurrentUser(params);
List<OrganisationUnit> organisationUnits = asTypedList(params.getDimensionOrFilterItems(ORGUNIT_DIM_ID));
Collection<OrganisationUnit> roots = user != null ? user.getOrganisationUnits() : null;
if (params.isHierarchyMeta()) {
metaData.put(AnalyticsMetaDataKey.ORG_UNIT_HIERARCHY.getKey(), getParentGraphMap(organisationUnits, roots));
}
if (params.isShowHierarchy()) {
Map<Object, List<?>> ancestorMap = organisationUnits.stream().collect(Collectors.toMap(OrganisationUnit::getUid, ou -> ou.getAncestorNames(roots, true)));
internalMetaData.put(AnalyticsMetaDataKey.ORG_UNIT_ANCESTORS.getKey(), ancestorMap);
metaData.put(AnalyticsMetaDataKey.ORG_UNIT_NAME_HIERARCHY.getKey(), getParentNameGraphMap(organisationUnits, roots, true));
}
grid.setMetaData(ImmutableMap.copyOf(metaData));
grid.setInternalMetaData(ImmutableMap.copyOf(internalMetaData));
}
}
use of org.hisp.dhis.analytics.DataQueryParams in project dhis2-core by dhis2.
the class DataQueryServiceTest method testGetFromAnalyticalObjectC.
@Test
public void testGetFromAnalyticalObjectC() {
Chart chart = new Chart();
chart.setSeries(DimensionalObject.DATA_X_DIM_ID);
chart.setCategory(ouGroupSetA.getUid());
chart.getFilterDimensions().add(DimensionalObject.PERIOD_DIM_ID);
chart.addDataDimensionItem(deA);
chart.addDataDimensionItem(pdA);
chart.addDataDimensionItem(pdB);
OrganisationUnitGroupSetDimension ouGroupSetDim = new OrganisationUnitGroupSetDimension();
ouGroupSetDim.setDimension(ouGroupSetA);
ouGroupSetDim.setItems(Lists.newArrayList(ouGroupA, ouGroupB, ouGroupC));
chart.getOrganisationUnitGroupSetDimensions().add(ouGroupSetDim);
chart.getPeriods().add(PeriodType.getPeriodFromIsoString("2012"));
DataQueryParams params = dataQueryService.getFromAnalyticalObject(chart);
assertNotNull(params);
assertEquals(1, params.getDataElements().size());
assertEquals(2, params.getProgramDataElements().size());
assertEquals(1, params.getFilterPeriods().size());
assertEquals(2, params.getDimensions().size());
assertEquals(1, params.getFilters().size());
assertEquals(3, params.getDimensionOptions(ouGroupSetA.getUid()).size());
}
Aggregations