use of org.hisp.dhis.common.DimensionalItemObject in project dhis2-core by dhis2.
the class DefaultQueryPlanner method groupByAggregationType.
/**
* Groups the given query in sub queries based on the aggregation type of
* its data elements. The aggregation type can be sum, average aggregation
* or average disaggregation. Sum means that the data elements have sum
* aggregation operator. Average aggregation means that the data elements
* have the average aggregation operator and that the period type of the
* data elements have higher or equal frequency than the aggregation period
* type. Average disaggregation means that the data elements have the
* average aggregation operator and that the period type of the data
* elements have lower frequency than the aggregation period type. Average
* bool means that the data elements have the average aggregation operator
* and the bool value type.
* <p>
* If no data elements are present, the aggregation type will be determined
* based on the first data element in the first data element group in the
* first data element group set in the query.
* <p>
* If the aggregation type is already set/overridden in the request, the
* query will be returned unchanged. If there are no data elements or data
* element group sets specified the aggregation type will fall back to sum.
*
* @param params the {@link DataQueryParams}.
* @return a list of {@link DataQueryParams}.
*/
private List<DataQueryParams> groupByAggregationType(DataQueryParams params) {
List<DataQueryParams> queries = new ArrayList<>();
if (!params.getDataElements().isEmpty()) {
ListMap<AnalyticsAggregationType, DimensionalItemObject> aggregationTypeDataElementMap = QueryPlannerUtils.getAggregationTypeDataElementMap(params.getDataElements(), params.getAggregationType(), params.getPeriodType());
for (AnalyticsAggregationType aggregationType : aggregationTypeDataElementMap.keySet()) {
DataQueryParams query = DataQueryParams.newBuilder(params).withDataElements(aggregationTypeDataElementMap.get(aggregationType)).withAggregationType(aggregationType).build();
queries.add(query);
}
} else if (!params.getDataElementGroupSets().isEmpty()) {
DataElementGroup deg = params.getFirstDataElementGroup();
AnalyticsAggregationType aggregationType = ObjectUtils.firstNonNull(params.getAggregationType(), AnalyticsAggregationType.SUM);
if (deg != null && !deg.getMembers().isEmpty()) {
PeriodType periodType = PeriodType.getPeriodTypeByName(params.getPeriodType());
AnalyticsAggregationType degAggType = AnalyticsAggregationType.fromAggregationType(deg.getAggregationType());
aggregationType = ObjectUtils.firstNonNull(params.getAggregationType(), degAggType, AnalyticsAggregationType.SUM);
aggregationType = QueryPlannerUtils.getAggregationType(aggregationType, deg.getValueType(), periodType, deg.getPeriodType());
}
DataQueryParams query = DataQueryParams.newBuilder(params).withAggregationType(aggregationType).build();
queries.add(query);
} else if (filterHasDataElementsOfSameAggregationTypeAndValueType(params)) {
ListMap<AnalyticsAggregationType, DimensionalItemObject> aggregationTypeDataElementMap = QueryPlannerUtils.getAggregationTypeDataElementMap(params.getFilterOptions(DATA_X_DIM_ID, DataDimensionItemType.DATA_ELEMENT), params.getAggregationType(), params.getPeriodType());
for (AnalyticsAggregationType aggregationType : aggregationTypeDataElementMap.keySet()) {
DataQueryParams query = DataQueryParams.newBuilder(params).withAggregationType(aggregationType).build();
queries.add(query);
}
} else {
DataQueryParams query = DataQueryParams.newBuilder(params).withAggregationType(ObjectUtils.firstNonNull(params.getAggregationType(), AnalyticsAggregationType.SUM)).build();
queries.add(query);
}
logQuerySplit(queries, "aggregation type");
return queries;
}
use of org.hisp.dhis.common.DimensionalItemObject in project dhis2-core by dhis2.
the class JdbcEnrollmentAnalyticsManager method getWhereClause.
/**
* Returns a from and where SQL clause. If this is a program indicator with
* non-default boundaries, the relationship with the reporting period is
* specified with where conditions on the enrollment or incident dates. If
* the default boundaries is used, or the params does not include program
* indicators, the periods are joined in from the analytics tables the
* normal way. A where clause can never have a mix of indicators with
* non-default boundaries and regular analytics table periods.
*
* @param params the {@link EventQueryParams}.
*/
@Override
protected String getWhereClause(EventQueryParams params) {
String sql = "";
SqlHelper hlp = new SqlHelper();
// ---------------------------------------------------------------------
// Periods
// ---------------------------------------------------------------------
sql += hlp.whereAnd() + " " + timeFieldSqlRenderer.renderTimeFieldSql(params);
if (params.isOrganisationUnitMode(OrganisationUnitSelectionMode.SELECTED)) {
sql += hlp.whereAnd() + " ou in (" + getQuotedCommaDelimitedString(getUids(params.getDimensionOrFilterItems(ORGUNIT_DIM_ID))) + ") ";
} else if (params.isOrganisationUnitMode(OrganisationUnitSelectionMode.CHILDREN)) {
sql += hlp.whereAnd() + " ou in (" + getQuotedCommaDelimitedString(getUids(params.getOrganisationUnitChildren())) + ") ";
} else // Descendants
{
sql += hlp.whereAnd() + " (";
for (DimensionalItemObject object : params.getDimensionOrFilterItems(ORGUNIT_DIM_ID)) {
OrganisationUnit unit = (OrganisationUnit) object;
sql += "uidlevel" + unit.getLevel() + " = '" + unit.getUid() + "' or ";
}
sql = removeLastOr(sql) + ") ";
}
// ---------------------------------------------------------------------
// Organisation unit group sets
// ---------------------------------------------------------------------
List<DimensionalObject> dynamicDimensions = params.getDimensionsAndFilters(Sets.newHashSet(DimensionType.ORGANISATION_UNIT_GROUP_SET, DimensionType.CATEGORY));
for (DimensionalObject dim : dynamicDimensions) {
String col = quoteAlias(dim.getDimensionName());
sql += "and " + col + " in (" + getQuotedCommaDelimitedString(getUids(dim.getItems())) + ") ";
}
if (params.hasProgramStage()) {
sql += "and ps = '" + params.getProgramStage().getUid() + "' ";
}
for (QueryItem item : params.getItems()) {
if (item.hasFilter()) {
for (QueryFilter filter : item.getFilters()) {
String field = getSelectSql(filter, item, params.getEarliestStartDate(), params.getLatestEndDate());
if (IN.equals(filter.getOperator())) {
InQueryFilter inQueryFilter = new InQueryFilter(field, statementBuilder.encode(filter.getFilter(), false), item.isText());
sql += hlp.whereAnd() + " " + inQueryFilter.getSqlFilter();
} else {
sql += "and " + field + " " + filter.getSqlOperator() + " " + getSqlFilter(filter, item) + " ";
}
}
}
}
for (QueryItem item : params.getItemFilters()) {
if (item.hasFilter()) {
for (QueryFilter filter : item.getFilters()) {
sql += "and " + getSelectSql(filter, item, params.getEarliestStartDate(), params.getLatestEndDate()) + " " + filter.getSqlOperator() + " " + getSqlFilter(filter, item) + " ";
}
}
}
if (params.hasProgramIndicatorDimension() && params.getProgramIndicator().hasFilter()) {
String filter = programIndicatorService.getAnalyticsSql(params.getProgramIndicator().getFilter(), params.getProgramIndicator(), params.getEarliestStartDate(), params.getLatestEndDate());
String sqlFilter = ExpressionUtils.asSql(filter);
sql += "and (" + sqlFilter + ") ";
}
if (params.hasProgramStatus()) {
sql += "and enrollmentstatus in (" + params.getProgramStatus().stream().map(p -> encode(p.name(), true)).collect(joining(",")) + ") ";
}
if (params.isCoordinatesOnly()) {
sql += "and (longitude is not null and latitude is not null) ";
}
if (params.isGeometryOnly()) {
sql += "and " + quoteAlias(params.getCoordinateField()) + " is not null ";
}
if (params.isCompletedOnly()) {
sql += "and completeddate is not null ";
}
if (params.hasBbox()) {
sql += "and " + quoteAlias(params.getCoordinateField()) + " && ST_MakeEnvelope(" + params.getBbox() + ",4326) ";
}
return sql;
}
use of org.hisp.dhis.common.DimensionalItemObject in project dhis2-core by dhis2.
the class DimensionalItem method getDescription.
@Override
public final Object getDescription(ExprContext ctx, CommonExpressionVisitor visitor) {
DimensionalItemId itemId = getDimensionalItemId(ctx, visitor);
DimensionalItemObject item = visitor.getDimensionService().getDataDimensionalItemObject(itemId);
if (item == null) {
throw new ParserExceptionWithoutContext("Can't find " + itemId.getDimensionItemType().name() + " for '" + itemId + "'");
}
visitor.getItemDescriptions().put(ctx.getText(), item.getDisplayName());
return ValidationUtils.getNullReplacementValue(getItemValueType(item));
}
use of org.hisp.dhis.common.DimensionalItemObject in project dhis2-core by dhis2.
the class DimensionalItem method evaluate.
@Override
public final Object evaluate(ExprContext ctx, CommonExpressionVisitor visitor) {
DimensionalItemId itemId = getDimensionalItemId(ctx, visitor);
DimensionalItemObject item = visitor.getParams().getItemMap().get(itemId);
Object value = (item != null) ? visitor.getParams().getValueMap().get(item) : null;
return visitor.getState().handleNulls(value, getItemValueType(item));
}
use of org.hisp.dhis.common.DimensionalItemObject in project dhis2-core by dhis2.
the class DimensionServiceTest method testGetDataDimensionalItemObjectMapReturnsItemsWithAllOffsets.
@Test
void testGetDataDimensionalItemObjectMapReturnsItemsWithAllOffsets() {
Map<DimensionalItemId, DimensionalItemObject> result;
DimensionalItemId deAId = new DimensionalItemId(DATA_ELEMENT, deA.getUid());
DimensionalItemId deBId = new DimensionalItemId(DATA_ELEMENT, deB.getUid());
DimensionalItemId deCId = new DimensionalItemId(DATA_ELEMENT, deC.getUid());
DimensionalItemId deAOffset1Id = new DimensionalItemId(DATA_ELEMENT, deA.getUid(), queryModsA);
DimensionalItemId deBOffset1Id = new DimensionalItemId(DATA_ELEMENT, deB.getUid(), queryModsA);
DimensionalItemId deCOffset1Id = new DimensionalItemId(DATA_ELEMENT, deC.getUid(), queryModsA);
DimensionalItemId deAOffset2Id = new DimensionalItemId(DATA_ELEMENT, deA.getUid(), queryModsB);
DimensionalItemId deBOffset2Id = new DimensionalItemId(DATA_ELEMENT, deB.getUid(), queryModsB);
DimensionalItemId deCOffset2Id = new DimensionalItemId(DATA_ELEMENT, deC.getUid(), queryModsB);
DataElement deAOffset1 = makeDataElementWithQueryModsFrom(deA, queryModsA);
DataElement deBOffset1 = makeDataElementWithQueryModsFrom(deB, queryModsA);
DataElement deCOffset1 = makeDataElementWithQueryModsFrom(deC, queryModsA);
DataElement deAOffset2 = makeDataElementWithQueryModsFrom(deA, queryModsB);
DataElement deBOffset2 = makeDataElementWithQueryModsFrom(deB, queryModsB);
DataElement deCOffset2 = makeDataElementWithQueryModsFrom(deC, queryModsB);
Set<DimensionalItemId> dimensionalItemIds = Sets.newHashSet(deAId, deBId, deCId, deAOffset1Id, deBOffset1Id, deCOffset1Id, deAOffset2Id, deBOffset2Id, deCOffset2Id);
ImmutableMap<DimensionalItemId, DimensionalItemObject> dimensionalItemMap = ImmutableMap.<DimensionalItemId, DimensionalItemObject>builder().put(deAId, deA).put(deBId, deB).put(deCId, deC).put(deAOffset1Id, deAOffset1).put(deBOffset1Id, deBOffset1).put(deCOffset1Id, deCOffset1).put(deAOffset2Id, deAOffset2).put(deBOffset2Id, deBOffset2).put(deCOffset2Id, deCOffset2).build();
// When
result = dimensionService.getDataDimensionalItemObjectMap(dimensionalItemIds);
// Then
assertMapEquals(dimensionalItemMap, result);
}
Aggregations