Central now resolves site Akka remoting addresses from the Sites DB table (NodeAAddress/NodeBAddress) instead of relying on runtime RegisterSite messages. Eliminates the race condition where sites starting before central had their registration dead-lettered. Addresses are cached in CentralCommunicationActor with 60s periodic refresh and on-demand refresh when sites are added/edited/deleted via UI or CLI.
359 lines
13 KiB
Plaintext
359 lines
13 KiB
Plaintext
@page "/admin/sites"
|
|
@using ScadaLink.Security
|
|
@using ScadaLink.Commons.Entities.Sites
|
|
@using ScadaLink.Commons.Interfaces.Repositories
|
|
@using ScadaLink.Communication
|
|
@using ScadaLink.DeploymentManager
|
|
@attribute [Authorize(Policy = AuthorizationPolicies.RequireAdmin)]
|
|
@inject ISiteRepository SiteRepository
|
|
@inject ArtifactDeploymentService ArtifactDeploymentService
|
|
@inject CommunicationService CommunicationService
|
|
@inject AuthenticationStateProvider AuthStateProvider
|
|
|
|
<div class="container-fluid mt-3">
|
|
<div class="d-flex justify-content-between align-items-center mb-3">
|
|
<h4 class="mb-0">Site Management</h4>
|
|
<div>
|
|
<button class="btn btn-outline-warning btn-sm me-1" @onclick="DeployArtifactsToAllSites"
|
|
disabled="@_deploying">
|
|
@if (_deploying)
|
|
{
|
|
<span class="spinner-border spinner-border-sm me-1" role="status"></span>
|
|
}
|
|
Deploy Artifacts to All Sites
|
|
</button>
|
|
<button class="btn btn-primary btn-sm" @onclick="ShowAddForm">Add Site</button>
|
|
</div>
|
|
</div>
|
|
|
|
<ToastNotification @ref="_toast" />
|
|
<ConfirmDialog @ref="_confirmDialog" />
|
|
|
|
@if (_loading)
|
|
{
|
|
<LoadingSpinner IsLoading="true" />
|
|
}
|
|
else if (_errorMessage != null)
|
|
{
|
|
<div class="alert alert-danger">@_errorMessage</div>
|
|
}
|
|
else
|
|
{
|
|
@if (_showForm)
|
|
{
|
|
<div class="card mb-3">
|
|
<div class="card-body">
|
|
<h6 class="card-title">@(_editingSite == null ? "Add New Site" : "Edit Site")</h6>
|
|
<div class="row g-2 align-items-end">
|
|
<div class="col-md-3">
|
|
<label class="form-label small">Name</label>
|
|
<input type="text" class="form-control form-control-sm" @bind="_formName" />
|
|
</div>
|
|
<div class="col-md-3">
|
|
<label class="form-label small">Identifier</label>
|
|
<input type="text" class="form-control form-control-sm" @bind="_formIdentifier"
|
|
disabled="@(_editingSite != null)" />
|
|
</div>
|
|
<div class="col-md-3">
|
|
<label class="form-label small">Description</label>
|
|
<input type="text" class="form-control form-control-sm" @bind="_formDescription" />
|
|
</div>
|
|
<div class="col-md-3">
|
|
<button class="btn btn-success btn-sm me-1" @onclick="SaveSite">Save</button>
|
|
<button class="btn btn-outline-secondary btn-sm" @onclick="CancelForm">Cancel</button>
|
|
</div>
|
|
</div>
|
|
<div class="row g-2 align-items-end mt-1">
|
|
<div class="col-md-6">
|
|
<label class="form-label small">Node A Address</label>
|
|
<input type="text" class="form-control form-control-sm" @bind="_formNodeAAddress"
|
|
placeholder="akka.tcp://scadalink@host:port/user/site-communication" />
|
|
</div>
|
|
<div class="col-md-6">
|
|
<label class="form-label small">Node B Address (optional)</label>
|
|
<input type="text" class="form-control form-control-sm" @bind="_formNodeBAddress"
|
|
placeholder="akka.tcp://scadalink@host:port/user/site-communication" />
|
|
</div>
|
|
</div>
|
|
@if (_formError != null)
|
|
{
|
|
<div class="text-danger small mt-1">@_formError</div>
|
|
}
|
|
</div>
|
|
</div>
|
|
}
|
|
|
|
<table class="table table-sm table-striped table-hover">
|
|
<thead class="table-dark">
|
|
<tr>
|
|
<th>ID</th>
|
|
<th>Name</th>
|
|
<th>Identifier</th>
|
|
<th>Description</th>
|
|
<th>Node A</th>
|
|
<th>Node B</th>
|
|
<th>Data Connections</th>
|
|
<th style="width: 260px;">Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
@if (_sites.Count == 0)
|
|
{
|
|
<tr>
|
|
<td colspan="8" class="text-muted text-center">No sites configured.</td>
|
|
</tr>
|
|
}
|
|
@foreach (var site in _sites)
|
|
{
|
|
<tr>
|
|
<td>@site.Id</td>
|
|
<td>@site.Name</td>
|
|
<td><code>@site.SiteIdentifier</code></td>
|
|
<td class="text-muted small">@(site.Description ?? "—")</td>
|
|
<td class="small text-truncate" style="max-width: 200px;" title="@site.NodeAAddress">@(site.NodeAAddress ?? "—")</td>
|
|
<td class="small text-truncate" style="max-width: 200px;" title="@site.NodeBAddress">@(site.NodeBAddress ?? "—")</td>
|
|
<td>
|
|
@{
|
|
var conns = _siteConnections.GetValueOrDefault(site.Id);
|
|
}
|
|
@if (conns != null && conns.Count > 0)
|
|
{
|
|
@foreach (var conn in conns)
|
|
{
|
|
<span class="badge bg-info text-dark me-1">@conn.Name (@conn.Protocol)</span>
|
|
}
|
|
}
|
|
else
|
|
{
|
|
<span class="text-muted small">None</span>
|
|
}
|
|
</td>
|
|
<td>
|
|
<button class="btn btn-outline-primary btn-sm py-0 px-1 me-1"
|
|
@onclick="() => EditSite(site)">Edit</button>
|
|
<button class="btn btn-outline-warning btn-sm py-0 px-1 me-1"
|
|
@onclick="() => DeployArtifacts(site)"
|
|
disabled="@_deploying">Deploy Artifacts</button>
|
|
<button class="btn btn-outline-danger btn-sm py-0 px-1"
|
|
@onclick="() => DeleteSite(site)">Delete</button>
|
|
</td>
|
|
</tr>
|
|
}
|
|
</tbody>
|
|
</table>
|
|
}
|
|
</div>
|
|
|
|
@code {
|
|
private async Task<string> GetCurrentUserAsync()
|
|
{
|
|
var authState = await AuthStateProvider.GetAuthenticationStateAsync();
|
|
return authState.User.FindFirst("Username")?.Value ?? "unknown";
|
|
}
|
|
|
|
private List<Site> _sites = new();
|
|
private Dictionary<int, List<DataConnection>> _siteConnections = new();
|
|
private bool _loading = true;
|
|
private string? _errorMessage;
|
|
|
|
private bool _showForm;
|
|
private Site? _editingSite;
|
|
private string _formName = string.Empty;
|
|
private string _formIdentifier = string.Empty;
|
|
private string? _formDescription;
|
|
private string? _formNodeAAddress;
|
|
private string? _formNodeBAddress;
|
|
private string? _formError;
|
|
|
|
private bool _deploying;
|
|
|
|
private ToastNotification _toast = default!;
|
|
private ConfirmDialog _confirmDialog = default!;
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
await LoadDataAsync();
|
|
}
|
|
|
|
private async Task LoadDataAsync()
|
|
{
|
|
_loading = true;
|
|
_errorMessage = null;
|
|
try
|
|
{
|
|
_sites = (await SiteRepository.GetAllSitesAsync()).ToList();
|
|
_siteConnections.Clear();
|
|
foreach (var site in _sites)
|
|
{
|
|
var connections = await SiteRepository.GetDataConnectionsBySiteIdAsync(site.Id);
|
|
if (connections.Count > 0)
|
|
{
|
|
_siteConnections[site.Id] = connections.ToList();
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_errorMessage = $"Failed to load sites: {ex.Message}";
|
|
}
|
|
_loading = false;
|
|
}
|
|
|
|
private void ShowAddForm()
|
|
{
|
|
_editingSite = null;
|
|
_formName = string.Empty;
|
|
_formIdentifier = string.Empty;
|
|
_formDescription = null;
|
|
_formNodeAAddress = null;
|
|
_formNodeBAddress = null;
|
|
_formError = null;
|
|
_showForm = true;
|
|
}
|
|
|
|
private void EditSite(Site site)
|
|
{
|
|
_editingSite = site;
|
|
_formName = site.Name;
|
|
_formIdentifier = site.SiteIdentifier;
|
|
_formDescription = site.Description;
|
|
_formNodeAAddress = site.NodeAAddress;
|
|
_formNodeBAddress = site.NodeBAddress;
|
|
_formError = null;
|
|
_showForm = true;
|
|
}
|
|
|
|
private void CancelForm()
|
|
{
|
|
_showForm = false;
|
|
_editingSite = null;
|
|
_formError = null;
|
|
}
|
|
|
|
private async Task SaveSite()
|
|
{
|
|
_formError = null;
|
|
|
|
if (string.IsNullOrWhiteSpace(_formName))
|
|
{
|
|
_formError = "Name is required.";
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
if (_editingSite != null)
|
|
{
|
|
_editingSite.Name = _formName.Trim();
|
|
_editingSite.Description = _formDescription?.Trim();
|
|
_editingSite.NodeAAddress = _formNodeAAddress?.Trim();
|
|
_editingSite.NodeBAddress = _formNodeBAddress?.Trim();
|
|
await SiteRepository.UpdateSiteAsync(_editingSite);
|
|
}
|
|
else
|
|
{
|
|
if (string.IsNullOrWhiteSpace(_formIdentifier))
|
|
{
|
|
_formError = "Identifier is required.";
|
|
return;
|
|
}
|
|
var site = new Site(_formName.Trim(), _formIdentifier.Trim())
|
|
{
|
|
Description = _formDescription?.Trim(),
|
|
NodeAAddress = _formNodeAAddress?.Trim(),
|
|
NodeBAddress = _formNodeBAddress?.Trim()
|
|
};
|
|
await SiteRepository.AddSiteAsync(site);
|
|
}
|
|
|
|
await SiteRepository.SaveChangesAsync();
|
|
CommunicationService.RefreshSiteAddresses();
|
|
_showForm = false;
|
|
_editingSite = null;
|
|
_toast.ShowSuccess(_editingSite == null ? "Site created." : "Site updated.");
|
|
await LoadDataAsync();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_formError = $"Save failed: {ex.Message}";
|
|
}
|
|
}
|
|
|
|
private async Task DeleteSite(Site site)
|
|
{
|
|
var confirmed = await _confirmDialog.ShowAsync(
|
|
$"Delete site '{site.Name}' ({site.SiteIdentifier})? This cannot be undone.",
|
|
"Delete Site");
|
|
|
|
if (!confirmed) return;
|
|
|
|
try
|
|
{
|
|
await SiteRepository.DeleteSiteAsync(site.Id);
|
|
await SiteRepository.SaveChangesAsync();
|
|
CommunicationService.RefreshSiteAddresses();
|
|
_toast.ShowSuccess($"Site '{site.Name}' deleted.");
|
|
await LoadDataAsync();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_toast.ShowError($"Delete failed: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
private async Task DeployArtifacts(Site site)
|
|
{
|
|
_deploying = true;
|
|
try
|
|
{
|
|
var command = await ArtifactDeploymentService.BuildDeployArtifactsCommandAsync();
|
|
var user = await GetCurrentUserAsync();
|
|
var result = await ArtifactDeploymentService.RetryForSiteAsync(
|
|
site.SiteIdentifier, command, user);
|
|
|
|
if (result.IsSuccess)
|
|
_toast.ShowSuccess($"Artifacts deployed to '{site.Name}'.");
|
|
else
|
|
_toast.ShowError($"Deploy to '{site.Name}' failed: {result.Error}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_toast.ShowError($"Deploy to '{site.Name}' failed: {ex.Message}");
|
|
}
|
|
finally
|
|
{
|
|
_deploying = false;
|
|
}
|
|
}
|
|
|
|
private async Task DeployArtifactsToAllSites()
|
|
{
|
|
_deploying = true;
|
|
try
|
|
{
|
|
var command = await ArtifactDeploymentService.BuildDeployArtifactsCommandAsync();
|
|
var user = await GetCurrentUserAsync();
|
|
var result = await ArtifactDeploymentService.DeployToAllSitesAsync(command, user);
|
|
|
|
if (result.IsSuccess)
|
|
{
|
|
var summary = result.Value!;
|
|
_toast.ShowSuccess(
|
|
$"Artifacts deployed: {summary.SuccessCount} succeeded, {summary.FailureCount} failed.");
|
|
}
|
|
else
|
|
{
|
|
_toast.ShowError($"Artifact deployment failed: {result.Error}");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_toast.ShowError($"Artifact deployment failed: {ex.Message}");
|
|
}
|
|
finally
|
|
{
|
|
_deploying = false;
|
|
}
|
|
}
|
|
}
|