Search in sources :

Example 91 with DimensionalItemObject

use of org.hisp.dhis.common.DimensionalItemObject in project dhis2-core by dhis2.

the class PredictionDataValueFetcher method addValueToMap.

/**
 * Adds a non-deleted value to the value map.
 * <p>
 * The two types of dimensional item object that are needed from the data
 * value table are DataElement (the sum of all category option combos for
 * that data element) and DataElementOperand (a particular combination of
 * DataElement and CategoryOptionCombo).
 */
private void addValueToMap(DataValue dv, MapMapMap<CategoryOptionCombo, Period, DimensionalItemObject, Object> map) {
    Object value = getObjectValue(dv.getValue(), dv.getDataElement().getValueType());
    if (value != null) {
        DataElementOperand dataElementOperand = new DataElementOperand(dv.getDataElement(), dv.getCategoryOptionCombo());
        addToMap(dataElementOperand, dataElementOperands, dv, value, map);
        addToMap(dv.getDataElement(), dataElements, dv, value, map);
    }
}
Also used : DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject)

Example 92 with DimensionalItemObject

use of org.hisp.dhis.common.DimensionalItemObject in project dhis2-core by dhis2.

the class PredictionAnalyticsDataFetcher method getValuesInternal.

/**
 * Queries analytics for data.
 */
private List<FoundDimensionItemValue> getValuesInternal(List<OrganisationUnit> orgUnits, Set<DimensionalItemObject> dimensionItems, boolean hasAttributeOptions) {
    List<FoundDimensionItemValue> values = new ArrayList<>();
    if (dimensionItems.isEmpty()) {
        return values;
    }
    DataQueryParams.Builder paramsBuilder = DataQueryParams.newBuilder().withPeriods(Lists.newArrayList(periods)).withDataDimensionItems(Lists.newArrayList(dimensionItems)).withOrganisationUnits(orgUnits);
    if (hasAttributeOptions) {
        paramsBuilder.withAttributeOptionCombos(Collections.emptyList());
    }
    Grid grid = analyticsService.getAggregatedDataValues(paramsBuilder.build());
    int peInx = grid.getIndexOfHeader(DimensionalObject.PERIOD_DIM_ID);
    int dxInx = grid.getIndexOfHeader(DimensionalObject.DATA_X_DIM_ID);
    int ouInx = grid.getIndexOfHeader(DimensionalObject.ORGUNIT_DIM_ID);
    int aoInx = hasAttributeOptions ? grid.getIndexOfHeader(DimensionalObject.ATTRIBUTEOPTIONCOMBO_DIM_ID) : 0;
    int vlInx = grid.getWidth() - 1;
    for (List<Object> row : grid.getRows()) {
        String pe = (String) row.get(peInx);
        String dx = (String) row.get(dxInx);
        String ou = (String) row.get(ouInx);
        String ao = hasAttributeOptions ? (String) row.get(aoInx) : null;
        Object vl = row.get(vlInx);
        Period period = periodLookup.get(pe);
        DimensionalItemObject item = analyticsItemsLookup.get(dx);
        OrganisationUnit orgUnit = orgUnitLookup.get(ou);
        CategoryOptionCombo attributeOptionCombo = hasAttributeOptions ? cocLookup.get(ao, () -> categoryService.getCategoryOptionCombo(ao)) : null;
        values.add(new FoundDimensionItemValue(orgUnit, period, attributeOptionCombo, item, vl));
    }
    return values;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Grid(org.hisp.dhis.common.Grid) ArrayList(java.util.ArrayList) Period(org.hisp.dhis.period.Period) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) FoundDimensionItemValue(org.hisp.dhis.common.FoundDimensionItemValue) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) DimensionalObject(org.hisp.dhis.common.DimensionalObject) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo)

Example 93 with DimensionalItemObject

use of org.hisp.dhis.common.DimensionalItemObject in project dhis2-core by dhis2.

the class PredictionContextGenerator method getContexts.

/**
 * Generates prediction contexts. Each prediction context contains all the
 * values required to evaluate a predictor to (possibly) generate one
 * prediction.
 * <p>
 * All the data used to generate contexts has the same organisation unit.
 *
 * @param outputPeriods output periods (predict within each period)
 * @param values input prediction values (all with the same orgUnit)
 * @param defaultCategoryOptionCombo system default cat option combo
 * @return contexts for prediction evaluation
 */
public static List<PredictionContext> getContexts(List<Period> outputPeriods, List<FoundDimensionItemValue> values, CategoryOptionCombo defaultCategoryOptionCombo) {
    List<PredictionContext> contexts = new ArrayList<>();
    MapMapMap<CategoryOptionCombo, Period, DimensionalItemObject, Object> aocMap = getAocMap(values, defaultCategoryOptionCombo);
    for (Map.Entry<CategoryOptionCombo, MapMap<Period, DimensionalItemObject, Object>> e : aocMap.entrySet()) {
        CategoryOptionCombo aoc = e.getKey();
        MapMap<Period, DimensionalItemObject, Object> periodValueMap = e.getValue();
        for (Period outputPeriod : outputPeriods) {
            contexts.add(new PredictionContext(aoc, outputPeriod, periodValueMap, firstNonNull(periodValueMap.get(outputPeriod), new HashMap<>())));
        }
    }
    return contexts;
}
Also used : ArrayList(java.util.ArrayList) Period(org.hisp.dhis.period.Period) MapMapMap(org.hisp.dhis.common.MapMapMap) MapMap(org.hisp.dhis.common.MapMap) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) MapMapMap(org.hisp.dhis.common.MapMapMap) Map(java.util.Map) HashMap(java.util.HashMap) MapMap(org.hisp.dhis.common.MapMap) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo)

Example 94 with DimensionalItemObject

use of org.hisp.dhis.common.DimensionalItemObject in project dhis2-core by dhis2.

the class GeoFeatureService method getCoordinates.

/**
 * Get the {@link GeoFeature} coordinate from {@link DimensionalItemObject}
 * <p>
 * The coordinate value is retrieved from {@link DimensionalItemObject}'s
 * geoJsonAttribute value.
 *
 * @param feature the {@link GeoFeature}
 * @param unit the {@link DimensionalItemObject} contains the coordinate
 *        values.
 * @param geoJsonAttribute The {@link Attribute} which has
 *        {@link ValueType#GEOJSON} and is assigned to
 *        {@link OrganisationUnit}.
 * @return the given {@link GeoFeature} with updated coordinate value and
 *         coordinate type.
 */
private void getCoordinates(GeoFeature feature, DimensionalItemObject unit, Attribute geoJsonAttribute) {
    if (geoJsonAttribute == null) {
        getCoordinates(feature, unit);
        return;
    }
    if (!unit.getClass().isAssignableFrom(OrganisationUnit.class)) {
        return;
    }
    OrganisationUnit organisationUnit = (OrganisationUnit) unit;
    Optional<AttributeValue> geoJsonAttributeValue = organisationUnit.getAttributeValues().stream().filter(attributeValue -> attributeValue.getAttribute().getUid().equals(geoJsonAttribute.getUid())).findFirst();
    if (!geoJsonAttributeValue.isPresent() || StringUtils.isBlank(geoJsonAttributeValue.get().getValue())) {
        getCoordinates(feature, unit);
        return;
    }
    try {
        GeoJsonObject geoJsonObject = new ObjectMapper().readValue(geoJsonAttributeValue.get().getValue(), GeoJsonObject.class);
        GeoFeature geoJsonFeature = geoJsonObject.accept(new GeoFeatureVisitor());
        if (geoJsonFeature == null) {
            return;
        }
        feature.setTy(geoJsonFeature.getTy());
        feature.setCo(geoJsonFeature.getCo());
    } catch (JsonProcessingException e) {
        log.error(String.format("Couldn't read GeoJson value from organisationUnit %s: ", organisationUnit), e);
        getCoordinates(feature, unit);
    }
}
Also used : Date(java.util.Date) ValueType(org.hisp.dhis.common.ValueType) ValidationUtils(org.hisp.dhis.system.util.ValidationUtils) GeoFeature(org.hisp.dhis.webapi.webdomain.GeoFeature) StringUtils(org.apache.commons.lang3.StringUtils) MultiPolygon(org.geojson.MultiPolygon) MultiLineString(org.geojson.MultiLineString) FeatureCollection(org.geojson.FeatureCollection) CoordinateObject(org.hisp.dhis.common.coordinate.CoordinateObject) Map(java.util.Map) OrganisationUnitGroupService(org.hisp.dhis.organisationunit.OrganisationUnitGroupService) ImmutableMap(com.google.common.collect.ImmutableMap) GeoJsonObject(org.geojson.GeoJsonObject) GeoJsonObjectVisitor(org.geojson.GeoJsonObjectVisitor) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) OrganisationUnitGroup(org.hisp.dhis.organisationunit.OrganisationUnitGroup) Set(java.util.Set) Collectors(java.util.stream.Collectors) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Builder(lombok.Builder) AttributeService(org.hisp.dhis.attribute.AttributeService) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) DimensionalObject(org.hisp.dhis.common.DimensionalObject) Optional(java.util.Optional) DhisApiVersion(org.hisp.dhis.common.DhisApiVersion) ORGUNIT_DIM_ID(org.hisp.dhis.common.DimensionalObject.ORGUNIT_DIM_ID) ORGUNIT_GROUP_DIM_ID(org.hisp.dhis.common.DimensionalObject.ORGUNIT_GROUP_DIM_ID) DataQueryService(org.hisp.dhis.analytics.DataQueryService) AttributeValue(org.hisp.dhis.attribute.AttributeValue) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) Getter(lombok.Getter) Feature(org.geojson.Feature) Attribute(org.hisp.dhis.attribute.Attribute) ArrayList(java.util.ArrayList) GeometryCollection(org.geojson.GeometryCollection) HashSet(java.util.HashSet) HttpServletRequest(javax.servlet.http.HttpServletRequest) Lists(com.google.common.collect.Lists) LineString(org.geojson.LineString) Service(org.springframework.stereotype.Service) DisplayProperty(org.hisp.dhis.common.DisplayProperty) DataQueryRequest(org.hisp.dhis.common.DataQueryRequest) Polygon(org.geojson.Polygon) DimensionalObjectUtils(org.hisp.dhis.common.DimensionalObjectUtils) ContextUtils(org.hisp.dhis.webapi.utils.ContextUtils) HttpServletResponse(javax.servlet.http.HttpServletResponse) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) AggregationType(org.hisp.dhis.analytics.AggregationType) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) OrganisationUnitGroupSet(org.hisp.dhis.organisationunit.OrganisationUnitGroupSet) MultiPoint(org.geojson.MultiPoint) DebugUtils(org.hisp.dhis.commons.util.DebugUtils) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ObjectUtils(org.hisp.dhis.util.ObjectUtils) FeatureType(org.hisp.dhis.organisationunit.FeatureType) CurrentUserService(org.hisp.dhis.user.CurrentUserService) Point(org.geojson.Point) Comparator(java.util.Comparator) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) AttributeValue(org.hisp.dhis.attribute.AttributeValue) GeoJsonObject(org.geojson.GeoJsonObject) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) GeoFeature(org.hisp.dhis.webapi.webdomain.GeoFeature)

Example 95 with DimensionalItemObject

use of org.hisp.dhis.common.DimensionalItemObject in project dhis2-core by dhis2.

the class VectorFunction method visitSampledPeriods.

/**
 * Visits each of the sample periods and compiles a list of the double
 * values produced.
 */
private List<Double> visitSampledPeriods(ExprContext ctx, CommonExpressionVisitor visitor) {
    ExpressionParams params = visitor.getParams();
    ExpressionState state = visitor.getState();
    List<Double> values = new ArrayList<>();
    for (Period p : params.getSamplePeriods()) {
        state.setItemsFound(0);
        state.setItemValuesFound(0);
        Map<DimensionalItemObject, Object> valueMap = firstNonNull(params.getPeriodValueMap().get(p), Collections.emptyMap());
        Double value = visitWithValueMap(ctx, visitor, valueMap);
        if ((params.getMissingValueStrategy() == SKIP_IF_ANY_VALUE_MISSING && state.getItemValuesFound() < state.getItemsFound()) || (params.getMissingValueStrategy() == SKIP_IF_ALL_VALUES_MISSING && state.getItemsFound() != 0 && state.getItemValuesFound() == 0)) {
            value = null;
        }
        if (value != null) {
            values.add(value);
        }
    }
    return values;
}
Also used : ExpressionParams(org.hisp.dhis.expression.ExpressionParams) ExpressionState(org.hisp.dhis.parser.expression.ExpressionState) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) ArrayList(java.util.ArrayList) Period(org.hisp.dhis.period.Period) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) AntlrParserUtils.castDouble(org.hisp.dhis.antlr.AntlrParserUtils.castDouble)

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