Skip to content

Commit

Permalink
Merge pull request #1958 from njorocs/patients-scheduled-for-app-rpt
Browse files Browse the repository at this point in the history
KHP3-6210:Refactor data fetch for next appointment date column. Chang…
  • Loading branch information
patryllus authored Jul 22, 2024
2 parents b453451 + 4b90614 commit edebc3d
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<EncounterType> 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());

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Integer, Object> data = evaluationService.evaluateToMap(queryBuilder, Integer.class, Object.class, context);
c.setData(data);
return c;
}
}

0 comments on commit edebc3d

Please sign in to comment.