forked from CMSgov/qpp-measures-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
233 lines (197 loc) · 6.93 KB
/
index.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
// Libraries
const fs = require('fs');
const path = require('path');
const YAML = require('yamljs');
const uniq = require('lodash/uniq');
const yearRegEx = /^[0-9]{4}/;
const benchmarkJsonFileRegEx = /^[0-9]{4}\.json$/;
const Constants = require('./constants.js');
/**
* @return {Array<number>}
* An array of all the years that the library has data for
*/
exports.getValidPerformanceYears = function() {
return Constants.validPerformanceYears;
};
/**
*
* @return {void}
* Adds any new program name fields from the mvp.json file for the given performance year
*/
exports.updateProgramNames = function(performanceYear) {
let programNames;
const programNamesFilePath = path.join(__dirname, 'util/program-names', 'program-names.json');
const mvpFilePath = path.join(__dirname, 'mvp', performanceYear.toString(), 'mvp.json');
let mvpData = [];
try {
programNames = JSON.parse(fs.readFileSync(programNamesFilePath));
mvpData = JSON.parse(
fs.readFileSync(mvpFilePath));
mvpData.forEach(mvp => {
if (!programNames[mvp.mvpId]) {
programNames[mvp.mvpId] = mvp.mvpId;
}
});
fs.writeFileSync(programNamesFilePath, JSON.stringify(programNames, null, 2));
} catch (e) {
console.log('Error parsing the program-names.json or mvp.json file: ' + ' --> ' + e);
}
};
/**
*
* @return {{}} - program names -
* An object keyed by program name containing the current program names
*/
exports.getProgramNames = function() {
const programNamesFilePath = path.join(__dirname, 'util/program-names', 'program-names.json');
try {
return JSON.parse(fs.readFileSync(programNamesFilePath));
} catch (e) {
console.log('Error parsing the program-names.json file: ' + ' --> ' + e);
}
};
/**
*
* @return {{}} - benchmarks data -
* An object keyed by performance year with array values
* containing the benchmarks for that performance year
*/
exports.getBenchmarksData = function() {
const benchmarksByYear = {};
fs.readdirSync(path.join(__dirname, 'benchmarks')).forEach(function(file) {
if (benchmarkJsonFileRegEx.test(file)) {
benchmarksByYear[file.match(yearRegEx)[0]] = require(path.join(__dirname, 'benchmarks', file));
}
});
return benchmarksByYear;
};
/**
*
* @return {Array<number>}
* An array of years that the library has benchmarks for
*/
exports.getBenchmarksYears = function() {
const benchmarksYears = new Set();
fs.readdirSync(path.join(__dirname, 'benchmarks')).forEach(function(file) {
if (benchmarkJsonFileRegEx.test(file)) {
benchmarksYears.add(+file.match(yearRegEx)[0]);
}
});
return Array.from(benchmarksYears);
};
/**
* @return {{}} - Object representation of the Benchmarks Schema
*/
exports.getBenchmarksSchema = function(performanceYear = Constants.currentPerformanceYear) {
return YAML.load(path.join(__dirname, 'benchmarks', performanceYear.toString(), 'benchmarks-schema.yaml'));
};
/**
* @return {Array<Measure>}
*/
exports.getMeasuresData = function(performanceYear = 2017) {
return JSON.parse(
fs.readFileSync(path.join(__dirname, 'measures', performanceYear.toString(), 'measures-data.json')));
};
/**
* @return {{}} - Object representation of the Measures Schema
*/
exports.getMeasuresSchema = function(performanceYear = 2017) {
return YAML.load(path.join(__dirname, 'measures', performanceYear.toString(), 'measures-schema.yaml'));
};
/**
* @return {Array<ClinicalCluster>}
*/
exports.getClinicalClusterData = function(performanceYear = 2017) {
let clusterData = [];
try {
clusterData = JSON.parse(
fs.readFileSync(path.join(__dirname, 'clinical-clusters', performanceYear.toString(), 'clinical-clusters.json')));
} catch (e) {
console.log('QPP measures data not found for year: ' + performanceYear + ' --> ' + e);
}
return clusterData;
};
/**
* @return {{}} - Object representation of the Clinical Cluster Schema
*/
exports.getClinicalClusterSchema = function(performanceYear = 2017) {
return YAML.load(path.join(__dirname, 'clinical-clusters', performanceYear.toString(), 'clinical-clusters-schema.yaml'));
};
/**
* @return {Array<MVP>}
*/
exports.getMVPData = function(performanceYear = 2023, mvpIds = []) {
const filePath = path.join(__dirname, 'mvp', performanceYear.toString(), 'mvp-enriched.json');
let mvpData;
if (fs.existsSync(filePath)) {
mvpData = JSON.parse(fs.readFileSync(filePath));
} else {
mvpData = this.createMVPDataFile(performanceYear, mvpIds);
}
if (mvpIds.length) {
mvpData = mvpData.filter(mvpDataItem => mvpIds.includes(mvpDataItem.mvpId));
}
return mvpData;
};
/**
* @return {Array<MVP>}
*/
exports.createMVPDataFile = function(performanceYear) {
const mvpFilePath = path.join(__dirname, 'mvp', performanceYear.toString(), 'mvp-enriched.json');
const measureFilePath = path.join(__dirname, 'measures', performanceYear.toString(), 'measures-data.json');
let mvpData = [];
let measuresData = [];
try {
mvpData = JSON.parse(
fs.readFileSync(path.join(__dirname, 'mvp', performanceYear.toString(), 'mvp.json')));
measuresData = this.getMeasuresData(performanceYear);
} catch (e) {
console.log('QPP mvp / measures data not found for year: ' + performanceYear + ' --> ' + e);
}
const mvpIds = [];
mvpData.forEach(mvpDataItem => {
if (mvpDataItem.mvpId !== 'app1') {
mvpIds.push(mvpDataItem.mvpId);
}
});
// Reset the allowedPrograms to remove any MVP ID program names (we will add them back on line 173)
// This allows for removal of a measure from an MVP data item
measuresData.forEach(measure => {
measure.allowedPrograms = measure.allowedPrograms ? measure.allowedPrograms.filter(program => !mvpIds.includes(program)) : [];
});
// Hydrate measures
mvpData.forEach(mvp => {
Constants.mvpMeasuresHelper.forEach(item => {
mvp[item.enrichedMeasureKey] = [];
this.populateMeasuresforMVPs(mvp, mvpData, measuresData, item.measureIdKey, item.enrichedMeasureKey);
});
});
mvpData.forEach(mvp => {
Constants.mvpMeasuresHelper.forEach(item => {
delete mvp[item.measureIdKey];
});
});
fs.writeFileSync(mvpFilePath, JSON.stringify(mvpData, null, 2));
fs.writeFileSync(measureFilePath, JSON.stringify(measuresData, null, 2));
return mvpData;
};
exports.populateMeasuresforMVPs = function(mvpDataItem, mvpDataArray, measuresData, measureIdKey, enrichedMeasureKey) {
mvpDataItem[measureIdKey].forEach(measureId => {
const measure = measuresData.find(m => m.measureId === measureId);
if (measure) {
mvpDataArray.forEach(m => {
if (m[measureIdKey].includes(measureId)) {
measure.allowedPrograms.push(m.mvpId);
}
});
measure.allowedPrograms = uniq(measure.allowedPrograms);
mvpDataItem[enrichedMeasureKey].push(measure);
}
});
};
/**
* @return {{}} - Object representation of the MVP Schema
*/
exports.getMVPSchema = function(performanceYear = 2023) {
return YAML.load(path.join(__dirname, 'mvp', performanceYear.toString(), 'mvp-schema.yaml'));
};