From 537948154898d977fd3a7c60961215197c59baa0 Mon Sep 17 00:00:00 2001 From: ArneD Date: Thu, 25 Jul 2024 11:21:05 +0200 Subject: [PATCH] feat: add snapshot status endpoint --- .../Snapshots/SnapshotsController.cs | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/BuildingRegistry.Projector/Snapshots/SnapshotsController.cs diff --git a/src/BuildingRegistry.Projector/Snapshots/SnapshotsController.cs b/src/BuildingRegistry.Projector/Snapshots/SnapshotsController.cs new file mode 100644 index 000000000..c323556b1 --- /dev/null +++ b/src/BuildingRegistry.Projector/Snapshots/SnapshotsController.cs @@ -0,0 +1,53 @@ +namespace BuildingRegistry.Projector.Snapshots +{ + using System.Threading; + using System.Threading.Tasks; + using Asp.Versioning; + using Be.Vlaanderen.Basisregisters.Api; + using Dapper; + using Microsoft.AspNetCore.Mvc; + using Microsoft.Data.SqlClient; + using Microsoft.Extensions.Configuration; + + [ApiVersion("1.0")] + [ApiRoute("snapshots")] + public class SnapshotsController : ApiController + { + private const string SnapshotConnectionStringKey = "Events"; + private const string SnapshotVerificationsTableName = "[BuildingRegistry].[SnapshotVerificationStates]"; + private const string SnaphotsTableName = "[BuildingRegistry].[Snapshots]"; + + [HttpGet] + public async Task Get( + [FromServices] IConfiguration configuration, + CancellationToken cancellationToken) + { + await using var sqlConnection = new SqlConnection(configuration.GetConnectionString(SnapshotConnectionStringKey)); + var failedSnapshotCount = + await sqlConnection.QuerySingleAsync( + $"SELECT Count(*) FROM {SnapshotVerificationsTableName} WHERE Status = 'Failed'", + cancellationToken); + + var differenceInDaysOfLastVerification = + await sqlConnection.QuerySingleAsync( + $"SELECT DATEDIFF(DAY," + + $"(SELECT Created FROM {SnaphotsTableName} WHERE Id = (SELECT MAX(SnapshotId) FROM {SnapshotVerificationsTableName}))," + + $"(SELECT MAX(Created) FROM [BuildingRegistry].[Snapshots])) As DaysDiff", + cancellationToken); + + return Ok(new SnapshotStatusResponse(failedSnapshotCount, differenceInDaysOfLastVerification)); + } + } + + public sealed class SnapshotStatusResponse + { + public int FailedSnapshotsCount { get; set; } + public int DifferenceInDaysOfLastVerification { get; set; } + + public SnapshotStatusResponse(int failedSnapshotsCount, int differenceInDaysOfLastVerification) + { + FailedSnapshotsCount = failedSnapshotsCount; + DifferenceInDaysOfLastVerification = differenceInDaysOfLastVerification; + } + } +}