Closed nupoorkhandelwal closed 2 months ago
The logic is correct, it considers only the latest value.
Time taken to run the report:
Changed the logic for below:
Need to pair on this with @Gojo-Taqi .
Backup queries-
'use strict';
({params, imports}) => {
return params.db.objects('ProgramEncounter')
.filtered(`programEnrolment.individual.voided = false AND programEnrolment.voided = false AND programEnrolment.program.name = 'Adolescent' AND programEnrolment.programExitDateTime = null AND voided = false AND encounterDateTime <> null AND (encounterType.name = 'Annual Visit - Baseline' OR encounterType.name = 'Annual Visit - Endline')`)
.filtered('TRUEPREDICATE sort(programEnrolment.individual.uuid asc , encounterDateTime desc) Distinct(programEnrolment.individual.uuid)')
.filter(enc => enc.getObservationReadableValue('BMI (kg/m²)') <= 14.5 )
.map(enc => enc.programEnrolment.individual)
};
Pre-release stats
Below is the script that was used after referring to the readme
'use strict';
({params, imports}) => {
const isChildUndernourished = (enrolment) => {
const encounter = enrolment.lastFulfilledEncounter('Annual Visit - Baseline', 'Annual Visit - Endline');
if(_.isNil(encounter)) return false;
// const obs = encounter.findObservation("BMI (kg/m²)");
return encounter.findObservation("BMI (kg/m²)").getValue() <= 14.5;
};
return params.db.objects('Individual')
.filtered(`voided = false and SUBQUERY(enrolments, $enrolment,$enrolment.program.name = 'Adolescent' and $enrolment.programExitDateTime = null and $enrolment.voided = false).@count > 0`)
.filter((individual) => _.some(individual.enrolments, enrolment => enrolment.program.name === 'Adolescent' && _.isNil(enrolment.programExitDateTime) && !enrolment.voided && isChildUndernourished(enrolment)))
};
Moving this card back to support ready
Link to the support ticket - https://avni.freshdesk.com/a/tickets/3655
.filter(enc => enc.getObservationReadableValue('BMI (kg/m²)') <= 14.5)
is the one slows down the report as it extracts the observation value and then checks if it's less than 14.5. And we can't do this check at the realm level as observations.valueJSON is of type JSON string, and it's impossible to parse JSON strings directly within Realm queries. So the most optimal script we can do is:
'use strict';
({params, imports}) => {
const bmiObservationUUID = '97e3a847-f41b-42e2-beaf-ad6c22bdc227';
return params.db.objects('ProgramEncounter')
.filtered(`programEnrolment.individual.voided = false AND
programEnrolment.voided = false AND
programEnrolment.program.name = 'Adolescent' AND
programEnrolment.programExitDateTime = null AND
voided = false AND
encounterDateTime <> null AND
(encounterType.name = 'Annual Visit - Baseline' OR
encounterType.name = 'Annual Visit - Endline') AND
ANY observations.concept.uuid = $0`, bmiObservationUUID)
.filtered('TRUEPREDICATE sort(programEnrolment.individual.uuid ASC, encounterDateTime DESC) Distinct(programEnrolment.individual.uuid)')
.filter(enc => enc.getObservationReadableValue('BMI (kg/m²)') <= 14.5)
.map(enc => enc.programEnrolment.individual);
};
I added this
ANY observations.concept.uuid = $0, bmiObservationUUID
because the 'BMI (kg/m²)' field is non-mandatory. So tried to filter out the ones that don't have this field.
This takes about 9258ms, as the earlier one took about 11439ms. It seems a bit better than the earlier.
Report Card | Before Fix | After Fix |
---|---|---|
Adolescents having moderate anaemia | 6331 | 6212 |
Adolescents with severe anaemia | 6223 | 6123 |
Adolescents having sickle cell disease | 299 | 275 |
Adolescents having addiction | 29412 | 321 |
Adolescents exited | 12 | 14 |
Adolescents who dropped out of school | 10822 | 254 |
Adolescents having severe malnutrition | 11439 | 9275 |
Adolescents having chronic sickness | 8468 | 488 |
Adolescents absent due to menstrual disorders | 10486 | 226 |
Active adolescent | 12 | 12 |
Context
Currently, there are two issues with Offline reports cards available for SR -
Update logic for the following cards so that latest value is picked to check the criteria
Performance improvement / Slowness