Beispiel #1
0
        public GitHubEntityData GetData(params object[] args)
        {
            GitHubEntityData gitHubCountriesData = new GitHubEntityData();

            try
            {
                foreach (var githubDataSrc in _GitHubSources)
                {
                    HttpRequestManager httpRequestManager = new HttpRequestManager(githubDataSrc.Url);
                    githubDataSrc.RawData = httpRequestManager.Get(githubDataSrc.Url);
                }

                GitHubTimeSeriesCSVParses csvParser = new GitHubTimeSeriesCSVParses();
                gitHubCountriesData = csvParser.ParseData(_DataSource);
            }
            catch (Exception e)
            {
                throw e;
            }

            return(gitHubCountriesData);
        }
        public GitHubEntityData ParseData(GitHubDataSources sourceData)
        {
            GitHubEntityData gitHubCountriesData = new GitHubEntityData();

            //Get data from sources to parse
            string confirmedData = sourceData.Sources.Where(s => s.Id == GitHubSeriesType.Confirmed.ToString()).FirstOrDefault().RawData;
            string deathsData    = sourceData.Sources.Where(s => s.Id == GitHubSeriesType.Deaths.ToString()).FirstOrDefault().RawData;
            string recoveredData = sourceData.Sources.Where(s => s.Id == GitHubSeriesType.Recovered.ToString()).FirstOrDefault().RawData;

            //Get All the rows
            string[] countriesConfirmedRows = confirmedData.Split("\n");
            string[] countriesDeathsRows    = deathsData.Split("\n");
            string[] countriesRecoveredRows = recoveredData.Split("\n");

            string[] headerCols = countriesConfirmedRows[0].Split(",");

            //for each row, starting with 1, 0 stands for the header
            for (int i = 1; i < countriesConfirmedRows.Length - 1; i++)
            {
                string[] countriesConfirmedCols = GitHubTimeSeriesCSVParses.CleanUp(countriesConfirmedRows[i]).Split(",");

                Country country;
                if (gitHubCountriesData.CountryList.Where(c => c.Name == countriesConfirmedCols[1].Trim().ToUpper()) != null && gitHubCountriesData.CountryList.Where(c => c.Name == countriesConfirmedCols[1].Trim().ToUpper()).Count() > 0)
                {
                    country = gitHubCountriesData.CountryList.Where(c => c.Name == countriesConfirmedCols[1].Trim().ToUpper()).FirstOrDefault();
                }
                else
                {
                    gitHubCountriesData.CountryList.Add(
                        country = new Country()
                    {
                        Name      = countriesConfirmedCols[1].Trim().ToUpper(),
                        Latitude  = Convert.ToDouble(countriesConfirmedCols[2].Trim()),
                        Longitude = Convert.ToDouble(countriesConfirmedCols[3].Trim())
                    }
                        );
                }

                Region region = new Region();
                region.Latitude  = Convert.ToDouble(countriesConfirmedCols[2].Trim());
                region.Longitude = Convert.ToDouble(countriesConfirmedCols[3].Trim());

                if (string.IsNullOrEmpty(countriesConfirmedCols[0].Trim()))
                {
                    region.Name = countriesConfirmedCols[1].Trim().ToUpper();
                }
                else
                {
                    region.Name = countriesConfirmedCols[0].Trim().ToUpper();
                }

                country.Regions = country.Regions ?? new List <Region>();
                country.Regions.Add(region);


                string[] countriesDeathsCols = null;


                foreach (var deathsRow in countriesDeathsRows)
                {
                    if (!string.IsNullOrEmpty(deathsRow.Trim()))
                    {
                        string[] cols    = GitHubTimeSeriesCSVParses.CleanUp(deathsRow).Split(",");
                        string   ctrName = cols[1].Trim().ToUpper();
                        string   regName = cols[0].ToUpper().Trim() == string.Empty ? ctrName : cols[0].ToUpper().Trim();
                        if (regName + ctrName == region.Name + country.Name)
                        {
                            countriesDeathsCols = cols;
                            break;
                        }
                    }
                }

                string[] countriesRecoveredCols = null;
                foreach (var recoveredRow in countriesRecoveredRows)
                {
                    if (!string.IsNullOrEmpty(recoveredRow.Trim()))
                    {
                        string[] cols    = GitHubTimeSeriesCSVParses.CleanUp(recoveredRow).Split(",");
                        string   ctrName = cols[1].Trim().ToUpper();
                        string   regName = cols[0].ToUpper().Trim() == string.Empty ? ctrName : cols[0].ToUpper().Trim();

                        if (regName + ctrName == region.Name + country.Name)
                        {
                            countriesRecoveredCols = cols;
                            break;
                        }
                    }
                }


                //for each column date; Format: MM/DD/YYYY
                for (int j = 4; j < countriesConfirmedCols.Length - 1; j++)
                {
                    if (Convert.ToInt64(countriesConfirmedCols[j].Trim()) > 0)
                    {
                        DateTime dt = DateTime.Parse($"{headerCols[j].Split("/")[1]}/{DateConverter.ConvertMonthToString(headerCols[j].Split("/")[0])}/{headerCols[j].Split("/")[2]}");

                        DailyUpdate dailyUpdate = new DailyUpdate()
                        {
                            Date        = dt,
                            TotalCases  = Convert.ToInt64(countriesConfirmedCols[j].Trim()),
                            TotalDeaths = countriesDeathsCols != null?Convert.ToInt64(countriesDeathsCols[j].Trim()) : 0,
                                              TotalRecovered = countriesRecoveredCols != null?Convert.ToInt64(countriesRecoveredCols[j].Trim()) : 0
                        };

                        dailyUpdate.ActiveCases = dailyUpdate.TotalCases - (dailyUpdate.TotalRecovered + dailyUpdate.TotalDeaths);

                        try
                        {
                            DailyUpdate dailyUpdateYesterday = gitHubCountriesData.CountryList.Where(c => c.Name == country.Name).FirstOrDefault().Regions.Where(r => r.Name == region.Name).FirstOrDefault().DailyUpdates.Where(d => d.Date.Date == dailyUpdate.Date.AddDays(-1).Date).FirstOrDefault();
                            if (dailyUpdateYesterday != null)
                            {
                                dailyUpdate.NewCases  = dailyUpdate.TotalCases - dailyUpdateYesterday.TotalCases;
                                dailyUpdate.NewDeaths = dailyUpdate.TotalDeaths - dailyUpdateYesterday.TotalDeaths;
                            }
                        }
                        catch (Exception)
                        {
                        }


                        region.DailyUpdates = region.DailyUpdates ?? new List <DailyUpdate>();
                        region.DailyUpdates.Add(dailyUpdate);
                    }
                }
            }

            return(gitHubCountriesData);
        }