From cde0a5321845bf0286bbf83e3777ef399b7f3e88 Mon Sep 17 00:00:00 2001 From: Nicholas Ingosi Magaja Date: Thu, 27 Jun 2019 14:49:33 +0300 Subject: [PATCH] MEPTS-179 Routine fixes, base cohort and gender filter removal (#108) * Fetch the entire list of the lactation map as oppossed to the last obs * Include the equal boundaries to the second criteria for the routine * Include equality bounds to the criteria 1 routine * Including the Observations between the dates in the criteria 3 of routine * Merged with the work done here Mepts 213 pregnant breast feeding queries * Exclude 1 day from te six months boundary for criteria 1 * Attach bounadries to the viral load comapred to repoerting period and lower limit * Adding 1 day to the lower boundaries of the viral load results and suppression * Remove the boundaries on the 15 months date in criteria 2 routine * Revert the removal of boundaries in the fifteen months criteria for routine * We don't exclude restart treatment anymore --- .../pvls/BreastfeedingDateCalculation.java | 19 +++++++++------- .../calculation/pvls/RoutineCalculation.java | 22 ++++++++++--------- .../library/cohorts/TxNewCohortQueries.java | 11 ++-------- .../library/queries/PregnantQueries.java | 8 +++---- .../library/queries/ViralLoadQueries.java | 4 ++-- .../reporting/utils/EptsCalculationUtils.java | 4 ++-- 6 files changed, 33 insertions(+), 35 deletions(-) diff --git a/api/src/main/java/org/openmrs/module/eptsreports/reporting/calculation/pvls/BreastfeedingDateCalculation.java b/api/src/main/java/org/openmrs/module/eptsreports/reporting/calculation/pvls/BreastfeedingDateCalculation.java index a5a749a25c..8b6b4ebfe5 100644 --- a/api/src/main/java/org/openmrs/module/eptsreports/reporting/calculation/pvls/BreastfeedingDateCalculation.java +++ b/api/src/main/java/org/openmrs/module/eptsreports/reporting/calculation/pvls/BreastfeedingDateCalculation.java @@ -66,7 +66,7 @@ public CalculationResultMap evaluate( cohort, Arrays.asList(location), Arrays.asList(yes), - TimeQualifier.LAST, + TimeQualifier.ANY, null, context); @@ -129,7 +129,8 @@ private Date getResultantDate( ListResult patientResult = (ListResult) patientStateMap.get(pId); - Obs lactattingObs = EptsCalculationUtils.resultForPatient(lactatingMap, pId); + ListResult lactatingResults = (ListResult) lactatingMap.get(pId); + List lactatingObs = EptsCalculationUtils.extractResultValues(lactatingResults); Obs criteriaHivObs = EptsCalculationUtils.resultForPatient(criteriaHivStartMap, pId); ListResult deliveryDateResult = (ListResult) deliveryDateMap.get(pId); List deliveryDateObsList = EptsCalculationUtils.extractResultValues(deliveryDateResult); @@ -138,7 +139,7 @@ private Date getResultantDate( // get a list of all eligible dates List allEligibleDates = Arrays.asList( - this.isLactating(lastVlDate, lactattingObs), + this.isLactating(lastVlDate, lactatingObs), this.hasHIVStartDate(lastVlDate, criteriaHivObs), this.hasDeliveryDate(lastVlDate, deliveryDateObsList), this.isInBreastFeedingInProgram(lastVlDate, patientStateList)); @@ -172,12 +173,14 @@ private Date hasDeliveryDate(Date lastVlDate, List deliveryDateObsList) { return deliveryDate; } - private Date isLactating(Date lastVlDate, Obs lactantObs) { + private Date isLactating(Date lastVlDate, List lactantObsList) { Date lactatingDate = null; - if (lactantObs != null - && this.isInBreastFeedingViralLoadRange( - lastVlDate, lactantObs.getEncounter().getEncounterDatetime())) { - lactatingDate = lactantObs.getEncounter().getEncounterDatetime(); + for (Obs lactantObs : lactantObsList) { + if (lactantObs.getObsDatetime() != null + && this.isInBreastFeedingViralLoadRange( + lastVlDate, lactantObs.getEncounter().getEncounterDatetime())) { + lactatingDate = lactantObs.getEncounter().getEncounterDatetime(); + } } return lactatingDate; } diff --git a/api/src/main/java/org/openmrs/module/eptsreports/reporting/calculation/pvls/RoutineCalculation.java b/api/src/main/java/org/openmrs/module/eptsreports/reporting/calculation/pvls/RoutineCalculation.java index ab4be49605..d361d17676 100644 --- a/api/src/main/java/org/openmrs/module/eptsreports/reporting/calculation/pvls/RoutineCalculation.java +++ b/api/src/main/java/org/openmrs/module/eptsreports/reporting/calculation/pvls/RoutineCalculation.java @@ -34,6 +34,8 @@ import org.openmrs.module.eptsreports.reporting.calculation.generic.InitialArtStartDateCalculation; import org.openmrs.module.eptsreports.reporting.utils.EptsCalculationUtils; import org.openmrs.module.eptsreports.reporting.utils.EptsReportConstants.PatientsOnRoutineEnum; +import org.openmrs.module.reporting.common.DateUtil; +import org.openmrs.module.reporting.common.DurationUnit; import org.openmrs.module.reporting.common.TimeQualifier; import org.springframework.stereotype.Component; @@ -164,8 +166,8 @@ private boolean isOnRoutine( && lastVlObs.getObsDatetime() != null && criteria != null && onArtForMoreThan3Months.contains(pId) - && lastVlObs.getObsDatetime().after(latestVlLowerDateLimit) - && lastVlObs.getObsDatetime().before(onOrBefore)) { + && lastVlObs.getObsDatetime().compareTo(latestVlLowerDateLimit) >= 0 + && lastVlObs.getObsDatetime().compareTo(onOrBefore) <= 0) { // get all the VL results for each patient from enrollment ListResult vlObsResultFromEndOfReportingPeriod = @@ -268,8 +270,8 @@ private boolean isOnRoutineCriteria2( && allVls.getValueNumeric() != null && allVls.getValueNumeric() < 1000) { if (criteria.equals(PatientsOnRoutineEnum.ADULTCHILDREN) - && allVls.getObsDatetime().compareTo(twelveMonths) < 0 - && allVls.getObsDatetime().compareTo(fifteenMonths) > 0) { + && allVls.getObsDatetime().compareTo(twelveMonths) <= 0 + && allVls.getObsDatetime().compareTo(fifteenMonths) >= 0) { return true; } else if (criteria.equals(PatientsOnRoutineEnum.BREASTFEEDINGPREGNANT) && allVls.getObsDatetime().before(lastViralLoadDate)) { @@ -292,15 +294,15 @@ private boolean isOnRoutineCriteria1( Date nineMonths = EptsCalculationUtils.addMonths(artInitiationDate, 9); boolean withinAdultLimits = - lastViralLoadDate.compareTo(sixMonths) > 0 + lastViralLoadDate.compareTo(sixMonths) >= 0 && lastViralLoadDate.compareTo(nineMonths) <= 0; boolean withinBreastfeedingLimits = - lastViralLoadDate.compareTo(threeMonths) > 0 + lastViralLoadDate.compareTo(threeMonths) >= 0 && lastViralLoadDate.compareTo(sixMonths) <= 0; - + Date sixMonthsLess1Day = DateUtil.adjustDate(sixMonths, -1, DurationUnit.DAYS); if (criteria.equals(PatientsOnRoutineEnum.ADULTCHILDREN) && withinAdultLimits) { return !EptsCalculationUtils.anyObsBetween( - allViralLoadForPatient, artInitiationDate, sixMonths); + allViralLoadForPatient, artInitiationDate, sixMonthsLess1Day); } else if (criteria.equals(PatientsOnRoutineEnum.BREASTFEEDINGPREGNANT) && withinBreastfeedingLimits) { return !EptsCalculationUtils.anyObsBetween( @@ -316,8 +318,8 @@ private List getViralLoadForPatientTakenWithin12Months( // populate viralLoadForPatientTakenWithin12Months with obs which fall within the 12month window for (Obs obs : vLoadList) { if (obs != null - && obs.getObsDatetime().after(latestVlLowerDateLimit) - && obs.getObsDatetime().before(onOrBefore)) { + && obs.getObsDatetime().compareTo(latestVlLowerDateLimit) >= 0 + && obs.getObsDatetime().compareTo(onOrBefore) <= 0) { viralLoadForPatientTakenWithin12Months.add(obs); } } diff --git a/api/src/main/java/org/openmrs/module/eptsreports/reporting/library/cohorts/TxNewCohortQueries.java b/api/src/main/java/org/openmrs/module/eptsreports/reporting/library/cohorts/TxNewCohortQueries.java index f58536850e..6bc3f09c01 100644 --- a/api/src/main/java/org/openmrs/module/eptsreports/reporting/library/cohorts/TxNewCohortQueries.java +++ b/api/src/main/java/org/openmrs/module/eptsreports/reporting/library/cohorts/TxNewCohortQueries.java @@ -157,7 +157,6 @@ public CohortDefinition getTxNewBreastfeedingComposition() { EptsReportUtils.map( getPatientsWhoGaveBirthWithinReportingPeriod(), "startDate=${onOrAfter},endDate=${onOrBefore},location=${location}")); - cd.addSearch("FEMININO", EptsReportUtils.map(genderCohorts.femaleCohort(), "")); cd.addSearch( "LACTANTE", EptsReportUtils.map( @@ -169,8 +168,7 @@ public CohortDefinition getTxNewBreastfeedingComposition() { Arrays.asList(commonMetadata.getYesConcept())), "onOrAfter=${onOrAfter},onOrBefore=${onOrBefore},locationList=${location}")); - String compositionString = - "(DATAPARTO OR INICIOLACTANTE OR LACTANTEPROGRAMA OR LACTANTE) AND FEMININO"; + String compositionString = "(DATAPARTO OR INICIOLACTANTE OR LACTANTEPROGRAMA OR LACTANTE)"; cd.setCompositionString(compositionString); return cd; @@ -197,16 +195,11 @@ public CohortDefinition getTxNewCompositionCohort(String cohortName) { Mapped.map( hivCohortQueries.getPatientsTransferredFromOtherHealthFacility(), "onOrAfter=${onOrAfter},onOrBefore=${onOrBefore},location=${location}"); - Mapped restartedTreatment = - Mapped.map( - hivCohortQueries.getPatientsWhoRestartedTreatment(), - "onOrAfter=${onOrAfter},onOrBefore=${onOrBefore},locationList=${location}"); txNewComposition.getSearches().put("startedART", startedART); txNewComposition.getSearches().put("transferredIn", transferredIn); - txNewComposition.getSearches().put("restartedTreatment", restartedTreatment); - txNewComposition.setCompositionString("startedART NOT (transferredIn OR restartedTreatment)"); + txNewComposition.setCompositionString("startedART NOT transferredIn"); return txNewComposition; } } diff --git a/api/src/main/java/org/openmrs/module/eptsreports/reporting/library/queries/PregnantQueries.java b/api/src/main/java/org/openmrs/module/eptsreports/reporting/library/queries/PregnantQueries.java index 9219bef358..dbd3b48894 100644 --- a/api/src/main/java/org/openmrs/module/eptsreports/reporting/library/queries/PregnantQueries.java +++ b/api/src/main/java/org/openmrs/module/eptsreports/reporting/library/queries/PregnantQueries.java @@ -37,7 +37,7 @@ public static String getPregnantWhileOnArt( + adultInitailEncounter + "," + adultSegEncounter - + ") and e.encounter_datetime between :startDate and :endDate and e.location_id=:location and pe.gender='F'" + + ") and e.encounter_datetime between :startDate and :endDate and e.location_id=:location " + " union" + " Select p.patient_id" + " from patient p" @@ -51,7 +51,7 @@ public static String getPregnantWhileOnArt( + adultInitailEncounter + "," + adultSegEncounter - + ") and e.encounter_datetime between :startDate and :endDate and e.location_id=:location and pe.gender='F' " + + ") and e.encounter_datetime between :startDate and :endDate and e.location_id=:location " + " union" + " Select p.patient_id" + " from patient p" @@ -65,12 +65,12 @@ public static String getPregnantWhileOnArt( + adultInitailEncounter + "," + adultSegEncounter - + ") and e.encounter_datetime between :startDate and :endDate and e.location_id=:location and pe.gender='F'" + + ") and e.encounter_datetime between :startDate and :endDate and e.location_id=:location " + " union" + " select pp.patient_id from patient_program pp" + " inner join person pe on pp.patient_id=pe.person_id" + " where pp.program_id=" + etvProgram - + " and pp.voided=0 and pp.date_enrolled between :startDate and :endDate and pp.location_id=:location and pe.gender='F'"; + + " and pp.voided=0 and pp.date_enrolled between :startDate and :endDate and pp.location_id=:location "; } } diff --git a/api/src/main/java/org/openmrs/module/eptsreports/reporting/library/queries/ViralLoadQueries.java b/api/src/main/java/org/openmrs/module/eptsreports/reporting/library/queries/ViralLoadQueries.java index fe466625b6..c3e4551a46 100644 --- a/api/src/main/java/org/openmrs/module/eptsreports/reporting/library/queries/ViralLoadQueries.java +++ b/api/src/main/java/org/openmrs/module/eptsreports/reporting/library/queries/ViralLoadQueries.java @@ -38,7 +38,7 @@ public static String getPatientsWithViralLoadSuppression( + " AND o.concept_id=" + vlConceptQuestion + " AND o.value_numeric IS NOT NULL AND" - + " e.encounter_datetime BETWEEN date_add(:endDate, interval -12 MONTH) and :endDate AND" + + " e.encounter_datetime BETWEEN date_add(date_add(:endDate, interval -12 MONTH), interval 1 day) AND :endDate AND" + " e.location_id=:location GROUP BY p.patient_id" + ") ultima_carga" + " INNER JOIN obs ON obs.person_id=ultima_carga.patient_id AND obs.obs_datetime=" @@ -69,7 +69,7 @@ public static String getPatientsHavingViralLoadInLast12Months( + ") AND o.concept_id=" + vlConceptQuestion + " AND o.value_numeric IS NOT NULL AND" - + " e.encounter_datetime BETWEEN date_add(:endDate, interval -12 MONTH) AND :endDate AND" + + " e.encounter_datetime BETWEEN date_add(date_add(:endDate, interval -12 MONTH), interval 1 day) AND :endDate AND" + " e.location_id=:location"; } } diff --git a/api/src/main/java/org/openmrs/module/eptsreports/reporting/utils/EptsCalculationUtils.java b/api/src/main/java/org/openmrs/module/eptsreports/reporting/utils/EptsCalculationUtils.java index 9e019c1d57..e8c32abd9e 100644 --- a/api/src/main/java/org/openmrs/module/eptsreports/reporting/utils/EptsCalculationUtils.java +++ b/api/src/main/java/org/openmrs/module/eptsreports/reporting/utils/EptsCalculationUtils.java @@ -385,8 +385,8 @@ public static boolean dateBetween(Date startDate, Date endDate, Date dateInQuest public static boolean anyObsBetween(List vlObsList, Date startDate, Date endDate) { for (Obs obs : vlObsList) { if (obs.getObsDatetime() != null - && obs.getObsDatetime().after(startDate) - && obs.getObsDatetime().before(endDate)) { + && obs.getObsDatetime().compareTo(startDate) >= 0 + && obs.getObsDatetime().compareTo(endDate) <= 0) { return true; } }