Skip to content

Commit

Permalink
Sanjana | BDSHR - 874 | Cleaning up of computed patient age related c…
Browse files Browse the repository at this point in the history
…olumns from encounter and patient death details.Queries to comput age dynamically from patient.since dob can be updated.
  • Loading branch information
SanjanaB committed Aug 17, 2015
1 parent fcffd63 commit 21262ac
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.util.Date;
import java.util.List;

import static org.sharedhealth.datasense.util.DateUtil.*;
import static org.sharedhealth.datasense.util.TrUrl.isConceptUrl;
import static org.sharedhealth.datasense.util.TrUrl.isReferenceTermUrl;

Expand Down Expand Up @@ -95,9 +94,6 @@ private void mapDateOfDeathAndPatientAge(PatientDeathDetails patientDeathDetails
Observation dateOfDeathObs = findObservation(composition, deathNoteObservation, datasenseProperties.getDateOfDeathUuid());
Date dateOfDeath = getDateValue(encounter, dateOfDeathObs);
patientDeathDetails.setDateOfDeath(dateOfDeath);
patientDeathDetails.setPatientAgeInYears(getYears(patientDeathDetails.getPatient().getDateOfBirth(), dateOfDeath));
patientDeathDetails.setPatientAgeInMonths(getMonths(patientDeathDetails.getPatient().getDateOfBirth(), dateOfDeath));
patientDeathDetails.setPatientAgeInDays(getDays(patientDeathDetails.getPatient().getDateOfBirth(), dateOfDeath));
}

private Date getDateValue(Encounter encounter, Observation dateOfDeathObs) {
Expand Down
27 changes: 0 additions & 27 deletions src/main/java/org/sharedhealth/datasense/model/Encounter.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ public class Encounter {
private Patient patient;
private Date encounterDateTime;
private String locationCode;
private int patientAgeInYears;
private int patientAgeInMonths;
private int patientAgeInDays;

public void setEncounterType(String encounterType) {
this.encounterType = encounterType;
Expand Down Expand Up @@ -69,30 +66,6 @@ public String getLocationCode() {
return null;
}

public int getPatientAgeInYears() {
return patientAgeInYears;
}

public void setPatientAgeInYears(int patientAgeInYears) {
this.patientAgeInYears = patientAgeInYears;
}

public int getPatientAgeInMonths() {
return patientAgeInMonths;
}

public void setPatientAgeInMonths(int patientAgeInMonths) {
this.patientAgeInMonths = patientAgeInMonths;
}

public int getPatientAgeInDays() {
return patientAgeInDays;
}

public void setPatientAgeInDays(int patientAgeInDays) {
this.patientAgeInDays = patientAgeInDays;
}

public String getEncounterId() {
return encounterId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ public class PatientDeathDetails extends BaseResource {
private Patient patient;
private Encounter encounter;
private Date dateOfDeath;
private int patientAgeInYears;
private int patientAgeInMonths;
private int patientAgeInDays;
private String circumstancesOfDeath;
private String causeOfDeathCode;
private String causeOfDeathConceptUuid;
Expand Down Expand Up @@ -38,30 +35,6 @@ public void setDateOfDeath(Date dateOfDeath) {
this.dateOfDeath = dateOfDeath;
}

public int getPatientAgeInYears() {
return patientAgeInYears;
}

public void setPatientAgeInYears(int patientAgeInYears) {
this.patientAgeInYears = patientAgeInYears;
}

public int getPatientAgeInMonths() {
return patientAgeInMonths;
}

public void setPatientAgeInMonths(int patientAgeInMonths) {
this.patientAgeInMonths = patientAgeInMonths;
}

public int getPatientAgeInDays() {
return patientAgeInDays;
}

public void setPatientAgeInDays(int patientAgeInDays) {
this.patientAgeInDays = patientAgeInDays;
}

public String getCircumstancesOfDeath() {
return circumstancesOfDeath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

import java.util.Date;

import static org.sharedhealth.datasense.util.DateUtil.*;

@Component("clinicalEncounterProcessor")
public class ClinicalEncounterProcessor implements ResourceProcessor {
private EncounterDao encounterDao;
Expand Down Expand Up @@ -57,7 +55,6 @@ private Encounter mapEncounterFields(org.hl7.fhir.instance.model.Encounter fhirE
String encounterDate = composition.getComposition().getDateSimple().toString();
Date encounterDateTime = DateUtil.parseDate(encounterDate);
encounter.setEncounterDateTime(encounterDateTime);
setPatientAge(patient.getDateOfBirth(), encounterDateTime, encounter);
return encounter;
}

Expand All @@ -66,9 +63,4 @@ public void setNext(ResourceProcessor nextProcessor) {
this.nextProcessor = nextProcessor;
}

public void setPatientAge(Date birthDate, Date encounterDate, Encounter encounter) {
encounter.setPatientAgeInYears(getYears(birthDate, encounterDate));
encounter.setPatientAgeInMonths(getMonths(birthDate, encounterDate));
encounter.setPatientAgeInDays(getDays(birthDate, encounterDate));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,12 @@ public void save(Encounter encounter) {
map.put("encounter_type", encounter.getEncounterType());
map.put("visit_type", encounter.getEncounterVisitType());
map.put("patient_hid", encounter.getPatient().getHid());
map.put("patient_age_years", encounter.getPatientAgeInYears());
map.put("patient_age_months", encounter.getPatientAgeInMonths());
map.put("patient_age_days", encounter.getPatientAgeInDays());
map.put("location_id", encounter.getLocationCode());
map.put("facility_id", encounter.getFacility().getFacilityId());
jdbcTemplate.update("insert into encounter (encounter_id, encounter_datetime, encounter_type, visit_type, " +
"patient_hid, " +
"patient_age_years, patient_age_months, patient_age_days, encounter_location_id, facility_id) " +
"patient_hid, encounter_location_id, facility_id) " +
"values(:encounter_id, :encounter_datetime, :encounter_type , :visit_type , :patient_hid, " +
":patient_age_years, :patient_age_months," +
" :patient_age_days , :location_id, :facility_id)", map);
":location_id, :facility_id)", map);
}

public void deleteExisting(String healthId, String encounterId){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,16 @@ public void save(PatientDeathDetails patientDeathDetails) {
map.put("patient_hid", patientDeathDetails.getPatient().getHid());
map.put("encounter_id", patientDeathDetails.getEncounter().getEncounterId());
map.put("date_of_death", patientDeathDetails.getDateOfDeath());
map.put("patient_age_years", patientDeathDetails.getPatientAgeInYears());
map.put("patient_age_months", patientDeathDetails.getPatientAgeInMonths());
map.put("patient_age_days", patientDeathDetails.getPatientAgeInDays());
map.put("circumstances_of_death", patientDeathDetails.getCircumstancesOfDeath());
map.put("cause_concept_uuid", patientDeathDetails.getCauseOfDeathConceptUuid());
map.put("cause_code", patientDeathDetails.getCauseOfDeathCode());
map.put("uuid", patientDeathDetails.getUuid());


String sql = "insert into patient_death_details (patient_hid, encounter_id, date_of_death, patient_age_years, patient_age_months, patient_age_days," +
String sql = "insert into patient_death_details (patient_hid, encounter_id, date_of_death, " +
"circumstances_of_death, cause_concept_uuid, cause_code, uuid) " +
"values(:patient_hid, :encounter_id, :date_of_death, :patient_age_years, :patient_age_months, :patient_age_days, " +
" :circumstances_of_death, :cause_concept_uuid, :cause_code, :uuid)";
"values(:patient_hid, :encounter_id, :date_of_death, " +
":circumstances_of_death, :cause_concept_uuid, :cause_code, :uuid)";

jdbcTemplate.update(sql, map);
}
Expand Down
12 changes: 12 additions & 0 deletions src/main/resources/db/changelog/db-changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -668,4 +668,16 @@
<column name="updated_at" type="datetime" defaultValueComputed="now()"/>
</addColumn>
</changeSet>
<changeSet id="sharedhealth-datasense-20150818-1513" author="sanjana" context="setup">
<comment>Dropping patient age columns in encounter table</comment>
<dropColumn columnName="patient_age_years" tableName="encounter"/>
<dropColumn columnName="patient_age_months" tableName="encounter"/>
<dropColumn columnName="patient_age_days" tableName="encounter"/>
</changeSet>
<changeSet id="sharedhealth-datasense-20150818-1520" author="sanjana" context="setup">
<comment>Dropping patient age columns in encounter table</comment>
<dropColumn columnName="patient_age_years" tableName="patient_death_details"/>
<dropColumn columnName="patient_age_months" tableName="patient_death_details"/>
<dropColumn columnName="patient_age_days" tableName="patient_death_details"/>
</changeSet>
</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ public class AqsExecutorIntegrationTest {
public void shouldExecuteMultipleQueriesAndReturnResults() throws ExecutionException, InterruptedException {
QueryDefinition admissionsMale = new QueryDefinition();
admissionsMale.setQueryName("ipd_male_0_to_4");
admissionsMale.setQueryString("select count(distinct e.patient_hid) as v1 from encounter e, patient p where facility_id=':paramFacilityId:' and e.patient_hid=p.patient_hid and e.patient_age_years >= 0 and e.patient_age_years < 5 and p.gender='M' and LOWER(e.visit_type)='inpatient' and e.encounter_datetime =':paramStartDate:';");
admissionsMale.setQueryString("select count(distinct e.patient_hid) as v1 from encounter e, patient p where facility_id=':paramFacilityId:' and e.patient_hid=p.patient_hid and TIMESTAMPDIFF(YEAR, p.dob, e.encounter_datetime) between 0 and 5 and p.gender='M' and LOWER(e.visit_type)='inpatient' and e.encounter_datetime =':paramStartDate:';");

QueryDefinition emergencyVisitsFemale = new QueryDefinition();
emergencyVisitsFemale.setQueryName("emergency_female_15_to_24");
emergencyVisitsFemale.setQueryString("select count(distinct e.patient_hid) as v1 from encounter e, patient p where facility_id=':paramFacilityId:' and e.patient_hid=p.patient_hid and e.patient_age_years >= 15 and e.patient_age_years < 25 and p.gender='F' and LOWER(e.visit_type)='emergency' and e.encounter_datetime=':paramStartDate:';");
emergencyVisitsFemale.setQueryString("select count(distinct e.patient_hid) as v1 from encounter e, patient p where facility_id=':paramFacilityId:' and e.patient_hid=p.patient_hid and TIMESTAMPDIFF(YEAR, p.dob, e.encounter_datetime) between 15 and 25 and p.gender='F' and LOWER(e.visit_type)='emergency' and e.encounter_datetime=':paramStartDate:';");


Map<String, Object> params = new HashMap<String, Object>() {{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,6 @@ public void shouldSaveDateOfDeathFromDateOfDeathObservationResource() {
assertEquals(DateUtil.parseDate("2014-12-28T00:00:00+05:30"), patientDeathDetails.getDateOfDeath());
}

@Test
public void shouldSavePatientAgeFromDateOfDeathObservationResource() {
deathNoteHandler.process(deathNoteResource, composition);
PatientDeathDetails patientDeathDetails = findByEncounterId("shrEncounterId");
assertEquals(1, patientDeathDetails.getPatientAgeInYears());
assertEquals(12, patientDeathDetails.getPatientAgeInMonths());
assertEquals(365, patientDeathDetails.getPatientAgeInDays());
}

@Test
public void shouldSaveCircumstancesOfDeathObservationResource() {
deathNoteHandler.process(deathNoteResource, composition);
Expand All @@ -115,7 +106,7 @@ public void shouldSaveCauseOfDeath() {
}

private PatientDeathDetails findByEncounterId(String shrEncounterId) {
String sql = "select patient_hid, encounter_id, date_of_death, patient_age_years, patient_age_months, patient_age_days, " +
String sql = "select patient_hid, encounter_id, date_of_death, " +
"circumstances_of_death, cause_concept_uuid, cause_code, uuid " +
"from patient_death_details where encounter_id= :encounter_id";
HashMap<String, Object> map = new HashMap<>();
Expand All @@ -133,9 +124,6 @@ public PatientDeathDetails mapRow(ResultSet rs, int rowNum) throws SQLException
patientDeathDetails.setEncounter(encounter);

patientDeathDetails.setDateOfDeath(rs.getTimestamp("date_of_death"));
patientDeathDetails.setPatientAgeInYears(rs.getInt("patient_age_years"));
patientDeathDetails.setPatientAgeInMonths(rs.getInt("patient_age_months"));
patientDeathDetails.setPatientAgeInDays(rs.getInt("patient_age_days"));
patientDeathDetails.setCircumstancesOfDeath(rs.getString("circumstances_of_death"));
patientDeathDetails.setCauseOfDeathConceptUuid(rs.getString("cause_concept_uuid"));
patientDeathDetails.setCauseOfDeathCode(rs.getString("cause_code"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void shouldSaveEncounter() throws Exception {
private Encounter findEncounterById(String encounterId) {
List<Encounter> encounters = jdbcTemplate.query(
"select encounter_id ,encounter_datetime, encounter_type, visit_type, patient_hid, " +
"patient_age_years, patient_age_months, patient_age_days,encounter_location_id, facility_id " +
"encounter_location_id, facility_id " +
"from encounter where encounter_id= :encounter_id", Collections.singletonMap("encounter_id",
encounterId),
new RowMapper<Encounter>() {
Expand All @@ -96,9 +96,6 @@ public Encounter mapRow(ResultSet rs, int rowNum) throws SQLException {
encounter.setEncounterDateTime(new Date(rs.getTimestamp("encounter_datetime").getTime()));
encounter.setEncounterType(rs.getString("encounter_type"));
encounter.setEncounterVisitType(rs.getString("visit_type"));
encounter.setPatientAgeInYears(rs.getInt("patient_age_years"));
encounter.setPatientAgeInMonths(rs.getInt("patient_age_months"));
encounter.setPatientAgeInDays(rs.getInt("patient_age_days"));
encounter.setLocationCode(rs.getString("encounter_location_id"));

Patient patient = new Patient();
Expand Down
26 changes: 13 additions & 13 deletions src/test/resources/csv/encounters.csv
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
encounter_id,encounter_datetime,encounter_type,visit_type,patient_hid,patient_age_years,patient_age_months,patient_age_days,encounter_location_id,facility_id,created_at
100,2014-12-23,REG,outpatient,129,7,1,20,302618,1000
103,2014-12-23,Consultation,outpatient,129,7,1,20,302618,1000
110,2014-12-23,REG,outpatient,122,6,1,20,302618,1000
111,2014-12-23,Consultation,outpatient,122,6,1,20,302618,1000
101,2014-12-23,REG,inpatient,121,3,2,20,302618,1000
102,2014-12-23,REG,outpatient,124,12,1,20,302618,1000
104,2014-12-23,REG,outpatient,127,34,1,20,302618,1000
105,2014-12-23,Consultation,outpatient,127,34,1,20,302618,1000
106,2014-12-23,Consultation,inpatient,121,3,2,20,302618,1000
107,2014-12-23,REG,inpatient,130,6,1,20,302618,1000
108,2014-12-23,Consultation,inpatient,130,6,1,20,302618,1000
109,2014-12-23,Consultation,inpatient,130,6,1,20,302618,1000
encounter_id,encounter_datetime,encounter_type,visit_type,patient_hid,encounter_location_id,facility_id,created_at
100,2014-12-23,REG,outpatient,129,302618,1000
103,2014-12-23,Consultation,outpatient,129,302618,1000
110,2014-12-23,REG,outpatient,122,302618,1000
111,2014-12-23,Consultation,outpatient,122,302618,1000
101,2014-12-23,REG,inpatient,121,302618,1000
102,2014-12-23,REG,outpatient,124,302618,1000
104,2014-12-23,REG,outpatient,127,302618,1000
105,2014-12-23,Consultation,outpatient,127,302618,1000
106,2014-12-23,Consultation,inpatient,121,302618,1000
107,2014-12-23,REG,inpatient,130,302618,1000
108,2014-12-23,Consultation,inpatient,130,302618,1000
109,2014-12-23,Consultation,inpatient,130,302618,1000
4 changes: 2 additions & 2 deletions src/test/resources/dhis/queries/query_list_1.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
"queries": [
{
"queryName": "Query_1",
"query": "select count(distinct e.patient_hid) as v1 from encounter e, patient p where facility_id=:FACILITY: and e.patient_hid=p.patient_hid and e.patient_age_years >= :MIN_YEAR: and e.patient_age_years < :MAX_YEAR: and p.gender=':GENDER:' and e.visit_type=':VISIT_TYPE:' and encounter_datetime=':ENC_DATE:';"
"query": "select count(distinct e.patient_hid) as v1 from encounter e, patient p where facility_id=:FACILITY: and e.patient_hid=p.patient_hid and TIMESTAMPDIFF(YEAR, p.dob, e.encounter_datetime) between :MIN_YEAR: and :MAX_YEAR: and p.gender=':GENDER:' and e.visit_type=':VISIT_TYPE:' and encounter_datetime=':ENC_DATE:';"
},
{
"queryName": "Query_2",
"query": "select count(distinct e.patient_hid) as v2 from encounter e, patient p where facility_id=:FACILITY: and e.patient_hid=p.patient_hid and e.patient_age_years >= :MIN_YEAR: and e.patient_age_years < :MAX_YEAR: and p.gender=':GENDER2:' and e.visit_type=':VISIT_TYPE:' and encounter_datetime=':ENC_DATE:';"
"query": "select count(distinct e.patient_hid) as v2 from encounter e, patient p where facility_id=:FACILITY: and e.patient_hid=p.patient_hid and TIMESTAMPDIFF(YEAR, p.dob, e.encounter_datetime) between :MIN_YEAR: and :MAX_YEAR: and p.gender=':GENDER2:' and e.visit_type=':VISIT_TYPE:' and encounter_datetime=':ENC_DATE:';"
}
]
}
Expand Down

0 comments on commit 21262ac

Please sign in to comment.