forked from AMPATH/etl-rest-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathetl-factory.js
302 lines (280 loc) · 12.8 KB
/
etl-factory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
"use strict";
var _ = require('underscore');
var walk = require('walk');
//Report Indicators Json Schema Path
var indicatorsSchema = require('./reports/indicators.json');
var reports = [];
//iterate the report folder picking files satisfying regex *report.json
reports.push.apply(reports,require('./reports/hiv-summary-report.json'));
reports.push.apply(reports,require('./reports/moh-731-report.json'));
reports.push.apply(reports,require('./reports/patient-register-report.json'));
//var walker = walk.walk("./reports", []);
//walker.on("file", function (root, fileStats, next) {
// //test file to determine if its report json
// if (fileStats.name.match(".*(-report.json)")) {
// console.log('pushing file schema to reports array>>>>' + fileStats.name);
// reports.push.apply(reports, require('./reports/' + fileStats.name));
// }
// next();
//});
//create an array of reports --->push your report using reports.push(report1,report2,... report(n));
//reports.push(hivSummaryReport);
//etl-factory builds and generates queries dynamically in a generic way using indicator-schema and report-schema json files
module.exports = function () {
return {
buildPatientListExpression: buildPatientListExpression,
buildIndicatorsSchema: buildIndicatorsSchema,
buildIndicatorsSchemaWithSections: buildIndicatorsSchemaWithSections,
singleReportToSql: singleReportToSql,
reportIndicatorToSql: reportIndicatorToSql
};
function buildPatientListExpression(queryParams, successCallback) {
//Check for undefined params
if (queryParams === null || queryParams === undefined) return "";
//Initialize returned obj
var result = {
whereClause: '',
resource: ''
};
var whereClause = '';
//create WhereClause
_.each(indicatorsSchema, function (indicator) {
_.each(queryParams.reportIndicator.split(','), function (indicatorName) {
if (indicator.name === indicatorName) {
if (indicator.expression != '') {
whereClause += '(' + indicator.expression + ') or ';
}
}
});
});
var lastIndex = whereClause.lastIndexOf(' or ');
whereClause = whereClause.substring(0, lastIndex);
if (whereClause !== '')result.whereClause = ' and ' + whereClause;
//identify resource/table
_.each(reports, function (report) {
if (report.name === queryParams.reportName) {
result.resource = report.table['schema'] + '.' + report.table['tableName'];
}
});
successCallback(result);
}
function buildIndicatorsSchema(queryParams, successCallback) {
//Check for undefined params
if (queryParams === null || queryParams === undefined) return "";
var result = [];
//Load json schema into the query builder
_.each(reports, function (report) {
if (report.name === queryParams.reportName) {
_.each(report.indicators, function (reportIndicator) {
_.each(indicatorsSchema, function (indicator) {
if (indicator.name === reportIndicator.expression) {
result.push(indicator);
}
});
});
}
});
successCallback(result);
}
/**
Returns the report json schema,resolved from request parameter reportName
**/
function buildIndicatorsSchemaWithSections(queryParams, successCallback) {
//Check for undefined params
var allReportSections;
if (queryParams === null || queryParams === undefined) return "";
var result = [];
//Load json schema into the query builder
_.each(reports, function (report) {
if (report.name === queryParams.reportName) {
allReportSections = report.sections;
_.each(report.indicators, function (reportIndicator) {
_.each(indicatorsSchema, function (indicator) {
if (indicator.name === reportIndicator.expression) {
//add section infor
try {
if (reportIndicator.section !== undefined) {
var res = {section_key: reportIndicator.section, indicator_key: indicator};
} else {
var res = {section_key: "", indicator_key: indicator};
}
result.push(res);
} catch (e) {
console.log(reportIndicator)
result.push(indicator);
}
}
});
});
}
});
successCallback([result, allReportSections]);
}
//function to create query parts given report name
function createQueryPartsByReportName(reportName, requestParams) {
if (requestParams === null || requestParams === undefined) return "";
var queryParts = {};
_.each(reports, function (report) {
if (report.name === reportName) {
{
queryParts = {
columns:indicatorsToColumns(report, requestParams.countBy, requestParams),
concatColumns:concatColumnsToColumns(report),
table: report.table['schema'] + '.' + report.table['tableName'],
alias: report.table['alias'],
joins: joinsToSql(report.joins),
where: filtersToSql(requestParams.whereParams, report.parameters, report.filters),
group: groupClauseToSql(report.groupClause, requestParams.groupBy, report.parameters),
order: [{column: 't1.location_id', asc: true}],
offset: requestParams.offset,
limit: requestParams.limit
};
}
}
});
return queryParts;
}
function singleReportToSql(requestParams) {
if (requestParams === null || requestParams === undefined) return "";
var queryPartsArray = [];
_.each(reports, function (report) {
if (report.name === requestParams.reportName) {
if (report.reports) {
//its a multi report.Loop through the multi reports
_.each(report.reports, function (subReport) {
queryPartsArray.push(createQueryPartsByReportName(subReport.reportName, requestParams))
})
} else {
var queryParts = {
columns:indicatorsToColumns(report, requestParams.countBy, requestParams),
concatColumns:concatColumnsToColumns(report),
table: report.table['schema'] + '.' + report.table['tableName'],
alias: report.table['alias'],
joins: joinsToSql(report.joins),
where: filtersToSql(requestParams.whereParams, report.parameters, report.filters),
group: groupClauseToSql(report.groupClause, requestParams.groupBy, report.parameters),
order: [{column: 't1.location_id', asc: true}],
offset: requestParams.offset,
limit: requestParams.limit
};
}
queryPartsArray.push(queryParts);
}
});
return queryPartsArray;
}
//converts a set of indicators into sql columns
function indicatorsToColumns(report, countBy, requestParam) {
var result = [];
//converts a set of supplementColumns into sql columns
_.each(supplementColumnsToColumns(report), function (column) {
result.push(column);
});
//converts set of indicators to sql columns
_.each(report.indicators, function (singleIndicator) {
_.each(indicatorsSchema, function (indicator) {
if (requestParam.requestIndicators) {
//compare request partams indicator list corresponds to the singleIndicator
_.each(requestParam.requestIndicators.split(','), function (requestIndicatorName) {
if (indicator.name === requestIndicatorName) {
if (indicator.name === singleIndicator.expression) {
var column = singleIndicator.sql + ' as ' + singleIndicator.label;
column = column.replace('$expression', indicator.expression);
result.push(column);
}
}
});
} else {
if (indicator.name === singleIndicator.expression) {
var column = singleIndicator.sql + ' as ' + indicator.name;
column = column.replace('$expression', indicator.expression);
result.push(column);
}
}
});
});
return result;
}
//converts a set of supplement columns of type single into sql columns
function supplementColumnsToColumns(report) {
var result = [];
_.each(report.supplementColumns, function (supplementColumn) {
if( supplementColumn.type==='single'){
var column = supplementColumn.sql + ' as ' + supplementColumn.label;
result.push(column);
}
});
return result;
}
//converts a set of supplement columns of type multiple into sql columns
function concatColumnsToColumns(report) {
var result = '';
_.each(report.supplementColumns, function (supplementColumn) {
if( supplementColumn.type==='multiple'){
var column = supplementColumn.sql + ' as ' + supplementColumn.label;
result += column;
result += ', ';
}
});
var lastIndex = result.lastIndexOf(',');
result = result.substring(0, lastIndex);
return result;
}
//takes an expression of indicators like "number_with_no_vl / on_arvs" and converts into sql
function reportIndicatorToSql(reportIndicator) {
}
//converts an array of tables into sql
function joinsToSql(joins) {
var result = [];
_.each(joins, function (join) {
var r = [join['schema'] + '.' + join['tableName'], join['alias'], join['joinExpression'], join['joinType']];
result.push(r);
});
return result;
}
//converts an array of group clause into squel consumable
function groupClauseToSql(groupClauses, groupBy, reportParams) {
var result = [];
_.each(groupBy.split(','), function (by) {
_.each(groupClauses, function (groupClause) {
if (groupClause["parameter"] === by) {
_.each(reportParams, function (reportParam) {
if (reportParam["name"] === groupClause["parameter"]) {
_.each(reportParam["defaultValue"], function (value) {
result.push(value["expression"]);
});
}
});
}
});
});
return result;
}
//converts an array of filters into sql
function filtersToSql(whereParams, reportParams, reportFilters) {
var result = [];
var expression = '';
var parameters = [];
_.each(reportFilters, function (reportFilter) {
_.each(whereParams, function (whereParam) {
//checks whether param value is set, if not set the filter is not pushed.
//also checks if report filter parameter passed is eq to where param
if (whereParam["name"] === reportFilter["parameter"] && whereParam["value"]) {
expression += reportFilter["expression"];
expression += ' and ';
_.each(reportParams, function (reportParam) {
if (reportParam["name"] === whereParam["name"]) {
console.log('i am pushing this:',whereParam["value"] );
parameters.push(whereParam["value"]);
}
});
}
});
});
var lastIndex = expression.lastIndexOf('and');
expression = expression.substring(0, lastIndex);
result.push(expression);
result.push.apply(result, parameters);
return result;
}
}();