160 lines
9.3 KiB
C#
160 lines
9.3 KiB
C#
using System.Runtime.CompilerServices;
|
|
using System.Security.Claims;
|
|
using Microsoft.AspNetCore.Components.Authorization;
|
|
using ZB.MOM.WW.MxGateway.Server.Dashboard;
|
|
using ZB.MOM.WW.MxGateway.Server.Security.Authentication;
|
|
using ZB.MOM.WW.MxGateway.Server.Security.Authorization;
|
|
internal sealed class StubSnapshotService : IDashboardSnapshotService
|
|
{
|
|
/// <inheritdoc />
|
|
public string Scene { get; set; } = "mixed";
|
|
public DashboardSnapshot GetSnapshot()
|
|
{
|
|
string scene = Scene;
|
|
var at = new DateTimeOffset(2026, 11, 13, 20, 0, 0, TimeSpan.Zero);
|
|
bool mixed = scene == "mixed";
|
|
return new(
|
|
GeneratedAt: at,
|
|
GatewayStartedAt: at.AddHours(-3),
|
|
GatewayUptime: TimeSpan.FromHours(3),
|
|
GatewayStatus: mixed ? "Degraded" : "Healthy",
|
|
GatewayVersion: "test",
|
|
Sessions: scene == "zero" ? [] : [new("fixture-session", "synthetic", mixed ? ZB.MOM.WW.MxGateway.Contracts.Proto.SessionState.Faulted : ZB.MOM.WW.MxGateway.Contracts.Proto.SessionState.Ready,
|
|
null, null, null, at, at, null, 123, null, null, 0, mixed ? "Synthetic fixture fault" : null)],
|
|
Workers: [],
|
|
Metrics: [new("mxgateway.sessions.open", scene == "zero" ? 0 : 1), new("mxgateway.workers.running", scene == "zero" ? 0 : 1), new("mxgateway.events.worker_queue.depth", mixed ? 3 : 0),
|
|
new("mxgateway.commands.failed", 104), new("mxgateway.events.received", 105), new("mxgateway.faults", 106), new("mxgateway.queues.overflows", 107)],
|
|
Faults: mixed ? [new("Worker", "fixture-session", 123, "Faulted", "Synthetic worker stopped responding. " + new string('x', 300) + " <script>fixture</script>", at)] : [],
|
|
ApiKeys:
|
|
[
|
|
new DashboardApiKeySummary(
|
|
KeyId: "synthetic-tagged",
|
|
DisplayName: "Tags only",
|
|
Scopes: new HashSet<string>(StringComparer.Ordinal),
|
|
Constraints: ApiKeyConstraints.Empty with { DashboardTags = ["team-a", "team-b"] },
|
|
CreatedUtc: DateTimeOffset.UnixEpoch,
|
|
LastUsedUtc: null,
|
|
RevokedUtc: null),
|
|
new DashboardApiKeySummary(
|
|
KeyId: "synthetic-plain",
|
|
DisplayName: "Unconstrained",
|
|
Scopes: new HashSet<string>(StringComparer.Ordinal),
|
|
Constraints: ApiKeyConstraints.Empty,
|
|
CreatedUtc: DateTimeOffset.UnixEpoch,
|
|
LastUsedUtc: null,
|
|
RevokedUtc: null),
|
|
],
|
|
Configuration: null!,
|
|
Galaxy: DashboardGalaxySummary.Unknown with { Status = DashboardGalaxyStatus.Healthy, LastDeployTime = at, LastSuccessAt = at, LastQueriedAt = at, ObjectCount = 201, AreaCount = 202, AttributeCount = 203, HistorizedAttributeCount = 204, AlarmAttributeCount = 205 });
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public IAsyncEnumerable<DashboardSnapshot> WatchSnapshotsAsync(CancellationToken cancellationToken) =>
|
|
new IdleSnapshotFeed().WatchAsync(cancellationToken);
|
|
}
|
|
|
|
// Parks until the page is disposed, so the base page's watch loop neither spins nor pushes a
|
|
// second snapshot mid-render.
|
|
internal sealed class IdleSnapshotFeed : IDashboardSnapshotFeed
|
|
{
|
|
/// <inheritdoc />
|
|
public async IAsyncEnumerable<DashboardSnapshot> WatchAsync(
|
|
[EnumeratorCancellation] CancellationToken cancellationToken)
|
|
{
|
|
await Task.Delay(Timeout.Infinite, cancellationToken).ConfigureAwait(false);
|
|
|
|
yield break;
|
|
}
|
|
}
|
|
|
|
// Grants management so the create dialog is reachable; no test here exercises a mutation.
|
|
internal sealed class ManagingApiKeyService : IDashboardApiKeyManagementService
|
|
{
|
|
/// <inheritdoc />
|
|
public bool CanManage(ClaimsPrincipal user) => user.IsInRole(DashboardRoles.Admin);
|
|
|
|
/// <inheritdoc />
|
|
public Task<DashboardApiKeyManagementResult> CreateAsync(
|
|
ClaimsPrincipal user,
|
|
DashboardApiKeyManagementRequest request,
|
|
CancellationToken cancellationToken) =>
|
|
Task.FromResult(DashboardApiKeyManagementResult.Fail("not exercised"));
|
|
|
|
/// <inheritdoc />
|
|
public Task<DashboardApiKeyManagementResult> RevokeAsync(
|
|
ClaimsPrincipal user,
|
|
string keyId,
|
|
CancellationToken cancellationToken) =>
|
|
Task.FromResult(DashboardApiKeyManagementResult.Fail("not exercised"));
|
|
|
|
/// <inheritdoc />
|
|
public Task<DashboardApiKeyManagementResult> RotateAsync(
|
|
ClaimsPrincipal user,
|
|
string keyId,
|
|
CancellationToken cancellationToken) =>
|
|
Task.FromResult(DashboardApiKeyManagementResult.Fail("not exercised"));
|
|
|
|
/// <inheritdoc />
|
|
public Task<DashboardApiKeyManagementResult> DeleteAsync(
|
|
ClaimsPrincipal user,
|
|
string keyId,
|
|
CancellationToken cancellationToken) =>
|
|
Task.FromResult(DashboardApiKeyManagementResult.Fail("not exercised"));
|
|
}
|
|
|
|
internal sealed class StubAuthenticationStateProvider(Microsoft.AspNetCore.Components.NavigationManager nav) : AuthenticationStateProvider
|
|
{
|
|
public override Task<AuthenticationState> GetAuthenticationStateAsync()
|
|
{
|
|
var query = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(new Uri(nav.Uri).Query);
|
|
string role = query.TryGetValue("principal", out var value) ? value.ToString() : "admin";
|
|
var identity = role == "anonymous" ? new ClaimsIdentity() : new ClaimsIdentity(
|
|
[new Claim(ClaimTypes.Name, "fixture-user"), new Claim(ClaimTypes.Role, role == "viewer" ? DashboardRoles.Viewer : DashboardRoles.Admin)], "synthetic");
|
|
return Task.FromResult(new AuthenticationState(new ClaimsPrincipal(identity)));
|
|
}
|
|
}
|
|
// In-memory Galaxy projection and read-only fake values for the actual BrowsePage.
|
|
internal sealed class FixtureGalaxyCache : ZB.MOM.WW.GalaxyRepository.IGalaxyHierarchyCache
|
|
{
|
|
public ZB.MOM.WW.GalaxyRepository.GalaxyHierarchyCacheEntry Current { get; }
|
|
public FixtureGalaxyCache()
|
|
{
|
|
var plant = new ZB.MOM.WW.GalaxyRepository.Grpc.GalaxyObject { GobjectId = 1, TagName = "SyntheticPlant", BrowseName = "SyntheticPlant", IsArea = true };
|
|
var pump = new ZB.MOM.WW.GalaxyRepository.Grpc.GalaxyObject { GobjectId = 2, ParentGobjectId = 1, TagName = "Pump01", BrowseName = "Pump01" };
|
|
pump.Attributes.Add(new ZB.MOM.WW.GalaxyRepository.Grpc.GalaxyAttribute { AttributeName = "PV", FullTagReference = "Pump01.PV", DataTypeName = "Float" });
|
|
pump.Attributes.Add(new ZB.MOM.WW.GalaxyRepository.Grpc.GalaxyAttribute { AttributeName = new string('x', 200), FullTagReference = "Pump01." + new string('x', 200), DataTypeName = "Float" });
|
|
var sibling = new ZB.MOM.WW.GalaxyRepository.Grpc.GalaxyObject { GobjectId = 3, ParentGobjectId = 1, TagName = "Sibling01", BrowseName = "Sibling01" };
|
|
sibling.Attributes.Add(new ZB.MOM.WW.GalaxyRepository.Grpc.GalaxyAttribute { AttributeName = "PV", FullTagReference = "Sibling01.PV", DataTypeName = "Float" });
|
|
ZB.MOM.WW.GalaxyRepository.Grpc.GalaxyObject[] rows = [plant, pump, sibling, new() { GobjectId=4, ParentGobjectId=2, TagName="PumpChild", BrowseName="PumpChild" }];
|
|
Current = ZB.MOM.WW.GalaxyRepository.GalaxyHierarchyCacheEntry.Empty with {
|
|
Status = ZB.MOM.WW.GalaxyRepository.GalaxyCacheStatus.Healthy, Sequence = 1,
|
|
Objects = rows, Index = ZB.MOM.WW.GalaxyRepository.GalaxyHierarchyIndex.Build(rows), ObjectCount = rows.Length, AttributeCount = 3,
|
|
LastSuccessAt = DateTimeOffset.UtcNow,
|
|
};
|
|
}
|
|
public Task RefreshAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
|
public Task WaitForFirstLoadAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
|
}
|
|
internal sealed class FixtureLiveData : IDashboardLiveDataService
|
|
{
|
|
public Task<DashboardLiveReadResult> ReadAsync(IReadOnlyCollection<string> tagAddresses, CancellationToken cancellationToken) =>
|
|
Task.FromResult(new DashboardLiveReadResult(tagAddresses.Select(tag => new DashboardTagValue(tag, true, "[redacted]", "Float", 192, true, DateTimeOffset.UnixEpoch, null)).ToArray(), null, "synthetic-no-worker", null));
|
|
public Task<DashboardAlarmQueryResult> QueryAlarmsAsync(CancellationToken cancellationToken) =>
|
|
Task.FromResult(new DashboardAlarmQueryResult([], null, null));
|
|
}
|
|
|
|
internal sealed class FaultableBrowseService(ZB.MOM.WW.GalaxyRepository.IGalaxyHierarchyCache cache) : IDashboardBrowseService
|
|
{
|
|
private readonly DashboardBrowseService _real = new(cache);
|
|
public bool FailNext { get; set; }
|
|
public int Attempts { get; private set; }
|
|
public ulong CurrentCacheSequence => _real.CurrentCacheSequence;
|
|
public BrowseLevelResult GetRoots(BrowseFilterArgs filter) => _real.GetRoots(filter);
|
|
public BrowseLevelResult GetChildren(int id, BrowseFilterArgs filter)
|
|
{
|
|
Attempts++;
|
|
if (FailNext) { FailNext = false; throw new InvalidOperationException("Synthetic hierarchy outage " + new string('x', 600) + " <script>fixture</script>"); }
|
|
return _real.GetChildren(id, filter);
|
|
}
|
|
}
|