import mkdirp from 'mkdirp' import path from 'path' import fsp from 'fs/promises' function normalize(data) { return data.rows.map(row => { const obj = {} row.forEach((col, i) => { obj[data.headers[i]] = col }) return obj }) } function percentChange (current, prev) { const change = current - prev let percent = (change / Math.abs(prev)) * 100 if (isNaN(percent)) return 0 const sign = percent > 0 ? '+' : '-' if (Math.abs(percent) === Infinity) percent = 100 return `${sign}${Math.abs(percent.toFixed(2)).toLocaleString()}` } function replaceVariables (template, variables) { Object.keys(variables).forEach(key => { let value = variables[key] if (typeof value === 'number') value = value.toLocaleString() template = template.split(`{${key}}`).join(value) }) return template } async function readJSON(file) { return JSON.parse(await fsp.readFile(file, 'utf-8')) } async function process () { const template = await fsp.readFile('./data/parser/reddit/template.md', 'utf-8') const data = { tests: normalize(await readJSON('./public/data/overall/testing/by-county/-- All --.json')).reverse(), cases: normalize(await readJSON('./public/data/overall/cases/by-county/-- All --.json')).reverse(), hospitalizations: normalize(await readJSON('./public/data/overall/hospitalizations/by-county/-- All --.json')).reverse(), deaths: normalize(await readJSON('./public/data/overall/deaths/by-county/-- All --.json')).reverse(), vaccinations: normalize(await readJSON('./public/data/overall/vaccinations/by-county/-- All --.json')).reverse(), } function formatRow(data, index) { const prevData = { tests: data.tests[index + 1], cases: data.cases[index + 1], hospitalizations: data.hospitalizations[index + 1], deaths: data.deaths[index + 1], vaccinations: data.vaccinations[index + 1], } const currentData = { tests: data.tests[index], cases: data.cases[index], hospitalizations: data.hospitalizations[index], deaths: data.deaths[index], vaccinations: data.vaccinations[index], } currentData.hospitalizations.total_hospitalizations = data.hospitalizations.reduce((total, r) => { return total + r.hospitalizations }, 0) const date = new Date(`${currentData.tests.report_date} 23:59:59`) const date_short = `${`${(date.getMonth() + 1)}`.padStart(2, '0')}/${`${date.getDate()}`.padStart(2, '0')}` const computedData = { date, date_short, date_long: date.toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' }), current_percent_positive: Math.round(currentData.tests.seven_day_percent_positive), current_tests: 0, // currentData.tests.combined_performed_running_total.toLocaleString(), current_cases: currentData.cases.total_cases.toLocaleString(), current_hospitalizations: currentData.hospitalizations.total_hospitalizations.toLocaleString(), current_deaths: currentData.deaths.total_deaths.toLocaleString(), current_vaccinations: currentData.vaccinations.total_vaccinations.toLocaleString(), current_tests_increase: 0, //(currentData.tests.combined_performed_running_total - prevData.tests.combined_performed_running_total).toLocaleString(), current_cases_increase: currentData.cases.cases.toLocaleString(), current_hospitalizations_increase: currentData.hospitalizations.hospitalizations.toLocaleString(), current_deaths_increase: currentData.deaths.deaths.toLocaleString(), current_vaccinations_increase: currentData.vaccinations.vaccinations.toLocaleString(), current_tests_change_percent: percentChange(currentData.tests.combined_performed, prevData.tests.combined_performed), current_cases_change_percent: percentChange(currentData.cases.cases, prevData.cases.cases), current_hospitalizations_change_percent: percentChange(currentData.hospitalizations.hospitalizations, prevData.hospitalizations.hospitalizations), current_deaths_change_percent: percentChange(currentData.deaths.deaths, prevData.deaths.deaths), current_vaccinations_change_percent: percentChange(currentData.vaccinations.vaccinations, prevData.vaccinations.vaccinations), } return computedData } const results = [] for (let i = 0; i < 30; i++) { results.push(formatRow(data, i)) } const variables = { ...results.at(0), table_rows: results.map(row => { return `| ${[ row.date_short, `${row.current_percent_positive}%`, `${row.current_cases_increase} [${row.current_cases_change_percent}%]`, `${row.current_hospitalizations_increase} [${row.current_hospitalizations_change_percent}%]`, `${row.current_deaths_increase} [${row.current_deaths_change_percent}%]`, `${row.current_vaccinations_increase} [${row.current_vaccinations_change_percent}%]`, ].join(' | ')} |` }).join('\n') } console.log(replaceVariables(template, variables)) } process()