Search in sources :

Example 56 with DimensionalItemObject

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;
}
Also used : DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) PeriodType(org.hisp.dhis.period.PeriodType) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) ArrayList(java.util.ArrayList) DataElementGroup(org.hisp.dhis.dataelement.DataElementGroup) AnalyticsAggregationType(org.hisp.dhis.analytics.AnalyticsAggregationType)

Example 57 with DimensionalItemObject

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;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) QueryItem(org.hisp.dhis.common.QueryItem) QueryFilter(org.hisp.dhis.common.QueryFilter) InQueryFilter(org.hisp.dhis.common.InQueryFilter) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) SqlHelper(org.hisp.dhis.commons.util.SqlHelper) TextUtils.getQuotedCommaDelimitedString(org.hisp.dhis.commons.util.TextUtils.getQuotedCommaDelimitedString) DimensionalObject(org.hisp.dhis.common.DimensionalObject) InQueryFilter(org.hisp.dhis.common.InQueryFilter)

Example 58 with DimensionalItemObject

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));
}
Also used : ParserExceptionWithoutContext(org.hisp.dhis.antlr.ParserExceptionWithoutContext) DimensionalItemId(org.hisp.dhis.common.DimensionalItemId) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) ValueTypedDimensionalItemObject(org.hisp.dhis.common.ValueTypedDimensionalItemObject)

Example 59 with DimensionalItemObject

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));
}
Also used : DimensionalItemId(org.hisp.dhis.common.DimensionalItemId) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) ValueTypedDimensionalItemObject(org.hisp.dhis.common.ValueTypedDimensionalItemObject) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) ValueTypedDimensionalItemObject(org.hisp.dhis.common.ValueTypedDimensionalItemObject)

Example 60 with DimensionalItemObject

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);
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) DimensionalItemId(org.hisp.dhis.common.DimensionalItemId) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) BaseDimensionalItemObject(org.hisp.dhis.common.BaseDimensionalItemObject) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Aggregations

DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)178 Test (org.junit.jupiter.api.Test)63 ArrayList (java.util.ArrayList)51 DimensionalObject (org.hisp.dhis.common.DimensionalObject)48 DataQueryParams (org.hisp.dhis.analytics.DataQueryParams)42 Period (org.hisp.dhis.period.Period)41 BaseDimensionalObject (org.hisp.dhis.common.BaseDimensionalObject)40 HashMap (java.util.HashMap)33 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)28 DhisSpringTest (org.hisp.dhis.DhisSpringTest)26 DataElementOperand (org.hisp.dhis.dataelement.DataElementOperand)22 BaseDimensionalItemObject (org.hisp.dhis.common.BaseDimensionalItemObject)20 DimensionalItemId (org.hisp.dhis.common.DimensionalItemId)20 DataElement (org.hisp.dhis.dataelement.DataElement)20 List (java.util.List)17 Indicator (org.hisp.dhis.indicator.Indicator)17 Grid (org.hisp.dhis.common.Grid)16 ProgramIndicator (org.hisp.dhis.program.ProgramIndicator)16 ListMap (org.hisp.dhis.common.ListMap)15 QueryItem (org.hisp.dhis.common.QueryItem)15