Phases 4-6: Complete Central UI — Admin, Design, Deployment, and Operations pages

Phase 4 — Operator/Admin UI:
- Sites, DataConnections, Areas (hierarchical), API Keys (auto-generated) CRUD
- Health Dashboard (live refresh, per-site metrics from CentralHealthAggregator)
- Instance list with filtering/staleness/lifecycle actions
- Deployment status tracking with auto-refresh

Phase 5 — Authoring UI:
- Template authoring with inheritance tree, tabs (attrs/alarms/scripts/compositions)
- Lock indicators, on-demand validation, collision detection
- Shared scripts with syntax check
- External systems, DB connections, notification lists, Inbound API methods

Phase 6 — Deployment Operations UI:
- Staleness indicators, validation gating
- Debug view (instance selection, attribute/alarm live tables)
- Site event log viewer (filters, keyword search, keyset pagination)
- Parked message management, Audit log viewer with JSON state

Shared components: DataTable, ConfirmDialog, ToastNotification, LoadingSpinner, TimestampDisplay
623 tests pass, zero warnings. All Bootstrap 5, clean corporate design.
This commit is contained in:
Joseph Doherty
2026-03-16 21:47:37 -04:00
parent 6ea38faa6f
commit 3b2320bd35
22 changed files with 4821 additions and 32 deletions

View File

@@ -1,11 +1,294 @@
@page "/deployment/debug-view"
@using ScadaLink.Security
@using ScadaLink.Commons.Entities.Instances
@using ScadaLink.Commons.Entities.Sites
@using ScadaLink.Commons.Interfaces.Repositories
@using ScadaLink.Commons.Messages.DebugView
@using ScadaLink.Commons.Messages.Streaming
@using ScadaLink.Commons.Types.Enums
@using ScadaLink.Communication
@attribute [Authorize(Policy = AuthorizationPolicies.RequireDeployment)]
@inject ITemplateEngineRepository TemplateEngineRepository
@inject ISiteRepository SiteRepository
@inject CommunicationService CommunicationService
@implements IDisposable
<div class="container mt-4">
<h4>Debug View</h4>
<p class="text-muted">Real-time debug view will be available in a future phase.</p>
<div class="alert alert-info" role="alert">
<strong>Note:</strong> Debug view streams are lost on failover. If the connection drops, you will need to re-open the debug view.
<div class="container-fluid mt-3">
<div class="d-flex justify-content-between align-items-center mb-3">
<h4 class="mb-0">Debug View</h4>
<div class="alert alert-info py-1 px-2 mb-0 small">
Debug view streams are lost on failover. Re-open if connection drops.
</div>
</div>
<ToastNotification @ref="_toast" />
@if (_loading)
{
<LoadingSpinner IsLoading="true" />
}
else
{
<div class="row mb-3 g-2">
<div class="col-md-3">
<label class="form-label small">Site</label>
<select class="form-select form-select-sm" @bind="_selectedSiteId" @bind:after="LoadInstancesForSite">
<option value="0">Select site...</option>
@foreach (var site in _sites)
{
<option value="@site.Id">@site.Name (@site.SiteIdentifier)</option>
}
</select>
</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>
@foreach (var inst in _siteInstances)
{
<option value="@inst.UniqueName">@inst.UniqueName (@inst.State)</option>
}
</select>
</div>
<div class="col-md-3 d-flex align-items-end gap-2">
@if (!_connected)
{
<button class="btn btn-primary btn-sm" @onclick="Connect"
disabled="@(string.IsNullOrEmpty(_selectedInstanceName) || _selectedSiteId == 0 || _connecting)">
@if (_connecting) { <span class="spinner-border spinner-border-sm me-1"></span> }
Connect
</button>
}
else
{
<button class="btn btn-outline-danger btn-sm" @onclick="Disconnect">Disconnect</button>
<span class="badge bg-success align-self-center">Connected</span>
}
</div>
</div>
@if (_connected && _snapshot != null)
{
<div class="row">
@* Attribute Values *@
<div class="col-md-7">
<div class="card">
<div class="card-header py-2 d-flex justify-content-between">
<strong>Attribute Values</strong>
<small class="text-muted">@_attributeValues.Count values</small>
</div>
<div class="card-body p-0" style="max-height: 500px; overflow-y: auto;">
<table class="table table-sm table-striped mb-0">
<thead class="table-light sticky-top">
<tr>
<th>Attribute</th>
<th>Value</th>
<th>Quality</th>
<th>Timestamp</th>
</tr>
</thead>
<tbody>
@foreach (var av in _attributeValues.Values.OrderBy(a => a.AttributeName))
{
<tr>
<td class="small">@av.AttributeName</td>
<td class="small font-monospace"><strong>@av.Value</strong></td>
<td>
<span class="badge @(av.Quality == "Good" ? "bg-success" : "bg-warning text-dark")">@av.Quality</span>
</td>
<td class="small text-muted">@av.Timestamp.LocalDateTime.ToString("HH:mm:ss.fff")</td>
</tr>
}
</tbody>
</table>
</div>
</div>
</div>
@* Alarm States *@
<div class="col-md-5">
<div class="card">
<div class="card-header py-2 d-flex justify-content-between">
<strong>Alarm States</strong>
<small class="text-muted">@_alarmStates.Count alarms</small>
</div>
<div class="card-body p-0" style="max-height: 500px; overflow-y: auto;">
<table class="table table-sm table-striped mb-0">
<thead class="table-light sticky-top">
<tr>
<th>Alarm</th>
<th>State</th>
<th>Priority</th>
<th>Timestamp</th>
</tr>
</thead>
<tbody>
@foreach (var alarm in _alarmStates.Values.OrderBy(a => a.AlarmName))
{
<tr class="@GetAlarmRowClass(alarm.State)">
<td class="small">@alarm.AlarmName</td>
<td>
<span class="badge @GetAlarmStateBadge(alarm.State)">@alarm.State</span>
</td>
<td class="small">@alarm.Priority</td>
<td class="small text-muted">@alarm.Timestamp.LocalDateTime.ToString("HH:mm:ss.fff")</td>
</tr>
}
</tbody>
</table>
</div>
</div>
</div>
</div>
<div class="text-muted small mt-2">
Snapshot received: @_snapshot.SnapshotTimestamp.LocalDateTime.ToString("HH:mm:ss") |
@_attributeValues.Count attributes, @_alarmStates.Count alarms
</div>
}
else if (_connected)
{
<LoadingSpinner IsLoading="true" Message="Waiting for snapshot..." />
}
}
</div>
@code {
private List<Site> _sites = new();
private List<Instance> _siteInstances = new();
private int _selectedSiteId;
private string _selectedInstanceName = string.Empty;
private bool _loading = true;
private bool _connected;
private bool _connecting;
private DebugViewSnapshot? _snapshot;
private Dictionary<string, AttributeValueChanged> _attributeValues = new();
private Dictionary<string, AlarmStateChanged> _alarmStates = new();
private Timer? _refreshTimer;
private ToastNotification _toast = default!;
protected override async Task OnInitializedAsync()
{
try
{
_sites = (await SiteRepository.GetAllSitesAsync()).ToList();
}
catch (Exception ex)
{
_toast.ShowError($"Failed to load sites: {ex.Message}");
}
_loading = false;
}
private async Task LoadInstancesForSite()
{
_siteInstances.Clear();
_selectedInstanceName = string.Empty;
if (_selectedSiteId == 0) return;
try
{
_siteInstances = (await TemplateEngineRepository.GetInstancesBySiteIdAsync(_selectedSiteId))
.Where(i => i.State == InstanceState.Enabled)
.ToList();
}
catch (Exception ex)
{
_toast.ShowError($"Failed to load instances: {ex.Message}");
}
}
private async Task Connect()
{
if (string.IsNullOrEmpty(_selectedInstanceName) || _selectedSiteId == 0) return;
_connecting = true;
try
{
var site = _sites.FirstOrDefault(s => s.Id == _selectedSiteId);
if (site == null) return;
var request = new SubscribeDebugViewRequest(_selectedInstanceName, Guid.NewGuid().ToString("N"));
_snapshot = await CommunicationService.SubscribeDebugViewAsync(site.SiteIdentifier, request);
// Populate initial state from snapshot
_attributeValues.Clear();
foreach (var av in _snapshot.AttributeValues)
{
_attributeValues[av.AttributeName] = av;
}
_alarmStates.Clear();
foreach (var al in _snapshot.AlarmStates)
{
_alarmStates[al.AlarmName] = al;
}
_connected = true;
_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(5), TimeSpan.FromSeconds(5));
}
catch (Exception ex)
{
_toast.ShowError($"Connect failed: {ex.Message}");
}
_connecting = false;
}
private void Disconnect()
{
_refreshTimer?.Dispose();
_refreshTimer = null;
if (_connected && _selectedSiteId > 0 && !string.IsNullOrEmpty(_selectedInstanceName))
{
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);
}
}
_connected = false;
_snapshot = null;
_attributeValues.Clear();
_alarmStates.Clear();
}
private static string GetAlarmStateBadge(AlarmState state) => state switch
{
AlarmState.Active => "bg-danger",
AlarmState.Normal => "bg-success",
_ => "bg-secondary"
};
private static string GetAlarmRowClass(AlarmState state) => state switch
{
AlarmState.Active => "table-danger",
_ => ""
};
public void Dispose()
{
_refreshTimer?.Dispose();
}
}

View File

@@ -1,8 +1,233 @@
@page "/deployment/deployments"
@using ScadaLink.Security
@using ScadaLink.Commons.Entities.Deployment
@using ScadaLink.Commons.Entities.Instances
@using ScadaLink.Commons.Interfaces.Repositories
@using ScadaLink.Commons.Types.Enums
@attribute [Authorize(Policy = AuthorizationPolicies.RequireDeployment)]
@inject IDeploymentManagerRepository DeploymentManagerRepository
@inject ITemplateEngineRepository TemplateEngineRepository
@implements IDisposable
<div class="container mt-4">
<h4>Deployments</h4>
<p class="text-muted">Deployment management will be available in a future phase.</p>
<div class="container-fluid mt-3">
<div class="d-flex justify-content-between align-items-center mb-3">
<h4 class="mb-0">Deployment Status</h4>
<div>
<span class="text-muted small me-2">Auto-refresh: 10s</span>
<button class="btn btn-outline-secondary btn-sm" @onclick="LoadDataAsync">Refresh</button>
</div>
</div>
@if (_loading)
{
<LoadingSpinner IsLoading="true" />
}
else if (_errorMessage != null)
{
<div class="alert alert-danger">@_errorMessage</div>
}
else
{
@* Summary cards *@
<div class="row mb-3">
<div class="col-md-3">
<div class="card border-warning">
<div class="card-body text-center py-2">
<h4 class="mb-0 text-warning">@_records.Count(r => r.Status == DeploymentStatus.Pending)</h4>
<small class="text-muted">Pending</small>
</div>
</div>
</div>
<div class="col-md-3">
<div class="card border-info">
<div class="card-body text-center py-2">
<h4 class="mb-0 text-info">@_records.Count(r => r.Status == DeploymentStatus.InProgress)</h4>
<small class="text-muted">In Progress</small>
</div>
</div>
</div>
<div class="col-md-3">
<div class="card border-success">
<div class="card-body text-center py-2">
<h4 class="mb-0 text-success">@_records.Count(r => r.Status == DeploymentStatus.Success)</h4>
<small class="text-muted">Successful</small>
</div>
</div>
</div>
<div class="col-md-3">
<div class="card border-danger">
<div class="card-body text-center py-2">
<h4 class="mb-0 text-danger">@_records.Count(r => r.Status == DeploymentStatus.Failed)</h4>
<small class="text-muted">Failed</small>
</div>
</div>
</div>
</div>
<table class="table table-sm table-striped table-hover">
<thead class="table-dark">
<tr>
<th>Deployment ID</th>
<th>Instance</th>
<th>Status</th>
<th>Deployed By</th>
<th>Started</th>
<th>Completed</th>
<th>Revision</th>
<th>Error</th>
</tr>
</thead>
<tbody>
@if (_records.Count == 0)
{
<tr>
<td colspan="8" class="text-muted text-center">No deployments recorded.</td>
</tr>
}
@foreach (var record in _pagedRecords)
{
<tr class="@GetRowClass(record.Status)">
<td><code class="small">@record.DeploymentId[..Math.Min(12, record.DeploymentId.Length)]...</code></td>
<td>@GetInstanceName(record.InstanceId)</td>
<td>
<span class="badge @GetStatusBadge(record.Status)">
@record.Status
@if (record.Status == DeploymentStatus.InProgress)
{
<span class="spinner-border spinner-border-sm ms-1" style="width: 0.7rem; height: 0.7rem;"></span>
}
</span>
</td>
<td class="small">@record.DeployedBy</td>
<td class="small">
<TimestampDisplay Value="@record.DeployedAt" />
</td>
<td class="small">
@if (record.CompletedAt.HasValue)
{
<TimestampDisplay Value="@record.CompletedAt.Value" />
}
else
{
<span class="text-muted">—</span>
}
</td>
<td class="small"><code>@(record.RevisionHash?[..Math.Min(8, record.RevisionHash?.Length ?? 0)])</code></td>
<td class="small text-danger">@(record.ErrorMessage ?? "")</td>
</tr>
}
</tbody>
</table>
@if (_totalPages > 1)
{
<nav>
<ul class="pagination pagination-sm justify-content-end">
<li class="page-item @(_currentPage <= 1 ? "disabled" : "")">
<button class="page-link" @onclick="() => GoToPage(_currentPage - 1)">Previous</button>
</li>
@for (int i = 1; i <= _totalPages; i++)
{
var page = i;
<li class="page-item @(page == _currentPage ? "active" : "")">
<button class="page-link" @onclick="() => GoToPage(page)">@(page)</button>
</li>
}
<li class="page-item @(_currentPage >= _totalPages ? "disabled" : "")">
<button class="page-link" @onclick="() => GoToPage(_currentPage + 1)">Next</button>
</li>
</ul>
</nav>
}
}
</div>
@code {
private List<DeploymentRecord> _records = new();
private List<DeploymentRecord> _pagedRecords = new();
private Dictionary<int, string> _instanceNames = new();
private bool _loading = true;
private string? _errorMessage;
private Timer? _refreshTimer;
private int _currentPage = 1;
private int _totalPages;
private const int PageSize = 25;
protected override async Task OnInitializedAsync()
{
await LoadDataAsync();
_refreshTimer = new Timer(_ =>
{
InvokeAsync(async () =>
{
await LoadDataAsync();
StateHasChanged();
});
}, null, TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10));
}
private async Task LoadDataAsync()
{
_loading = _records.Count == 0; // Only show loading on first load
_errorMessage = null;
try
{
_records = (await DeploymentManagerRepository.GetAllDeploymentRecordsAsync())
.OrderByDescending(r => r.DeployedAt)
.ToList();
// Build instance name lookup
var instances = await TemplateEngineRepository.GetAllInstancesAsync();
_instanceNames = instances.ToDictionary(i => i.Id, i => i.UniqueName);
_totalPages = Math.Max(1, (int)Math.Ceiling(_records.Count / (double)PageSize));
if (_currentPage > _totalPages) _currentPage = 1;
UpdatePage();
}
catch (Exception ex)
{
_errorMessage = $"Failed to load deployments: {ex.Message}";
}
_loading = false;
}
private void GoToPage(int page)
{
if (page < 1 || page > _totalPages) return;
_currentPage = page;
UpdatePage();
}
private void UpdatePage()
{
_pagedRecords = _records
.Skip((_currentPage - 1) * PageSize)
.Take(PageSize)
.ToList();
}
private string GetInstanceName(int instanceId) =>
_instanceNames.GetValueOrDefault(instanceId, $"#{instanceId}");
private static string GetStatusBadge(DeploymentStatus status) => status switch
{
DeploymentStatus.Pending => "bg-warning text-dark",
DeploymentStatus.InProgress => "bg-info text-dark",
DeploymentStatus.Success => "bg-success",
DeploymentStatus.Failed => "bg-danger",
_ => "bg-secondary"
};
private static string GetRowClass(DeploymentStatus status) => status switch
{
DeploymentStatus.Failed => "table-danger",
DeploymentStatus.InProgress => "table-info",
_ => ""
};
public void Dispose()
{
_refreshTimer?.Dispose();
}
}

View File

@@ -1,8 +1,368 @@
@page "/deployment/instances"
@using ScadaLink.Security
@using ScadaLink.Commons.Entities.Instances
@using ScadaLink.Commons.Entities.Sites
@using ScadaLink.Commons.Entities.Templates
@using ScadaLink.Commons.Entities.Deployment
@using ScadaLink.Commons.Interfaces.Repositories
@using ScadaLink.Commons.Types.Enums
@using ScadaLink.DeploymentManager
@attribute [Authorize(Policy = AuthorizationPolicies.RequireDeployment)]
@inject ITemplateEngineRepository TemplateEngineRepository
@inject ISiteRepository SiteRepository
@inject IDeploymentManagerRepository DeploymentManagerRepository
@inject DeploymentService DeploymentService
<div class="container mt-4">
<h4>Instances</h4>
<p class="text-muted">Instance management will be available in a future phase.</p>
<div class="container-fluid mt-3">
<div class="d-flex justify-content-between align-items-center mb-3">
<h4 class="mb-0">Instances</h4>
</div>
<ToastNotification @ref="_toast" />
<ConfirmDialog @ref="_confirmDialog" />
@if (_loading)
{
<LoadingSpinner IsLoading="true" />
}
else if (_errorMessage != null)
{
<div class="alert alert-danger">@_errorMessage</div>
}
else
{
@* Filters *@
<div class="row mb-3 g-2">
<div class="col-md-2">
<label class="form-label small">Site</label>
<select class="form-select form-select-sm" @bind="_filterSiteId" @bind:after="ApplyFilters">
<option value="0">All Sites</option>
@foreach (var site in _sites)
{
<option value="@site.Id">@site.Name</option>
}
</select>
</div>
<div class="col-md-2">
<label class="form-label small">Template</label>
<select class="form-select form-select-sm" @bind="_filterTemplateId" @bind:after="ApplyFilters">
<option value="0">All Templates</option>
@foreach (var tmpl in _templates)
{
<option value="@tmpl.Id">@tmpl.Name</option>
}
</select>
</div>
<div class="col-md-2">
<label class="form-label small">Status</label>
<select class="form-select form-select-sm" @bind="_filterStatus" @bind:after="ApplyFilters">
<option value="">All Statuses</option>
<option value="NotDeployed">Not Deployed</option>
<option value="Enabled">Enabled</option>
<option value="Disabled">Disabled</option>
</select>
</div>
<div class="col-md-3">
<label class="form-label small">Search</label>
<input type="text" class="form-control form-control-sm" placeholder="Instance name..."
@bind="_filterSearch" @bind:event="oninput" @bind:after="ApplyFilters" />
</div>
<div class="col-md-3 d-flex align-items-end">
<button class="btn btn-outline-secondary btn-sm" @onclick="LoadDataAsync">Refresh</button>
</div>
</div>
<table class="table table-sm table-striped table-hover">
<thead class="table-dark">
<tr>
<th>Instance Name</th>
<th>Template</th>
<th>Site</th>
<th>Area</th>
<th>Status</th>
<th>Staleness</th>
<th style="width: 240px;">Actions</th>
</tr>
</thead>
<tbody>
@if (_filteredInstances.Count == 0)
{
<tr>
<td colspan="7" class="text-muted text-center">No instances match the current filters.</td>
</tr>
}
@foreach (var inst in _pagedInstances)
{
<tr>
<td><strong>@inst.UniqueName</strong></td>
<td>@GetTemplateName(inst.TemplateId)</td>
<td>@GetSiteName(inst.SiteId)</td>
<td>@(inst.AreaId.HasValue ? GetAreaName(inst.AreaId.Value) : "—")</td>
<td>
<span class="badge @GetStateBadge(inst.State)">@inst.State</span>
</td>
<td>
@{
var isStale = _stalenessMap.GetValueOrDefault(inst.Id);
}
@if (inst.State == InstanceState.NotDeployed)
{
<span class="text-muted small">—</span>
}
else if (isStale)
{
<span class="badge bg-warning text-dark" title="Template changes pending">Stale</span>
}
else
{
<span class="badge bg-light text-dark">Current</span>
}
</td>
<td>
@if (inst.State == InstanceState.Enabled)
{
<button class="btn btn-outline-warning btn-sm py-0 px-1 me-1"
@onclick="() => DisableInstance(inst)" disabled="@_actionInProgress">Disable</button>
}
else if (inst.State == InstanceState.Disabled)
{
<button class="btn btn-outline-success btn-sm py-0 px-1 me-1"
@onclick="() => EnableInstance(inst)" disabled="@_actionInProgress">Enable</button>
}
<button class="btn btn-outline-danger btn-sm py-0 px-1"
@onclick="() => DeleteInstance(inst)" disabled="@_actionInProgress">Delete</button>
</td>
</tr>
}
</tbody>
</table>
@* Pagination *@
@if (_totalPages > 1)
{
<nav>
<ul class="pagination pagination-sm justify-content-end">
<li class="page-item @(_currentPage <= 1 ? "disabled" : "")">
<button class="page-link" @onclick="() => GoToPage(_currentPage - 1)">Previous</button>
</li>
@for (int i = 1; i <= _totalPages; i++)
{
var page = i;
<li class="page-item @(page == _currentPage ? "active" : "")">
<button class="page-link" @onclick="() => GoToPage(page)">@(page)</button>
</li>
}
<li class="page-item @(_currentPage >= _totalPages ? "disabled" : "")">
<button class="page-link" @onclick="() => GoToPage(_currentPage + 1)">Next</button>
</li>
</ul>
</nav>
}
<div class="text-muted small">
@_filteredInstances.Count instance(s) total
</div>
}
</div>
@code {
private List<Instance> _allInstances = new();
private List<Instance> _filteredInstances = new();
private List<Instance> _pagedInstances = new();
private List<Site> _sites = new();
private List<Template> _templates = new();
private List<Area> _allAreas = new();
private Dictionary<int, bool> _stalenessMap = new();
private bool _loading = true;
private string? _errorMessage;
private bool _actionInProgress;
private int _filterSiteId;
private int _filterTemplateId;
private string _filterStatus = string.Empty;
private string _filterSearch = string.Empty;
private int _currentPage = 1;
private int _totalPages;
private const int PageSize = 25;
private ToastNotification _toast = default!;
private ConfirmDialog _confirmDialog = default!;
protected override async Task OnInitializedAsync()
{
await LoadDataAsync();
}
private async Task LoadDataAsync()
{
_loading = true;
_errorMessage = null;
try
{
_allInstances = (await TemplateEngineRepository.GetAllInstancesAsync()).ToList();
_sites = (await SiteRepository.GetAllSitesAsync()).ToList();
_templates = (await TemplateEngineRepository.GetAllTemplatesAsync()).ToList();
// Load areas for all sites
_allAreas.Clear();
foreach (var site in _sites)
{
var areas = await TemplateEngineRepository.GetAreasBySiteIdAsync(site.Id);
_allAreas.AddRange(areas);
}
// Check staleness for deployed instances
_stalenessMap.Clear();
foreach (var inst in _allInstances.Where(i => i.State != InstanceState.NotDeployed))
{
try
{
var comparison = await DeploymentService.GetDeploymentComparisonAsync(inst.Id);
_stalenessMap[inst.Id] = comparison.IsSuccess && comparison.Value.IsStale;
}
catch
{
_stalenessMap[inst.Id] = false;
}
}
ApplyFilters();
}
catch (Exception ex)
{
_errorMessage = $"Failed to load instances: {ex.Message}";
}
_loading = false;
}
private void ApplyFilters()
{
_filteredInstances = _allInstances.Where(i =>
{
if (_filterSiteId > 0 && i.SiteId != _filterSiteId) return false;
if (_filterTemplateId > 0 && i.TemplateId != _filterTemplateId) return false;
if (!string.IsNullOrEmpty(_filterStatus) && i.State.ToString() != _filterStatus) return false;
if (!string.IsNullOrWhiteSpace(_filterSearch) &&
!i.UniqueName.Contains(_filterSearch, StringComparison.OrdinalIgnoreCase)) return false;
return true;
}).OrderBy(i => i.UniqueName).ToList();
_totalPages = Math.Max(1, (int)Math.Ceiling(_filteredInstances.Count / (double)PageSize));
if (_currentPage > _totalPages) _currentPage = 1;
UpdatePage();
}
private void GoToPage(int page)
{
if (page < 1 || page > _totalPages) return;
_currentPage = page;
UpdatePage();
}
private void UpdatePage()
{
_pagedInstances = _filteredInstances
.Skip((_currentPage - 1) * PageSize)
.Take(PageSize)
.ToList();
}
private string GetTemplateName(int templateId) =>
_templates.FirstOrDefault(t => t.Id == templateId)?.Name ?? $"#{templateId}";
private string GetSiteName(int siteId) =>
_sites.FirstOrDefault(s => s.Id == siteId)?.Name ?? $"#{siteId}";
private string GetAreaName(int areaId) =>
_allAreas.FirstOrDefault(a => a.Id == areaId)?.Name ?? $"#{areaId}";
private static string GetStateBadge(InstanceState state) => state switch
{
InstanceState.Enabled => "bg-success",
InstanceState.Disabled => "bg-secondary",
InstanceState.NotDeployed => "bg-light text-dark",
_ => "bg-secondary"
};
private async Task EnableInstance(Instance inst)
{
_actionInProgress = true;
try
{
var user = "system"; // Would come from auth context
var result = await DeploymentService.EnableInstanceAsync(inst.Id, user);
if (result.IsSuccess)
{
_toast.ShowSuccess($"Instance '{inst.UniqueName}' enabled.");
await LoadDataAsync();
}
else
{
_toast.ShowError($"Enable failed: {result.Error}");
}
}
catch (Exception ex)
{
_toast.ShowError($"Enable failed: {ex.Message}");
}
_actionInProgress = false;
}
private async Task DisableInstance(Instance inst)
{
var confirmed = await _confirmDialog.ShowAsync(
$"Disable instance '{inst.UniqueName}'? The instance actor will be stopped.",
"Disable Instance");
if (!confirmed) return;
_actionInProgress = true;
try
{
var user = "system";
var result = await DeploymentService.DisableInstanceAsync(inst.Id, user);
if (result.IsSuccess)
{
_toast.ShowSuccess($"Instance '{inst.UniqueName}' disabled.");
await LoadDataAsync();
}
else
{
_toast.ShowError($"Disable failed: {result.Error}");
}
}
catch (Exception ex)
{
_toast.ShowError($"Disable failed: {ex.Message}");
}
_actionInProgress = false;
}
private async Task DeleteInstance(Instance inst)
{
var confirmed = await _confirmDialog.ShowAsync(
$"Delete instance '{inst.UniqueName}'? This will remove it from the site. Store-and-forward messages will NOT be cleared.",
"Delete Instance");
if (!confirmed) return;
_actionInProgress = true;
try
{
var user = "system";
var result = await DeploymentService.DeleteInstanceAsync(inst.Id, user);
if (result.IsSuccess)
{
_toast.ShowSuccess($"Instance '{inst.UniqueName}' deleted.");
await LoadDataAsync();
}
else
{
_toast.ShowError($"Delete failed: {result.Error}");
}
}
catch (Exception ex)
{
_toast.ShowError($"Delete failed: {ex.Message}");
}
_actionInProgress = false;
}
}