-
Notifications
You must be signed in to change notification settings - Fork 54
/
build-json-schema.js
255 lines (237 loc) · 6.41 KB
/
build-json-schema.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
// Usage: node build-json-schema.js
const populationTypes = [
'tg',
'msm',
'agyw',
'mhr',
'fsw',
'pwid',
'ow',
'sc',
'pbfw'
];
maleOnlyPopulationTypes = [3, 4];
femaleOnlyPopulationTypes = [2, 5, 9, 7];
// const populationTypes = ['sc', 'pbfw'];
const ageGroups = [
'15_19',
'20_24',
'25_29',
'30_34',
'35_39',
'40_44',
'45_49',
'50_54',
'55_59',
'60_64',
'above_65'
];
const genders = ['male', 'female'];
const subPopulationTypes = ['msm', 'fsw', 'pwid', 'tg'];
checkForCompability = (population_type, gender) => {
if (gender === 'male') {
if (femaleOnlyPopulationTypes.includes(population_type)) {
return -1;
} else {
return 1;
}
} else {
if (maleOnlyPopulationTypes.includes(population_type)) {
return -1;
} else {
return 1;
}
}
};
//const
const generateExpression = (data) => {
if (checkForCompability(data.populationType, data.gender) === -1) {
return `if((pd.person_id is not null), -1, NULL)`;
} else {
if (data.isOldPop) {
return `if((pd.${data.column} = '${
data.value
}' AND (pd.population_type = ${
data.populationType
} OR pd.old_population_type = ${
data.oldPopulationType
} ) AND pd.gender = '${getGender(data.gender)}' AND (pd.age ${
data.ageGroup
})), ${checkForCompability(data.populationType, data.gender)}, NULL)`;
} else if (data.isSubPop) {
return `if((pd.${data.column} = '${
data.value
}' AND (pd.population_type = ${data.populationType} OR ${
data.subPopulationType
}) AND pd.gender = '${getGender(data.gender)}' AND (pd.age ${
data.ageGroup
})), ${checkForCompability(data.populationType, data.gender)}, NULL)`;
} else {
return `if((pd.${data.column} = '${
data.value
}' AND pd.population_type = ${
data.populationType
} AND pd.gender = '${getGender(data.gender)}' AND (pd.age ${
data.ageGroup
})), ${checkForCompability(data.populationType, data.gender)}, NULL)`;
}
}
};
const getPopulationType = (populationType) => {
switch (populationType) {
case 'tg':
return 1;
case 'agyw':
return 2;
case 'msm':
return 3;
case 'mhr':
return 4;
case 'fsw':
return 5;
case 'pwid':
return 6;
case 'ow':
return 7;
case 'sc':
return 8;
case 'pbfw':
return 9;
default:
return 0;
}
};
const getGender = (gender) => {
return gender === 'male' ? 'M' : 'F';
};
const getAgeGroup = (ageGroup) => {
switch (ageGroup) {
case '15_19':
return 'between 15 AND 19';
case '20_24':
return 'between 20 AND 24';
case '25_29':
return 'between 25 AND 29';
case '30_34':
return 'between 30 AND 34';
case '35_39':
return 'between 35 AND 39';
case '40_44':
return 'between 40 AND 44';
case '45_49':
return 'between 45 AND 49';
case '50_54':
return 'between 50 AND 54';
case '55_59':
return 'between 55 AND 59';
case '60_64':
return 'between 60 AND 64';
case 'above_65':
return '> 65';
default:
return 0;
}
};
const getOldPopulationType = (population) => {
if (population === 'mhr' || population === 'ow') {
return 300;
} else if (population === 'sc') {
return 100;
}
};
const getSubPopulationType = (population) => {
///'msm', 'fsw', 'pwid', 'tg'
switch (population) {
case 'msm':
return `pd.sub_population_type = 10 OR pd.sub_population_type = 20`;
case 'fsw':
return `pd.sub_population_type = 30`;
case 'pwid':
return `pd.sub_population_type = 40`;
case 'tg':
return `pd.sub_population_type = 50 OR pd.sub_population_type = 60`;
default:
return ``;
}
};
const generateTotalExpression = (data) => {
if (data.isOldPop) {
return `if((pd.${data.column} = '${data.value}' AND (pd.population_type=${data.populationType} OR pd.old_population_type=${data.oldPopulationType})), 1, NULL)`;
} else if (data.isSubPop) {
return `if((pd.${data.column} = '${data.value}' AND (pd.population_type=${data.populationType} OR ${data.subPopulationType})), 1, NULL)`;
} else {
return `if((pd.${data.column} = '${data.value}' AND pd.population_type = ${data.populationType}), 1, NULL)`;
}
};
/**
* @param {database column} column
* @returns An array of derived columns
*/
const generateColumns = (column, value, alias) => {
let columns = [];
for (let pt of populationTypes) {
for (let ag of ageGroups) {
for (let g of genders) {
columns.push({
type: 'derived_column',
alias: `${alias}_${pt}_${ag}_${g}`,
expressionType: 'simple_expression',
expressionOptions: {
expression: generateExpression({
column: column,
populationType: getPopulationType(pt),
gender: g,
value: value,
ageGroup: getAgeGroup(ag),
isOldPop: pt === 'ow' || pt === 'mhr' || pt === 'sc',
isSubPop: subPopulationTypes.includes(pt),
oldPopulationType: getOldPopulationType(pt),
subPopulationType: getSubPopulationType(pt)
})
}
});
}
}
// Add total column
columns.push({
type: 'derived_column',
alias: `total_${alias}_${pt}`,
expressionType: 'simple_expression',
expressionOptions: {
expression: generateTotalExpression({
isOldPop: pt === 'ow' || pt === 'mhr' || pt === 'sc',
isSubPop: subPopulationTypes.includes(pt),
oldPopulationType: getOldPopulationType(pt),
subPopulationType: getSubPopulationType(pt),
populationType: getPopulationType(pt),
column: column,
value: value
})
}
});
}
return columns;
};
const generateAggregate = (alias) => {
let aggregates = [];
for (let pt of populationTypes) {
for (let ag of ageGroups) {
for (let g of genders) {
aggregates.push({
type: 'simple_column',
alias: `${alias}_${pt}_${ag}_${g}`,
column: `sum(b.${alias}_${pt}_${ag}_${g})`
});
}
}
// Add total column
aggregates.push({
type: 'simple_column',
alias: `total_${alias}_${pt}`,
column: `sum(b.total_${alias}_${pt})`
});
}
return aggregates;
};
const aggsSchema = generateAggregate('edp');
const aggs = JSON.stringify(aggsSchema, null, 2);