-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement loading and calculating the vary of country-statistics
- Loading branch information
Showing
18 changed files
with
352 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/Covid19Api.Presentation/Response/CountryVaryStatisticContainerDto.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Covid19Api.Presentation.Response | ||
{ | ||
public record CountryVaryStatisticContainerDto(DateTime Time, IEnumerable<CountryVaryStatisticDto> Vary); | ||
} |
4 changes: 4 additions & 0 deletions
4
src/Covid19Api.Presentation/Response/CountryVaryStatisticDto.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
namespace Covid19Api.Presentation.Response | ||
{ | ||
public record CountryVaryStatisticDto(string ValueType, double? Vary, int? ValueYesterday, int ValueToday); | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Covid19Api.Services.Abstractions/Calculators/ICountryVaryStatisticsCalculator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System.Collections.Generic; | ||
using Covid19Api.Domain; | ||
using Covid19Api.Presentation.Response; | ||
|
||
namespace Covid19Api.Services.Abstractions.Calculators | ||
{ | ||
public interface ICountryVaryStatisticsCalculator | ||
{ | ||
IEnumerable<CountryVaryStatisticContainerDto> Calculate(IEnumerable<CountryStatistic> countryStatistics); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
src/Covid19Api.Services/Calculators/CountryVaryStatisticsCalculator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Covid19Api.Domain; | ||
using Covid19Api.Presentation.Response; | ||
using Covid19Api.Services.Abstractions.Calculators; | ||
|
||
namespace Covid19Api.Services.Calculators | ||
{ | ||
public class CountryVaryStatisticsCalculator : ICountryVaryStatisticsCalculator | ||
{ | ||
public IEnumerable<CountryVaryStatisticContainerDto> Calculate(IEnumerable<CountryStatistic> countryStatistics) | ||
{ | ||
var countryStatisticsByFetchedAt = countryStatistics | ||
.OrderBy(statistic => statistic.FetchedAt) | ||
.GroupBy(statistic => statistic.FetchedAt) | ||
.ToList(); | ||
|
||
for (var i = 0; i < countryStatisticsByFetchedAt.Count; i++) | ||
{ | ||
var currentGroup = countryStatisticsByFetchedAt.ElementAt(i); | ||
|
||
if (i == 0) | ||
{ | ||
yield return CreateEmpty(currentGroup.Key.Date, currentGroup.ToList()); | ||
continue; | ||
} | ||
|
||
var previousGroup = countryStatisticsByFetchedAt.ElementAt(i - 1); | ||
|
||
yield return new CountryVaryStatisticContainerDto(currentGroup.Key.Date, Calculate(currentGroup.ToList(), previousGroup.ToList())); | ||
} | ||
} | ||
|
||
private static IEnumerable<CountryVaryStatisticDto> Calculate(IReadOnlyList<CountryStatistic> current, | ||
IReadOnlyList<CountryStatistic> previous) | ||
{ | ||
var currentTotalSum = current.Sum(c => c.TotalCases); | ||
var previousTotalSum = previous.Sum(c => c.TotalCases); | ||
yield return new CountryVaryStatisticDto(ValueKeys.Total, | ||
CalculateVary(currentTotalSum, previousTotalSum), previousTotalSum, currentTotalSum); | ||
|
||
var currentNewSum = current.Sum(c => c.NewCases); | ||
var previousNewSum = previous.Sum(c => c.NewCases); | ||
yield return new CountryVaryStatisticDto(ValueKeys.New, | ||
CalculateVary(currentNewSum, previousNewSum), previousNewSum, currentNewSum); | ||
|
||
var currentActiveSum = current.Sum(c => c.ActiveCases); | ||
var previousActiveSum = previous.Sum(c => c.ActiveCases); | ||
yield return new CountryVaryStatisticDto(ValueKeys.Active, | ||
CalculateVary(currentActiveSum, previousActiveSum), previousActiveSum, currentActiveSum); | ||
|
||
var currentDeathsSum = current.Sum(c => c.TotalDeaths); | ||
var previousDeathsSum = previous.Sum(c => c.TotalDeaths); | ||
yield return new CountryVaryStatisticDto(ValueKeys.Deaths, | ||
CalculateVary(currentDeathsSum, previousDeathsSum), previousDeathsSum, currentDeathsSum); | ||
|
||
var currentNewDeathsSum = current.Sum(c => c.NewDeaths); | ||
var previousNewDeathsSum = previous.Sum(c => c.NewDeaths); | ||
yield return new CountryVaryStatisticDto(ValueKeys.NewDeaths, | ||
CalculateVary(currentNewDeathsSum, previousNewDeathsSum), previousNewDeathsSum, currentNewDeathsSum); | ||
|
||
var currentRecoveredSum = current.Sum(c => c.RecoveredCases); | ||
var previousRecoveredSum = previous.Sum(c => c.RecoveredCases); | ||
yield return new CountryVaryStatisticDto(ValueKeys.Recovered, | ||
CalculateVary(currentRecoveredSum, previousRecoveredSum), previousRecoveredSum, currentRecoveredSum); | ||
} | ||
|
||
private static double CalculateVary(int current, int previous) | ||
{ | ||
return current > previous | ||
? CalculateIncrease(current, previous) | ||
: CalculateDecrease(current, previous); | ||
} | ||
|
||
private static double CalculateIncrease(int current, int previous) | ||
{ | ||
var difference = current - previous; | ||
return difference / (double) current * 100; | ||
} | ||
|
||
private static double CalculateDecrease(int current, int previous) | ||
{ | ||
var difference = previous - current; | ||
var decrease = difference / (double) previous * 100; | ||
|
||
return decrease * -1; | ||
} | ||
|
||
private static CountryVaryStatisticContainerDto CreateEmpty(DateTime fetchedAt, | ||
IReadOnlyList<CountryStatistic> currentStatistics) | ||
{ | ||
return new(fetchedAt, new List<CountryVaryStatisticDto> | ||
{ | ||
new(ValueKeys.Total, null, null, currentStatistics.Sum(c => c.TotalCases)), | ||
new(ValueKeys.New, null, null, currentStatistics.Sum(c => c.NewCases)), | ||
new(ValueKeys.Active, null, null, currentStatistics.Sum(c => c.ActiveCases)), | ||
new(ValueKeys.Total, null, null, currentStatistics.Sum(c => c.TotalDeaths)), | ||
new(ValueKeys.NewDeaths, null, null, currentStatistics.Sum(c => c.NewDeaths)), | ||
new(ValueKeys.Recovered, null, null, currentStatistics.Sum(c => c.RecoveredCases)), | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System.Runtime.CompilerServices; | ||
|
||
[assembly: InternalsVisibleTo("Covid19Api.Services.Tests")] | ||
namespace Covid19Api.Services.Calculators | ||
{ | ||
internal static class ValueKeys | ||
{ | ||
public const string Total = "Total"; | ||
public const string New = "New"; | ||
public const string Active = "Active"; | ||
public const string Deaths = "Deaths"; | ||
public const string NewDeaths = "NewDeaths"; | ||
public const string Recovered = "Recovered"; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...eCases.Abstractions/Queries/CountryStatistics/CalculateVaryForCountriesStatisticsQuery.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Covid19Api.Presentation.Response; | ||
using Covid19Api.UseCases.Abstractions.Base; | ||
using Covid19Api.UseCases.Abstractions.Models; | ||
using MediatR; | ||
|
||
namespace Covid19Api.UseCases.Abstractions.Queries.CountryStatistics | ||
{ | ||
public record CalculateVaryForCountriesStatisticsQuery(DateTime MinFetchedAt) : ICacheableRequest, IRequest<IEnumerable<CountryVaryStatisticContainerDto>> | ||
{ | ||
public CacheConfiguration GetCacheConfiguration() => new(nameof(CalculateVaryForCountriesStatisticsQuery), TimeSpan.FromMinutes(30)); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...UseCases.Abstractions/Queries/CountryStatistics/CalculateVaryForCountryStatisticsQuery.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Covid19Api.Presentation.Response; | ||
using Covid19Api.UseCases.Abstractions.Base; | ||
using Covid19Api.UseCases.Abstractions.Models; | ||
using MediatR; | ||
|
||
namespace Covid19Api.UseCases.Abstractions.Queries.CountryStatistics | ||
{ | ||
public record CalculateVaryForCountryStatisticsQuery(string Country, DateTime MinFetchedAt) : ICacheableRequest, IRequest<IEnumerable<CountryVaryStatisticContainerDto>> | ||
{ | ||
public CacheConfiguration GetCacheConfiguration() => new(nameof(CalculateVaryForCountryStatisticsQuery), TimeSpan.FromMinutes(30)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...Api.UseCases/Queries/CountryStatistics/CalculateVaryForCountriesStatisticsQueryHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Covid19Api.Presentation.Response; | ||
using Covid19Api.Repositories.Abstractions; | ||
using Covid19Api.Services.Abstractions.Calculators; | ||
using Covid19Api.UseCases.Abstractions.Queries.CountryStatistics; | ||
using MediatR; | ||
|
||
namespace Covid19Api.UseCases.Queries.CountryStatistics | ||
{ | ||
public class CalculateVaryForCountriesStatisticsQueryHandler : IRequestHandler<CalculateVaryForCountriesStatisticsQuery, IEnumerable<CountryVaryStatisticContainerDto>> | ||
{ | ||
private readonly ICountryStatisticsReadRepository countryStatisticsReadRepository; | ||
private readonly ICountryVaryStatisticsCalculator countryVaryStatisticsCalculator; | ||
|
||
public CalculateVaryForCountriesStatisticsQueryHandler(ICountryStatisticsReadRepository countryStatisticsReadRepository, ICountryVaryStatisticsCalculator countryVaryStatisticsCalculator) | ||
{ | ||
this.countryStatisticsReadRepository = countryStatisticsReadRepository; | ||
this.countryVaryStatisticsCalculator = countryVaryStatisticsCalculator; | ||
} | ||
|
||
public async Task<IEnumerable<CountryVaryStatisticContainerDto>> Handle(CalculateVaryForCountriesStatisticsQuery request, CancellationToken cancellationToken) | ||
{ | ||
var historicalCountriesStatistics = await this.countryStatisticsReadRepository.HistoricalAsync(request.MinFetchedAt); | ||
|
||
return countryVaryStatisticsCalculator.Calculate(historicalCountriesStatistics); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...19Api.UseCases/Queries/CountryStatistics/CalculateVaryForCountryStatisticsQueryHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Covid19Api.Presentation.Response; | ||
using Covid19Api.Repositories.Abstractions; | ||
using Covid19Api.Services.Abstractions.Calculators; | ||
using Covid19Api.UseCases.Abstractions.Queries.CountryStatistics; | ||
using MediatR; | ||
|
||
namespace Covid19Api.UseCases.Queries.CountryStatistics | ||
{ | ||
public class CalculateVaryForCountryStatisticsQueryHandler : IRequestHandler<CalculateVaryForCountryStatisticsQuery, IEnumerable<CountryVaryStatisticContainerDto>> | ||
{ | ||
private readonly ICountryStatisticsReadRepository countryStatisticsReadRepository; | ||
private readonly ICountryVaryStatisticsCalculator countryVaryStatisticsCalculator; | ||
|
||
public CalculateVaryForCountryStatisticsQueryHandler(ICountryStatisticsReadRepository countryStatisticsReadRepository, ICountryVaryStatisticsCalculator countryVaryStatisticsCalculator) | ||
{ | ||
this.countryStatisticsReadRepository = countryStatisticsReadRepository; | ||
this.countryVaryStatisticsCalculator = countryVaryStatisticsCalculator; | ||
} | ||
|
||
public async Task<IEnumerable<CountryVaryStatisticContainerDto>> Handle(CalculateVaryForCountryStatisticsQuery request, CancellationToken cancellationToken) | ||
{ | ||
var historicalCountriesStatistics = await this.countryStatisticsReadRepository.HistoricalAsync(request.MinFetchedAt, request.Country); | ||
|
||
return this.countryVaryStatisticsCalculator.Calculate(historicalCountriesStatistics); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.