diff --git a/Hcs.WebApp/BackgroundServices/OperationExecutionState.cs b/Hcs.WebApp/BackgroundServices/OperationExecutionState.cs index 3d07cf1..4d9c555 100644 --- a/Hcs.WebApp/BackgroundServices/OperationExecutionState.cs +++ b/Hcs.WebApp/BackgroundServices/OperationExecutionState.cs @@ -14,6 +14,7 @@ namespace Hcs.WebApp.BackgroundServices public bool Ready { get; set; } + public event Action OnOperationCreated; public event OperationStarted OnOperationStarted; public event OperationExecuted OnOperationExecuted; public event OperationEnded OnOperationEnded; @@ -21,6 +22,8 @@ namespace Hcs.WebApp.BackgroundServices public void EnqueueOperation(Operation operation) { operationsInQueue.Enqueue(operation); + + OnOperationCreated?.Invoke(operation); } public bool TryDequeueOperation(out Operation operation) diff --git a/Hcs.WebApp/Components/Pages/Operations.razor b/Hcs.WebApp/Components/Pages/Operations.razor new file mode 100644 index 0000000..91b6ed6 --- /dev/null +++ b/Hcs.WebApp/Components/Pages/Operations.razor @@ -0,0 +1,154 @@ +@page "/operations" + +@using Hcs.WebApp.BackgroundServices +@using Hcs.WebApp.Services +@using Microsoft.AspNetCore.Authorization + +@implements IDisposable + +@attribute [Authorize] + +@inject AuthenticationStateProvider AuthenticationStateProvider +@inject HeadquartersService HeadquartersService +@inject OperationExecutionState OperationExecutionState +@inject ResultWaitState ResultWaitState + +Операции + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +@code { + enum OperationPageState + { + Init, + Loading, + Idle + } + + OperationPageState state; + RadzenDataGrid dataGrid; + ICollection? operations; + + protected override async Task OnAfterRenderAsync(bool firstRender) + { + await base.OnAfterRenderAsync(firstRender); + + if (firstRender) + { + ChangeState(OperationPageState.Loading); + + var state = await AuthenticationStateProvider.GetAuthenticationStateAsync(); + if (state.User.IsInRole(AppRole.ADMINISTRATOR_TYPE) || state.User.IsInRole(AppRole.OPERATOR_TYPE)) + { + operations = await HeadquartersService.GetOperationsAsync(); + + OperationExecutionState.OnOperationCreated += OnOperationCreated; + OperationExecutionState.OnOperationStarted += OnOperationStarted; + OperationExecutionState.OnOperationExecuted += OnOperationExecuted; + OperationExecutionState.OnOperationEnded += OnOperationEnded; + + ResultWaitState.OnOperationEnded += OnOperationEnded; + } + + ChangeState(OperationPageState.Idle); + } + } + + void ChangeState(OperationPageState state) + { + if (this.state == state) return; + + this.state = state; + + StateHasChanged(); + } + + void OnOperationCreated(Operation operation) + { + InvokeAsync(() => + { + operations?.Add(operation); + + dataGrid.Reload(); + }); + } + + void OnOperationStarted(int operationId, int campaignId, DateTime startedAt) + { + InvokeAsync(() => + { + var targetOperation = operations?.FirstOrDefault(x => x.Id == operationId); + if (targetOperation != null) + { + targetOperation.StartedAt = startedAt; + + dataGrid.Reload(); + } + }); + } + + void OnOperationExecuted(int operationId, int campaignId, string messageGuid) + { + InvokeAsync(() => + { + var targetOperation = operations?.FirstOrDefault(x => x.Id == operationId); + if (targetOperation != null) + { + targetOperation.MessageGuid = messageGuid; + + dataGrid.Reload(); + } + }); + } + + void OnOperationEnded(int operationId, int campaignId, DateTime endedAt, string failureReason) + { + InvokeAsync(() => + { + var targetOperation = operations?.FirstOrDefault(x => x.Id == operationId); + if (targetOperation != null) + { + targetOperation.EndedAt = endedAt; + targetOperation.FailureReason = failureReason; + + dataGrid.Reload(); + } + }); + } + + public void Dispose() + { + OperationExecutionState.OnOperationCreated -= OnOperationCreated; + OperationExecutionState.OnOperationStarted -= OnOperationStarted; + OperationExecutionState.OnOperationExecuted -= OnOperationExecuted; + OperationExecutionState.OnOperationEnded -= OnOperationEnded; + + ResultWaitState.OnOperationEnded -= OnOperationEnded; + } +} diff --git a/Hcs.WebApp/Services/HeadquartersService.cs b/Hcs.WebApp/Services/HeadquartersService.cs index f2fd474..d218ad8 100644 --- a/Hcs.WebApp/Services/HeadquartersService.cs +++ b/Hcs.WebApp/Services/HeadquartersService.cs @@ -41,6 +41,12 @@ namespace Hcs.WebApp.Services select operation).ToListAsync(); } + public async Task> GetOperationsAsync() + { + using var context = GetNewContext(); + return await context.Operations.ToListAsync(); + } + public async Task> GetOperationsAsync(int campaignId) { using var context = GetNewContext();