Skip to content

Commit

Permalink
Merge pull request #1358 from henrykorir/POC-610
Browse files Browse the repository at this point in the history
POC-610: Optimized the DQA Query
Alfred-Mutai authored Jan 12, 2024
2 parents e2695bf + a6b01d6 commit b1e7019
Showing 1 changed file with 123 additions and 150 deletions.
273 changes: 123 additions & 150 deletions reports/DQA/dqa-chart-abstraction-report-dao.js
Original file line number Diff line number Diff line change
@@ -28,157 +28,131 @@ export class DQAChartAbstractionDAO {
limit === 'all' ? '' : `limit ` + limit + ` offset ` + offset;
let sqlQuery =
`
SELECT
h.person_uuid as uuid,
-- cc.identifier as ccc_number,
-- np.identifier as upi_number,
-- ov.identifier as ovcid_id,
max(case WHEN id.identifier_type in (28) then id.identifier end) AS ccc_number,
max(case WHEN id.identifier_type in (43) then id.identifier end) AS ovc_number,
max(case WHEN id.identifier_type in (45) then id.identifier end) AS upi_number,
EXTRACT(YEAR FROM (FROM_DAYS(DATEDIFF(NOW(), h.birthdate)))) AS age,
IF( date(h.tb_screening_datetime) > date_sub(h.endDate, interval 6 month),
"YES",
"NO") as tb_screened_this_visit,
h.gender as 'sex_gender',
e.height,
e.weight,
DATE_FORMAT(h.birthdate, '%Y-%m-%d') AS birthdate,
h.encounter_id,
DATE_FORMAT(h.encounter_date, '%Y-%m-%d') as last_appointment_date,
h.rtc_date,
h.location_id,
h.cur_arv_meds,
h.tb_screen,
h.tb_screening_datetime,
e.hiv_start_date,
h.arv_start_date,
e.cd4_1,
-- ls.encounter_datetime AS last_clinical_encounter,
e.encounter_datetime AS last_clinical_encounter,
DATE_FORMAT(h.rtc_date, '%Y-%m-%d') as next_appointment,
h.vl_1,
case
when e.weight is not null
and e.height is not null then 'YES'
else 'NO'
end as nutrition,
case
when e.visit_type in (37, 58) then 'CARG'
when e.visit_type in (43, 80, 104, 118, 120, 123) then 'Treatment Supporter'
else 'Self'
end as visit_type,
h.cur_arv_meds_names AS drugs_given,
case
when e.height is not null
and e.weight is not null then e.weight /((e.height / 100) * (e.height / 100))
else null
end as BMI,
IF(p.program_id = 9,
'DC',
'Standard') AS DSD,
TIMESTAMPDIFF(DAY,
h.encounter_date,
h.rtc_date) AS drugs_duration,
IF(h.ipt_start_date = h.encounter_date,
1,
0) AS ipt_started_this_visit,
DATE_FORMAT(h.ipt_start_date, '%Y-%m-%d') AS last_ipt_start_date,
case
when e.on_ipt = 1 then 'CONTINUING'
when e.ipt_completion_date is null
and e.ipt_stop_date is not null then 'DISCONTINUED'
when e.ipt_completion_date is not null
then 'COMPLETED'
else 'NA'
end as tpt_status,
h.ipt_stop_date,
h.ipt_completion_date,
e.cur_who_stage,
e.cd4_1,
fv.muac,
IF(h.ipt_stop_date = h.encounter_date
OR h.ipt_completion_date = h.encounter_date,
1,
0) AS ipt_ended_this_visit,
fv.systolic_bp as sysBP,
fv.diastolic_bp as dysBP,
h.status,
IF((o.value_coded IS NULL), 'NO', 'YES') AS is_crag_screened,
case
when (h.vl_1 < 200) then 'suppressed'
when (h.vl_1 >= 200) then 'unsuppressed'
end as vl_suppression,
case
when (e.tb_screening_result = 6971) then 'Pr TB'
when (e.tb_screening_result = 6137) then 'Confirmed'
when (e.tb_screening_result = 1107 OR e.tb_screening_result = 10678) then 'No TB'
when (e.tb_screening_result = 10767) then 'TB Rx'
when (e.tb_screening_result = 10974) then 'INH'
end as tb_screening_result
SELECT
h.person_uuid AS uuid,
fpiv.ccc AS ccc_number,
fpiv.nupi AS upi_number,
fpiv.ovcid AS ovcid_id,
EXTRACT(YEAR FROM (FROM_DAYS(DATEDIFF(NOW(), h.birthdate)))) AS age,
IF(DATE(h.tb_screening_datetime) > DATE_SUB(h.endDate, INTERVAL 6 MONTH),
'YES',
'NO') AS tb_screened_this_visit,
h.gender AS 'sex_gender',
e.height,
e.weight,
DATE_FORMAT(h.birthdate, '%Y-%m-%d') AS birthdate,
h.encounter_id,
DATE_FORMAT(h.encounter_date, '%Y-%m-%d') AS last_appointment_date,
h.rtc_date,
h.location_id,
h.cur_arv_meds,
h.tb_screen,
h.tb_screening_datetime,
e.hiv_start_date,
h.arv_start_date,
e.cd4_1,
e.encounter_datetime AS last_clinical_encounter,
DATE_FORMAT(h.rtc_date, '%Y-%m-%d') AS next_appointment,
h.vl_1,
CASE
WHEN
e.weight IS NOT NULL
AND e.height IS NOT NULL
THEN
'YES'
ELSE 'NO'
END AS nutrition,
CASE
WHEN e.visit_type IN (37 , 58) THEN 'CARG'
WHEN e.visit_type IN (43 , 80, 104, 118, 120, 123) THEN 'Treatment Supporter'
ELSE 'Self'
END AS visit_type,
h.cur_arv_meds_names AS drugs_given,
CASE
WHEN
e.height IS NOT NULL
AND e.weight IS NOT NULL
THEN
e.weight / ((e.height / 100) * (e.height / 100))
ELSE NULL
END AS BMI,
IF(p.program_id = 9, 'DC', 'Standard') AS DSD,
TIMESTAMPDIFF(DAY,
h.encounter_date,
h.rtc_date) AS drugs_duration,
IF(h.ipt_start_date = h.encounter_date,
1,
0) AS ipt_started_this_visit,
DATE_FORMAT(h.ipt_start_date, '%Y-%m-%d') AS last_ipt_start_date,
CASE
WHEN e.on_ipt = 1 THEN 'CONTINUING'
WHEN
e.ipt_completion_date IS NULL
AND e.ipt_stop_date IS NOT NULL
THEN
'DISCONTINUED'
WHEN e.ipt_completion_date IS NOT NULL THEN 'COMPLETED'
ELSE 'NA'
END AS tpt_status,
h.ipt_stop_date,
h.ipt_completion_date,
e.cur_who_stage,
e.cd4_1,
fv.muac,
IF(h.ipt_stop_date = h.encounter_date
OR h.ipt_completion_date = h.encounter_date,
1,
0) AS ipt_ended_this_visit,
fv.systolic_bp AS sysBP,
fv.diastolic_bp AS dysBP,
h.status,
IF((o.value_coded IS NULL), 'NO', 'YES') AS is_crag_screened,
CASE
WHEN (h.vl_1 < 200) THEN 'suppressed'
WHEN (h.vl_1 >= 200) THEN 'unsuppressed'
END AS vl_suppression,
CASE
WHEN (e.tb_screening_result = 6971) THEN 'Pr TB'
WHEN (e.tb_screening_result = 6137) THEN 'Confirmed'
WHEN (e.tb_screening_result IN (1107 , 10678)) THEN 'No TB'
WHEN (e.tb_screening_result = 10767) THEN 'TB Rx'
WHEN (e.tb_screening_result = 10974) THEN 'INH'
END AS tb_screening_result
FROM
etl.hiv_monthly_report_dataset_v1_2 h
LEFT JOIN
etl.flat_hiv_summary_v15b e on
(h.encounter_id = e.encounter_id)
-- LEFT JOIN
-- etl.flat_hiv_summary_v15b ls on
-- (ls.next_clinical_datetime_hiv is null
-- and ls.person_id = e.person_id)
left JOIN (
SELECT
*,
MAX(encounter_datetime) AS max_date
FROM
etl.flat_vitals
WHERE
-- systolic_bp IS NOT NULL
-- AND diastolic_bp IS NOT NULL
-- AND
encounter_datetime <= '` +
etl.hiv_monthly_report_dataset_v1_2 h
LEFT JOIN
etl.flat_hiv_summary_v15b e ON (h.encounter_id = e.encounter_id)
LEFT JOIN
-- etl.flat_hiv_summary_v15b ls ON (ls.next_clinical_datetime_hiv IS NULL
-- AND ls.person_id = e.person_id)
-- LEFT JOIN
-- (SELECT
-- *, MAX(encounter_datetime) AS max_date
-- FROM
-- etl.flat_vitals
-- WHERE
-- encounter_datetime <= '` +
endDate +
`'
GROUP BY
person_id ,
encounter_datetime
ORDER BY
encounter_datetime) fv ON
e.person_id = fv.person_id
INNER JOIN
amrs.person t1 ON
(h.person_id = t1.person_id)
INNER JOIN
amrs.person_name person_name ON
(t1.person_id = person_name.person_id
AND (person_name.voided = 0))
INNER JOIN
amrs.patient_identifier id ON
(t1.person_id = id.patient_id
AND id.voided = 0)
-- INNER JOIN
-- amrs.patient_identifier cc ON
-- (t1.person_id = cc.patient_id
-- and cc.identifier_type in (28)
-- AND cc.voided = 0)
-- LEFT JOIN
-- amrs.patient_identifier ov ON
-- (t1.person_id = ov.patient_id
-- and ov.identifier_type in (43)
-- AND ov.voided = 0)
-- LEFT JOIN
-- amrs.patient_identifier np ON
-- (t1.person_id = np.patient_id
-- and np.identifier_type in (45)
-- AND np.voided = 0)
left join amrs.patient_program p on
(p.patient_id = h.person_id
and p.program_id in (4, 9)
and p.date_completed is null
and p.voided = 0)
LEFT JOIN amrs.obs o ON
(o.encounter_id = h.encounter_id
AND o.person_id = h.person_id and o.concept_id = 9812 and o.voided = 0)
WHERE
-- GROUP BY person_id , encounter_datetime
-- ORDER BY encounter_datetime)
etl.flat_vitals fv ON (e.person_id = fv.person_id)
INNER JOIN
amrs.person t1 ON (h.person_id = t1.person_id)
inner JOIN
etl.flat_patient_identifiers_v1 fpiv ON (t1.person_id = fpiv.patient_id and fpiv.ccc is not null)
LEFT JOIN
amrs.patient_program p ON (p.patient_id = h.person_id
AND p.program_id IN (4 , 9)
AND p.date_completed IS NULL
AND p.voided = 0)
LEFT JOIN
amrs.obs o ON (o.encounter_id = e.encounter_id
AND o.person_id = h.person_id
AND o.concept_id = 9812
AND o.voided = 0 and e.is_clinical_encounter = 1)
WHERE
e.encounter_datetime >= "2022-01-01" AND (e.weight is not null) AND
h.endDate >= '` +
startDate +
`'
@@ -193,8 +167,7 @@ AND h.endDate <= '` +
`
GROUP BY
h.person_id
ORDER BY
RAND() DESC ` +
` +
limitOffSetDefinition;

return new Promise((resolve, reject) => {

0 comments on commit b1e7019

Please sign in to comment.