-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add snapshot status endpoint (#798)
- Loading branch information
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
src/ParcelRegistry.Projector/Snapshots/SnapshotsController.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,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<IActionResult> Get( | ||
[FromServices] IConfiguration configuration, | ||
CancellationToken cancellationToken) | ||
{ | ||
await using var sqlConnection = new SqlConnection(configuration.GetConnectionString(SnapshotConnectionStringKey)); | ||
var failedSnapshotCount = | ||
await sqlConnection.QuerySingleAsync<int>( | ||
$"SELECT Count(*) FROM {SnapshotVerificationsTableName} WHERE Status = 'Failed'", | ||
cancellationToken); | ||
|
||
var differenceInDaysOfLastVerification = | ||
await sqlConnection.QuerySingleAsync<int>( | ||
$"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; | ||
} | ||
} | ||
} |