Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add snapshot status endpoint #405

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
namespace StreetNameRegistry.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 = "[StreetNameRegistry].[SnapshotVerificationStates]";
private const string SnaphotsTableName = "[StreetNameRegistry].[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 [StreetNameRegistry].[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;
}
}
}
Loading