131 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			131 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
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
 | 
						|
 | 
						|
  const hospitalizations_last_14_days = []
 | 
						|
 | 
						|
  function computeHospitalizationsLast14Days() {
 | 
						|
    return hospitalizations_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] = []
 | 
						|
    hospitalizations_last_14_days.unshift(+row.confirmed_case_hospitalization)
 | 
						|
    hospitalizations_last_14_days.length = 14
 | 
						|
 | 
						|
    counties[county].push({
 | 
						|
      report_date: row['report_date'],
 | 
						|
      population: info.population,
 | 
						|
      hospitalizations: parseInt(row.confirmed_case_hospitalization),
 | 
						|
      hospitalizations_per_capita: parseInt(row.confirmed_case_hospitalization) / info.population,
 | 
						|
      hospitalizations_last_14_days: computeHospitalizationsLast14Days(),
 | 
						|
      hospitalizations_last_14_days_per_capita: computeHospitalizationsLast14Days() / 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 hospitalizations_last_14_days = []
 | 
						|
 | 
						|
  function computeHospitalizationsLast14Days() {
 | 
						|
    return hospitalizations_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]
 | 
						|
 | 
						|
    if (oldCounty !== county) {
 | 
						|
      hospitalizations_last_14_days = []
 | 
						|
      oldCounty = county
 | 
						|
    }
 | 
						|
    hospitalizations_last_14_days.unshift(+row.confirmed_case_hospitalization)
 | 
						|
    hospitalizations_last_14_days.length = 14
 | 
						|
 | 
						|
    report_dates.add(row['report_date'])
 | 
						|
    return {
 | 
						|
      county,
 | 
						|
      report_date: row['report_date'],
 | 
						|
      population: info.population,
 | 
						|
      hospitalizations: parseInt(row.confirmed_case_hospitalization),
 | 
						|
      hospitalizations_per_capita: parseInt(row.confirmed_case_hospitalization) / info.population,
 | 
						|
      hospitalizations_last_14_days: computeHospitalizationsLast14Days(county),
 | 
						|
      hospitalizations_last_14_days_per_capita: computeHospitalizationsLast14Days(county) / 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/overall/hospitalizations/`,
 | 
						|
  }
 | 
						|
 | 
						|
  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
 |