-
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: add country-statistics aggregates
- Loading branch information
Showing
23 changed files
with
555 additions
and
13 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
src/Covid19Api.AutoMapper/CountryStatisticsAggregateProfile.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,18 @@ | ||
using AutoMapper; | ||
using Covid19Api.Domain; | ||
using Covid19Api.Presentation.Response; | ||
|
||
namespace Covid19Api.AutoMapper | ||
{ | ||
public class CountryStatisticsAggregateProfile : Profile | ||
{ | ||
public CountryStatisticsAggregateProfile() | ||
{ | ||
this.CreateMap<CountryStatisticsAggregate, CountryStatisticsAggregateDto>() | ||
.ConvertUsing(source => new CountryStatisticsAggregateDto(source.Id, source.Country, source.CountryCode, | ||
source.Total, source.New, source.Deaths, source.NewDeaths, source.Recovered, source.Active, | ||
source.Month, | ||
source.Year)); | ||
} | ||
} | ||
} |
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
9 changes: 9 additions & 0 deletions
9
src/Covid19Api.Mongo.Migrator/Configuration/CountryAggregatesStartConfiguration.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,9 @@ | ||
namespace Covid19Api.Mongo.Migrator.Configuration | ||
{ | ||
public class CountryAggregatesStartConfiguration | ||
{ | ||
public int Month { get; set; } = 9; | ||
|
||
public int Year { get; set; } = 2020; | ||
} | ||
} |
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
71 changes: 71 additions & 0 deletions
71
src/Covid19Api.Mongo.Migrator/Migrations/000001_CountryAggregatesMigration.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,71 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Covid19Api.Mongo.Migrator.Abstractions; | ||
using Covid19Api.Mongo.Migrator.Configuration; | ||
using Covid19Api.UseCases.Abstractions.Commands; | ||
using Covid19Api.UseCases.Abstractions.Queries; | ||
using MediatR; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Covid19Api.Mongo.Migrator.Migrations | ||
{ | ||
// ReSharper disable once UnusedType.Global | ||
public class CountryAggregatesMigration : DatabaseMigration | ||
{ | ||
private readonly CountryAggregatesStartConfiguration options; | ||
private readonly IMediator mediator; | ||
|
||
// ReSharper disable once SuggestBaseTypeForParameter | ||
public CountryAggregatesMigration(ILogger<CountryAggregatesMigration> logger, | ||
IOptions<CountryAggregatesStartConfiguration> options, IMediator mediator) : base(logger) | ||
{ | ||
this.mediator = mediator; | ||
this.options = options?.Value ?? throw new ArgumentNullException(nameof(options)); | ||
} | ||
|
||
public override int Number => 1; | ||
protected override string Name => nameof(CountryAggregatesMigration); | ||
|
||
protected override async Task ExecuteAsync() | ||
{ | ||
var next = new DateTime(this.options.Year, this.options.Month, 1, 0, 0, 0, DateTimeKind.Utc); | ||
var end = DateTime.UtcNow.Date; | ||
|
||
while (true) | ||
{ | ||
if (next.Month > end.Month && next.Year >= end.Year) | ||
break; | ||
|
||
var loadCountriesStatisticsQuery = new LoadLatestCountriesStatisticsQuery(); | ||
var countries = (await this.mediator.Send(loadCountriesStatisticsQuery)) | ||
.Select(country => country.Country).ToList(); | ||
|
||
foreach (var country in countries.ToList()) | ||
{ | ||
var query = new LoadCountryStatisticsAggregate(country, next.Month, next.Year); | ||
var aggregate = await this.mediator.Send(query); | ||
|
||
await Task.Delay(50); | ||
if (aggregate is null) continue; | ||
|
||
countries.Remove(country); | ||
} | ||
|
||
if (!countries.Any()) | ||
{ | ||
next = next.AddMonths(1); | ||
await Task.Delay(100); | ||
continue; | ||
} | ||
|
||
var command = new AggregateCountryStatisticsCommand(countries.ToArray(), next.Month, next.Year); | ||
await this.mediator.Send(command); | ||
|
||
next = next.AddMonths(1); | ||
await Task.Delay(100); | ||
} | ||
} | ||
} | ||
} |
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
92 changes: 92 additions & 0 deletions
92
src/Covid19Api.Mongo.Scaffolder/Updates/000003_CountryStatisticAggregateUpdateDefinition.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,92 @@ | ||
using System.Threading.Tasks; | ||
using Covid19Api.Domain; | ||
using Covid19Api.Mongo.Scaffolder.Abstractions; | ||
using Covid19Api.Mongo.Scaffolder.Extensions; | ||
using Microsoft.Extensions.Logging; | ||
using MongoDB.Driver; | ||
|
||
namespace Covid19Api.Mongo.Scaffolder.Updates | ||
{ | ||
// ReSharper disable once UnusedType.Global | ||
public class CountryStatisticAggregateUpdateDefinition : DatabaseUpdateDefinition | ||
{ | ||
private readonly Covid19ApiDbContext databaseContext; | ||
|
||
// ReSharper disable once SuggestBaseTypeForParameter | ||
public CountryStatisticAggregateUpdateDefinition(ILogger<CountryStatisticAggregateUpdateDefinition> logger, | ||
Covid19ApiDbContext databaseContext) : base(logger) | ||
{ | ||
this.databaseContext = databaseContext; | ||
} | ||
|
||
public override int Version => 3; | ||
|
||
protected override async Task ExecuteAsync() | ||
{ | ||
await this.databaseContext.Database.CreateCollectionIfNotExistsAsync(CollectionNames | ||
.CountryStatisticsAggregates); | ||
|
||
var collection = | ||
this.databaseContext.Database.GetCollection<CountryStatisticsAggregate>(CollectionNames | ||
.CountryStatisticsAggregates); | ||
|
||
var countryIndex = Builders<CountryStatisticsAggregate> | ||
.IndexKeys | ||
.Ascending(statistics => statistics.Country); | ||
|
||
var countryIndexModel = new CreateIndexModel<CountryStatisticsAggregate>(countryIndex, new CreateIndexOptions | ||
{ | ||
Name = $"{CollectionNames.CountryStatisticsAggregates}_country" | ||
}); | ||
|
||
await collection.Indexes.CreateOneAsync(countryIndexModel); | ||
|
||
var monthIndex = Builders<CountryStatisticsAggregate> | ||
.IndexKeys | ||
.Descending(statistics => statistics.Month); | ||
|
||
var monthIndexModel = new CreateIndexModel<CountryStatisticsAggregate>(monthIndex, new CreateIndexOptions | ||
{ | ||
Name = $"{CollectionNames.CountryStatisticsAggregates}_month_descending" | ||
}); | ||
|
||
await collection.Indexes.CreateOneAsync(monthIndexModel); | ||
|
||
var yearIndex = Builders<CountryStatisticsAggregate> | ||
.IndexKeys | ||
.Descending(statistics => statistics.Year); | ||
|
||
var yearIndexModel = new CreateIndexModel<CountryStatisticsAggregate>(yearIndex, new CreateIndexOptions | ||
{ | ||
Name = $"{CollectionNames.CountryStatisticsAggregates}_year_descending" | ||
}); | ||
|
||
await collection.Indexes.CreateOneAsync(yearIndexModel); | ||
|
||
var yearMonthIndex = Builders<CountryStatisticsAggregate> | ||
.IndexKeys | ||
.Combine(yearIndex, monthIndex); | ||
|
||
var yearMonthIndexModel = new CreateIndexModel<CountryStatisticsAggregate>(yearMonthIndex, | ||
new CreateIndexOptions | ||
{ | ||
Name = $"{CollectionNames.CountryStatisticsAggregates}_year_month", | ||
}); | ||
|
||
await collection.Indexes.CreateOneAsync(yearMonthIndexModel); | ||
|
||
var countryYearMonthIndex = Builders<CountryStatisticsAggregate> | ||
.IndexKeys | ||
.Combine(countryIndex, yearIndex, monthIndex); | ||
|
||
var countryYearMonthIndexModel = new CreateIndexModel<CountryStatisticsAggregate>(countryYearMonthIndex, | ||
new CreateIndexOptions | ||
{ | ||
Name = $"{CollectionNames.CountryStatisticsAggregates}_country_year_month", | ||
Unique = true | ||
}); | ||
|
||
await collection.Indexes.CreateOneAsync(countryYearMonthIndexModel); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"Serilog": { | ||
"MinimumLevel": "Information", | ||
"MinimumLevel": "Debug", | ||
"WriteTo": [ | ||
{ | ||
"Name": "Console", | ||
|
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
37 changes: 37 additions & 0 deletions
37
src/Covid19Api.Presentation/Response/CountryStatisticsAggregateDto.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,37 @@ | ||
using System; | ||
|
||
namespace Covid19Api.Presentation.Response | ||
{ | ||
public class CountryStatisticsAggregateDto | ||
{ | ||
public CountryStatisticsAggregateDto(Guid id, string country, string? countryCode, int total, int @new, | ||
int deaths, | ||
int newDeaths, | ||
int recovered, int active, int month, int year) | ||
{ | ||
this.Id = id; | ||
this.Country = country; | ||
this.CountryCode = countryCode; | ||
this.Total = total; | ||
this.New = @new; | ||
this.Deaths = deaths; | ||
this.NewDeaths = newDeaths; | ||
this.Recovered = recovered; | ||
this.Active = active; | ||
this.Month = month; | ||
this.Year = year; | ||
} | ||
|
||
public Guid Id { get; set; } | ||
public string Country { get; set; } | ||
public string? CountryCode { get; set; } | ||
public int Total { get; set; } | ||
public int New { get; set; } | ||
public int Deaths { get; set; } | ||
public int NewDeaths { get; set; } | ||
public int Recovered { get; set; } | ||
public int Active { get; set; } | ||
public int Month { get; set; } | ||
public int Year { get; set; } | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Covid19Api.Repositories.Abstractions/ICountryStatisticsAggregatesRepository.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,12 @@ | ||
using System.Threading.Tasks; | ||
using Covid19Api.Domain; | ||
|
||
namespace Covid19Api.Repositories.Abstractions | ||
{ | ||
public interface ICountryStatisticsAggregatesRepository | ||
{ | ||
Task StoreAsync(CountryStatisticsAggregate countryStatisticsAggregate); | ||
|
||
Task<CountryStatisticsAggregate?> FindAsync(string country, int month, int year); | ||
} | ||
} |
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.