feat: replace debug view polling with real-time SignalR streaming

The debug view polled every 2s by re-subscribing for full snapshots. Now a
persistent DebugStreamBridgeActor on central subscribes once and receives
incremental Akka stream events from the site, forwarding them to the Blazor
component via callbacks and to the CLI via a new SignalR hub at
/hubs/debug-stream. Adds `debug stream` CLI command with auto-reconnect.
This commit is contained in:
Joseph Doherty
2026-03-21 01:34:53 -04:00
parent d91aa83665
commit fd2e96fea2
15 changed files with 777 additions and 75 deletions

View File

@@ -10,7 +10,7 @@
@attribute [Authorize(Policy = AuthorizationPolicies.RequireDeployment)]
@inject ITemplateEngineRepository TemplateEngineRepository
@inject ISiteRepository SiteRepository
@inject CommunicationService CommunicationService
@inject DebugStreamService DebugStreamService
@inject IJSRuntime JS
@implements IDisposable
@@ -38,11 +38,11 @@
</div>
<div class="col-md-4">
<label class="form-label small">Instance</label>
<select class="form-select form-select-sm" @bind="_selectedInstanceName">
<option value="">Select instance...</option>
<select class="form-select form-select-sm" @bind="_selectedInstanceId" @bind:after="OnInstanceSelectionChanged">
<option value="0">Select instance...</option>
@foreach (var inst in _siteInstances)
{
<option value="@inst.UniqueName">@inst.UniqueName (@inst.State)</option>
<option value="@inst.Id">@inst.UniqueName (@inst.State)</option>
}
</select>
</div>
@@ -50,7 +50,7 @@
@if (!_connected)
{
<button class="btn btn-primary btn-sm" @onclick="Connect"
disabled="@(string.IsNullOrEmpty(_selectedInstanceName) || _selectedSiteId == 0 || _connecting)">
disabled="@(_selectedInstanceId == 0 || _selectedSiteId == 0 || _connecting)">
@if (_connecting) { <span class="spinner-border spinner-border-sm me-1"></span> }
Connect
</button>
@@ -58,7 +58,10 @@
else
{
<button class="btn btn-outline-danger btn-sm" @onclick="Disconnect">Disconnect</button>
<span class="badge bg-success align-self-center">Connected</span>
<span class="badge bg-success align-self-center">
<span class="spinner-grow spinner-grow-sm me-1" style="width: 0.5rem; height: 0.5rem;"></span>
Live
</span>
}
</div>
</div>
@@ -153,7 +156,7 @@
private List<Site> _sites = new();
private List<Instance> _siteInstances = new();
private int _selectedSiteId;
private string _selectedInstanceName = string.Empty;
private int _selectedInstanceId;
private bool _loading = true;
private bool _connected;
private bool _connecting;
@@ -162,7 +165,7 @@
private Dictionary<string, AttributeValueChanged> _attributeValues = new();
private Dictionary<string, AlarmStateChanged> _alarmStates = new();
private Timer? _refreshTimer;
private DebugStreamSession? _session;
private ToastNotification _toast = default!;
protected override async Task OnInitializedAsync()
@@ -183,14 +186,14 @@
if (!firstRender) return;
var storedSiteId = await JS.InvokeAsync<string>("localStorage.getItem", "debugView.siteId");
var storedInstanceName = await JS.InvokeAsync<string>("localStorage.getItem", "debugView.instanceName");
var storedInstanceId = await JS.InvokeAsync<string>("localStorage.getItem", "debugView.instanceId");
if (!string.IsNullOrEmpty(storedSiteId) && int.TryParse(storedSiteId, out var siteId)
&& !string.IsNullOrEmpty(storedInstanceName))
&& !string.IsNullOrEmpty(storedInstanceId) && int.TryParse(storedInstanceId, out var instanceId))
{
_selectedSiteId = siteId;
await LoadInstancesForSite();
_selectedInstanceName = storedInstanceName;
_selectedInstanceId = instanceId;
StateHasChanged();
await Connect();
}
@@ -199,7 +202,7 @@
private async Task LoadInstancesForSite()
{
_siteInstances.Clear();
_selectedInstanceName = string.Empty;
_selectedInstanceId = 0;
if (_selectedSiteId == 0) return;
try
{
@@ -213,58 +216,64 @@
}
}
private void OnInstanceSelectionChanged()
{
// No-op; selection is tracked via _selectedInstanceId binding
}
private async Task Connect()
{
if (string.IsNullOrEmpty(_selectedInstanceName) || _selectedSiteId == 0) return;
if (_selectedInstanceId == 0 || _selectedSiteId == 0) return;
_connecting = true;
try
{
var site = _sites.FirstOrDefault(s => s.Id == _selectedSiteId);
if (site == null) return;
var session = await DebugStreamService.StartStreamAsync(
_selectedInstanceId,
onEvent: evt =>
{
switch (evt)
{
case AttributeValueChanged av:
_attributeValues[av.AttributeName] = av;
_ = InvokeAsync(StateHasChanged);
break;
case AlarmStateChanged al:
_alarmStates[al.AlarmName] = al;
_ = InvokeAsync(StateHasChanged);
break;
}
},
onTerminated: () =>
{
_connected = false;
_session = null;
_ = InvokeAsync(() =>
{
_toast.ShowError("Debug stream terminated (site disconnected).");
StateHasChanged();
});
});
var request = new SubscribeDebugViewRequest(_selectedInstanceName, Guid.NewGuid().ToString("N"));
_snapshot = await CommunicationService.SubscribeDebugViewAsync(site.SiteIdentifier, request);
_session = session;
// Populate initial state from snapshot
_attributeValues.Clear();
foreach (var av in _snapshot.AttributeValues)
{
foreach (var av in session.InitialSnapshot.AttributeValues)
_attributeValues[av.AttributeName] = av;
}
_alarmStates.Clear();
foreach (var al in _snapshot.AlarmStates)
{
_alarmStates[al.AlarmName] = al;
}
_alarmStates.Clear();
foreach (var al in session.InitialSnapshot.AlarmStates)
_alarmStates[al.AlarmName] = al;
_snapshot = session.InitialSnapshot;
_connected = true;
// Persist selection to localStorage for auto-reconnect on refresh
await JS.InvokeVoidAsync("localStorage.setItem", "debugView.siteId", _selectedSiteId.ToString());
await JS.InvokeVoidAsync("localStorage.setItem", "debugView.instanceName", _selectedInstanceName);
await JS.InvokeVoidAsync("localStorage.setItem", "debugView.siteIdentifier", site.SiteIdentifier);
await JS.InvokeVoidAsync("localStorage.setItem", "debugView.instanceId", _selectedInstanceId.ToString());
_toast.ShowSuccess($"Connected to {_selectedInstanceName}");
// Periodic refresh (simulating SignalR push by re-subscribing)
_refreshTimer = new Timer(async _ =>
{
try
{
var refreshRequest = new SubscribeDebugViewRequest(_selectedInstanceName, Guid.NewGuid().ToString("N"));
var newSnapshot = await CommunicationService.SubscribeDebugViewAsync(site.SiteIdentifier, refreshRequest);
foreach (var av in newSnapshot.AttributeValues)
_attributeValues[av.AttributeName] = av;
foreach (var al in newSnapshot.AlarmStates)
_alarmStates[al.AlarmName] = al;
_snapshot = newSnapshot;
await InvokeAsync(StateHasChanged);
}
catch
{
// Connection may have dropped
}
}, null, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(2));
var instance = _siteInstances.FirstOrDefault(i => i.Id == _selectedInstanceId);
_toast.ShowSuccess($"Streaming {instance?.UniqueName ?? "instance"}");
}
catch (Exception ex)
{
@@ -275,23 +284,15 @@
private async Task Disconnect()
{
_refreshTimer?.Dispose();
_refreshTimer = null;
if (_connected && _selectedSiteId > 0 && !string.IsNullOrEmpty(_selectedInstanceName))
if (_session != null)
{
var site = _sites.FirstOrDefault(s => s.Id == _selectedSiteId);
if (site != null)
{
var request = new UnsubscribeDebugViewRequest(_selectedInstanceName, Guid.NewGuid().ToString("N"));
CommunicationService.UnsubscribeDebugView(site.SiteIdentifier, request);
}
DebugStreamService.StopStream(_session.SessionId);
_session = null;
}
// Clear persisted selection — user explicitly disconnected
await JS.InvokeVoidAsync("localStorage.removeItem", "debugView.siteId");
await JS.InvokeVoidAsync("localStorage.removeItem", "debugView.instanceName");
await JS.InvokeVoidAsync("localStorage.removeItem", "debugView.siteIdentifier");
await JS.InvokeVoidAsync("localStorage.removeItem", "debugView.instanceId");
_connected = false;
_snapshot = null;
@@ -314,6 +315,9 @@
public void Dispose()
{
_refreshTimer?.Dispose();
if (_session != null)
{
DebugStreamService.StopStream(_session.SessionId);
}
}
}