use of org.hisp.dhis.common.DimensionalItemObject in project dhis2-core by dhis2.
the class PredictionAnalyticsDataFetcherTest method testGetValues.
// -------------------------------------------------------------------------
// Tests
// -------------------------------------------------------------------------
@Test
void testGetValues() {
// ---------------------------------------------------------------------
// Items with Attribute Option Combos
// ---------------------------------------------------------------------
CategoryOptionCombo aocC = createCategoryOptionCombo('C');
CategoryOptionCombo aocD = createCategoryOptionCombo('D');
when(categoryService.getCategoryOptionCombo(aocC.getUid())).thenReturn(aocC);
when(categoryService.getCategoryOptionCombo(aocD.getUid())).thenReturn(aocD);
ProgramIndicator programIndicatorA = createProgramIndicator('A', programA, "expressionA", "filterA");
ProgramIndicator programIndicatorB = createProgramIndicator('B', programB, "expressionB", "filterB");
trackedEntityAttributeA = createTrackedEntityAttribute('A');
Set<DimensionalItemObject> aocItems = Sets.newHashSet(programIndicatorA, programIndicatorB, trackedEntityAttributeA);
DataQueryParams aocParams = DataQueryParams.newBuilder().withPeriods(Lists.newArrayList(periods)).withDataDimensionItems(Lists.newArrayList(aocItems)).withOrganisationUnits(orgUnits).withAttributeOptionCombos(Collections.emptyList()).build();
Grid aocGrid = new ListGrid();
aocGrid.addHeader(new GridHeader(PERIOD_DIM_ID, "Period", ValueType.TEXT, false, true));
aocGrid.addHeader(new GridHeader(DATA_X_DIM_ID, "DimensionItem", ValueType.TEXT, false, true));
aocGrid.addHeader(new GridHeader(ORGUNIT_DIM_ID, "OrganisationUnit", ValueType.TEXT, false, true));
aocGrid.addHeader(new GridHeader(ATTRIBUTEOPTIONCOMBO_DIM_ID, "AOC", ValueType.TEXT, false, true));
aocGrid.addHeader(new GridHeader("value", "Value", ValueType.NUMBER, false, true));
aocGrid.addRow().addValue(periodA.getIsoDate()).addValue(programIndicatorA.getUid()).addValue(orgUnitA.getUid()).addValue(aocC.getUid()).addValue(10.0);
aocGrid.addRow().addValue(periodB.getIsoDate()).addValue(programIndicatorB.getUid()).addValue(orgUnitA.getUid()).addValue(aocC.getUid()).addValue(20.0);
aocGrid.addRow().addValue(periodB.getIsoDate()).addValue(trackedEntityAttributeA.getUid()).addValue(orgUnitA.getUid()).addValue(aocD.getUid()).addValue(30.0);
aocGrid.addRow().addValue(periodA.getIsoDate()).addValue(programIndicatorA.getUid()).addValue(orgUnitB.getUid()).addValue(aocC.getUid()).addValue(40.0);
when(analyticsService.getAggregatedDataValues(aocParams)).thenReturn(aocGrid);
FoundDimensionItemValue expected1;
FoundDimensionItemValue expected2;
FoundDimensionItemValue expected3;
FoundDimensionItemValue expected4;
expected1 = new FoundDimensionItemValue(orgUnitA, periodA, aocC, programIndicatorA, 10.0);
expected2 = new FoundDimensionItemValue(orgUnitA, periodB, aocC, programIndicatorB, 20.0);
expected3 = new FoundDimensionItemValue(orgUnitA, periodB, aocD, trackedEntityAttributeA, 30.0);
expected4 = new FoundDimensionItemValue(orgUnitB, periodA, aocC, programIndicatorA, 40.0);
// ---------------------------------------------------------------------
// Items without Attribute Option Combos
// ---------------------------------------------------------------------
ProgramIndicator programIndicatorC = createProgramIndicator('C', programA, "expressionC", "filterC");
ProgramIndicator programIndicatorD = createProgramIndicator('D', programB, "expressionD", "filterD");
programIndicatorC.setAnalyticsType(ENROLLMENT);
programIndicatorD.setAnalyticsType(ENROLLMENT);
Set<DimensionalItemObject> nonAocItems = Sets.newHashSet(programIndicatorC, programIndicatorD);
DataQueryParams nonAocParams = DataQueryParams.newBuilder().withPeriods(Lists.newArrayList(periods)).withDataDimensionItems(Lists.newArrayList(nonAocItems)).withOrganisationUnits(orgUnits).build();
Grid nonAocGrid = new ListGrid();
nonAocGrid.addHeader(new GridHeader(PERIOD_DIM_ID, "Period", ValueType.TEXT, false, true));
nonAocGrid.addHeader(new GridHeader(DATA_X_DIM_ID, "DimensionItem", ValueType.TEXT, false, true));
nonAocGrid.addHeader(new GridHeader(ORGUNIT_DIM_ID, "OrganisationUnit", ValueType.TEXT, false, true));
nonAocGrid.addHeader(new GridHeader("value", "Value", ValueType.NUMBER, false, true));
nonAocGrid.addRow().addValue(periodA.getIsoDate()).addValue(programIndicatorC.getUid()).addValue(orgUnitA.getUid()).addValue(100.0);
nonAocGrid.addRow().addValue(periodA.getIsoDate()).addValue(programIndicatorD.getUid()).addValue(orgUnitB.getUid()).addValue(200.0);
nonAocGrid.addRow().addValue(periodB.getIsoDate()).addValue(programIndicatorC.getUid()).addValue(orgUnitA.getUid()).addValue(300.0);
when(analyticsService.getAggregatedDataValues(nonAocParams)).thenReturn(nonAocGrid);
FoundDimensionItemValue expected5;
FoundDimensionItemValue expected6;
FoundDimensionItemValue expected7;
CategoryOptionCombo noAoc = null;
expected5 = new FoundDimensionItemValue(orgUnitA, periodA, noAoc, programIndicatorC, 100.0);
expected6 = new FoundDimensionItemValue(orgUnitB, periodA, noAoc, programIndicatorD, 200.0);
expected7 = new FoundDimensionItemValue(orgUnitA, periodB, noAoc, programIndicatorC, 300.0);
// ---------------------------------------------------------------------
// Do the test
// ---------------------------------------------------------------------
Set<DimensionalItemObject> items = Sets.union(aocItems, nonAocItems);
fetcher.init(periods, items);
List<FoundDimensionItemValue> actual = fetcher.getValues(orgUnits);
assertContainsOnly(actual, expected1, expected2, expected3, expected4, expected5, expected6, expected7);
}
use of org.hisp.dhis.common.DimensionalItemObject in project dhis2-core by dhis2.
the class DataValidationTaskTest method verifySimpleValidation_oneRule_noErrors.
/**
* Verify that a single rule passes against a Data Element
*/
@Test
void verifySimpleValidation_oneRule_noErrors() {
Expression leftExpression = createExpression2('A', "#{FUrCpcvMAmC.OrDRjJL9bTS}");
Expression rightExpression = createExpression2('B', "-10");
ValidationRuleExtended vre = createValidationRuleExtended(leftExpression, rightExpression, Operator.not_equal_to);
List<PeriodTypeExtended> periodTypes = new ArrayList<>();
PeriodTypeExtended periodType = createPeriodTypeExtended(vre);
periodType.addDataElement(deA);
periodTypes.add(periodType);
CategoryOptionCombo categoryOptionCombo = createCategoryOptionCombo('A', 'B');
ValidationRunContext ctx = ValidationRunContext.newBuilder().withOrgUnits(organisationUnits).withItemMap(new HashMap<>()).withOrgUnitGroupMap(new HashMap<>()).withDefaultAttributeCombo(categoryOptionCombo).withPeriodTypeXs(periodTypes).withMaxResults(500).build();
List<DeflatedDataValue> deflatedDataValues = new ArrayList<>();
DataValue dv = createDataValue(deA, createPeriod("201901"), ouA, "12.4", createCategoryOptionCombo('B', 'C'));
DeflatedDataValue ddv = new DeflatedDataValue(dv);
deflatedDataValues.add(ddv);
when(dataValueService.getDeflatedDataValues(any(DataExportParams.class))).thenReturn(deflatedDataValues);
Map<DimensionalItemObject, Object> vals = new HashMap<>();
vals.put(deA, 12.4);
mockExpressionService(leftExpression, vals, 8.4);
mockExpressionService(rightExpression, vals, -10.0);
when(expressionService.getExpressionValue(ExpressionParams.builder().expression("8.4!=-10.0").parseType(SIMPLE_TEST).build())).thenReturn(true);
subject.init(organisationUnits, ctx, analyticsService);
subject.run();
assertThat(ctx.getValidationResults().size(), is(0));
}
use of org.hisp.dhis.common.DimensionalItemObject in project dhis2-core by dhis2.
the class DefaultExpressionService method getDimensionalItemObjectsInIndicators.
@Override
public Set<DimensionalItemObject> getDimensionalItemObjectsInIndicators(Collection<Indicator> indicators) {
Set<DimensionalItemObject> items = Sets.newHashSet();
for (Indicator indicator : indicators) {
items.addAll(getDimensionalItemObjectsInExpression(indicator.getNumerator()));
items.addAll(getDimensionalItemObjectsInExpression(indicator.getDenominator()));
}
return items;
}
use of org.hisp.dhis.common.DimensionalItemObject in project dhis2-core by dhis2.
the class HibernateDataValueStore method getDataElementOperandValues.
@Override
public MapMapMap<Period, String, DimensionalItemObject, Double> getDataElementOperandValues(Collection<DataElementOperand> dataElementOperands, Collection<Period> periods, OrganisationUnit orgUnit) {
MapMapMap<Period, String, DimensionalItemObject, Double> result = new MapMapMap<>();
Collection<Integer> periodIdList = IdentifiableObjectUtils.getIdentifiers(periods);
SetMap<DataElement, DataElementOperand> deosByDataElement = getDeosByDataElement(dataElementOperands);
if (periods.size() == 0 || dataElementOperands.size() == 0) {
return result;
}
String sql = "select dv.dataelementid, coc.uid, dv.attributeoptioncomboid, dv.periodid, " + "sum( cast( dv.value as " + statementBuilder.getDoubleColumnType() + " ) ) as value " + "from datavalue dv " + "join organisationunit o on o.organisationunitid = dv.sourceid " + "join categoryoptioncombo coc on coc.categoryoptioncomboid = dv.categoryoptioncomboid " + "where o.path like '" + orgUnit.getPath() + "%' " + "and dv.periodid in (" + TextUtils.getCommaDelimitedString(periodIdList) + ") " + "and dv.value is not null " + "and dv.deleted is false " + "and ( ";
String snippit = "";
for (DataElement dataElement : deosByDataElement.keySet()) {
sql += snippit + "( dv.dataelementid = " + dataElement.getId() + getDisaggRestriction(deosByDataElement.get(dataElement)) + " ) ";
snippit = "or ";
}
sql += ") group by dv.dataelementid, coc.uid, dv.attributeoptioncomboid, dv.periodid";
SqlRowSet rowSet = jdbcTemplate.queryForRowSet(sql);
Map<Integer, DataElement> dataElementsById = IdentifiableObjectUtils.getIdentifierMap(deosByDataElement.keySet());
Map<Integer, Period> periodsById = IdentifiableObjectUtils.getIdentifierMap(periods);
while (rowSet.next()) {
Integer dataElementId = rowSet.getInt(1);
String categoryOptionComboUid = rowSet.getString(2);
Integer periodId = rowSet.getInt(4);
Double value = rowSet.getDouble(5);
DataElement dataElement = dataElementsById.get(dataElementId);
Period period = periodsById.get(periodId);
Set<DataElementOperand> deos = deosByDataElement.get(dataElement);
for (DataElementOperand deo : deos) {
if (deo.getCategoryOptionCombo() == null || deo.getCategoryOptionCombo().getUid() == categoryOptionComboUid) {
Double existingValue = result.getValue(period, categoryOptionComboUid, deo);
if (existingValue != null) {
value += existingValue;
}
result.putEntry(period, categoryOptionComboUid, deo, value);
}
}
}
return result;
}
use of org.hisp.dhis.common.DimensionalItemObject in project dhis2-core by dhis2.
the class HibernateDataValueStore method getDataValueMapByAttributeCombo.
@Override
public MapMap<String, DimensionalItemObject, Double> getDataValueMapByAttributeCombo(SetMap<String, DataElementOperand> dataElementOperandsToGet, Date date, OrganisationUnit source, Collection<PeriodType> periodTypes, DataElementCategoryOptionCombo attributeCombo, Set<CategoryOptionGroup> cogDimensionConstraints, Set<DataElementCategoryOption> coDimensionConstraints, MapMap<String, DataElementOperand, Date> lastUpdatedMap) {
MapMap<String, DimensionalItemObject, Double> map = new MapMap<>();
if (dataElementOperandsToGet.isEmpty() || periodTypes.isEmpty() || (cogDimensionConstraints != null && cogDimensionConstraints.isEmpty()) || (coDimensionConstraints != null && coDimensionConstraints.isEmpty())) {
return map;
}
String joinCo = coDimensionConstraints == null && cogDimensionConstraints == null ? StringUtils.EMPTY : "join categoryoptioncombos_categoryoptions c_c on dv.attributeoptioncomboid = c_c.categoryoptioncomboid ";
String joinCog = cogDimensionConstraints == null ? StringUtils.EMPTY : "join categoryoptiongroupmembers cogm on c_c.categoryoptionid = cogm.categoryoptionid ";
String whereCo = coDimensionConstraints == null ? StringUtils.EMPTY : "and c_c.categoryoptionid in (" + TextUtils.getCommaDelimitedString(getIdentifiers(coDimensionConstraints)) + ") ";
String whereCog = cogDimensionConstraints == null ? StringUtils.EMPTY : "and cogm.categoryoptiongroupid in (" + TextUtils.getCommaDelimitedString(getIdentifiers(cogDimensionConstraints)) + ") ";
String whereCombo = attributeCombo == null ? StringUtils.EMPTY : "and dv.attributeoptioncomboid = " + attributeCombo.getId() + " ";
String sql = "select de.uid, coc.uid, aoc.uid, dv.value, dv.lastupdated, p.startdate, p.enddate " + "from datavalue dv " + "inner join dataelement de on dv.dataelementid = de.dataelementid " + "inner join categoryoptioncombo coc on dv.categoryoptioncomboid = coc.categoryoptioncomboid " + "inner join categoryoptioncombo aoc on dv.attributeoptioncomboid = aoc.categoryoptioncomboid " + "inner join period p on p.periodid = dv.periodid " + joinCo + joinCog + "where de.uid in (" + TextUtils.getQuotedCommaDelimitedString(dataElementOperandsToGet.keySet()) + ") " + "and dv.sourceid = " + source.getId() + " " + "and p.startdate <= '" + DateUtils.getMediumDateString(date) + "' " + "and p.enddate >= '" + DateUtils.getMediumDateString(date) + "' " + "and p.periodtypeid in (" + TextUtils.getCommaDelimitedString(getIds(periodTypes)) + ") " + "and dv.deleted is false " + whereCo + whereCog + whereCombo;
SqlRowSet rowSet = jdbcTemplate.queryForRowSet(sql);
MapMap<String, DataElementOperand, Long> checkForDuplicates = new MapMap<>();
int rowCount = 0;
while (rowSet.next()) {
rowCount++;
String dataElement = rowSet.getString(1);
String categoryOptionCombo = rowSet.getString(2);
String attributeOptionCombo = rowSet.getString(3);
Double value = MathUtils.parseDouble(rowSet.getString(4));
Date lastUpdated = rowSet.getDate(5);
Date periodStartDate = rowSet.getDate(6);
Date periodEndDate = rowSet.getDate(7);
long periodInterval = periodEndDate.getTime() - periodStartDate.getTime();
if (value != null) {
Set<DataElementOperand> deos = dataElementOperandsToGet.get(dataElement);
for (DataElementOperand deo : deos) {
if (deo.getCategoryOptionCombo() == null || deo.getCategoryOptionCombo().getUid().equals(categoryOptionCombo)) {
Double existingValue = map.getValue(attributeOptionCombo, deo);
Long existingPeriodInterval = checkForDuplicates.getValue(attributeOptionCombo, deo);
if (existingPeriodInterval != null) {
if (existingPeriodInterval < periodInterval) {
// Do not overwrite the previous value if for a shorter interval
continue;
} else if (existingPeriodInterval > periodInterval) {
// Overwrite previous value if for a longer interval
existingValue = null;
if (lastUpdatedMap != null) {
lastUpdatedMap.putEntry(attributeOptionCombo, deo, lastUpdated);
}
}
}
if (existingValue != null) {
value += existingValue;
}
map.putEntry(attributeOptionCombo, deo, value);
if (lastUpdatedMap != null && lastUpdated != null) {
Date existingLastUpdated = lastUpdatedMap.getValue(attributeOptionCombo, deo);
if (existingLastUpdated == null || lastUpdated.after(existingLastUpdated)) {
lastUpdatedMap.putEntry(attributeOptionCombo, deo, lastUpdated);
}
}
checkForDuplicates.putEntry(attributeOptionCombo, deo, periodInterval);
}
}
}
}
log.trace("getDataValueMapByAttributeCombo: " + rowCount + " rows into " + map.size() + " map entries from \"" + sql + "\"");
return map;
}
Aggregations