Search in sources :

Example 31 with DimensionalObject

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

the class QueryPlannerTest method planQueryStartEndDateQueryGrouperA.

/**
     * Splits in 4 queries for each period to satisfy optimal for a total
     * of 4 queries, because all queries have different periods.
     */
@Test
public void planQueryStartEndDateQueryGrouperA() {
    DataQueryParams params = DataQueryParams.newBuilder().withDataElements(getList(deA, deB)).withOrganisationUnits(getList(ouA)).withPeriods(getList(createPeriod("200101"), createPeriod("200102"), createPeriod("200103"), createPeriod("200104"))).build();
    List<Function<DataQueryParams, List<DataQueryParams>>> queryGroupers = Lists.newArrayList();
    queryGroupers.add(q -> queryPlanner.groupByStartEndDate(q));
    QueryPlannerParams plannerParams = QueryPlannerParams.newBuilder().withOptimalQueries(4).withTableName(ANALYTICS_TABLE_NAME).withQueryGroupers(queryGroupers).build();
    DataQueryGroups queryGroups = queryPlanner.planQuery(params, plannerParams);
    List<DataQueryParams> queries = queryGroups.getAllQueries();
    assertEquals(4, queries.size());
    assertEquals(1, queryGroups.getSequentialQueries().size());
    assertEquals(4, queryGroups.getLargestGroupSize());
    for (DataQueryParams query : queries) {
        assertNotNull(query.getStartDate());
        assertNotNull(query.getEndDate());
        assertDimensionNameNotNull(query);
        DimensionalObject periodDim = query.getDimension(PERIOD_DIM_ID);
        assertNotNull(periodDim.getDimensionName());
        assertTrue(periodDim.isFixed());
    }
}
Also used : DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) Function(java.util.function.Function) DataQueryGroups(org.hisp.dhis.analytics.DataQueryGroups) QueryPlannerParams(org.hisp.dhis.analytics.QueryPlannerParams) DimensionalObject(org.hisp.dhis.common.DimensionalObject) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 32 with DimensionalObject

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

the class BaseAnalyticalObject method getDimensionalObject.

/**
     * Assembles a DimensionalObject based on the persisted properties of this
     * AnalyticalObject. Collapses indicators, data elements, data element
     * operands and data sets into the dx dimension.
     * <p>
     * Collapses fixed and relative periods into the pe dimension. Collapses
     * fixed and user organisation units into the ou dimension.
     *
     * @param dimension    the dimension identifier.
     * @param date         the date used for generating relative periods.
     * @param user         the current user.
     * @param dynamicNames whether to use dynamic or static names.
     * @param format       the I18nFormat.
     * @return a DimensionalObject.
     */
protected DimensionalObject getDimensionalObject(String dimension, Date date, User user, boolean dynamicNames, List<OrganisationUnit> organisationUnitsAtLevel, List<OrganisationUnit> organisationUnitsInGroups, I18nFormat format) {
    List<DimensionalItemObject> items = new ArrayList<>();
    DimensionType type = null;
    if (DATA_X_DIM_ID.equals(dimension)) {
        items.addAll(getDataDimensionNameableObjects());
        type = DimensionType.DATA_X;
    } else if (PERIOD_DIM_ID.equals(dimension)) {
        setPeriodNames(periods, dynamicNames, format);
        items.addAll(periods);
        if (hasRelativePeriods()) {
            items.addAll(relatives.getRelativePeriods(date, format, dynamicNames));
        }
        type = DimensionType.PERIOD;
    } else if (ORGUNIT_DIM_ID.equals(dimension)) {
        items.addAll(organisationUnits);
        items.addAll(transientOrganisationUnits);
        if (userOrganisationUnit && user != null && user.hasOrganisationUnit()) {
            items.addAll(user.getOrganisationUnits());
        }
        if (userOrganisationUnitChildren && user != null && user.hasOrganisationUnit()) {
            user.getOrganisationUnits().forEach(ou -> items.addAll(ou.getSortedChildren()));
        }
        if (userOrganisationUnitGrandChildren && user != null && user.hasOrganisationUnit()) {
            user.getOrganisationUnits().forEach(ou -> items.addAll(ou.getSortedGrandChildren()));
        }
        if (organisationUnitLevels != null && !organisationUnitLevels.isEmpty() && organisationUnitsAtLevel != null) {
            // Must be set externally
            items.addAll(organisationUnitsAtLevel);
        }
        if (itemOrganisationUnitGroups != null && !itemOrganisationUnitGroups.isEmpty() && organisationUnitsInGroups != null) {
            // Must be set externally
            items.addAll(organisationUnitsInGroups);
        }
        type = DimensionType.ORGANISATION_UNIT;
    } else if (CATEGORYOPTIONCOMBO_DIM_ID.equals(dimension)) {
        items.addAll(transientCategoryOptionCombos);
        type = DimensionType.CATEGORY_OPTION_COMBO;
    } else if (STATIC_DIMS.contains(dimension)) {
        type = DimensionType.STATIC;
    } else {
        // Embedded dimensions
        Optional<DimensionalObject> object = Optional.empty();
        if ((object = getDimensionFromEmbeddedObjects(dimension, DimensionType.DATA_ELEMENT_GROUP_SET, dataElementGroupSetDimensions)).isPresent()) {
            items.addAll(object.get().getItems());
            type = DimensionType.DATA_ELEMENT_GROUP_SET;
        }
        if ((object = getDimensionFromEmbeddedObjects(dimension, DimensionType.ORGANISATION_UNIT_GROUP_SET, organisationUnitGroupSetDimensions)).isPresent()) {
            items.addAll(object.get().getItems());
            type = DimensionType.ORGANISATION_UNIT_GROUP_SET;
        }
        if ((object = getDimensionFromEmbeddedObjects(dimension, DimensionType.CATEGORY, categoryDimensions)).isPresent()) {
            items.addAll(object.get().getItems());
            type = DimensionType.CATEGORY;
        }
        if ((object = getDimensionFromEmbeddedObjects(dimension, DimensionType.CATEGORY_OPTION_GROUP_SET, categoryOptionGroupSetDimensions)).isPresent()) {
            items.addAll(object.get().getItems());
            type = DimensionType.CATEGORY_OPTION_GROUP_SET;
        }
        // Tracked entity attribute
        Map<String, TrackedEntityAttributeDimension> attributes = Maps.uniqueIndex(attributeDimensions, TrackedEntityAttributeDimension::getUid);
        if (attributes.containsKey(dimension)) {
            TrackedEntityAttributeDimension tead = attributes.get(dimension);
            return new BaseDimensionalObject(dimension, DimensionType.PROGRAM_ATTRIBUTE, null, tead.getDisplayName(), tead.getLegendSet(), tead.getFilter());
        }
        // Tracked entity data element
        Map<String, TrackedEntityDataElementDimension> dataElements = Maps.uniqueIndex(dataElementDimensions, TrackedEntityDataElementDimension::getUid);
        if (dataElements.containsKey(dimension)) {
            TrackedEntityDataElementDimension tedd = dataElements.get(dimension);
            return new BaseDimensionalObject(dimension, DimensionType.PROGRAM_DATA_ELEMENT, null, tedd.getDisplayName(), tedd.getLegendSet(), tedd.getFilter());
        }
        // Tracked entity program indicator
        Map<String, TrackedEntityProgramIndicatorDimension> programIndicators = Maps.uniqueIndex(programIndicatorDimensions, TrackedEntityProgramIndicatorDimension::getUid);
        if (programIndicators.containsKey(dimension)) {
            TrackedEntityProgramIndicatorDimension teid = programIndicators.get(dimension);
            return new BaseDimensionalObject(dimension, DimensionType.PROGRAM_INDICATOR, null, teid.getDisplayName(), teid.getLegendSet(), teid.getFilter());
        }
    }
    IdentifiableObjectUtils.removeDuplicates(items);
    return new BaseDimensionalObject(dimension, type, items);
}
Also used : TrackedEntityAttributeDimension(org.hisp.dhis.trackedentity.TrackedEntityAttributeDimension) ArrayList(java.util.ArrayList) TrackedEntityProgramIndicatorDimension(org.hisp.dhis.trackedentity.TrackedEntityProgramIndicatorDimension) TrackedEntityDataElementDimension(org.hisp.dhis.trackedentity.TrackedEntityDataElementDimension) DimensionalObject(org.hisp.dhis.common.DimensionalObject)

Example 33 with DimensionalObject

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

the class UserCredentials method getDimensionConstraints.

/**
     * Returns the dimensions to use as constrains (filters) in data analytics
     * aggregation.
     */
public Set<DimensionalObject> getDimensionConstraints() {
    Set<DimensionalObject> constraints = new HashSet<>();
    for (CategoryOptionGroupSet cogs : cogsDimensionConstraints) {
        cogs.setDimensionType(DimensionType.CATEGORY_OPTION_GROUP_SET);
        constraints.add(cogs);
    }
    for (DataElementCategory cat : catDimensionConstraints) {
        cat.setDimensionType(DimensionType.CATEGORY);
        constraints.add(cat);
    }
    return constraints;
}
Also used : CategoryOptionGroupSet(org.hisp.dhis.dataelement.CategoryOptionGroupSet) DataElementCategory(org.hisp.dhis.dataelement.DataElementCategory) HashSet(java.util.HashSet) DimensionalObject(org.hisp.dhis.common.DimensionalObject)

Example 34 with DimensionalObject

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

the class AnalyticsUtils method getDimensionItemNameMap.

/**
     * Returns a mapping between identifiers and names for the given query.
     *
     * @param params the data query parameters.
     * @return a mapping between identifiers and names.
     */
public static Map<String, String> getDimensionItemNameMap(DataQueryParams params) {
    List<DimensionalObject> dimensions = params.getDimensionsAndFilters();
    Map<String, String> map = new HashMap<>();
    Calendar calendar = PeriodType.getCalendar();
    for (DimensionalObject dimension : dimensions) {
        for (DimensionalItemObject item : dimension.getItems()) {
            if (DimensionType.PERIOD.equals(dimension.getDimensionType()) && !calendar.isIso8601()) {
                Period period = (Period) item;
                DateTimeUnit dateTimeUnit = calendar.fromIso(period.getStartDate());
                map.put(period.getPeriodType().getIsoDate(dateTimeUnit), period.getDisplayName());
            } else {
                map.put(item.getDimensionItem(), item.getDisplayProperty(params.getDisplayProperty()));
            }
            if (DimensionType.ORGANISATION_UNIT.equals(dimension.getDimensionType()) && params.isHierarchyMeta()) {
                OrganisationUnit unit = (OrganisationUnit) item;
                map.putAll(NameableObjectUtils.getUidDisplayPropertyMap(unit.getAncestors(), params.getDisplayProperty()));
            }
        }
        map.put(dimension.getDimension(), dimension.getDisplayProperty(params.getDisplayProperty()));
    }
    return map;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Calendar(org.hisp.dhis.calendar.Calendar) DateTimeUnit(org.hisp.dhis.calendar.DateTimeUnit) Period(org.hisp.dhis.period.Period) DateUtils.getMediumDateString(org.hisp.dhis.system.util.DateUtils.getMediumDateString) DimensionalObject(org.hisp.dhis.common.DimensionalObject)

Example 35 with DimensionalObject

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

the class AnalyticsUtils method getUidDisplayPropertyMap.

/**
     * Returns a mapping between identifiers and display properties for the given 
     * list of dimensions.
     * 
     * @param dimensions the dimensions.
     * @param hierarchyMeta indicates whether to include meta data about the
     *        organisation unit hierarchy.
     * @return a mapping between identifiers and display properties.
     */
public static Map<String, String> getUidDisplayPropertyMap(List<DimensionalObject> dimensions, boolean hierarchyMeta, DisplayProperty displayProperty) {
    Map<String, String> map = new HashMap<>();
    for (DimensionalObject dimension : dimensions) {
        boolean hierarchy = hierarchyMeta && DimensionType.ORGANISATION_UNIT.equals(dimension.getDimensionType());
        for (DimensionalItemObject object : dimension.getItems()) {
            Set<DimensionalItemObject> objects = Sets.newHashSet(object);
            if (hierarchy) {
                OrganisationUnit unit = (OrganisationUnit) object;
                objects.addAll(unit.getAncestors());
            }
            map.putAll(NameableObjectUtils.getUidDisplayPropertyMap(objects, displayProperty));
        }
        map.put(dimension.getDimension(), dimension.getDisplayProperty(displayProperty));
    }
    return map;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DateUtils.getMediumDateString(org.hisp.dhis.system.util.DateUtils.getMediumDateString) DimensionalObject(org.hisp.dhis.common.DimensionalObject)

Aggregations

DimensionalObject (org.hisp.dhis.common.DimensionalObject)44 ArrayList (java.util.ArrayList)14 BaseDimensionalObject (org.hisp.dhis.common.BaseDimensionalObject)14 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)13 DataQueryParams (org.hisp.dhis.analytics.DataQueryParams)11 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)10 Test (org.junit.Test)8 List (java.util.List)6 DhisConvenienceTest (org.hisp.dhis.DhisConvenienceTest)5 Grid (org.hisp.dhis.common.Grid)5 DataElement (org.hisp.dhis.dataelement.DataElement)5 Period (org.hisp.dhis.period.Period)5 DateUtils.getMediumDateString (org.hisp.dhis.system.util.DateUtils.getMediumDateString)5 Calendar (org.hisp.dhis.calendar.Calendar)4 User (org.hisp.dhis.user.User)4 CategoryOptionGroupSet (org.hisp.dhis.dataelement.CategoryOptionGroupSet)3 DataSet (org.hisp.dhis.dataset.DataSet)3 Indicator (org.hisp.dhis.indicator.Indicator)3 OrganisationUnitGroup (org.hisp.dhis.organisationunit.OrganisationUnitGroup)3 ListGrid (org.hisp.dhis.system.grid.ListGrid)3