shoc-backend/SeaHaven.Services/Implementation/WorkOrderOpsHealthService.cs
2026-07-24 16:40:46 -03:00

66 lines
2.6 KiB
C#

using Microsoft.Extensions.Options;
using SeaHaven.DataServices.Interfaces;
using SeaHaven.Services.DTOs;
using SeaHaven.Services.Interfaces;
namespace SeaHaven.Services.Implementation
{
public class WorkOrderOpsHealthService : IWorkOrderOpsHealthService
{
private readonly IWorkOrderOpsHealthDataService _healthData;
private readonly IWorkOrderJobRunStateAccessor _runState;
private readonly WorkOrderOpsHealthOptions _options;
public WorkOrderOpsHealthService(
IWorkOrderOpsHealthDataService healthData,
IWorkOrderJobRunStateAccessor runState,
IOptions<WorkOrderOpsHealthOptions> options)
{
_healthData = healthData;
_runState = runState;
_options = options.Value;
}
public async Task<WorkOrderOpsHealthDto> GetHealthAsync(CancellationToken cancellationToken = default)
{
var since = DateTime.UtcNow.AddHours(-24);
var syncRejected = await _healthData.CountSyncRejectedSinceAsync(since, cancellationToken);
var fieldLocks = await _healthData.CountFieldLocksAsync(cancellationToken);
var state = _runState.State;
var ledgerWeekRolled = await _healthData.GetLastWeekRolledProcessedAtAsync(cancellationToken);
return new WorkOrderOpsHealthDto
{
LastWeekRolledRunUtc = state.LastWeekRolledRunUtc ?? ledgerWeekRolled,
LastPastDueCacheRunUtc = state.LastPastDueCacheRunUtc,
LastWeekRolledError = state.LastWeekRolledError,
LastPastDueCacheError = state.LastPastDueCacheError,
SyncRejectedLast24h = syncRejected,
FieldLockCount = fieldLocks,
SyncEnabled = _options.SyncEnabled,
IngestEnabled = _options.IngestEnabled,
LegacyDeprecationEnabled = _options.LegacyDeprecationEnabled,
LegacySunsetDate = _options.LegacySunsetDate,
CheckedAtUtc = DateTime.UtcNow
};
}
}
/// <summary>Options bridged from API configuration for health snapshots.</summary>
public class WorkOrderOpsHealthOptions
{
public bool SyncEnabled { get; set; } = true;
public bool IngestEnabled { get; set; } = true;
public bool LegacyDeprecationEnabled { get; set; }
public string? LegacySunsetDate { get; set; }
}
/// <summary>Shared in-memory job run timestamps (API hosted services + ops health).</summary>
public class WorkOrderJobRunStateAccessor : IWorkOrderJobRunStateAccessor
{
public WorkOrderJobRunState State { get; } = new();
}
}