Files
mapo-core/MP.Stats/Pages/StatusChecks.razor.cs
2026-05-04 14:56:56 +02:00

229 lines
7.1 KiB
C#

using EgwCoreLib.Razor;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Configuration;
using MP.Data.DbModels.Energy;
using MP.Stats.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MP.Stats.Pages
{
public partial class StatusChecks
{
#region Protected Properties
[Inject]
protected IConfiguration ConfMan { get; set; } = null!;
[Inject]
protected MpStatsService StatService { get; set; } = null!;
#endregion Protected Properties
#region Protected Methods
protected override void OnInitialized()
{
MServ.PageName = "Energy Check";
MServ.PageIcon = "fa fa-list-check";
sortField = "dtEvento";
timeoutMin = ConfMan.GetValue<int>("SpecialConf:TimeoutEnergyFlux");
}
protected override async Task OnParametersSetAsync()
{
await ReloadDataAsync();
UpdateTable();
}
#endregion Protected Methods
#region Private Fields
private List<MaccEnergyCheckModel> AllRecord = new();
private List<MaccEnergyCheckModel> SortRecord = new();
private List<MaccEnergyCheckModel> ListPaged = new();
[Inject]
protected MessageService MServ { get; set; } = null!;
private int numRecPage = 10;
private int pageNum = 1;
private int timeoutMin = 30;
private int totalCount = 0;
#endregion Private Fields
#region Private Methods
/// <summary>
/// Scadute se evento > 5 minuti
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
private string CheckScadute(MaccEnergyCheckModel item)
{
DateTime adesso = DateTime.Now;
var dataAge = adesso.Subtract(item.dtEvento).TotalMinutes;
string status = "table-success";
// se supero limite
if (dataAge >= timeoutMin)
{
// se doppio del limite danger sennò warning...
status = dataAge <= 2 * timeoutMin ? "table-warning" : "table-danger";
}
return status;
}
private async Task ReloadDataAsync()
{
AllRecord = await StatService.MacchineEnergyCheckGetAll();
totalCount = AllRecord.Count;
}
private void SaveNumRec(int newNum)
{
numRecPage = newNum;
UpdateTable();
}
protected void SortRequested(Sorter.SortCallBack e)
{
if (sortField == e.ParamName)
{
sortAsc = e.IsAscending;
}
sortField = e.ParamName;
UpdateTable();
}
private bool sortAsc = true;
private string sortField = "";
private void SavePage(int newNum)
{
pageNum = newNum;
UpdateTable();
}
private void UpdateTable()
{
// se ho ordinamento riordino...
if (!string.IsNullOrEmpty(sortField))
{
switch (sortField)
{
case "IdxMacc":
if (sortAsc)
{
SortRecord = AllRecord.OrderBy(x => x.IdxMacchina).ToList();
}
else
{
SortRecord = AllRecord.OrderByDescending(x => x.IdxMacchina).ToList();
}
break;
case "Nome":
if (sortAsc)
{
SortRecord = AllRecord.OrderBy(x => x.Nome).ToList();
}
else
{
SortRecord = AllRecord.OrderByDescending(x => x.Nome).ToList();
}
break;
case "CodFlux":
if (sortAsc)
{
SortRecord = AllRecord.OrderBy(x => x.CodFlux).ToList();
}
else
{
SortRecord = AllRecord.OrderByDescending(x => x.CodFlux).ToList();
}
break;
case "Descr":
if (sortAsc)
{
SortRecord = AllRecord.OrderBy(x => x.Descrizione).ToList();
}
else
{
SortRecord = AllRecord.OrderByDescending(x => x.Descrizione).ToList();
}
break;
case "dtEvento":
if (sortAsc)
{
SortRecord = AllRecord.OrderBy(x => x.dtEvento).ToList();
}
else
{
SortRecord = AllRecord.OrderByDescending(x => x.dtEvento).ToList();
}
break;
case "Valore":
if (sortAsc)
{
SortRecord = AllRecord.OrderBy(x => x.Valore).ToList();
}
else
{
SortRecord = AllRecord.OrderByDescending(x => x.Valore).ToList();
}
break;
default:
SortRecord = AllRecord.OrderBy(x => x.IdxMacchina).ToList();
break;
}
}
else
{
SortRecord = AllRecord.OrderBy(x => x.IdxMacchina).ToList();
}
// esegue paginazione
if (totalCount > numRecPage)
{
ListPaged = SortRecord.Skip((pageNum - 1) * numRecPage).Take(numRecPage).ToList();
}
else
{
ListPaged = SortRecord;
}
// calcolo pareto x scadenze...
DateTime adesso = DateTime.Now;
ParetoTime = AllRecord
.GroupBy(x =>
{
double dataAge = (adesso - x.dtEvento).TotalMinutes;
return dataAge < timeoutMin ? "success" : dataAge < timeoutMin * 2 ? "warning" : "danger";
})
.ToDictionary(g => g.Key, g => g.Count());
// Fallback per le categorie con 0 elementi (opzionale ma consigliato)
ParetoTime["success"] = ParetoTime.GetValueOrDefault("success", 0);
ParetoTime["warning"] = ParetoTime.GetValueOrDefault("warning", 0);
ParetoTime["danger"] = ParetoTime.GetValueOrDefault("danger", 0);
}
/// <summary>
/// Pareto dei tempi secondo scadenza
/// </summary>
private Dictionary<string, int> ParetoTime = new();
#endregion Private Methods
}
}