Add deaths chart & new data.
This commit is contained in:
134
data/parser/overall/deaths.js
Normal file
134
data/parser/overall/deaths.js
Normal file
@@ -0,0 +1,134 @@
|
||||
import mkdirp from 'mkdirp'
|
||||
import path from 'path'
|
||||
import fsp from 'fs/promises'
|
||||
import Papa from 'papaparse'
|
||||
import { getCounty } from '../../util.js'
|
||||
|
||||
async function processByCounty({ date, zip }, countyInfo, output) {
|
||||
output = {
|
||||
directory: path.join(output.directory, 'by-county'),
|
||||
file: county => `${county}.json`
|
||||
}
|
||||
|
||||
const csv = await zip.entryData('epicurve_rpt_date.csv').then(res => res.toString())
|
||||
|
||||
const rows = Papa.parse(csv, {
|
||||
header: true
|
||||
}).data
|
||||
|
||||
let deaths_last_14_days = []
|
||||
function computeDeathsLast14Days() {
|
||||
return deaths_last_14_days.reduce((prev, current) => prev + current, 0)
|
||||
}
|
||||
|
||||
const counties = rows.reduce((counties, row) => {
|
||||
const county = getCounty(row.county)
|
||||
const info = countyInfo[county]
|
||||
if (!county) return counties
|
||||
if (!counties[county]) counties[county] = []
|
||||
|
||||
deaths_last_14_days.unshift(+row.deaths)
|
||||
deaths_last_14_days.length = 14
|
||||
|
||||
counties[county].push({
|
||||
report_date: row['report_date'],
|
||||
population: info.population,
|
||||
deaths: parseInt(row.deaths),
|
||||
deaths_per_capita: parseInt(row.deaths) / info.population,
|
||||
deaths_14_days: computeDeathsLast14Days(),
|
||||
death_rate_14_days: computeDeathsLast14Days() / info.population,
|
||||
total_deaths: parseInt(row.death_cum),
|
||||
total_deaths_per_capita: parseInt(row.death_cum) / info.population
|
||||
})
|
||||
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, zip }, countyInfo, output) {
|
||||
output = {
|
||||
directory: output.directory,
|
||||
file: `combined.json`
|
||||
}
|
||||
|
||||
const csv = await zip.entryData('epicurve_rpt_date.csv').then(res => res.toString())
|
||||
|
||||
const rows = Papa.parse(csv, {
|
||||
header: true
|
||||
}).data
|
||||
|
||||
const report_dates = new Set()
|
||||
|
||||
let deaths_last_14_days = []
|
||||
function computeDeathsLast14Days() {
|
||||
return deaths_last_14_days.reduce((prev, current) => prev + current, 0)
|
||||
}
|
||||
|
||||
let oldCounty = null
|
||||
const results = rows.map(row => {
|
||||
const county = getCounty(row.county)
|
||||
if (!county) return null
|
||||
if (['-- All --', '-- Unknown --'].includes(county)) return null
|
||||
|
||||
const info = countyInfo[county]
|
||||
report_dates.add(row['report_date'])
|
||||
|
||||
if (oldCounty !== county) {
|
||||
deaths_last_14_days = []
|
||||
oldCounty = county
|
||||
}
|
||||
deaths_last_14_days.unshift(+row.deaths)
|
||||
deaths_last_14_days.length = 14
|
||||
|
||||
return {
|
||||
county,
|
||||
report_date: row['report_date'],
|
||||
population: info.population,
|
||||
deaths: parseInt(row.deaths),
|
||||
deaths_per_capita: parseInt(row.deaths) / info.population,
|
||||
deaths_14_days: computeDeathsLast14Days(),
|
||||
death_rate_14_days: computeDeathsLast14Days() / info.population,
|
||||
total_deaths: parseInt(row.death_cum),
|
||||
total_deaths_per_capita: parseInt(row.death_cum) / info.population
|
||||
}
|
||||
}).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 processSingleZip (zip, countyInfo) {
|
||||
const output = {
|
||||
directory: `./public/data/state/deaths/`,
|
||||
}
|
||||
|
||||
try {
|
||||
await fsp.rm(output.directory, { recursive: true })
|
||||
} catch (e) {}
|
||||
|
||||
await processByCounty(zip, countyInfo, output)
|
||||
await processCombined(zip, countyInfo, output)
|
||||
}
|
||||
|
||||
function process (zips, countyInfo) {
|
||||
return processSingleZip(zips.at(-1), countyInfo)
|
||||
}
|
||||
|
||||
export default process
|
||||
Reference in New Issue
Block a user