From 0971b81fa6f426606891370e7567f052fc5e4ed5 Mon Sep 17 00:00:00 2001 From: Alfred-Mutai Date: Wed, 2 Aug 2023 11:34:58 +0300 Subject: [PATCH 1/2] POC-341: Revise Patient Requiring Viral Order report to Generate the report as per the selected Filters --- .../differentiated-care-program-base.json | 12 ++---------- .../json-reports/labs-report-aggregate.json | 2 +- .../json-reports/labs-report-base.json | 4 ++-- etl-routes.js | 3 +++ 4 files changed, 8 insertions(+), 13 deletions(-) diff --git a/app/reporting-framework/json-reports/differentiated-care-program-base.json b/app/reporting-framework/json-reports/differentiated-care-program-base.json index 818bcd0bb..7a94e37c7 100644 --- a/app/reporting-framework/json-reports/differentiated-care-program-base.json +++ b/app/reporting-framework/json-reports/differentiated-care-program-base.json @@ -11,18 +11,10 @@ }, { "table": "(SELECT fli.person_id, fli.hiv_viral_load as vl_1, fli.test_datetime as vl_1_date FROM etl.flat_labs_and_imaging fli INNER JOIN (SELECT person_id, MAX(test_datetime) AS max_vl_1_date FROM etl.flat_labs_and_imaging fli where fli.hiv_viral_load is not null GROUP BY person_id) max_dates ON fli.person_id = max_dates.person_id AND fli.test_datetime = max_dates.max_vl_1_date and fli.hiv_viral_load is not null)", - "alias": "fli_latest_vl", + "alias": "fli1", "join": { "type": "LEFT", - "joinCondition": "fli_latest_vl.person_id = dcr.person_id" - } - }, - { - "table": "etl.flat_patient_identifiers_v1", - "alias": "flat_identifiers", - "join": { - "type": "LEFT", - "joinCondition": "dcr.person_id = flat_identifiers.patient_id" + "joinCondition": "fli1.person_id = dcr.person_id" } } ], diff --git a/app/reporting-framework/json-reports/labs-report-aggregate.json b/app/reporting-framework/json-reports/labs-report-aggregate.json index ca750adf1..f39874bdc 100644 --- a/app/reporting-framework/json-reports/labs-report-aggregate.json +++ b/app/reporting-framework/json-reports/labs-report-aggregate.json @@ -72,7 +72,7 @@ { "filterType": "tableColumns", "conditionExpression": "(t1.next_clinical_datetime_hiv is null or date(t1.next_clinical_datetime_hiv) > ?)", - "parameterName": "endDate" + "parameterName": "reportMonth" }, { "filterType": "tableColumns", diff --git a/app/reporting-framework/json-reports/labs-report-base.json b/app/reporting-framework/json-reports/labs-report-base.json index 60b797ab0..776c9941b 100644 --- a/app/reporting-framework/json-reports/labs-report-base.json +++ b/app/reporting-framework/json-reports/labs-report-base.json @@ -69,7 +69,7 @@ "alias": "needs_vl_in_period", "expressionType": "simple_expression", "expressionOptions": { - "expression": "if(case when (t2.cur_arv_meds is not null and t2.vl_1 > 1000) and (timestampdiff(day,t2.vl_1_date, '{endDate}') >= 90) then true when (timestampdiff(month,t2.arv_start_date, '{endDate}') <= 12) and (t2.vl_1_date is null or timestampdiff(month,t2.vl_1_date, '{endDate}') >= 6) and (timestampdiff(month,t2.arv_start_date, '{endDate}')>=6) then true when (timestampdiff(month,t2.arv_start_date, '{endDate}') >= 12) and (t2.vl_1_date is null or timestampdiff(month,t2.vl_1_date, '{endDate}') >= 12) then true else false end,1,0)" + "expression": "if(case when (t2.cur_arv_meds is not null and t2.vl_1 > 1000) and (timestampdiff(day,t2.vl_1_date, '{reportMonth}') >= 90) then true when (timestampdiff(month,t2.arv_start_date, '{reportMonth}') <= 12) and (t2.vl_1_date is null or timestampdiff(month,t2.vl_1_date, '{reportMonth}') >= 6) and (timestampdiff(month,t2.arv_start_date, '{reportMonth}')>=6) then true when (timestampdiff(month,t2.arv_start_date, '{reportMonth}') >= 12) and (t2.vl_1_date is null or timestampdiff(month,t2.vl_1_date, '{reportMonth}') >= 12) then true else false end,1,0)" } }, { @@ -152,7 +152,7 @@ { "filterType": "tableColumns", "conditionExpression": "(t2.next_clinical_datetime_hiv is null or date(t2.next_clinical_datetime_hiv) > ?)", - "parameterName": "endDate" + "parameterName": "reportMonth" }, { "filterType": "tableColumns", diff --git a/etl-routes.js b/etl-routes.js index feffa7dab..3f836b008 100755 --- a/etl-routes.js +++ b/etl-routes.js @@ -4374,6 +4374,9 @@ module.exports = (function () { endDate: Joi.string() .optional() .description('The end date to filter by'), + reportMonth: Joi.string() + .required() + .description('The report month in the format "YYYY-MM-DD"'), startIndex: Joi.number() .required() .description('The startIndex to control pagination'), From 312ecee1d8ee2021da6a10895bcde7a8f9d76bf4 Mon Sep 17 00:00:00 2001 From: Alfred-Mutai Date: Thu, 10 Aug 2023 15:44:21 +0300 Subject: [PATCH 2/2] POC-341: Patients requiring VL --- .../clinical-reminder-report.json | 2 +- .../json-reports/labs-report-base.json | 98 ++++++------------- ...atients-requiring-viral-load-template.json | 44 ++++----- 3 files changed, 47 insertions(+), 97 deletions(-) diff --git a/app/reporting-framework/json-reports/clinical-reminder-report.json b/app/reporting-framework/json-reports/clinical-reminder-report.json index af6355db0..f4fe3c818 100644 --- a/app/reporting-framework/json-reports/clinical-reminder-report.json +++ b/app/reporting-framework/json-reports/clinical-reminder-report.json @@ -126,7 +126,7 @@ "alias": "needs_vl_coded", "expressionType": "simple_expression", "expressionOptions": { - "expression": "CASE WHEN (ls.cur_arv_meds IS NULL) THEN 0 WHEN (TIMESTAMPDIFF(MONTH, ls.vl_1_date, DATE('{referenceDate}')) > 3) AND ls.vl_1 > 1000 AND ls.arv_start_date < ls.vl_1_date THEN 1 WHEN ls.vl_1 > 200 AND (TIMESTAMPDIFF(MONTH,ls.vl_1_date,DATE('{referenceDate}')) > 3) THEN 2 WHEN (TIMESTAMPDIFF(MONTH, ls.arv_start_date, '{referenceDate}') >= 12) AND (ls.vl_2 IS NULL) THEN 3 WHEN (ls.is_pregnant OR (t6.relationship = 2 AND TIMESTAMPDIFF(MONTH, t6.date_created, '{referenceDate}') BETWEEN 0 AND 24)) AND (TIMESTAMPDIFF(MONTH, ls.vl_1_date, '{referenceDate}') >= 6) THEN 4 WHEN (t1.is_pregnant OR (t6.relationship = 2 AND TIMESTAMPDIFF(MONTH, t6.date_created, '{referenceDate}') BETWEEN 0 AND 24)) AND ls.vl_1 >= 200 AND (TIMESTAMPDIFF(MONTH, ls.vl_1_date, '{referenceDate}') >= 3) THEN 5 WHEN (TIMESTAMPDIFF(MONTH, ls.arv_start_date, '{referenceDate}') = 3) AND (ls.vl_1_date IS NULL) THEN 6 WHEN (ls.cur_arv_meds <> ls.prev_arv_meds) AND ls.prev_arv_meds IS NOT NULL THEN 7 WHEN ls.vl_1 < 200 AND (TIMESTAMPDIFF(MONTH, ls.vl_1_date, DATE('{referenceDate}')) >= 12) THEN 8 ELSE 0 END" + "expression": "CASE WHEN (ls.cur_arv_meds IS NULL) THEN 0 WHEN (TIMESTAMPDIFF(MONTH, ls.vl_1_date, DATE('{referenceDate}')) > 3) AND ls.vl_1 > 1000 AND ls.arv_start_date < ls.vl_1_date THEN 1 WHEN ls.vl_1 > 200 AND (TIMESTAMPDIFF(MONTH,ls.vl_1_date,DATE('{referenceDate}')) > 3) THEN 2 WHEN (TIMESTAMPDIFF(MONTH, ls.arv_start_date, '{referenceDate}') >= 12) AND (ls.vl_2 IS NULL) THEN 3 WHEN (ls.is_pregnant OR (t6.relationship = 2 AND TIMESTAMPDIFF(MONTH, t6.date_created, '{referenceDate}') BETWEEN 0 AND 24)) AND (TIMESTAMPDIFF(MONTH, ls.vl_1_date, '{referenceDate}') >= 6) THEN 4 WHEN (t1.is_pregnant OR (t6.relationship = 2 AND TIMESTAMPDIFF(MONTH, t6.date_created, '{referenceDate}') BETWEEN 0 AND 24)) AND ls.vl_1 >= 200 AND (TIMESTAMPDIFF(MONTH, ls.vl_1_date, '{referenceDate}') >= 3) THEN 5 WHEN (TIMESTAMPDIFF(MONTH, ls.arv_start_date, '{referenceDate}') = 3) AND (ls.vl_1_date IS NULL) THEN 6 WHEN (ls.cur_arv_meds <> ls.prev_arv_meds) AND ls.prev_arv_meds IS NOT NULL AND TIMESTAMPDIFF(MONTH, ls.encounter_datetime, DATE('{referenceDate}')) >= 3 THEN 7 WHEN ls.vl_1 < 200 AND (TIMESTAMPDIFF(MONTH, ls.vl_1_date, DATE('{referenceDate}')) >= 12) THEN 8 ELSE 0 END" } }, { diff --git a/app/reporting-framework/json-reports/labs-report-base.json b/app/reporting-framework/json-reports/labs-report-base.json index 776c9941b..e33f87d87 100644 --- a/app/reporting-framework/json-reports/labs-report-base.json +++ b/app/reporting-framework/json-reports/labs-report-base.json @@ -7,54 +7,38 @@ "sources": [ { "table": "etl.flat_hiv_summary_v15b", - "alias": "t2" + "alias": "fhs" }, { - "table": "etl.flat_patient_identifiers_v1", - "alias": "flat_identifiers", - "join": { - "type": "LEFT", - "joinCondition": "t2.person_id = flat_identifiers.patient_id" - } - }, - { - "table": "amrs.location", - "alias": "l", + "table": "(SELECT a.person_id, MAX(encounter_id) AS max_encounter_id FROM etl.flat_hiv_summary_v15b a LEFT JOIN amrs.person p ON p.person_id = a.person_id WHERE p.dead = 0 and p.birthdate is not null GROUP BY a.person_id)", + "alias": "max_encounters", "join": { "type": "INNER", - "joinCondition": "t2.location_uuid = l.uuid" + "joinCondition": "fhs.person_id = max_encounters.person_id AND fhs.encounter_id = max_encounters.max_encounter_id" } }, { "table": "amrs.person", - "alias": "t3", + "alias": "p", "join": { "type": "INNER", - "joinCondition": "t3.person_id = t2.person_id" - } - }, - { - "table": "amrs.person_attribute", - "alias": "contacts", - "join": { - "type": "LEFT", - "joinCondition": "t2.person_id = contacts.person_id AND (contacts.voided IS NULL || contacts.voided = 0) AND contacts.person_attribute_type_id in (10, 48)" + "joinCondition": "p.person_id = fhs.person_id" } }, { - "table": "amrs.encounter_type", - "alias": "et", + "table": "etl.flat_patient_identifiers_v1", + "alias": "flat_identifiers", "join": { "type": "LEFT", - "joinCondition": "t2.encounter_type = et.encounter_type_id" + "joinCondition": "fhs.person_id = flat_identifiers.patient_id" } }, { - "table": "amrs.person_address", - "alias": "pa", + "table": "amrs.location", + "alias": "l", "join": { - "type": "LEFT", - "joinCondition": "t2.person_id = pa.person_id" + "type": "INNER", + "joinCondition": "fhs.location_uuid = l.uuid" } } ], @@ -62,15 +46,7 @@ { "type": "simple_column", "alias": "person_id", - "column": "t2.person_id" - }, - { - "type": "derived_column", - "alias": "needs_vl_in_period", - "expressionType": "simple_expression", - "expressionOptions": { - "expression": "if(case when (t2.cur_arv_meds is not null and t2.vl_1 > 1000) and (timestampdiff(day,t2.vl_1_date, '{reportMonth}') >= 90) then true when (timestampdiff(month,t2.arv_start_date, '{reportMonth}') <= 12) and (t2.vl_1_date is null or timestampdiff(month,t2.vl_1_date, '{reportMonth}') >= 6) and (timestampdiff(month,t2.arv_start_date, '{reportMonth}')>=6) then true when (timestampdiff(month,t2.arv_start_date, '{reportMonth}') >= 12) and (t2.vl_1_date is null or timestampdiff(month,t2.vl_1_date, '{reportMonth}') >= 12) then true else false end,1,0)" - } + "column": "fhs.person_id" }, { "type": "simple_column", @@ -89,61 +65,48 @@ }, { "type": "simple_column", - "alias": "location", - "column": "l.name" + "alias": "location_id", + "column": "fhs.location_id" }, { "type": "simple_column", - "alias": "location_id", - "column": "t2.location_id" + "alias": "location", + "column": "l.name" }, { "type": "simple_column", "alias": "location_uuid", - "column": "t2.location_uuid" + "column": "fhs.location_uuid" }, { "type": "simple_column", "alias": "encounter_datetime", - "column": "t2.encounter_datetime" + "column": "fhs.encounter_datetime" }, { "type": "simple_column", "alias": "month", - "column": "MONTH(t2.encounter_datetime)" + "column": "MONTH(fhs.encounter_datetime)" }, { "type": "simple_column", "alias": "reporting_month", - "column": "date_format(t2.encounter_datetime, '%m/%Y')" + "column": "date_format(fhs.encounter_datetime, '%m/%Y')" }, { "type": "simple_column", "alias": "year", - "column": "YEAR(t2.encounter_datetime)" - }, - { - "type": "derived_column", - "alias": "phone_number", - "expressionType": "simple_expression", - "expressionOptions": { - "expression": " GROUP_CONCAT(DISTINCT contacts.value SEPARATOR ', ')" - } + "column": "YEAR(fhs.encounter_datetime)" }, { "type": "simple_column", "alias": "latest_rtc_date", - "column": "date_format(t2.rtc_date, '%Y-%m-%d')" - }, - { - "type": "simple_column", - "alias": "last_appointment", - "column": "CONCAT(COALESCE(DATE_FORMAT(t2.encounter_datetime, '%Y-%m-%d'),''),' ',COALESCE(et.name, ''))" + "column": "date_format(fhs.rtc_date, '%Y-%m-%d')" }, { "type": "simple_column", "alias": "cur_meds", - "column": "t2.cur_arv_meds" + "column": "fhs.cur_arv_meds" } ], "filters": { @@ -151,18 +114,13 @@ "conditions": [ { "filterType": "tableColumns", - "conditionExpression": "(t2.next_clinical_datetime_hiv is null or date(t2.next_clinical_datetime_hiv) > ?)", - "parameterName": "reportMonth" - }, - { - "filterType": "tableColumns", - "conditionExpression": "t2.location_uuid in ?", + "conditionExpression": "fhs.location_uuid in ? AND fhs.transfer_out is null AND fhs.out_of_care is null", "parameterName": "locationUuids" }, { "filterType": "tableColumns", - "conditionExpression": "coalesce(t2.death_date, t2.out_of_care) is null", - "parameterName": "" + "conditionExpression": "(TIMESTAMPDIFF(YEAR, p.birthdate, DATE('{reportMonth}')) BETWEEN 0 AND 24 AND ((TIMESTAMPDIFF(MONTH, fhs.arv_start_date, DATE('{reportMonth}')) >= 3 AND fhs.vl_1 IS NULL AND fhs.arv_start_date) OR (TIMESTAMPDIFF(MONTH, fhs.vl_1_date, '{reportMonth}') >= 6 AND fhs.vl_1 IS NOT NULL AND fhs.vl_1_date IS NOT NULL))) OR (TIMESTAMPDIFF(YEAR, p.birthdate, DATE('{reportMonth}')) >= 25 AND ((TIMESTAMPDIFF(MONTH, fhs.arv_start_date, DATE('{reportMonth}')) >= 3 AND fhs.arv_start_date IS NOT NULL AND fhs.vl_1 IS NULL) OR (TIMESTAMPDIFF(MONTH, fhs.arv_start_date, DATE('{reportMonth}')) >= 12 AND fhs.arv_start_date IS NOT NULL AND fhs.vl_1 IS NOT NULL AND fhs.vl_2 IS NULL) OR (TIMESTAMPDIFF(MONTH, fhs.vl_1_date, DATE('{reportMonth}')) >= 12 AND fhs.vl_1 IS NOT NULL AND fhs.vl_2 IS NOT NULL AND fhs.vl_1_date IS NOT NULL AND fhs.vl_1 < 200) OR (TIMESTAMPDIFF(MONTH, fhs.vl_1_date, DATE('{reportMonth}')) >= 3 AND fhs.vl_1 IS NOT NULL AND fhs.vl_2 IS NOT NULL AND fhs.vl_1_date IS NOT NULL AND fhs.vl_1 >= 200) OR ((fhs.is_pregnant = 1 OR fhs.is_mother_breastfeeding = 1) AND TIMESTAMPDIFF(MONTH, fhs.vl_1_date, DATE('{reportMonth}')) >= 6 AND fhs.vl_1_date IS NOT NULL))) OR (fhs.prev_arv_meds <> fhs.cur_arv_meds AND TIMESTAMPDIFF(MONTH, fhs.encounter_datetime, DATE('{reportMonth}')) >= 3)", + "parameterName": "reportMonth" } ] } diff --git a/app/reporting-framework/json-reports/patients-requiring-viral-load-template.json b/app/reporting-framework/json-reports/patients-requiring-viral-load-template.json index fa6a4a9a3..970dd69ca 100644 --- a/app/reporting-framework/json-reports/patients-requiring-viral-load-template.json +++ b/app/reporting-framework/json-reports/patients-requiring-viral-load-template.json @@ -12,7 +12,7 @@ "table": "amrs.person_name", "alias": "person_name", "join": { - "type": "INNER", + "type": "LEFT", "joinCondition": "t1.person_id = person_name.person_id AND (person_name.voided IS NULL || person_name.voided = 0) AND person_name.preferred = 1" } }, @@ -32,20 +32,12 @@ "joinCondition": "t1.person_id = contact.person_id AND (contact.voided IS NULL || contact.voided = 0) AND contact.person_attribute_type_id in (10, 48)" } }, - { - "table": "etl.flat_hiv_summary_v15b", - "alias": "fhsh", - "join": { - "type": "LEFT", - "joinCondition": "t1.person_id = fhsh.person_id AND fhsh.next_clinical_location_id IS NULL AND fhsh.encounter_type NOT IN (99999) " - } - }, { "table": "amrs.encounter_type", "alias": "ety", "join": { "type": "LEFT", - "joinCondition": "fhsh.encounter_type = ety.encounter_type_id" + "joinCondition": "fhs.encounter_type = ety.encounter_type_id" } }, { @@ -107,7 +99,7 @@ "alias": "has_pending_vl_test", "expressionType": "simple_expression", "expressionOptions": { - "expression": "case when (timestampdiff(day,t2.vl_order_date,now()) between 0 and 14) and (t2.vl_1_date is null or t2.vl_order_date > t2.vl_1_date) then true else false end" + "expression": "case when (timestampdiff(day,fhs.vl_order_date,now()) between 0 and 14) and (fhs.vl_1_date is null or fhs.vl_order_date > fhs.vl_1_date) then true else false end" } }, { @@ -115,7 +107,7 @@ "alias": "current_vl", "expressionType": "simple_expression", "expressionOptions": { - "expression": "t2.vl_1 " + "expression": "fhs.vl_1 " } }, { @@ -123,7 +115,7 @@ "alias": "current_vl_date", "expressionType": "simple_expression", "expressionOptions": { - "expression": "date_format(t2.vl_1_date,'%d-%m-%Y')" + "expression": "date_format(fhs.vl_1_date,'%d-%m-%Y')" } }, { @@ -131,7 +123,7 @@ "alias": "last_vl_order_date", "expressionType": "simple_expression", "expressionOptions": { - "expression": "date_format(t2.vl_order_date,'%d-%m-%Y')" + "expression": "date_format(fhs.vl_order_date,'%d-%m-%Y')" } }, { @@ -139,7 +131,7 @@ "alias": "days_since_last_order", "expressionType": "simple_expression", "expressionOptions": { - "expression": "timestampdiff(day, t2.vl_order_date, now())" + "expression": "timestampdiff(day, fhs.vl_order_date, now())" } }, { @@ -153,12 +145,12 @@ { "type": "simple_column", "alias": "latest_rtc_date", - "column": "date_format(fhsh.rtc_date, '%Y-%m-%d')" + "column": "date_format(fhs.rtc_date, '%Y-%m-%d')" }, { "type": "simple_column", "alias": "latest_vl", - "column": "fhsh.vl_1" + "column": "fhs.vl_1" }, { "type": "derived_column", @@ -167,19 +159,19 @@ "expressionOptions": { "caseOptions": [ { - "condition": "fhsh.vl_1 < 50", + "condition": "fhs.vl_1 < 50", "value": "LDL" }, { - "condition": "(fhsh.vl_1 >= 50 and fhsh.vl_1 < 200)", + "condition": "(fhs.vl_1 >= 50 and fhs.vl_1 < 200)", "value": "Low Risk Low Level Viremia" }, { - "condition": "(fhsh.vl_1 >= 200 and fhsh.vl_1 < 1000)", + "condition": "(fhs.vl_1 >= 200 and fhs.vl_1 < 1000)", "value": "High Risk Low Level Viremia" }, { - "condition": "(fhsh.vl_1 > 1000)", + "condition": "(fhs.vl_1 > 1000)", "value": "Suspected Treatment Failure" } ] @@ -188,27 +180,27 @@ { "type": "simple_column", "alias": "latest_vl_date", - "column": "date_format(fhsh.vl_1_date, '%Y-%m-%d')" + "column": "date_format(fhs.vl_1_date, '%Y-%m-%d')" }, { "type": "simple_column", "alias": "last_appointment", - "column": "CONCAT(COALESCE(DATE_FORMAT(fhsh.encounter_datetime, '%Y-%m-%d'),''),' ',COALESCE(ety.name, ''))" + "column": "CONCAT(COALESCE(DATE_FORMAT(fhs.encounter_datetime, '%Y-%m-%d'),''),' ',COALESCE(ety.name, ''))" }, { "type": "simple_column", "alias": "cur_meds", - "column": "fhsh.cur_arv_meds" + "column": "fhs.cur_arv_meds" }, { "type": "simple_column", "alias": "previous_vl", - "column": "fhsh.vl_2" + "column": "fhs.vl_2" }, { "type": "simple_column", "alias": "previous_vl_date", - "column": "date_format(fhsh.vl_2_date, '%Y-%m-%d')" + "column": "date_format(fhs.vl_2_date, '%Y-%m-%d')" }, { "type": "simple_column",