Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor data processing, standardize data keys/tables/charts #106

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/chartConfig/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ export const ontarioStatusCharts = [
icu,
];

export const phuStatusCharts = [
activeCases,
totalCases,
newCases,
totalDeaths,
newDeaths,
];

export const vaccineCharts = [
dailyDoses,
totalDoses,
Expand Down
2 changes: 1 addition & 1 deletion src/chartConfig/ontario/hospitalized.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const hospitalized = {
title: 'Patients hospitalized',
dataKeyX: 'date_string',
bars: [{
dataKey: 'Number of patients hospitalized with COVID-19',
dataKey: 'total_hospital',
name: 'Patients hospitalized',
fill: '#ef8c8c',
}],
Expand Down
2 changes: 1 addition & 1 deletion src/chartConfig/ontario/icu.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const icu = {
title: 'Patients in ICU',
dataKeyX: 'date_string',
bars: [{
dataKey: 'Number of patients in ICU on a ventilator due to COVID-19',
dataKey: 'total_vent',
fill: '#509ee3',
name: 'ICU (with ventilator)',
stackId: 'a'
Expand Down
2 changes: 1 addition & 1 deletion src/chartConfig/ontario/newCases.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const newCases = {
fill: '#f9d45c',
}],
lines: [{
dataKey: 'new_cases_rolling_average',
dataKey: 'avg_new_cases',
name: '7 day rolling average',
stroke: 'black',
strokeWidth: 2,
Expand Down
2 changes: 1 addition & 1 deletion src/chartConfig/ontario/newDeaths.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const newDeaths = {
fill: '#ef8c8c',
}],
lines: [{
dataKey: 'new_deaths_rolling_average',
dataKey: 'avg_new_deaths',
name: '7 day rolling average',
stroke: 'black',
strokeWidth: 2,
Expand Down
4 changes: 2 additions & 2 deletions src/chartConfig/ontario/positiveRate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ const positiveRate = {
title: 'Percent positive',
dataKeyX: 'date_string',
bars: [{
dataKey: 'percent_positive',
dataKey: 'new_percent_pos',
name: 'Percent positive',
fill: '#509ee3',
}],
lines: [ {
dataKey: 'tests_positive_rolling_average',
dataKey: 'avg_percent_pos',
name: '7 day rolling average',
stroke: 'black',
strokeWidth: 2,
Expand Down
6 changes: 3 additions & 3 deletions src/chartConfig/ontario/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ const tests = {
title: 'New tests',
dataKeyX: 'date_string',
bars: [{
dataKey: 'Total tests completed in the last day',
dataKey: 'new_tests',
name: 'New tests',
fill: '#509ee3',
}],
lines: [{
dataKey: 'Under Investigation',
dataKey: 'under_investigation',
name: 'Pending tests',
dot: false,
strokeWidth: 2,
stroke: 'teal',
}, {
dataKey: 'tests_rolling_average',
dataKey: 'avg_new_tests',
name: '7 day rolling average',
stroke: 'black',
strokeWidth: 2,
Expand Down
4 changes: 2 additions & 2 deletions src/chartConfig/ontario/totalCases.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ const totalCases = {
title: 'Total cases',
dataKeyX: 'date_string',
areas: [{
dataKey: 'vocsTotal',
dataKey: 'total_voc',
fill: '#ef8c8c',
stroke: '#ef8c8c',
name: 'Variants of concern',
stackId: 'a',
}, {
dataKey: 'totalNonVOC',
dataKey: 'total_non_voc',
fill: '#f9d45c',
stroke: '#f9d45c',
name: 'Vanilla cases',
Expand Down
2 changes: 1 addition & 1 deletion src/chartConfig/ontario/totalDeaths.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const totalDeaths = {
title: 'Total deaths',
dataKeyX: 'date_string',
areas: [{
dataKey: 'Deaths',
dataKey: 'total_deaths',
name: 'Total deaths',
fill: '#ef8c8c',
}],
Expand Down
13 changes: 5 additions & 8 deletions src/components/OntarioStatusTable.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
const hospitalField = 'Number of patients hospitalized with COVID-19';
const icuField = 'Number of patients in ICU due to COVID-19';

import DataTable from './DataTable';

const formatNumber = val => {
Expand Down Expand Up @@ -29,7 +26,7 @@ const columns = [{
highlight: 'negative',
}, {
label: 'Total cases',
key: 'Total Cases',
key: 'total_cases',
formatValue: formatNumber,
align: 'right',
headerColSpan: 2,
Expand All @@ -40,7 +37,7 @@ const columns = [{
highlight: 'negative',
}, {
label: 'Deaths',
key: 'Deaths',
key: 'total_deaths',
formatValue: formatNumber,
align: 'right',
headerColSpan: 2,
Expand All @@ -51,7 +48,7 @@ const columns = [{
highlight: 'negative',
}, {
label: 'Patients hospitalized',
key: hospitalField,
key: 'total_hospital',
formatValue: formatNumber,
align: 'right',
headerColSpan: 2,
Expand All @@ -62,7 +59,7 @@ const columns = [{
highlight: true,
}, {
label: 'Patients in ICU',
key: icuField,
key: 'total_icu',
formatValue: formatNumber,
align: 'right',
headerColSpan: 2,
Expand All @@ -73,7 +70,7 @@ const columns = [{
highlight: 'negative',
}, {
label: 'Variants of concern cases',
key: 'vocsTotal',
key: 'total_voc',
formatValue: formatNumber,
}];

Expand Down
24 changes: 20 additions & 4 deletions src/components/PHUStatusTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,37 @@ const PHUStatusTable = ({ dataSource = [], phuName }) => {
key: 'date_string',
}, {
label: 'Active cases',
key: 'ACTIVE_CASES',
key: 'active_cases',
formatValue: formatNumber,
align: 'right',
headerColSpan: 2,
}, {
label: 'New Cases',
key: 'new_cases',
label: '',
key: 'new_active_cases',
formatValue: formatDelta,
highlight: 'negative',
}, {
label: 'Total cases',
key: 'total_cases',
formatValue: formatNumber,
align: 'right',
headerColSpan: 2,
}, {
label: '',
key: 'new_cases',
formatValue: formatDelta,
highlight: 'negative',
}, {
label: 'Deaths',
key: 'DEATHS',
key: 'total_deaths',
formatValue: formatNumber,
align: 'right',
headerColSpan: 2,
}, {
label: '',
key: 'new_deaths',
formatValue: formatDelta,
highlight: 'negative',
}];

return (
Expand Down
70 changes: 3 additions & 67 deletions src/data/getOntarioStatuses.js
Original file line number Diff line number Diff line change
@@ -1,76 +1,12 @@
import jsonpFetch from './jsonpFetch';
import { processOntarioRecords } from './utils';

const dataUrl = 'https://data.ontario.ca/api/3/action/datastore_search?resource_id=ed270bb8-340b-41f9-a7c6-e8ef587e6d11&limit=1000';
const hospitalField = 'Number of patients hospitalized with COVID-19';
const icuField = 'Number of patients in ICU due to COVID-19';

const getOntarioStatuses = () => {
return new Promise((resolve) => {
jsonpFetch(dataUrl, function(data) {
const records = data.result.records;
records.sort((a,b) => new Date(a['Reported Date']) - new Date(b['Reported Date']));

let cases_last7days = [0, 0, 0, 0, 0, 0, 0];
let deaths_last7days = [0, 0, 0, 0, 0, 0, 0];
let tests_last7days = [0, 0, 0, 0, 0, 0, 0];
let percentTestsPositive_last7days = [0, 0, 0, 0, 0, 0, 0];
let yesterdayCases = 0;
let yesterdayDeaths = 0;
let yesterdayActiveCases = 0;
let yesterdayResolvedCases = 0;
let yesterdayHospital = 0;
let yesterdayIcu = 0;

records.map(record => {
record.new_cases = record['Total Cases'] - yesterdayCases;
record.new_deaths = Math.max(record['Deaths'] - yesterdayDeaths, 0);
record.icu_no_ventilator = record[icuField] - record['Number of patients in ICU on a ventilator due to COVID-19'];
record.date_string = new Date(record['Reported Date']).toLocaleString('en-us', {
month: 'short',
day: 'numeric',
});
record.active_cases = record['Total Cases'] - record['Resolved'] - record['Deaths'];
record.new_active_cases = record.active_cases - yesterdayActiveCases;
record.new_resolved = record['Resolved'] - yesterdayResolvedCases;
record.new_hospital = record[hospitalField] - yesterdayHospital;
record.new_icu = record[icuField] - yesterdayIcu;
record.percent_positive = ((record['new_cases'] / record['Total tests completed in the last day']) * 100).toFixed(2);
if (record.percent_positive > 100) {
record.percent_positive = 0;
} else if (isNaN(record.percent_positive)) {
record.percent_positive = 0;
}

percentTestsPositive_last7days.shift();
percentTestsPositive_last7days.push(Number(record.percent_positive || 0));
record.tests_positive_rolling_average = (percentTestsPositive_last7days.reduce((total, cases) => cases + total, 0) / 7.0).toFixed(2);

tests_last7days.shift();
tests_last7days.push(record['Total tests completed in the last day'] || 0);
record.tests_rolling_average = Math.round(tests_last7days.reduce((total, cases) => cases + total, 0) / 7.0);

cases_last7days.shift();
cases_last7days.push(record.new_cases);
const total_cases_last7days = cases_last7days.reduce((total, cases) => cases + total, 0);
record.new_cases_rolling_average = Math.round(total_cases_last7days / 7);

deaths_last7days.shift();
deaths_last7days.push(record.new_deaths);
const total_deaths_last7days = deaths_last7days.reduce((total, cases) => cases + total, 0);
record.new_deaths_rolling_average = Math.round(total_deaths_last7days / 7);

yesterdayIcu = record[icuField];
yesterdayHospital = record[hospitalField];
yesterdayCases = record['Total Cases'];
yesterdayDeaths = record['Deaths'];
yesterdayResolvedCases = record['Resolved'];
yesterdayActiveCases = record['active_cases'];

record.vocsTotal = record['Total_Lineage_B.1.1.7'] + record['Total_Lineage_B.1.351'] + record['Total_Lineage_P.1'];
record.totalNonVOC = record['Total Cases'] - record.vocsTotal;
return record;
});
resolve(records);
jsonpFetch(dataUrl, ({ result }) => {
resolve(processOntarioRecords(result.records));
});
});
};
Expand Down
32 changes: 6 additions & 26 deletions src/data/getPHUData.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import jsonpFetch from './jsonpFetch';
import { processPHURecords } from './utils';

const dataUrl = 'https://data.ontario.ca/en/api/3/action/datastore_search?resource_id=d1bfe1ad-6575-4352-8302-09ca81f7ddfc&limit=10000';

Expand All @@ -9,32 +10,11 @@ const getPHUdata = (phuName = '')=> {

return new Promise((resolve) => {
jsonpFetch(url, ({ result }) => {
const rawRecords = result.records;
rawRecords.sort((a, b) => new Date(a.FILE_DATE) - new Date(b.FILE_DATE));
const deDupedRecords = rawRecords.filter((record, index, array) => (
index === array.findIndex(other => record.FILE_DATE === other.FILE_DATE)
));

let yesterdayTotalCases = 0;
let cases_last7days = [0, 0, 0, 0, 0, 0, 0];
const records = deDupedRecords.map(record => {
record.total_cases = record.ACTIVE_CASES + record.DEATHS + record.RESOLVED_CASES;
record.new_cases = Math.max(0, record.total_cases - yesterdayTotalCases);
record.date_string = new Date(record.FILE_DATE).toLocaleString('en-us', {
month: 'short',
day: 'numeric',
});

cases_last7days.shift();
cases_last7days.push(record.new_cases);
const total_cases_last7days = cases_last7days.reduce((total, cases) => cases + total, 0);
record.new_cases_rolling_average = Math.round(total_cases_last7days / 7);

yesterdayTotalCases = record['total_cases'];
return record;
});

resolve(records);
resolve(processPHURecords(result.records.filter(
(record, index, array) => (
index === array.findIndex(other => record.FILE_DATE === other.FILE_DATE)
)
)));
});
});
};
Expand Down
Loading