-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
QPPA-1606: Ingest 2018 IA measures (#126)
This PR adds a script for ingesting the 2018 IA measures from the source CSV file. This adds the source CSV file, the script that parses that CSV, the staging IA measures, the changes to the build-measures script, and the changes to the final measures. Also added some simple tests for IA measures (seems like there weren't any in 2017) as well as tests for the ingestion script and updating the merge-measures tests to use IA measures. Reviewers: @kalvinwang @kencheeto
- Loading branch information
1 parent
ec9a3d0
commit 2e7b083
Showing
16 changed files
with
4,631 additions
and
26 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
const parse = require('csv-parse/lib/sync'); | ||
const _ = require('lodash'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
const Constants = require('../../../constants.js'); | ||
/** | ||
* `import-ia-measures` reads an IA CSV file and creates valid measures, | ||
* then writes the resulting json to a staging measures-data-ia.js file. | ||
*/ | ||
|
||
const IA_CSV_COLUMN_NAMES = { | ||
'Activity Name': 'title', | ||
'Activity Description': 'description', | ||
'Activity ID': 'measureId', | ||
'metricType': 'metricType', | ||
'firstPerformanceYear': 'firstPerformanceYear', | ||
'lastPerformanceYear': 'lastPerformanceYear', | ||
'weight': 'weight', | ||
'subcategoryId': 'subcategoryId', | ||
'cehrtEligible': 'cehrtEligible' | ||
}; | ||
|
||
// Accounts for TRUE, True, true, X, x... | ||
// and people sometimes insert extra spaces | ||
function cleanInput(input) { | ||
return input.trim().toLowerCase(); | ||
} | ||
|
||
// map specific csv input values to their representation in the measures schema | ||
function mapInput(rawInput) { | ||
const input = cleanInput(rawInput); | ||
if (input === '' || input === 'null') { | ||
return null; | ||
} else if (input === 'true') { | ||
return true; | ||
} else if (input === 'false') { | ||
return false; | ||
} else if (Constants.validPerformanceYears.includes(Number(input))) { | ||
return Number(input); | ||
} else { | ||
return rawInput.trim(); | ||
} | ||
} | ||
|
||
/** | ||
* [convertIaCsvsToMeasures description] | ||
* @param {array of objects} each object in the array represents | ||
* a new measure (row) | ||
* @return {array} Returns an array of measures objects | ||
* | ||
* Note: We trim all data sourced from CSVs because people sometimes unintentionally | ||
* include spaces or linebreaks | ||
*/ | ||
function convertIaCsvsToMeasures(iaCSVRows) { | ||
return iaCSVRows.map((row) => { | ||
const measure = {}; | ||
_.each(IA_CSV_COLUMN_NAMES, (measureKeyName, columnName) => { | ||
measure[measureKeyName] = mapInput(row[columnName]); | ||
measure['category'] = 'ia'; | ||
}); | ||
return measure; | ||
}); | ||
} | ||
|
||
function importIaMeasures(iaMeasuresPath, outputPath) { | ||
const csv = fs.readFileSync(path.join(__dirname, iaMeasuresPath), 'utf8'); | ||
const iaCSV = parse(csv, {columns: true}); | ||
|
||
const iaMeasures = convertIaCsvsToMeasures(iaCSV); | ||
const iaMeasuresJSON = JSON.stringify(iaMeasures, null, 2); | ||
|
||
fs.writeFileSync(path.join(__dirname, outputPath), iaMeasuresJSON); | ||
} | ||
|
||
const iaMeasuresPath = process.argv[2]; | ||
const outputPath = process.argv[3]; | ||
|
||
importIaMeasures(iaMeasuresPath, outputPath); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.