// Returns the difference between the existing total and the new total // (which is the count of new cases). Sets the new total as the current // total. This method needs to be called in date order or the new cases // will not be counted correctly. public Cases GetNewCases(string state, Cases newTotal) { state = state.ToLowerInvariant(); if (currentTotals.ContainsKey(state)) { // If there are already some cases for this state, calculate // the difference betwen the current total and this new total // to get the count of new cases. var current = currentTotals[state]; currentTotals[state] = newTotal; return(current.Difference(newTotal)); } else { // If there is no record for the current state, the number of // new cases is going to be equal to the total (because this // is the state's first time reporting). currentTotals[state] = newTotal; return(newTotal); } }
public DailyStateData(DateTimeOffset date, Cases totalCases, Cases newCases) { Date = date; Total = totalCases; New = newCases; }
public Data FormatAndCalculateData(SortedList <DateTimeOffset, DailyReport> reports) { var runningTotals = new RunningTotals(); var stateSet = new HashSet <string>(); var data = new Data(); // Prepare the dataset and target container for processing. // // Reports don't have data for each state for each day (as some states only // started reporting cases later in the outbreak). Need to do a first pass // of the data to find all of the states available for Australia. foreach (var day in reports) { // Keep a record of all of the dates, this will likely be the // x-axis labels wherever it is graphed. data.Dates.Add(day.Key); foreach (var state in day.Value.Data) { var stateName = state.Key; if (stateSet.Add(stateName)) { // Add a new dataset container for this state, we'll add // data to it in the next pass. var newStateData = new StateData() { DisplayName = state.Value.FormattedStateName }; data.States.Add(stateName, newStateData); } } } // There is a report every day, so we're just starting from the // first day of data and working our way forward. These days need to // be in order or the running totals will not work. foreach (var reportDay in reports) { var reportDayDate = reportDay.Key; var reportDayStates = reportDay.Value; foreach (var stateName in stateSet) { var state = data.States[stateName]; // If there is no data for the state on this day, it will be // recorded as all zeros. This will make sure we have a full // set of data for all states for all days. If there is data // for this day, we'll overwrite this value below. var totalCasesForState = new Cases(); if (reportDayStates.Data.ContainsKey(stateName)) { var reportDayStateData = reportDayStates.Data[stateName]; totalCasesForState = new Cases( reportDayStateData.Confirmed, reportDayStateData.Deaths, reportDayStateData.Recovered ); } var newCasesForState = runningTotals.GetNewCases(stateName, totalCasesForState); var dailyStateData = new DailyStateData(reportDayDate, totalCasesForState, newCasesForState); state.Data.Add(dailyStateData); } } return(data); }
public DailyStateData(DateTimeOffset date) { Date = date; Total = new Cases(); New = new Cases(); }