Esempio n. 1
0
 public CovidCountry(CovidCountry country)
 {
     Id        = country.Id;
     Name      = country.Name;
     Flag      = country.Flag;
     Continent = country.Continent;
 }
Esempio n. 2
0
        private async Task <Covid> GetCountryDataAsync(CovidCountry countryInfo, TimeSpan tokenExpirationHours, DateTime absoluteExpiration)
        {
            var totalsTask         = _diseaseSh.GetCountryTotals(countryInfo.Id);
            var timelineTask       = _diseaseSh.GetCountryTimeSeriesData(countryInfo.Id, _appSettings.Timeline.Limit);
            var regionTask         = CreateRegionsAsync(countryInfo, tokenExpirationHours, absoluteExpiration);
            var newsTask           = GetNewsAsync(countryInfo);
            var travelAdvisoryTask = GetTravelAdvisoryAsync(countryInfo);
            var vaccinesTask       = GetVaccineData();
            var therapeuticsTask   = GetTherapeuticData();

            await Task.WhenAll(totalsTask, timelineTask, regionTask, newsTask, travelAdvisoryTask, vaccinesTask, therapeuticsTask).ConfigureAwait(false);

            var covid = InitializeCovid(countryInfo, await totalsTask.ConfigureAwait(false));

            covid.Timelines = CreateTimeline((await timelineTask.ConfigureAwait(false)).Timeline !);
            covid.Regions   = await regionTask.ConfigureAwait(false);

            covid.News = await newsTask.ConfigureAwait(false);

            covid.TravelAdvisory = await travelAdvisoryTask.ConfigureAwait(false);

            covid.Vaccines = await vaccinesTask.ConfigureAwait(false);

            covid.Therapeutics = await therapeuticsTask.ConfigureAwait(false);

            return(covid);
        }
Esempio n. 3
0
        private async Task <Covid> GetGlobalDataAsync(CovidCountry countryInfo, TimeSpan tokenExpirationHours, DateTime absoluteExpiration)
        {
            var totalsTask       = _diseaseSh.GetGlobalTotals();
            var timelinesTask    = _diseaseSh.GetGlobalTimeSeriesData(_appSettings.Timeline.Limit);
            var continentsTask   = CreateContinentsAsync();
            var regionsTask      = GetTotalsForAllCountriesAsync(tokenExpirationHours, absoluteExpiration);
            var vaccinesTask     = GetVaccineData();
            var therapeuticsTask = GetTherapeuticData();

            await Task.WhenAll(totalsTask, timelinesTask, continentsTask, regionsTask, vaccinesTask, therapeuticsTask).ConfigureAwait(false);

            var totals = await totalsTask.ConfigureAwait(false);

            var covid = InitializeCovid(countryInfo, totals);

            covid.AffectedCountries = totals.AffectedCountries;
            covid.Timelines         = CreateTimeline(await timelinesTask.ConfigureAwait(false));
            covid.Regions           = CreateRegions(await regionsTask.ConfigureAwait(false));
            covid.Continents        = await continentsTask.ConfigureAwait(false);

            covid.Vaccines = await vaccinesTask.ConfigureAwait(false);

            covid.Therapeutics = await therapeuticsTask.ConfigureAwait(false);

            return(covid);
        }
        private void GeTInfoCovid(CountryItemViewModel countryItemViewModel)
        {
            if (Helper.MyRootCovid != null)
            {
                if (Helper.MyRootCovid.Countries.Count != 0)
                {
                    var infocovid = Helper.MyRootCovid.Countries.SingleOrDefault(c => c.CountryCode == countryItemViewModel.Alpha2Code);
                    if (infocovid != null)
                    {
                        CovidCountry = infocovid;
                    }
                    else
                    {
                        CovidCountry.Date = DateTime.Now.Date;
                    }
                }

                if (!Helper.MyRootCovid.Global.Equals(null))
                {
                    CovidGlobal      = Helper.MyRootCovid.Global;
                    CovidGlobal.Date = Helper.MyRootCovid.Date;
                }

                this.HasInfoGovid = true;
            }
            else
            {
                this.HasInfoGovid = false;
            }
        }
 public CountryItemViewModel(INavigationService navigationService)
 {
     _navigationService     = navigationService;
     this.CountryBorders    = new ObservableCollection <CountryItemViewModel>();
     this.CountryCurrencies = new ObservableCollection <Currency>();
     this.CountryLanguages  = new ObservableCollection <Language>();
     this.CovidCountry      = new CovidCountry();
     this.CovidGlobal       = new CovidGlobal();
 }
Esempio n. 6
0
        public async Task InsertAllCountryData(string country)
        {
            List <CovidCountryDTO> covidCountryDbList;

            var date            = new DateTime(2020, 01, 02);
            var casesYesterday  = -1;
            var deathsYesterday = -1;

            do
            {
                var    formatedDate = date.ToString("yyyyMMdd");
                Stream countryData;

                try {
                    countryData = await client.GetStreamAsync($"https://covid19-brazil-api.now.sh/api/report/v1/{country}/{formatedDate}");
                }
                catch (HttpRequestException) {
                    break;
                }

                var countryDTO = await JsonSerializer.DeserializeAsync <CovidContryListDTO>(countryData);

                covidCountryDbList = countryDTO.Data;

                CovidCountry covidDB = new CovidCountry()
                {
                    Country = country.ToLower()
                };

                covidCountryDbList.ForEach(covidCountryDb => {
                    covidDB.Date     = covidCountryDb.Date;
                    covidDB.Cases    = covidDB.Cases != null ? covidDB.Cases + covidCountryDb.Cases : covidCountryDb.Cases;
                    covidDB.Deaths   = covidDB.Deaths != null ? covidDB.Deaths + covidCountryDb.Deaths : covidCountryDb.Deaths;
                    covidDB.Suspects = covidDB.Suspects != null ? covidDB.Suspects + covidCountryDb.Suspects : covidCountryDb.Suspects;
                });

                covidDB.CasesToday  = covidDB.Cases != null && casesYesterday != -1 ? covidDB.Cases - casesYesterday : null;
                covidDB.DeathsToday = covidDB.Deaths != null && deathsYesterday != -1 ? covidDB.Deaths - deathsYesterday : null;

                if (covidDB.Date != null)
                {
                    await _collection.InsertOneAsync(covidDB);
                }

                casesYesterday  = covidDB.Cases != null ? (int)covidDB.Cases : casesYesterday;
                deathsYesterday = covidDB.Deaths != null ? (int)covidDB.Deaths : deathsYesterday;

                date = date.AddDays(1);
            } while (DateTime.Compare(date, DateTime.Now) < 0);
            await UpdateTodayData(country);
        }
Esempio n. 7
0
        public async Task UpdateTodayData(string country)
        {
            Stream covidData;

            try {
                covidData = await client.GetStreamAsync($"https://covid19-brazil-api.now.sh/api/report/v1/{country}");
            }
            catch (Exception) {
                return;
            }
            var covidDataDTO = await JsonSerializer.DeserializeAsync <CovidDataDTO>(covidData);

            CovidCountryDTO covidCountryDb = covidDataDTO.Data;
            CovidCountry    covidDB        = new CovidCountry()
            {
                Cases     = covidCountryDb.Confirmed,
                Deaths    = covidCountryDb.Deaths,
                Recovered = covidCountryDb.Recovered,
                Country   = covidCountryDb.Country.ToLower(),
                UpdatedAt = covidCountryDb.UpdatedAt,
                Date      = DateTime.Today
            };

            if (covidDB.Cases != null)
            {
                var filterBuilder = Builders <CovidCountry> .Filter;
                var today         = DateTime.Today;
                var yesterDay     = today.AddDays(-1);
                var filter        = filterBuilder.Gte(x => x.Date, yesterDay) &
                                    filterBuilder.Lt(x => x.Date, today) &
                                    filterBuilder.Eq(x => x.Country, country);

                var results = await _collection.Find <CovidCountry>(filter).Limit(1).ToListAsync();

                var yesterdayInfo = results.FirstOrDefault();

                covidDB.CasesToday  = yesterdayInfo != null && yesterdayInfo.Cases != null ? covidDB.Cases - yesterdayInfo.Cases : null;
                covidDB.DeathsToday = yesterdayInfo != null && yesterdayInfo.Deaths != null ? covidDB.Deaths - yesterdayInfo.Deaths : null;

                var filterReplace = filterBuilder.Gte(x => x.Date, today) &
                                    filterBuilder.Lte(x => x.Date, DateTime.Now) &
                                    filterBuilder.Eq(x => x.Country, country);

                await _collection.ReplaceOneAsync(filterReplace, covidDB, new ReplaceOptions()
                {
                    IsUpsert = true
                });
            }
        }
Esempio n. 8
0
 private Covid InitializeCovid(CovidCountry countryInfo, IApiTotals data)
 {
     return(new()
     {
         Country = new CovidCountry(countryInfo),
         Population = data.Population,
         Cases = data.Cases,
         Deaths = data.Deaths,
         Recovered = data.Recovered,
         CasesToday = data.TodayCases,
         DeathsToday = data.TodayDeaths,
         RecoveredToday = data.TodayRecovered,
         Tests = data.Tests,
         Critical = data.Critical,
         LastUpdated = DateTimeOffset.FromUnixTimeMilliseconds(data.Updated).LocalDateTime
     });
 }
Esempio n. 9
0
        public async Task <CovidCountry> InsertCountryData(string country, string date)
        {
            var countryData = await client.GetStreamAsync($"https://covid19-brazil-api.now.sh/api/report/v1/{country}/{date}");

            var countryDTO = await JsonSerializer.DeserializeAsync <CovidContryListDTO>(countryData);

            var          covidCountryDbList = countryDTO.Data;
            CovidCountry covidDB            = new CovidCountry();

            covidCountryDbList.ForEach(covidCountryDb => {
                covidDB.Date     = covidCountryDb.Date;
                covidDB.Cases    = covidDB.Cases != null ? covidDB.Cases + covidCountryDb.Cases : covidCountryDb.Cases;
                covidDB.Deaths   = covidDB.Deaths != null ? covidDB.Deaths + covidCountryDb.Deaths : covidCountryDb.Deaths;
                covidDB.Suspects = covidDB.Suspects != null ? covidDB.Suspects + covidCountryDb.Suspects : covidCountryDb.Suspects;
            });
            await _collection.InsertOneAsync(covidDB);

            return(covidDB);
        }
Esempio n. 10
0
        private async Task <CovidNews> GetNewsAsync(CovidCountry country)
        {
            var result = new CovidNews();

            try
            {
                if (!_appSettings.News.NameReplacement.TryGetValue(country.Id, out string?countryName))
                {
                    countryName = country.Name;
                }

                var data = await _coronaTracker.GetNews(countryName, _appSettings.News.DisplayLimit).ConfigureAwait(false);

                result.Items = data.Items.ConvertAll(s => new CovidNewsItem
                {
                    Title       = s.Title.Trim(),
                    Description = fixDescription(s.Description),
                    Url         = s.Url,
                    UrlToImage  = s.UrlToImage,
                    PublishedAt = DateTime.Parse(s.PublishedAt)
                }).Where(w => DateTime.Today.Subtract(w.PublishedAt).TotalDays <= _appSettings.News.NewsDayAgeLimit).ToList();
            }
            catch (Exception ex)
            {
                result.ErrorDetails = ex.ToHtml();
            }

            return(result);

            string fixDescription(string value)
            {
                var newValue = value.Trim();

                if (newValue.Length > _appSettings.News.DescriptionDisplayLimit)
                {
                    return(newValue.Truncate(_appSettings.News.DescriptionDisplayLimit) !);
                }

                return($"{newValue}{(newValue.EndsWith(".") ? "" : "...")}");
            }
        }
Esempio n. 11
0
        private async Task <List <CovidRegion> > CreateRegionsAsync(CovidCountry countryInfo, TimeSpan tokenExpirationHours, DateTime absoluteExpiration)
        {
            if (!_memoryCache.TryGetValue(CacheKey.CountryProvinceTotals, out List <ApiCountryProvinceTotals> data))
            {
                data = await _diseaseSh.GetTotalsForAllCountriesAndTheirProvinces().ConfigureAwait(false);

                _memoryCache.Set(CacheKey.CountryProvinceTotals, data, GetMemoryCacheEntryOptions(tokenExpirationHours, absoluteExpiration, CacheItemPriority.Low));
            }

            if (!_appSettings.Country.TranslateNames.TryGetValue(countryInfo.Name, out string?name))
            {
                name = countryInfo.Name;
            }

            return(data.Where(w => (w.Country == name || w.Country == countryInfo.Id) && !w.Province.IsNullOrEmpty())
                   .Select(s => new CovidRegion
            {
                Name = s.Province,
                Cases = s.Stats.Confirmed,
                Deaths = s.Stats.Deaths,
                Recovered = s.Stats.Recovered,
            }).OrderByDescending(o => o.Cases).ToList());
        }
Esempio n. 12
0
        private async Task <CovidTravelAdvisory> GetTravelAdvisoryAsync(CovidCountry country)
        {
            var result = new CovidTravelAdvisory();

            if (!_appSettings.Maps.ContainsKey(country.Name))
            {
                try
                {
                    var response = await _travelAdvisory.GetTravelAdvisoryAsync(country.Id).ConfigureAwait(false);

                    result.Country    = response.Data.Content.Name;
                    result.Score      = response.Data.Content.Advisory.Score;
                    result.Source     = response.Data.Content.Advisory.Sources_active;
                    result.DetailsUrl = response.Data.Content.Advisory.Source;
                    result.Updated    = DateTime.Parse(response.Data.Content.Advisory.Updated).ToLocalTime();
                }
                catch (Exception ex)
                {
                    result.ErrorDetails = ex.ToHtml();
                }
            }

            return(result);
        }
Esempio n. 13
0
        public async Task <CovidCountry> GetCountry(string country, string date)
        {
            CovidCountry covid = await _service.InsertCountryData(country, date);

            return(covid);
        }
Esempio n. 14
0
        public async Task <IActionResult> IndexAsync()
        {
            CovidCountry country = await _coronavirusCountryService.GetHistoryCountry("CANADA");

            return(View(country));
        }