Skip to content

Commit

Permalink
Merge pull request #1281 from AMPATH/ml-weekly-predictions
Browse files Browse the repository at this point in the history
ML weekly predictions API endpoint for patient list
  • Loading branch information
corneliouzbett authored Jul 20, 2023
2 parents a4f3a04 + 295bb0c commit 99dff0f
Show file tree
Hide file tree
Showing 8 changed files with 579 additions and 0 deletions.
12 changes: 12 additions & 0 deletions app/reporting-framework/base-mysql.report.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ import * as hiv_cervical_cancer_monthly_summary_lesions_pcf_aggregate from './js
import * as defaulter_list_aggregate from './json-reports/defaulter-list-aggregate.json';
import * as defaulter_list_base from './json-reports/defaulter-list-base.json';

// ML Weekly Predictions
import * as ml_weekly_predictions_aggregate from './json-reports/ml-predictions/ml-weekly-predictions-aggregate.json';
import * as ml_weekly_predictions_base from './json-reports/ml-predictions/ml-weekly-predictions-base.json';

//clinic clow report
import * as clinic_flow_provider_statistics_aggregate from './json-reports/clinic-flow-provider-statistics-aggregate.json';
import * as clinic_flow_provider_statistics_base from './json-reports/clinic-flow-provider-statistics-base.json';
Expand Down Expand Up @@ -1272,6 +1276,14 @@ export class BaseMysqlReport {
defaulterListBase: this.cloneJsonSchema(defaulter_list_base)
});
break;
case 'mlWeeklyPredictionsAggregate':
resolve({
main: this.cloneJsonSchema(ml_weekly_predictions_aggregate),
mlWeeklyPredictionsBase: this.cloneJsonSchema(
ml_weekly_predictions_base
)
});
break;
case 'clinicFlowProviderStatisticsAggregate':
resolve({
main: this.cloneJsonSchema(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"name": "mlWeeklyPredictionsAggregate",
"version": "1.0",
"tag": "",
"description": "",
"uses": [
{
"name": "mlWeeklyPredictionsBase",
"version": "1.0",
"type": "dataset_def"
}
],
"sources": [
{
"dataSet": "mlWeeklyPredictionsBase",
"alias": "t2"
}
],
"columns": [
{
"type": "simple_column",
"alias": "person_id",
"column": "t2.person_id"
},
{
"type": "simple_column",
"alias": "total_predictions",
"column": "count(distinct t2.person_id)"
}
],
"groupBy": {
"groupParam": "groupByParam",
"columns": ["person_id"],
"excludeParam": "excludeParam"
},
"dynamicJsonQueryGenerationDirectives": {
"patientListGenerator": {
"useTemplate": "patient-list-template",
"useTemplateVersion": "1.0",
"generatingDirectives": {
"joinDirectives": {
"joinType": "INNER",
"joinCondition": "<<base_column>> = <<template_column>>",
"baseColumn": "person_id",
"templateColumn": "person_id"
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
{
"name": "mlWeeklyPredictionsBase",
"version": "1.0",
"tag": "",
"description": "",
"uses": [],
"sources": [
{
"table": "predictions.ml_weekly_predictions",
"alias": "ml"
},
{
"table": "etl.flat_hiv_summary_v15b",
"alias": "de",
"join": {
"type": "LEFT",
"joinCondition": "de.encounter_id = ml.encounter_id"
}
},
{
"table": "etl.flat_patient_identifiers_v1",
"alias": "fi",
"join": {
"type": "LEFT",
"joinCondition": "de.person_id = fi.patient_id"
}
},
{
"table": "etl.program_visit_map",
"alias": "pm",
"join": {
"type": "LEFT",
"joinCondition": "de.visit_type = pm.visit_type_id"
}
},
{
"table": "amrs.program",
"alias": "p",
"join": {
"type": "LEFT",
"joinCondition": "p.program_id = pm.program_type_id"
}
},
{
"table": "etl.pre_appointment_summary",
"alias": "pre",
"join": {
"type": "LEFT",
"joinCondition": "pre.person_id = ml.person_id"
}
}
],
"columns": [
{
"type": "simple_column",
"alias": "person_id",
"column": "ml.person_id"
},
{
"type": "simple_column",
"alias": "ccc_number",
"column": "fi.ccc"
},
{
"type": "simple_column",
"alias": "ovcid_id",
"column": "fi.ovcid"
},
{
"type": "simple_column",
"alias": "upi_number",
"column": "fi.nupi"
},
{
"type": "simple_column",
"alias": "program",
"column": "p.name"
},
{
"type": "simple_column",
"alias": "predicted_risk",
"column": "ml.predicted_risk"
},
{
"type": "simple_column",
"alias": "week",
"column": "ml.week"
},
{
"type": "simple_column",
"alias": "predicted_prob_disengage",
"column": "ml.predicted_prob_disengage"
},
{
"type": "simple_column",
"alias": "prediction_generated_date",
"column": "DATE_FORMAT(ml.prediction_generated_date,'%Y-%m-%d')"
},
{
"type": "simple_column",
"alias": "rtc_date",
"column": "DATE_FORMAT(ml.rtc_date,'%Y-%m-%d')"
},
{
"type": "simple_column",
"alias": "follow_up_type",
"column": "pre.follow_up_type"
},
{
"type": "simple_column",
"alias": "follow_up_reason",
"column": "pre.follow_up_reason"
},
{
"type": "simple_column",
"alias": "rescheduled_date",
"column": "pre.rescheduled_date"
},
{
"type": "simple_column",
"alias": "reschedule_appointment",
"column": "pre.reschedule_appointment"
},
{
"type": "simple_column",
"alias": "contact_reached",
"column": "pre.contact_reached_phone_follow_up"
},
{
"type": "simple_column",
"alias": "attempted_home_visit",
"column": "pre.attempted_home_visit"
},
{
"type": "simple_column",
"alias": "reason_not_attempted_home_visit",
"column": "pre.reason_not_attempted_home_visit"
},
{
"type": "simple_column",
"alias": "was_client_found",
"column": "pre.was_client_found"
},
{
"type": "simple_column",
"alias": "reason_client_not_found",
"column": "pre.reason_client_not_found"
},
{
"type": "simple_column",
"alias": "home_visit_personnel",
"column": "pre.home_visit_personnel"
},
{
"type": "derived_column",
"alias": "was_follow_up_successful",
"expressionType": "simple_expression",
"expressionOptions": {
"expression": "if((pre.is_successful_phone_follow_up = 'YES' OR (pre.attempted_home_visit = 'YES' AND pre.was_client_found = 'YES')), 1, 0)"
}
},
{
"type": "simple_column",
"alias": "comments",
"column": "pre.comments"
}
],
"filters": {
"conditionJoinOperator": "and",
"conditions": [
{
"filterType": "tableColumns",
"conditionExpression": "ml.location_id in (?)",
"parameterName": "locations"
},
{
"filterType": "tableColumns",
"conditionExpression": "ml.week = ?",
"parameterName": "yearWeek"
},
{
"filterType": "tableColumns",
"conditionExpression": "(if((pre.is_successful_phone_follow_up = 'YES' OR (pre.attempted_home_visit = 'YES' AND pre.was_client_found = 'YES')), 1, 0)) = ?",
"parameterName": "successfulOutcome"
},
{
"filterType": "tableColumns",
"conditionExpression": "(if((pre.is_successful_phone_follow_up = 'NO' OR (pre.attempted_home_visit = 'YES' AND pre.was_client_found = 'NO')), 1, 0)) = ?",
"parameterName": "failedOutcome"
},
{
"filterType": "tableColumns",
"conditionExpression": "ml.predicted_risk is not null"
}
]
},
"orderBy": {
"orderByParam": "orderByParam",
"columns": [
{
"column": "ml.predicted_prob_disengage",
"order": "desc"
},
{
"column": "ml.prediction_generated_date",
"order": "desc"
},
{
"column": "pre.encounter_datetime",
"order": "desc"
}
]
}
}
11 changes: 11 additions & 0 deletions app/routes/ml-weekly-predictions.route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const preRequest = require('../../pre-request-processing');
const etlHelpers = require('../../etl-helpers');
const Boom = require('boom');

const {
MlWeeklyPredictionsService
} = require('../../service/ml-weekly-predictions.service');

const routes = [];

exports.routes = (server) => server.route(routes);
81 changes: 81 additions & 0 deletions etl-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ import { DefaulterListService } from './service/defaulter-list-service';
import { ClinicFlowService } from './service/clinic-flow-service';
import { getPatientCovidVaccinationStatus } from './service/covid-19/covid-19-vaccination-summary';
import { Covid19MonthlyReport } from './service/covid-19/covid-19-monthly-report';
import { MlWeeklyPredictionsService } from './service/ml-weekly-predictions.service';
import { getPatientPredictedScore } from './service/predictions/ml-prediction-service';

module.exports = (function () {
var routes = [
Expand Down Expand Up @@ -6211,6 +6213,85 @@ module.exports = (function () {
'Returns patient list of vaccinated i.e fully,partial and not vaccinated',
tags: ['api']
}
},
{
method: 'GET',
path: '/etl/ml-weekly-predictions',
config: {
auth: 'simple',
handler: function (request, reply) {
if (request.query.locationUuids) {
request.query.reportName = 'defaulter-list';
preRequest.resolveLocationIdsToLocationUuids(request, function () {
let requestParams = Object.assign(
{},
request.query,
request.params
);

const mlWeeklyPredictionsService = new MlWeeklyPredictionsService();

mlWeeklyPredictionsService
.getPatientListReport(requestParams)
.then((results) => {
_.each(results.result, (item) => {
item.cur_meds = etlHelpers.getARVNames(item.cur_meds);
});
reply(results);
})
.catch((err) => {
reply(Boom.internal('An error occured', err));
});
});
}
},
plugins: {
hapiAuthorization: {
role: privileges.canViewClinicDashBoard
},
openmrsLocationAuthorizer: {
locationParameter: [
{
type: 'query',
name: 'locationUuids' //name of the location parameter
}
]
}
},
description: "Get a location's ml weekly predictions list.",
notes: "Returns a location's ml weekly predictions list.",
tags: ['api'],
validate: {
options: {
allowUnknown: true
}
}
}
},
{
method: 'GET',
path: '/etl/predicted-score',
config: {
auth: 'simple',
plugins: {},
handler: function (request, reply) {
if (request.query.patientUuid) {
const patientUuid = request.query.patientUuid;
getPatientPredictedScore(patientUuid)
.then((results) => {
reply(results);
})
.catch((error) => {
reply(Boom.internal('An error occured', error));
});
} else {
reply(Boom.internal('Request misssing patient uuid'));
}
},
description: 'Patient predicted score of missing appointment.',
notes: 'Returns the patients predictions data.',
tags: ['api']
}
}
];

Expand Down
Loading

0 comments on commit 99dff0f

Please sign in to comment.