@using EgwCoreLib.Lux.Core.Stats @using EgwCoreLib.Lux.Data.Services @using EgwCoreLib.Lux.Data.Services.General @inject CalcRuidService Calc @inject IJSRuntime JSRuntime @implements IDisposable

Statistiche in tempo reale

@code { private StatsRealtimeDto? stats; private CancellationTokenSource? _cts; protected override async Task OnAfterRenderAsync(bool firstRender) { if (firstRender) { await InitCharts(); _cts = new CancellationTokenSource(); _ = UpdateLoop(_cts.Token); } } public void Dispose() { _cts?.Cancel(); _cts?.Dispose(); } private async Task InitCharts() { await JSRuntime.InvokeVoidAsync("chartsInterop.createChart", "chartRequestsMinute", new { type = "line", data = new { labels = new[] { "Now" }, datasets = new[] { new { label = "Richieste/minuto", data = new[] { 0 }, borderColor = "blue" } } } }); await JSRuntime.InvokeVoidAsync("chartsInterop.createChart", "chartRequestsHour", new { type = "bar", data = new { labels = new[] { "Now" }, datasets = new[] { new { label = "Richieste/ora", data = new[] { 0 }, backgroundColor = "green" } } } }); await JSRuntime.InvokeVoidAsync("chartsInterop.createChart", "chartAvgProcessing", new { type = "line", data = new { labels = new[] { "Now" }, datasets = new[] { new { label = "Tempo medio (s)", data = new[] { 0 }, borderColor = "orange" } } } }); await JSRuntime.InvokeVoidAsync("chartsInterop.createChart", "chartMaxProcessing", new { type = "line", data = new { labels = new[] { "Now" }, datasets = new[] { new { label = "Tempo massimo (s)", data = new[] { 0 }, borderColor = "red" } } } }); } // private async Task UpdateLoop(CancellationToken token) // { // var timer = new PeriodicTimer(TimeSpan.FromSeconds(5)); // try // { // while (await timer.WaitForNextTickAsync(token)) // { // stats = await Calc.GetStatsAsync(); // await JSRuntime.InvokeVoidAsync("chartsInterop.updateChart", // "chartRequestsMinute", // DateTime.Now.ToString("HH:mm:ss"), // stats.RequestsLastMinute); // await JSRuntime.InvokeVoidAsync("chartsInterop.updateBarChart", // "chartRequestsHour", // DateTime.Now.ToString("HH:mm:ss"), // stats.RequestsLastHour); // await JSRuntime.InvokeVoidAsync("chartsInterop.updateChart", // "chartAvgProcessing", // DateTime.Now.ToString("HH:mm:ss"), // stats.ProcessingAvgLastHour); // await JSRuntime.InvokeVoidAsync("chartsInterop.updateChart", // "chartMaxProcessing", // DateTime.Now.ToString("HH:mm:ss"), // stats.ProcessingMaxLastHour); // } // } // catch (OperationCanceledException) // { // // timer fermato // } // } private async Task UpdateLoop(CancellationToken token) { try { while (!token.IsCancellationRequested) { stats = await Calc.GetStatsAsync(); await JSRuntime.InvokeVoidAsync("chartsInterop.updateChart", "chartRequestsMinute", DateTime.Now.ToString("HH:mm:ss"), stats.RequestsLastMinute); await JSRuntime.InvokeVoidAsync("chartsInterop.updateBarChart", "chartRequestsHour", DateTime.Now.ToString("HH:mm:ss"), stats.RequestsLastHour); await JSRuntime.InvokeVoidAsync("chartsInterop.updateChart", "chartAvgProcessing", DateTime.Now.ToString("HH:mm:ss"), stats.ProcessingAvgLastHour); await JSRuntime.InvokeVoidAsync("chartsInterop.updateChart", "chartMaxProcessing", DateTime.Now.ToString("HH:mm:ss"), stats.ProcessingMaxLastHour); // attesa 5 secondi, rispettando il token await Task.Delay(TimeSpan.FromSeconds(5), token); } } catch (OperationCanceledException) { // loop fermato } } }