| | 1 | | using Dapper; |
| | 2 | | using Npgsql; |
| | 3 | |
|
| | 4 | | namespace Syki.Back.Features.Adm.GetDomainEventsSummary; |
| | 5 | |
|
| 0 | 6 | | public class GetDomainEventsSummaryService(NpgsqlDataSource dataSource) : IAdmService |
| | 7 | | { |
| | 8 | | public async Task<GetDomainEventsSummaryOut> Get() |
| | 9 | | { |
| 0 | 10 | | await using var connection = await dataSource.OpenConnectionAsync(); |
| | 11 | |
|
| 0 | 12 | | var result = new GetDomainEventsSummaryOut(); |
| | 13 | |
|
| | 14 | | const string summarySql = @" |
| | 15 | | SELECT |
| | 16 | | count(1) AS total, |
| | 17 | | count(1) FILTER (WHERE status = 'Pending') AS pending, |
| | 18 | | count(1) FILTER (WHERE status = 'Processing') AS processing, |
| | 19 | | count(1) FILTER (WHERE status = 'Success') AS success, |
| | 20 | | count(1) FILTER (WHERE status = 'Error') AS error |
| | 21 | | FROM syki.domain_events |
| | 22 | | "; |
| | 23 | |
|
| | 24 | | const string lastEventsSql = @" |
| | 25 | | SELECT occurred_at::timestamp(0) AS date, count(1) AS total |
| | 26 | | FROM syki.domain_events |
| | 27 | | GROUP BY occurred_at::timestamp(0) |
| | 28 | | ORDER BY date |
| | 29 | | "; |
| | 30 | |
|
| | 31 | | const string typesSql = @" |
| | 32 | | SELECT type, count(1) AS total |
| | 33 | | FROM syki.domain_events |
| | 34 | | GROUP BY TYPE |
| | 35 | | ORDER BY total DESC |
| | 36 | | "; |
| | 37 | |
|
| | 38 | | const string institutionsSql = @" |
| | 39 | | SELECT id, name |
| | 40 | | FROM syki.institutions |
| | 41 | | WHERE id <> '00000000-0000-0000-0000-000000000000' |
| | 42 | | ORDER BY name |
| | 43 | | "; |
| | 44 | |
|
| 0 | 45 | | result.Summary = await connection.QueryFirstAsync<DomainEventsSummaryOut>(summarySql); |
| 0 | 46 | | result.LastEvents = (await connection.QueryAsync<LastDomainEventOut>(lastEventsSql)).ToList(); |
| 0 | 47 | | result.EventTypes = (await connection.QueryAsync<DomainEventTypeCountOut>(typesSql)).ToList(); |
| 0 | 48 | | result.Institutions = (await connection.QueryAsync<TinyInstitutionOut>(institutionsSql)).ToList(); |
| | 49 | |
|
| 0 | 50 | | result.EventTypes.ForEach(x => x.Description = x.Type.ToDomainEventDescription()); |
| | 51 | |
|
| 0 | 52 | | return result; |
| 0 | 53 | | } |
| | 54 | | } |