public HopkinsModel GetHopkinsModel(string metric) { IHopkinsModelReader reader = _readers.First(o => o.GetType().Name.Contains(metric)); string csv = reader.GetRawModel(); HopkinsModel model = new HopkinsModel(); string dateUpdated = csv.Substring(0, csv.IndexOf(Environment.NewLine)); model.UpdateTime = DateTime.ParseExact(dateUpdated, "s", CultureInfo.InvariantCulture); var options = new CsvOptions { RowsToSkip = 1, }; model.Countries = new List <String>(); model.MapCountryDeaths = new Dictionary <string, List <int> >(); foreach (var line in CsvReader.ReadFromText(csv, options)) { if (model.Dates == null) { model.Dates = new List <DateTime>(); for (int i = FIRST_COL_DATE; i < line.Headers.Length; i++) { DateTime date = DateTime.ParseExact(line.Headers[i], DATE_FORMAT, CultureInfo.InvariantCulture); model.Dates.Add(date); } } string country = line[COUNTRY_STR]; if (!model.MapCountryDeaths.ContainsKey(country)) { model.MapCountryDeaths[country] = new List <int>(); for (int i = FIRST_COL_DATE; i < line.Headers.Length; i++) { model.MapCountryDeaths[country].Add(0); } model.Countries.Add(country); } for (int i = FIRST_COL_DATE; i < line.Headers.Length; i++) { string deaths = line[i]; if (deaths != String.Empty) { model.MapCountryDeaths[country][i - FIRST_COL_DATE] += Int32.Parse(deaths); } } } model.Countries.Sort(); return(model); }
public HopkinsModel GetHopkinsModel(string metric = "") { const string DATE_FORMAT = "dd/M/yyyy"; string json = _reader.GetRawModel(); EcdcModel ecdcModel = JsonConvert.DeserializeObject <EcdcModel>(json); HopkinsModel model = new HopkinsModel(); model.Countries = new List <string>(); model.MapCountryDeaths = new Dictionary <string, List <int> >(); model.MapCountryIsoCode = new Dictionary <string, string>(); foreach (var record in ecdcModel.records) { if (!model.MapCountryDeaths.ContainsKey(record.countriesAndTerritories)) { model.MapCountryDeaths[record.countriesAndTerritories] = new List <int>(); model.Countries.Add(record.countriesAndTerritories); model.MapCountryIsoCode[record.countriesAndTerritories] = record.countryterritoryCode; } model.MapCountryDeaths[record.countriesAndTerritories].Insert(0, Int32.Parse(record.deaths)); } model.Dates = new List <DateTime>(); // @@Bug: not all contries have the same number of dates string FirstCountry = model.Countries.First(); foreach (var record in ecdcModel.records) { if (record.countriesAndTerritories == FirstCountry) { DateTime date = DateTime.ParseExact(record.dateRep, DATE_FORMAT, CultureInfo.InvariantCulture); model.Dates.Insert(0, date); } } foreach (var entry in model.MapCountryDeaths) { for (int i = 1; i < entry.Value.Count; i++) { entry.Value[i] += entry.Value[i - 1]; } } model.UpdateTime = DateTime.Now; return(model); }