import mkdirp from 'mkdirp' import path from 'path' import fsp from 'fs/promises' import XLSX from 'xlsx' import { getCounty } from '../../util.js' async function processByCounty({ date, xlsx }, countyInfo, output) { output = { directory: path.join(output.directory, 'by-county'), file: county => `${county}.json` } const rows = XLSX.utils.sheet_to_json(xlsx.Sheets.VAX_ADMIN_BY_DAY_COUNTY) const counties = rows.reduce((counties, row) => { const county = getCounty(row.COUNTY_NAME) const info = countyInfo[county] if (!county) return counties if (!counties[county]) counties[county] = [] counties[county].push({ report_date: row.ADMIN_DATE, population: info.population, vaccinations: row.VAXADMIN, total_vaccinations: row.CUMVAXADMIN, }) return counties }, {}) for (const county in counties) { const data = { segment: { county }, headers: Object.keys(counties[county][0]), rows: counties[county].map(row => Object.values(row)) } await mkdirp(output.directory) await fsp.writeFile(path.join(output.directory, output.file(county)), JSON.stringify(data)) } } async function processCombined({ date, xlsx }, countyInfo, output) { output = { directory: output.directory, file: `combined.json` } const rows = XLSX.utils.sheet_to_json(xlsx.Sheets.VAX_ADMIN_BY_DAY_COUNTY) const report_dates = new Set() const results = rows.map(row => { const county = getCounty(row.COUNTY_NAME) if (!county) return null if (['GEORGIA'].includes(county)) return null const info = countyInfo[county] report_dates.add(row['report_date']) return { county, report_date: row.ADMIN_DATE, population: info.population, vaccinations: row.VAXADMIN, total_vaccinations: row.CUMVAXADMIN, } }).filter(row => !!row) const data = { segment: { report_date: Array.from(report_dates) }, headers: Object.keys(results[0]), rows: results.map(row => Object.values(row)) } await mkdirp(output.directory) await fsp.writeFile(path.join(output.directory, output.file), JSON.stringify(data)) } async function processSingleWorkbook (xlsx, countyInfo) { const output = { directory: `./public/data/overall/vaccinations/`, } try { await fsp.rm(output.directory, { recursive: true }) } catch (e) {} await processByCounty(xlsx, countyInfo, output) await processCombined(xlsx, countyInfo, output) } function process (vaccinationSources, countyInfo) { return processSingleWorkbook(vaccinationSources.at(-1), countyInfo) } export default process