Search in sources :

Example 1 with AnalyticsServiceTarget

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);
}
Also used : MonthlyPeriodType(org.hisp.dhis.period.MonthlyPeriodType) PeriodType(org.hisp.dhis.period.PeriodType) Program(org.hisp.dhis.program.Program) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) HashMap(java.util.HashMap) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ListGrid(org.hisp.dhis.system.grid.ListGrid) Grid(org.hisp.dhis.common.Grid) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Date(java.util.Date) DataElement(org.hisp.dhis.dataelement.DataElement) MockAnalyticsService(org.hisp.dhis.mock.MockAnalyticsService) MonthlyPeriodType(org.hisp.dhis.period.MonthlyPeriodType) Expression(org.hisp.dhis.expression.Expression) ProgramStage(org.hisp.dhis.program.ProgramStage) ProgramIndicator(org.hisp.dhis.program.ProgramIndicator) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) AnalyticsServiceTarget(org.hisp.dhis.analytics.AnalyticsServiceTarget)

Example 2 with AnalyticsServiceTarget

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"));
}
Also used : MonthlyPeriodType(org.hisp.dhis.period.MonthlyPeriodType) PeriodType(org.hisp.dhis.period.PeriodType) HashMap(java.util.HashMap) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ListGrid(org.hisp.dhis.system.grid.ListGrid) Grid(org.hisp.dhis.common.Grid) CurrentUserServiceTarget(org.hisp.dhis.user.CurrentUserServiceTarget) DataElement(org.hisp.dhis.dataelement.DataElement) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) AnalyticsServiceTarget(org.hisp.dhis.analytics.AnalyticsServiceTarget) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) MockCurrentUserService(org.hisp.dhis.mock.MockCurrentUserService) CurrentUserService(org.hisp.dhis.user.CurrentUserService) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Date(java.util.Date) MockAnalyticsService(org.hisp.dhis.mock.MockAnalyticsService) MonthlyPeriodType(org.hisp.dhis.period.MonthlyPeriodType) Expression(org.hisp.dhis.expression.Expression) MockCurrentUserService(org.hisp.dhis.mock.MockCurrentUserService) OrganisationUnitLevel(org.hisp.dhis.organisationunit.OrganisationUnitLevel) ProgramStage(org.hisp.dhis.program.ProgramStage) ProgramIndicator(org.hisp.dhis.program.ProgramIndicator)

Aggregations

Date (java.util.Date)2 HashMap (java.util.HashMap)2 AnalyticsServiceTarget (org.hisp.dhis.analytics.AnalyticsServiceTarget)2 Grid (org.hisp.dhis.common.Grid)2 DataElement (org.hisp.dhis.dataelement.DataElement)2 Expression (org.hisp.dhis.expression.Expression)2 MockAnalyticsService (org.hisp.dhis.mock.MockAnalyticsService)2 MonthlyPeriodType (org.hisp.dhis.period.MonthlyPeriodType)2 PeriodType (org.hisp.dhis.period.PeriodType)2 Program (org.hisp.dhis.program.Program)2 ProgramIndicator (org.hisp.dhis.program.ProgramIndicator)2 ProgramInstance (org.hisp.dhis.program.ProgramInstance)2 ProgramStage (org.hisp.dhis.program.ProgramStage)2 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)2 ListGrid (org.hisp.dhis.system.grid.ListGrid)2 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)2 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)2 TrackedEntityAttributeValue (org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue)2 MockCurrentUserService (org.hisp.dhis.mock.MockCurrentUserService)1 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)1