private Dictionary <string, int> GetCountriesWithTheBiggestChange(Covid19Stats stats, int count) { var countryNames = stats.Locations .GroupBy(p => p.Country) .Select(p => p.Key) .ToList(); var mergedData = countryNames .Select(name => new { Country = name, Data = GetMergedData(stats, name) }) .ToList(); return(mergedData .Select(p => new { Country = p.Country, Change = GetChange(p.Data.First().Value, p.Data) }) .OrderByDescending(p => p.Change) .Take(count) .ToDictionary(p => p.Country, p => p.Change)); }
private async Task <Covid19Stats> GetStatsAsync(CancellationToken cancellationToken) { return(await _memoryCache.GetOrCreateAsync(nameof(Covid19Stats), async entry => { Covid19Stats stats = await _httpClient.GetFromJsonAsync <Covid19Stats>(URL, cancellationToken).ConfigureAwait(false); entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(10); return stats; })); }
private Dictionary <DateTime, int> GetMergedData(Covid19Stats stats, string country = null) { return(stats.Locations .AsQueryable() .WhereIf(country != null, p => p.Country == country) .SelectMany(p => p.History) .GroupBy(p => p.Key) .Select(p => new KeyValuePair <DateTime, int>(p.Key, p.Sum(q => q.Value))) .OrderByDescending(p => p.Key) .ToDictionary(p => p.Key, p => p.Value)); }
private int GetChange(int latest, Covid19Stats stats) { return(latest - stats.Locations .Select(p => p.History .OrderByDescending(q => q.Key) .Skip(1) .First()) .OrderByDescending(p => p.Key) .Sum(p => p.Value)); }
public async Task <Covid19Numbers> GetJakartaNumbersAsync(CancellationToken cancellationToken) { Covid19Stats stats = await GetStatsAsync(cancellationToken).ConfigureAwait(false); return(stats.Regions.Single(region => region.Name == "DKI Jakarta").Numbers); }
public async Task <Covid19Numbers> GetIndonesiaNumbersAsync(CancellationToken cancellationToken) { Covid19Stats stats = await GetStatsAsync(cancellationToken).ConfigureAwait(false); return(stats.Numbers); }