[CHANGE] Reworked event console
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
@page "/Services"
|
||||
@using Manager.App.Services.System
|
||||
@using Manager.App.Components.Application.System
|
||||
@implements IDisposable
|
||||
|
||||
@inject BackgroundServiceManager ServiceManager
|
||||
@inject BackgroundServiceRegistry ServiceRegistry
|
||||
|
||||
<title>Services</title>
|
||||
|
||||
@@ -37,47 +38,5 @@
|
||||
</PagerContent>
|
||||
</MudDataGrid>
|
||||
|
||||
<MudPaper Elevation="0" Class="mt-3" Style="flex: 1; display: flex; flex-direction: column; min-height: 0;">
|
||||
<MudStack Class="ml-2 mb-2" Spacing="1">
|
||||
<MudText Typo="Typo.h5">Service events</MudText>
|
||||
<MudText Typo="Typo.caption">@($"{_serviceEvents.Count}/{VisibleEventCapacity} events")</MudText>
|
||||
</MudStack>
|
||||
<div class="console-container">
|
||||
<Virtualize Items="_serviceEvents" Context="serviceEvent">
|
||||
<div class="log-line">
|
||||
@($"{serviceEvent.Date:HH:mm:ss} | {serviceEvent.Severity} | {serviceEvent.Source} - {serviceEvent.Message}")
|
||||
</div>
|
||||
</Virtualize>
|
||||
</div>
|
||||
</MudPaper>
|
||||
|
||||
<style>
|
||||
.console-container {
|
||||
background-color: #1e1e1e;
|
||||
color: #9c9898;
|
||||
padding: 10px;
|
||||
border-radius: 8px;
|
||||
flex: 1;
|
||||
overflow-y: auto;
|
||||
font-family: monospace;
|
||||
}
|
||||
|
||||
.log-line {
|
||||
display: flex;
|
||||
justify-content: start;
|
||||
align-items: center;
|
||||
gap: 0.25rem;
|
||||
}
|
||||
|
||||
.log-info {
|
||||
color: #9cdcfe;
|
||||
}
|
||||
|
||||
.log-warning {
|
||||
color: #dcdcaa;
|
||||
}
|
||||
|
||||
.log-error {
|
||||
color: #f44747;
|
||||
}
|
||||
</style>
|
||||
<EventConsole AsyncEnumerable="@GetEventAsyncEnumerable()" InitialEvents="@GetInitialEvents()"
|
||||
Elevation="0" Class="mt-3" Style="flex: 1; display: flex; flex-direction: column; min-height: 0;"/>
|
||||
|
@@ -1,4 +1,3 @@
|
||||
using DotBased.Logging;
|
||||
using Manager.App.Extensions;
|
||||
using Manager.App.Services;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
@@ -7,58 +6,31 @@ namespace Manager.App.Components.Pages;
|
||||
|
||||
public partial class Services : ComponentBase
|
||||
{
|
||||
private const int VisibleEventCapacity = 500;
|
||||
private string _searchText = "";
|
||||
private List<ExtendedBackgroundService> _backgroundServices = [];
|
||||
|
||||
private List<ServiceEvent> _serviceEvents = [];
|
||||
private CancellationTokenSource _cts = new();
|
||||
private readonly CancellationTokenSource _cts = new();
|
||||
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
_backgroundServices = ServiceManager.GetServices();
|
||||
_ = Task.Run(() => ReadEventStreamsAsync(_cts.Token));
|
||||
_backgroundServices = ServiceRegistry.GetServices();
|
||||
}
|
||||
|
||||
private Func<ExtendedBackgroundService, bool> QuickFilter
|
||||
=> x => string.IsNullOrWhiteSpace(_searchText) || $"{x.Name} {x.Description} {x.State} {x.ExecuteInterval}".Contains(_searchText);
|
||||
|
||||
private async Task ReadEventStreamsAsync(CancellationToken token)
|
||||
private IAsyncEnumerable<ServiceEvent> GetEventAsyncEnumerable()
|
||||
{
|
||||
if (_backgroundServices.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var totalToGet = VisibleEventCapacity / _backgroundServices.Count;
|
||||
_serviceEvents.AddRange(_backgroundServices.SelectMany(x => x.ProgressEvents.Items.TakeLast(totalToGet)));
|
||||
|
||||
var asyncEnumerators = _backgroundServices.Select(x => x.ProgressEvents.GetStreamAsync());
|
||||
await foreach (var serviceEvent in AsyncEnumerableExtensions.Merge(asyncEnumerators, token))
|
||||
{
|
||||
if (!_serviceEvents.Contains(serviceEvent))
|
||||
{
|
||||
_serviceEvents.Add(serviceEvent);
|
||||
}
|
||||
|
||||
if (_serviceEvents.Count > VisibleEventCapacity)
|
||||
{
|
||||
_serviceEvents.RemoveAt(0);
|
||||
}
|
||||
|
||||
await InvokeAsync(StateHasChanged);
|
||||
}
|
||||
return AsyncEnumerableExtensions.Merge(asyncEnumerators, CancellationToken.None);
|
||||
}
|
||||
|
||||
private string GetLogClass(ServiceEvent serviceEvent) =>
|
||||
serviceEvent.Severity switch
|
||||
{
|
||||
LogSeverity.Info => "log-info",
|
||||
LogSeverity.Warning => "log-warning",
|
||||
LogSeverity.Error => "log-error",
|
||||
_ => "log-info"
|
||||
};
|
||||
|
||||
|
||||
private List<ServiceEvent> GetInitialEvents()
|
||||
{
|
||||
var totalToGet = 1000 / _backgroundServices.Count;
|
||||
var initial = _backgroundServices.SelectMany(x => x.ProgressEvents.Items.TakeLast(totalToGet));
|
||||
return initial.ToList();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_cts.Cancel();
|
||||
|
Reference in New Issue
Block a user