Files
scadaproj/components/ui-theme/validation/scadabridge-ui/host/BatchThreeFixtures.cs
T

87 lines
7.1 KiB
C#

using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using ZB.MOM.WW.ScadaBridge.CentralUI.Services;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Services;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Transport;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Sites;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates;
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Health;
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Notification;
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Audit;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit;
using ZB.MOM.WW.ScadaBridge.Commons.Types;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
using ZB.MOM.WW.ScadaBridge.Communication;
using ZB.MOM.WW.ScadaBridge.DeploymentManager;
using ZB.MOM.WW.ScadaBridge.DeploymentManager.PendingUpdates;
using ZB.MOM.WW.ScadaBridge.HealthMonitoring;
using ZB.MOM.WW.ScadaBridge.TemplateEngine.Services;
using ZB.MOM.WW.ScadaBridge.TemplateEngine.Flattening;
// Disposable read-only synthetic projections. Real service constructors are resolved but never
// receive an actor system or a database. Every mutation and external probe remains rejected.
internal static class BatchThreeFixtures
{
public static void Register(IServiceCollection services)
{
var sites = Enumerable.Range(1, 12).Select(i => new Site($"Synthetic facility {i:00} with a deliberately long local display name", $"fixture-{i:00}") { Id = i }).ToArray();
services.AddScoped(_ => ReadOnlyFixtureProxy.WithReads<ISiteRepository>(new Dictionary<string, Func<object?[], object?>>
{
["GetAllSitesAsync"] = _ => Task.FromResult<IReadOnlyList<Site>>(sites),
["GetAllDataConnectionsAsync"] = _ => Task.FromResult<IReadOnlyList<DataConnection>>([])
}));
services.AddScoped(_ => ReadOnlyFixtureProxy.WithReads<ITemplateEngineRepository>(new Dictionary<string, Func<object?[], object?>>
{
["GetAllTemplatesAsync"] = _ => Task.FromResult<IReadOnlyList<Template>>([]),
["GetTemplateStubsAsync"] = _ => Task.FromResult<IReadOnlyList<ZB.MOM.WW.ScadaBridge.Commons.Types.Templates.TemplateStub>>([]),
["GetAreasBySiteIdAsync"] = args => Task.FromResult<IReadOnlyList<Area>>(Enumerable.Range(1, 6).Select(i => new Area($"Synthetic area {i:00} with a long inspection label") { Id = (int)args[0]! * 100 + i, SiteId = (int)args[0]! }).ToArray()),
["GetTopologyInstancesAsync"] = _ => Task.FromResult<IReadOnlyList<Instance>>([])
}));
services.AddScoped<ZB.MOM.WW.ScadaBridge.CentralUI.Auth.SiteScopeService>();
services.AddScoped<AuthorizedManualFailoverService>();
services.AddSingleton<IPendingUpdatesCache>(new EmptyPending());
services.AddSingleton<IKpiSnapshotCache>(new SyntheticKpis());
var now = DateTimeOffset.UtcNow;
SiteHealthReport Report(string id) => new(id, 1, now, new Dictionary<string, ConnectionHealth>(), new Dictionary<string, ZB.MOM.WW.ScadaBridge.Commons.Messages.Health.TagResolutionStatus>(), 0, 0, new Dictionary<string, int>(), 0, 0, 0, 0);
var states = new Dictionary<string, SiteHealthState>
{
["healthy"] = new() { SiteId="healthy", IsOnline=true, LatestReport=Report("healthy"), LastReportReceivedAt=now, LastHeartbeatAt=now },
["offline"] = new() { SiteId="offline", IsOnline=false, LatestReport=Report("offline"), LastReportReceivedAt=now.AddMinutes(-8), LastHeartbeatAt=now.AddMinutes(-8) },
["warning"] = new() { SiteId="warning", IsOnline=true, IsMetricsStale=true, LatestReport=Report("warning"), LastReportReceivedAt=now.AddMinutes(-4), LastHeartbeatAt=now },
["unknown"] = new() { SiteId="unknown", IsOnline=true, LastHeartbeatAt=now }
};
services.AddSingleton(ReadOnlyFixtureProxy.WithReads<ICentralHealthAggregator>(new Dictionary<string, Func<object?[], object?>> { ["GetAllSiteStates"] = _ => states }));
services.AddSingleton(ReadOnlyFixtureProxy.EmptyReads<IKpiHistoryQueryService>());
services.AddSingleton(Options.Create(new HealthMonitoringOptions()));
services.AddSingleton(ReadOnlyFixtureProxy.EmptyReads<IManualFailoverService>());
services.AddSingleton(ReadOnlyFixtureProxy.EmptyReads<IDeploymentManagerRepository>());
services.AddSingleton(ReadOnlyFixtureProxy.EmptyReads<IFlatteningPipeline>());
services.AddSingleton(ReadOnlyFixtureProxy.EmptyReads<IAuditService>());
services.AddSingleton(ReadOnlyFixtureProxy.EmptyReads<IStaleInstanceProbe>());
services.AddSingleton(new CommunicationService(Options.Create(new CommunicationOptions()), NullLogger<CommunicationService>.Instance));
services.AddSingleton(new OperationLockManager());
services.AddSingleton(Options.Create(new DeploymentManagerOptions()));
services.AddSingleton(Options.Create(new CommunicationOptions()));
services.AddSingleton<ILogger<DeploymentService>>(NullLogger<DeploymentService>.Instance);
services.AddScoped<DiffService>(); services.AddScoped<RevisionHashService>();
services.AddSingleton<IDeploymentStatusNotifier>(new DeploymentStatusNotifier(NullLogger<DeploymentStatusNotifier>.Instance));
services.AddScoped<DeploymentService>(); services.AddScoped<AreaService>(); services.AddScoped<InstanceService>();
}
private sealed class EmptyPending : IPendingUpdatesCache
{
public event Action? Invalidated { add { } remove { } }
public Task<PendingUpdatesSnapshot> GetAsync(bool forceRefresh=false, CancellationToken cancellationToken=default) => Task.FromResult(new PendingUpdatesSnapshot(DateTimeOffset.UtcNow, []));
}
private sealed class SyntheticKpis : IKpiSnapshotCache
{
public Task<NotificationKpiResponse> GetNotificationKpisAsync(bool forceRefresh=false, CancellationToken cancellationToken=default) => Task.FromResult(new NotificationKpiResponse("local", true, null, 2, 0, 0, 4, null));
public Task<PerSiteNotificationKpiResponse> GetPerSiteNotificationKpisAsync(bool forceRefresh=false, CancellationToken cancellationToken=default) => Task.FromResult(new PerSiteNotificationKpiResponse("local", true, null, []));
public Task<PerNodeNotificationKpiResponse> GetPerNodeNotificationKpisAsync(bool forceRefresh=false, CancellationToken cancellationToken=default) => Task.FromResult(new PerNodeNotificationKpiResponse("local", true, null, []));
public Task<SiteCallKpiResponse> GetSiteCallKpisAsync(bool forceRefresh=false, CancellationToken cancellationToken=default) => Task.FromResult(new SiteCallKpiResponse("local", true, null, 0, 0, 0, 2, null, 0));
public Task<PerNodeSiteCallKpiResponse> GetPerNodeSiteCallKpisAsync(bool forceRefresh=false, CancellationToken cancellationToken=default) => Task.FromResult(new PerNodeSiteCallKpiResponse("local", true, null, []));
public Task<AuditLogKpiSnapshot> GetAuditKpiSnapshotAsync(bool forceRefresh=false, CancellationToken cancellationToken=default) => Task.FromResult(new AuditLogKpiSnapshot(3, 0, 0, DateTime.UtcNow));
}
}