From dfb2c2bf7f91dbdc026b48c08a09b4b39cc90607 Mon Sep 17 00:00:00 2001 From: Jonas van Daal Date: Fri, 26 Jul 2024 11:00:30 +0200 Subject: [PATCH] feat: add snapshot status endpoint (#798) --- .../Snapshots/SnapshotsController.cs | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/ParcelRegistry.Projector/Snapshots/SnapshotsController.cs diff --git a/src/ParcelRegistry.Projector/Snapshots/SnapshotsController.cs b/src/ParcelRegistry.Projector/Snapshots/SnapshotsController.cs new file mode 100644 index 00000000..94c1915d --- /dev/null +++ b/src/ParcelRegistry.Projector/Snapshots/SnapshotsController.cs @@ -0,0 +1,53 @@ +namespace ParcelRegistry.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 = "[ParcelRegistry].[SnapshotVerificationStates]"; + private const string SnaphotsTableName = "[ParcelRegistry].[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 [ParcelRegistry].[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; + } + } +}