From 4b90614d5ecd44a93924f86f6d4203d1ab187346 Mon Sep 17 00:00:00 2001 From: njorocs Date: Mon, 22 Jul 2024 14:55:00 +0300 Subject: [PATCH] KHP3-6210:Refactor data fetch for next appointment date column. Changed from using calculation to using etl --- .../PatientAppointmentOnDayReportBuilder.java | 15 +++-- ...AppointmentBetweenDatesDataDefinition.java | 47 ++++++++++++++++ ...tAppointmentBetweenDatesDataEvaluator.java | 56 +++++++++++++++++++ 3 files changed, 110 insertions(+), 8 deletions(-) create mode 100644 api/src/main/java/org/openmrs/module/kenyaemr/reporting/data/converter/definition/art/ETLNextAppointmentBetweenDatesDataDefinition.java create mode 100644 api/src/main/java/org/openmrs/module/kenyaemr/reporting/data/converter/definition/evaluator/art/ETLNextAppointmentBetweenDatesDataEvaluator.java diff --git a/api/src/main/java/org/openmrs/module/kenyaemr/reporting/builder/hiv/PatientAppointmentOnDayReportBuilder.java b/api/src/main/java/org/openmrs/module/kenyaemr/reporting/builder/hiv/PatientAppointmentOnDayReportBuilder.java index db1ac1628a..437d7c456a 100644 --- a/api/src/main/java/org/openmrs/module/kenyaemr/reporting/builder/hiv/PatientAppointmentOnDayReportBuilder.java +++ b/api/src/main/java/org/openmrs/module/kenyaemr/reporting/builder/hiv/PatientAppointmentOnDayReportBuilder.java @@ -18,17 +18,16 @@ import org.openmrs.module.kenyacore.report.builder.Builds; import org.openmrs.module.kenyacore.report.data.patient.definition.CalculationDataDefinition; import org.openmrs.module.kenyaemr.calculation.library.TelephoneNumberCalculation; -import org.openmrs.module.kenyaemr.calculation.library.hiv.LastReturnVisitDateCalculation; import org.openmrs.module.kenyaemr.metadata.CommonMetadata; import org.openmrs.module.kenyaemr.metadata.HivMetadata; import org.openmrs.module.kenyaemr.reporting.calculation.converter.EncounterDatetimeConverter; import org.openmrs.module.kenyaemr.reporting.cohort.definition.ScheduledAppointmentCohortDefinition; import org.openmrs.module.kenyaemr.reporting.data.converter.ArtDrugRefillAppointmentConverter; import org.openmrs.module.kenyaemr.reporting.data.converter.CalculationResultConverter; -import org.openmrs.module.kenyaemr.reporting.data.converter.CalculationResultDateYYMMDDConverter; import org.openmrs.module.kenyaemr.reporting.data.converter.CustomDateStringConverter; import org.openmrs.module.kenyaemr.reporting.data.converter.definition.HivProgramLastDiscontinuationDateDataDefinition; import org.openmrs.module.kenyaemr.reporting.data.converter.definition.HonouredAppointmentDataDefinition; +import org.openmrs.module.kenyaemr.reporting.data.converter.definition.art.ETLNextAppointmentBetweenDatesDataDefinition; import org.openmrs.module.metadatadeploy.MetadataUtils; import org.openmrs.module.reporting.cohort.definition.CohortDefinition; import org.openmrs.module.reporting.common.TimeQualifier; @@ -39,11 +38,7 @@ import org.openmrs.module.reporting.data.patient.definition.ConvertedPatientDataDefinition; import org.openmrs.module.reporting.data.patient.definition.EncountersForPatientDataDefinition; import org.openmrs.module.reporting.data.patient.definition.PatientIdentifierDataDefinition; -import org.openmrs.module.reporting.data.person.definition.AgeDataDefinition; -import org.openmrs.module.reporting.data.person.definition.ConvertedPersonDataDefinition; -import org.openmrs.module.reporting.data.person.definition.GenderDataDefinition; -import org.openmrs.module.reporting.data.person.definition.PersonIdDataDefinition; -import org.openmrs.module.reporting.data.person.definition.PreferredNameDataDefinition; +import org.openmrs.module.reporting.data.person.definition.*; import org.openmrs.module.reporting.dataset.definition.PatientDataSetDefinition; import org.openmrs.module.reporting.evaluation.parameter.Mapped; import org.openmrs.module.reporting.evaluation.parameter.Parameter; @@ -97,12 +92,16 @@ protected void addColumns(HybridReportDescriptor report, PatientDataSetDefinitio honouredVisitDs.addParameter(new Parameter("startDate", "Start Date", Date.class)); honouredVisitDs.addParameter(new Parameter("endDate", "End Date", Date.class)); + ETLNextAppointmentBetweenDatesDataDefinition nextAppointmentDateDataDefinition = new ETLNextAppointmentBetweenDatesDataDefinition(); + nextAppointmentDateDataDefinition.addParameter(new Parameter("startDate", "Start Date", Date.class)); + nextAppointmentDateDataDefinition.addParameter(new Parameter("endDate", "End Date", Date.class)); + List encounterTypes = Arrays.asList(hivConsultation, consultation); definition.setWhich(TimeQualifier.LAST); definition.setTypes(encounterTypes); dsd.addColumn("Last Visit Date", definition, "", new EncounterDatetimeConverter()); - dsd.addColumn("Next HIV Appointment date", new CalculationDataDefinition("Appointment date", new LastReturnVisitDateCalculation()), "", new DataConverter[]{new CalculationResultConverter()}); + dsd.addColumn("Next HIV Appointment date", nextAppointmentDateDataDefinition, "startDate=${startDate},endDate=${endDate}", new DateConverter(DATE_FORMAT)); dsd.addColumn("Last Discontinuation Date", new HivProgramLastDiscontinuationDateDataDefinition(), "", new CustomDateStringConverter()); dsd.addColumn("Has visit on day", honouredVisitDs, paramMapping, new ArtDrugRefillAppointmentConverter()); diff --git a/api/src/main/java/org/openmrs/module/kenyaemr/reporting/data/converter/definition/art/ETLNextAppointmentBetweenDatesDataDefinition.java b/api/src/main/java/org/openmrs/module/kenyaemr/reporting/data/converter/definition/art/ETLNextAppointmentBetweenDatesDataDefinition.java new file mode 100644 index 0000000000..7e4054df32 --- /dev/null +++ b/api/src/main/java/org/openmrs/module/kenyaemr/reporting/data/converter/definition/art/ETLNextAppointmentBetweenDatesDataDefinition.java @@ -0,0 +1,47 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.module.kenyaemr.reporting.data.converter.definition.art; + +import org.openmrs.module.reporting.data.BaseDataDefinition; +import org.openmrs.module.reporting.data.person.definition.PersonDataDefinition; +import org.openmrs.module.reporting.definition.configuration.ConfigurationPropertyCachingStrategy; +import org.openmrs.module.reporting.evaluation.caching.Caching; + +/** + * Next Appointment Data Definition + */ +@Caching(strategy=ConfigurationPropertyCachingStrategy.class) +public class ETLNextAppointmentBetweenDatesDataDefinition extends BaseDataDefinition implements PersonDataDefinition { + + public static final long serialVersionUID = 1L; + + /** + * Default Constructor + */ + public ETLNextAppointmentBetweenDatesDataDefinition() { + super(); + } + + /** + * Constructor to populate name only + */ + public ETLNextAppointmentBetweenDatesDataDefinition(String name) { + super(name); + } + + //***** INSTANCE METHODS ***** + + /** + * @see org.openmrs.module.reporting.data.DataDefinition#getDataType() + */ + public Class getDataType() { + return Double.class; + } +} diff --git a/api/src/main/java/org/openmrs/module/kenyaemr/reporting/data/converter/definition/evaluator/art/ETLNextAppointmentBetweenDatesDataEvaluator.java b/api/src/main/java/org/openmrs/module/kenyaemr/reporting/data/converter/definition/evaluator/art/ETLNextAppointmentBetweenDatesDataEvaluator.java new file mode 100644 index 0000000000..8195edc0ab --- /dev/null +++ b/api/src/main/java/org/openmrs/module/kenyaemr/reporting/data/converter/definition/evaluator/art/ETLNextAppointmentBetweenDatesDataEvaluator.java @@ -0,0 +1,56 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.module.kenyaemr.reporting.data.converter.definition.evaluator.art; + +import org.openmrs.annotation.Handler; +import org.openmrs.module.kenyaemr.reporting.data.converter.definition.art.ETLNextAppointmentBetweenDatesDataDefinition; +import org.openmrs.module.reporting.data.person.EvaluatedPersonData; +import org.openmrs.module.reporting.data.person.definition.PersonDataDefinition; +import org.openmrs.module.reporting.data.person.evaluator.PersonDataEvaluator; +import org.openmrs.module.reporting.evaluation.EvaluationContext; +import org.openmrs.module.reporting.evaluation.EvaluationException; +import org.openmrs.module.reporting.evaluation.querybuilder.SqlQueryBuilder; +import org.openmrs.module.reporting.evaluation.service.EvaluationService; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.Date; +import java.util.Map; + +/** + * Evaluates Next Appointment Data Definition + */ +@Handler(supports= ETLNextAppointmentBetweenDatesDataDefinition.class, order=50) +public class ETLNextAppointmentBetweenDatesDataEvaluator implements PersonDataEvaluator { + + @Autowired + private EvaluationService evaluationService; + + public EvaluatedPersonData evaluate(PersonDataDefinition definition, EvaluationContext context) throws EvaluationException { + EvaluatedPersonData c = new EvaluatedPersonData(definition, context); + + String qry = "select patient_id,\n" + + " date(mid(max(concat(visit_date, next_appointment_date, '')), 11)) as next_appointment_date\n" + + "from kenyaemr_etl.etl_patient_hiv_followup\n" + + "where date(visit_date) <= date(:endDate)\n" + + " and next_appointment_date between date(:startDate) and date(:endDate)\n" + + "GROUP BY patient_id;"; + + SqlQueryBuilder queryBuilder = new SqlQueryBuilder(); + queryBuilder.append(qry); + Date startDate = (Date)context.getParameterValue("startDate"); + Date endDate = (Date)context.getParameterValue("endDate"); + queryBuilder.addParameter("endDate", endDate); + queryBuilder.addParameter("startDate", startDate); + + Map data = evaluationService.evaluateToMap(queryBuilder, Integer.class, Object.class, context); + c.setData(data); + return c; + } +}