using Microsoft.Extensions.DependencyInjection;
using ZB.MOM.WW.ScadaBridge.Commons.Caching;
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Audit;
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Notification;
using ZB.MOM.WW.ScadaBridge.Commons.Types;
using ZB.MOM.WW.ScadaBridge.Communication;
namespace ZB.MOM.WW.ScadaBridge.CentralUI.Services;
///
/// Default — a DI singleton holding one
/// per KPI query.
///
/// Registered as a singleton on purpose: the whole point is that the memo outlives any one
/// circuit. It depends only on (itself a singleton) and
/// — the audit KPI resolves its scoped query service from
/// a fresh scope per flight, the same shape AuditLogQueryService uses internally so a
/// KPI round never shares a circuit-scoped DbContext.
///
///
public sealed class KpiSnapshotCache : IKpiSnapshotCache
{
///
/// Freshness window. Sized just under the Health dashboard's 10s tick so each tick still
/// yields ONE fresh query for the whole node (rather than one per circuit), while a
/// circuit whose tick lands mid-window is served the memoized value instead of duplicating it.
///
internal static readonly TimeSpan Ttl = TimeSpan.FromSeconds(8);
private readonly CommunicationService _communication;
private readonly IServiceScopeFactory _scopeFactory;
private readonly SingleFlightMemo _notificationKpis;
private readonly SingleFlightMemo _perSiteNotificationKpis;
private readonly SingleFlightMemo _perNodeNotificationKpis;
private readonly SingleFlightMemo _siteCallKpis;
private readonly SingleFlightMemo _perNodeSiteCallKpis;
private readonly SingleFlightMemo _auditKpis;
///
/// Initializes the shared KPI snapshot cache.
///
/// Central-side comms used to Ask the outbox / site-call singletons.
/// Opens a fresh DI scope per audit-KPI flight.
public KpiSnapshotCache(CommunicationService communication, IServiceScopeFactory scopeFactory)
: this(communication, scopeFactory, Ttl, clock: null)
{
}
///
/// Test seam: same cache with an explicit TTL and clock so freshness can be asserted
/// without real delays.
///
/// Central-side comms used to Ask the outbox / site-call singletons.
/// Opens a fresh DI scope per audit-KPI flight.
/// Freshness window.
/// Clock used for freshness.
internal KpiSnapshotCache(
CommunicationService communication,
IServiceScopeFactory scopeFactory,
TimeSpan ttl,
Func? clock)
{
_communication = communication ?? throw new ArgumentNullException(nameof(communication));
_scopeFactory = scopeFactory ?? throw new ArgumentNullException(nameof(scopeFactory));
_notificationKpis = new SingleFlightMemo(ttl, clock);
_perSiteNotificationKpis = new SingleFlightMemo(ttl, clock);
_perNodeNotificationKpis = new SingleFlightMemo(ttl, clock);
_siteCallKpis = new SingleFlightMemo(ttl, clock);
_perNodeSiteCallKpis = new SingleFlightMemo(ttl, clock);
_auditKpis = new SingleFlightMemo(ttl, clock);
}
///
public Task GetNotificationKpisAsync(
bool forceRefresh = false, CancellationToken cancellationToken = default) =>
_notificationKpis.GetAsync(
() => _communication.GetNotificationKpisAsync(new NotificationKpiRequest(NewCorrelationId())),
forceRefresh, cancellationToken);
///
public Task GetPerSiteNotificationKpisAsync(
bool forceRefresh = false, CancellationToken cancellationToken = default) =>
_perSiteNotificationKpis.GetAsync(
() => _communication.GetPerSiteNotificationKpisAsync(new PerSiteNotificationKpiRequest(NewCorrelationId())),
forceRefresh, cancellationToken);
///
public Task GetPerNodeNotificationKpisAsync(
bool forceRefresh = false, CancellationToken cancellationToken = default) =>
_perNodeNotificationKpis.GetAsync(
() => _communication.GetPerNodeNotificationKpisAsync(new PerNodeNotificationKpiRequest(NewCorrelationId())),
forceRefresh, cancellationToken);
///
public Task GetSiteCallKpisAsync(
bool forceRefresh = false, CancellationToken cancellationToken = default) =>
_siteCallKpis.GetAsync(
() => _communication.GetSiteCallKpisAsync(new SiteCallKpiRequest(NewCorrelationId())),
forceRefresh, cancellationToken);
///
public Task GetPerNodeSiteCallKpisAsync(
bool forceRefresh = false, CancellationToken cancellationToken = default) =>
_perNodeSiteCallKpis.GetAsync(
() => _communication.GetPerNodeSiteCallKpisAsync(new PerNodeSiteCallKpiRequest(NewCorrelationId())),
forceRefresh, cancellationToken);
///
public Task GetAuditKpiSnapshotAsync(
bool forceRefresh = false, CancellationToken cancellationToken = default) =>
_auditKpis.GetAsync(LoadAuditKpisAsync, forceRefresh, cancellationToken);
private async Task LoadAuditKpisAsync()
{
await using var scope = _scopeFactory.CreateAsyncScope();
var queryService = scope.ServiceProvider.GetRequiredService();
return await queryService.GetKpiSnapshotAsync();
}
private static string NewCorrelationId() => Guid.NewGuid().ToString("N");
}