示例#1
0
        public void DownloadCovid(MyFileChoice source)
        {
            string url;

            if (source == MyFileChoice.CurrentDeaths)
            {
                url = "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv";
            }
            else
            {
                url = "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv";
            }

            try {
                var request  = WebRequest.Create(url) as HttpWebRequest;
                var response = request.GetResponse() as HttpWebResponse;
                var status   = response.StatusCode; //returns OK if a response is received
                success = true;
                output  = status.ToString();

                // The using statement also closes the StreamReader
                using (StreamReader stream = new StreamReader(response.GetResponseStream())) {
                    ReadSingleValues(stream, source);
                }
            } catch (WebException ex) {
                output = ex.Message;
                return;
            }
        }
示例#2
0
        public void TestDownloadCovid_GetCurrentDate(MyFileChoice source)
        {
            //arrange
            LoadData loader = new LoadData();

            //act
            loader.DownloadCovid(source);
            DateTime lastLoggedDate = loader.dates.Last();
            DateTime todayDate      = DateTime.Now;

            Debug.WriteLine(lastLoggedDate);
            Debug.WriteLine(todayDate);

            TimeSpan date_span = (todayDate - lastLoggedDate).Duration();

            // Assert
            //return true if the last logged day is the current day +/- 2 days
            bool result = date_span.Days < 2 ? true : false;

            Assert.IsTrue(result, "the date differs by more than 2 days");
        }
示例#3
0
        public void ReadSingleValues(StreamReader str, MyFileChoice source)
        {
            //TODO : refactor code to download the dates only once
            //reference list of 0s
            List <int> tmp_timeSeries = new List <int> {
            };

            using (CsvReader csv = new CsvReader(str, CultureInfo.InvariantCulture)) {
                //1. read header to collect the dates
                csv.Read();
                csv.ReadHeader();
                CultureInfo culture = new CultureInfo("en-US");
                culture.Calendar.TwoDigitYearMax = 2099;
                string[]      headerRow   = csv.Context.HeaderRecord;
                List <string> headerDates = new List <string> {
                };
                foreach (string item in headerRow)
                {
                    if (DateTime.TryParse(item, culture,
                                          DateTimeStyles.None,
                                          out DateTime date))
                    {
                        dates.Add(date);
                        headerDates.Add(item);
                        tmp_timeSeries.Add(1);
                        l_output.Add(item);
                    }
                }

                //2. add missing data to the dictCountry = the last column of the table
                //last column points to the current day

                //change where the data is sent depending on the file

                if (source == MyFileChoice.CurrentDeaths)
                {
                    //initialise the time series for each country
                    foreach (string country in dictCountry.Keys)
                    {
                        dictCountry[country].timeSeries = new List <int>(tmp_timeSeries);
                    }

                    while (csv.Read())
                    {
                        string value       = csv.GetField(headerRow.Last());
                        string readCountry = csv.GetField("Country/Region");
                        if (dictCountry.ContainsKey(readCountry))
                        {
                            //sum all values corresponding to the same country - last value
                            dictCountry[readCountry].CurrentDeaths += Convert.ToInt32(value);

                            //sum all values corresponding to the same country - time series
                            for (int i = 0; i < headerDates.Count; i++)
                            {
                                dictCountry[readCountry].timeSeries[i] += Int32.Parse(csv.GetField(headerDates[i]));
                            }
                        }
                    }
                }
                else //confirmedCases
                {
                    while (csv.Read())
                    {
                        string values    = csv.GetField(headerRow.Last());
                        string str_count = csv.GetField("Country/Region");
                        if (dictCountry.ContainsKey(str_count))
                        {
                            //sum all values corresponding to the same country
                            dictCountry[str_count].CurrentConfirmedCases += Convert.ToInt32(values);
                        }
                    }
                }
            }
        }