use of org.hisp.dhis.analytics.AnalyticsServiceTarget in project dhis2-core by dhis2.
the class AnalyticsValidationServiceTest method setUpTest.
@Override
public void setUpTest() {
final String DATA_ELEMENT_A_UID = "DataElement";
final String TRACKED_ENTITY_ATTRIBUTE_UID = "TEAttribute";
final String PROGRAM_UID = "ProgramABCD";
final String PROGRAM_INDICATOR_UID = "ProgramIndA";
// A - ProgramTrackedEntityAttribute
final String EXPRESSION_A = "A{" + PROGRAM_UID + SEPARATOR + TRACKED_ENTITY_ATTRIBUTE_UID + "}";
// D - ProgramDataElement
final String EXPRESSION_D = "D{" + PROGRAM_UID + SEPARATOR + DATA_ELEMENT_A_UID + "}";
// I - ProgramIndicator
final String EXPRESSION_I = "I{" + PROGRAM_INDICATOR_UID + "}";
// Program Indicator expression
final String EX_INDICATOR = "#{" + PROGRAM_UID + SEPARATOR + DATA_ELEMENT_A_UID + "} + 4";
final String EXPRESSION_AI = EXPRESSION_A + " + " + EXPRESSION_I;
final String EXPRESSION_DI = EXPRESSION_D + " + " + EXPRESSION_I;
defaultCombo = categoryService.getDefaultCategoryOptionCombo();
orgUnitA = createOrganisationUnit('A');
organisationUnitService.addOrganisationUnit(orgUnitA);
PeriodType periodTypeMonthly = new MonthlyPeriodType();
testYear = Calendar.getInstance().get(Calendar.YEAR) - 1;
periodMar = createPeriod(periodTypeMonthly, getDate(testYear, 3, 1), getDate(testYear, 3, 31));
periodApr = createPeriod(periodTypeMonthly, getDate(testYear, 4, 1), getDate(testYear, 4, 30));
periodService.addPeriod(periodMar);
periodService.addPeriod(periodApr);
dayInPeriod = 15;
Date dateMar20 = getDate(testYear, 3, 20);
Date dateApr10 = getDate(testYear, 4, 10);
DataElement dataElementA = createDataElement('A', ValueType.INTEGER, AggregationType.SUM, DataElementDomain.TRACKER);
dataElementA.setUid(DATA_ELEMENT_A_UID);
dataElementService.addDataElement(dataElementA);
TrackedEntityAttribute entityAttribute = createTrackedEntityAttribute('A');
entityAttribute.setAggregationType(AggregationType.COUNT);
entityAttribute.setUid(TRACKED_ENTITY_ATTRIBUTE_UID);
entityAttributeService.addTrackedEntityAttribute(entityAttribute);
TrackedEntityInstance entityInstance = createTrackedEntityInstance('A', orgUnitA, entityAttribute);
entityInstanceService.addTrackedEntityInstance(entityInstance);
TrackedEntityAttributeValue trackedEntityAttributeValue = new TrackedEntityAttributeValue(entityAttribute, entityInstance);
trackedEntityAttributeValue.setValue("123");
entityAttributeValueService.addTrackedEntityAttributeValue(trackedEntityAttributeValue);
entityInstance.setTrackedEntityAttributeValues(Sets.newHashSet(trackedEntityAttributeValue));
entityInstanceService.updateTrackedEntityInstance(entityInstance);
Program program = createProgram('A', null, Sets.newHashSet(entityAttribute), Sets.newHashSet(orgUnitA, orgUnitA), null);
program.setUid(PROGRAM_UID);
programService.addProgram(program);
ProgramStage stageA = createProgramStage('A', 0);
stageA.setProgram(program);
stageA.addDataElement(dataElementA, 1);
programStageService.saveProgramStage(stageA);
ProgramIndicator programIndicator = createProgramIndicator('A', program, EX_INDICATOR, null);
programIndicator.setAggregationType(AggregationType.SUM);
programIndicator.setUid(PROGRAM_INDICATOR_UID);
programIndicatorService.addProgramIndicator(programIndicator);
program.setProgramStages(Sets.newHashSet(stageA));
program.getProgramIndicators().add(programIndicator);
programService.updateProgram(program);
ProgramInstance programInstance = programInstanceService.enrollTrackedEntityInstance(entityInstance, program, dateMar20, dateMar20, orgUnitA);
programInstanceService.addProgramInstance(programInstance);
ProgramStageInstance stageInstanceA = programStageInstanceService.createProgramStageInstance(programInstance, stageA, dateMar20, dateMar20, orgUnitA);
ProgramStageInstance stageInstanceB = programStageInstanceService.createProgramStageInstance(programInstance, stageA, dateApr10, dateApr10, orgUnitA);
stageInstanceA.setExecutionDate(dateMar20);
stageInstanceB.setExecutionDate(dateApr10);
stageInstanceA.setAttributeOptionCombo(defaultCombo);
stageInstanceB.setAttributeOptionCombo(defaultCombo);
programStageInstanceService.addProgramStageInstance(stageInstanceA);
programStageInstanceService.addProgramStageInstance(stageInstanceB);
categoryManager.addAndPruneAllOptionCombos();
Expression expressionA = new Expression(EXPRESSION_A, "ProgramTrackedEntityAttribute");
Expression expressionD = new Expression(EXPRESSION_D, "ProgramDataElement");
Expression expressionI = new Expression(EXPRESSION_I, "ProgramIndicator");
Expression expressionAI = new Expression(EXPRESSION_AI, "ProgramTrackedEntityAttribute + ProgramIndicator");
Expression expressionASlide = new Expression(EXPRESSION_A, "ProgramTrackedEntityAttribute Slide");
Expression expressionDSlide = new Expression(EXPRESSION_D, "ProgramDataElement Slide");
Expression expressionISlide = new Expression(EXPRESSION_I, "ProgramIndicator Slide");
Expression expressionDISlide = new Expression(EXPRESSION_DI, "ProgramDataElement + ProgramIndicator Slide");
expressionASlide.setSlidingWindow(true);
expressionDSlide.setSlidingWindow(true);
expressionISlide.setSlidingWindow(true);
ruleA = createValidationRule("A", not_equal_to, expressionA, expressionA, periodTypeMonthly);
ruleD = createValidationRule("D", not_equal_to, expressionD, expressionD, periodTypeMonthly);
ruleI = createValidationRule("I", not_equal_to, expressionI, expressionI, periodTypeMonthly);
ruleASlide = createValidationRule("T", not_equal_to, expressionASlide, expressionASlide, periodTypeMonthly);
ruleDSlide = createValidationRule("U", not_equal_to, expressionDSlide, expressionDSlide, periodTypeMonthly);
ruleISlide = createValidationRule("V", not_equal_to, expressionISlide, expressionISlide, periodTypeMonthly);
ruleX = createValidationRule("X", equal_to, expressionAI, expressionDISlide, periodTypeMonthly);
validationRuleService.saveValidationRule(ruleA);
validationRuleService.saveValidationRule(ruleD);
validationRuleService.saveValidationRule(ruleI);
validationRuleService.saveValidationRule(ruleASlide);
validationRuleService.saveValidationRule(ruleDSlide);
validationRuleService.saveValidationRule(ruleISlide);
validationRuleService.saveValidationRule(ruleX);
Map<Date, Grid> dateGridMap = new HashMap<>();
dateGridMap.put(periodMar.getStartDate(), newGrid(4, 1, 8, 3));
dateGridMap.put(periodApr.getStartDate(), newGrid(5, 1, 9, 2));
MockAnalyticsService mockAnalyticsSerivce = new MockAnalyticsService();
mockAnalyticsSerivce.setDateGridMap(dateGridMap);
setDependency(AnalyticsServiceTarget.class, AnalyticsServiceTarget::setAnalyticsService, mockAnalyticsSerivce, validationService);
}
use of org.hisp.dhis.analytics.AnalyticsServiceTarget in project dhis2-core by dhis2.
the class EventPredictionServiceTest method setUpTest.
@Override
public void setUpTest() {
final String DATA_ELEMENT_A_UID = "DataElemenA";
final String DATA_ELEMENT_D_UID = "DataElemenD";
final String DATA_ELEMENT_I_UID = "DataElemenI";
final String DATA_ELEMENT_E_UID = "DataElemenE";
final String DATA_ELEMENT_T_UID = "DataElemenT";
final String DATA_ELEMENT_X_UID = "DataElemenX";
final String TRACKED_ENTITY_ATTRIBUTE_UID = "TEAttribute";
final String PROGRAM_UID = "ProgramUidA";
final String PROGRAM_INDICATOR_A_UID = "ProgramIndA";
final String PROGRAM_INDICATOR_B_UID = "ProgramIndB";
final String PROGRAM_TRACKED_ENTITY_ATTRIBUTE_DIMENSION_ITEM = PROGRAM_UID + SEPARATOR + TRACKED_ENTITY_ATTRIBUTE_UID;
final String PROGRAM_DATA_ELEMENT_DIMENSION_ITEM = PROGRAM_UID + SEPARATOR + DATA_ELEMENT_X_UID;
// A
final String EXPRESSION_A = "sum( A{" + PROGRAM_TRACKED_ENTITY_ATTRIBUTE_DIMENSION_ITEM + "} )";
// -
// ProgramTrackedEntityAttribute
// D
final String EXPRESSION_D = "sum( D{" + PROGRAM_DATA_ELEMENT_DIMENSION_ITEM + "} )";
// -
// ProgramDataElement
// I
final String EXPRESSION_I = "sum( I{" + PROGRAM_INDICATOR_A_UID + "} + I{" + PROGRAM_INDICATOR_B_UID + "} )";
// -
// ProgramIndicators
// E
final String EXPRESSION_E = "sum( #{" + DATA_ELEMENT_E_UID + "} )";
// -
// Data
// element
// T -
final String EXPRESSION_T = EXPRESSION_A + " + " + EXPRESSION_E;
// Two
// things,
// event
// and
// data
// elemeent
// Program
final String EX_INDICATOR_A = "#{" + PROGRAM_DATA_ELEMENT_DIMENSION_ITEM + "} + 4";
// Indicator
// A
// expression
// Program
final String EX_INDICATOR_B = "V{enrollment_count}";
// Indicator B
// expression
defaultCombo = categoryService.getDefaultCategoryOptionCombo();
orgUnitA = createOrganisationUnit('A');
organisationUnitService.addOrganisationUnit(orgUnitA);
Set<OrganisationUnit> orgUnitASet = Sets.newHashSet(orgUnitA);
PeriodType periodTypeMonthly = new MonthlyPeriodType();
testYear = Calendar.getInstance().get(Calendar.YEAR) - 1;
periodMar = createPeriod(periodTypeMonthly, getDate(testYear, 3, 1), getDate(testYear, 3, 31));
periodApr = createPeriod(periodTypeMonthly, getDate(testYear, 4, 1), getDate(testYear, 4, 30));
periodMay = createPeriod(periodTypeMonthly, getDate(testYear, 5, 1), getDate(testYear, 5, 31));
periodService.addPeriod(periodMar);
periodService.addPeriod(periodApr);
periodService.addPeriod(periodMay);
Date dateMar20 = getDate(testYear, 3, 20);
Date dateApr10 = getDate(testYear, 4, 10);
predictorOutputA = createDataElement('A');
predictorOutputD = createDataElement('D');
predictorOutputI = createDataElement('I');
predictorOutputT = createDataElement('T');
DataElement dataElementE = createDataElement('E');
DataElement dataElementX = createDataElement('P', ValueType.INTEGER, AggregationType.SUM, DataElementDomain.TRACKER);
predictorOutputA.setUid(DATA_ELEMENT_A_UID);
predictorOutputD.setUid(DATA_ELEMENT_D_UID);
predictorOutputI.setUid(DATA_ELEMENT_I_UID);
predictorOutputT.setUid(DATA_ELEMENT_T_UID);
dataElementE.setUid(DATA_ELEMENT_E_UID);
dataElementX.setUid(DATA_ELEMENT_X_UID);
dataElementService.addDataElement(predictorOutputA);
dataElementService.addDataElement(predictorOutputD);
dataElementService.addDataElement(predictorOutputI);
dataElementService.addDataElement(predictorOutputT);
dataElementService.addDataElement(dataElementE);
dataElementService.addDataElement(dataElementX);
TrackedEntityAttribute entityAttribute = createTrackedEntityAttribute('A');
entityAttribute.setAggregationType(AggregationType.COUNT);
entityAttribute.setUid(TRACKED_ENTITY_ATTRIBUTE_UID);
entityAttributeService.addTrackedEntityAttribute(entityAttribute);
TrackedEntityInstance entityInstance = createTrackedEntityInstance('A', orgUnitA, entityAttribute);
entityInstanceService.addTrackedEntityInstance(entityInstance);
TrackedEntityAttributeValue trackedEntityAttributeValue = new TrackedEntityAttributeValue(entityAttribute, entityInstance);
trackedEntityAttributeValue.setValue("123");
entityAttributeValueService.addTrackedEntityAttributeValue(trackedEntityAttributeValue);
entityInstance.setTrackedEntityAttributeValues(Sets.newHashSet(trackedEntityAttributeValue));
entityInstanceService.updateTrackedEntityInstance(entityInstance);
Program program = createProgram('A', null, Sets.newHashSet(entityAttribute), orgUnitASet, null);
program.setUid(PROGRAM_UID);
programService.addProgram(program);
ProgramStage stageA = createProgramStage('A', 0);
stageA.setProgram(program);
stageA.addDataElement(dataElementX, 1);
programStageService.saveProgramStage(stageA);
ProgramIndicator programIndicatorA = createProgramIndicator('A', program, EX_INDICATOR_A, null);
programIndicatorA.setAggregationType(AggregationType.SUM);
programIndicatorA.setUid(PROGRAM_INDICATOR_A_UID);
programIndicatorService.addProgramIndicator(programIndicatorA);
ProgramIndicator programIndicatorB = createProgramIndicator('B', program, EX_INDICATOR_B, null);
programIndicatorB.setAnalyticsType(AnalyticsType.ENROLLMENT);
programIndicatorB.setAggregationType(AggregationType.COUNT);
programIndicatorB.setUid(PROGRAM_INDICATOR_B_UID);
programIndicatorService.addProgramIndicator(programIndicatorB);
program.setProgramStages(Sets.newHashSet(stageA));
program.getProgramIndicators().add(programIndicatorA);
program.getProgramIndicators().add(programIndicatorB);
programService.updateProgram(program);
ProgramInstance programInstance = programInstanceService.enrollTrackedEntityInstance(entityInstance, program, dateMar20, dateMar20, orgUnitA);
programInstanceService.addProgramInstance(programInstance);
ProgramStageInstance stageInstanceA = programStageInstanceService.createProgramStageInstance(programInstance, stageA, dateMar20, dateMar20, orgUnitA);
ProgramStageInstance stageInstanceB = programStageInstanceService.createProgramStageInstance(programInstance, stageA, dateApr10, dateApr10, orgUnitA);
stageInstanceA.setExecutionDate(dateMar20);
stageInstanceB.setExecutionDate(dateApr10);
stageInstanceA.setAttributeOptionCombo(defaultCombo);
stageInstanceB.setAttributeOptionCombo(defaultCombo);
programStageInstanceService.addProgramStageInstance(stageInstanceA);
programStageInstanceService.addProgramStageInstance(stageInstanceB);
categoryManager.addAndPruneAllOptionCombos();
Expression expressionA = new Expression(EXPRESSION_A, "ProgramTrackedEntityAttribute");
Expression expressionD = new Expression(EXPRESSION_D, "ProgramDataElement");
Expression expressionI = new Expression(EXPRESSION_I, "ProgramIndicators");
Expression expressionT = new Expression(EXPRESSION_T, "Two things");
OrganisationUnitLevel orgUnitLevel1 = new OrganisationUnitLevel(1, "Level1");
organisationUnitService.addOrganisationUnitLevel(orgUnitLevel1);
predictorA = createPredictor(predictorOutputA, defaultCombo, "A", expressionA, null, periodTypeMonthly, orgUnitLevel1, 2, 0, 0);
predictorD = createPredictor(predictorOutputD, defaultCombo, "D", expressionD, null, periodTypeMonthly, orgUnitLevel1, 2, 0, 0);
predictorI = createPredictor(predictorOutputI, defaultCombo, "I", expressionI, null, periodTypeMonthly, orgUnitLevel1, 2, 0, 0);
predictorT = createPredictor(predictorOutputT, defaultCombo, "T", expressionT, null, periodTypeMonthly, orgUnitLevel1, 2, 0, 0);
predictorService.addPredictor(predictorA);
predictorService.addPredictor(predictorD);
predictorService.addPredictor(predictorI);
Map<String, Grid> itemGridMap = new HashMap<>();
itemGridMap.put(PROGRAM_TRACKED_ENTITY_ATTRIBUTE_DIMENSION_ITEM, newGrid(PROGRAM_TRACKED_ENTITY_ATTRIBUTE_DIMENSION_ITEM, 1.0, 1.0));
itemGridMap.put(PROGRAM_DATA_ELEMENT_DIMENSION_ITEM, newGrid(PROGRAM_DATA_ELEMENT_DIMENSION_ITEM, 4.0, 5.0));
itemGridMap.put(PROGRAM_INDICATOR_A_UID, newGrid(PROGRAM_INDICATOR_A_UID, 8.0, 9.0));
itemGridMap.put(PROGRAM_INDICATOR_B_UID, newGrid(PROGRAM_INDICATOR_B_UID, 10.0, 11.0));
MockAnalyticsService mockAnalyticsSerivce = new MockAnalyticsService();
mockAnalyticsSerivce.setItemGridMap(itemGridMap);
setDependency(AnalyticsServiceTarget.class, AnalyticsServiceTarget::setAnalyticsService, mockAnalyticsSerivce, predictionService);
CurrentUserService mockCurrentUserService = new MockCurrentUserService(true, orgUnitASet, orgUnitASet);
setDependency(CurrentUserServiceTarget.class, CurrentUserServiceTarget::setCurrentUserService, mockCurrentUserService, predictionService);
dataValueService.addDataValue(createDataValue(dataElementE, periodMar, orgUnitA, defaultCombo, defaultCombo, "100"));
dataValueService.addDataValue(createDataValue(dataElementE, periodApr, orgUnitA, defaultCombo, defaultCombo, "200"));
dataValueService.addDataValue(createDataValue(dataElementE, periodMay, orgUnitA, defaultCombo, defaultCombo, "300"));
}
Aggregations