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());
}
}
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);
}
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;
}
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;
}
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;
}
Aggregations