Files
limanapp/LiMan.UI/Components/AggregateStats.razor.cs
2025-12-04 09:29:02 +01:00

356 lines
12 KiB
C#

using LiMan.DB;
using LiMan.DB.DBModels;
using LiMan.DB.DTO;
using LiMan.UI.Data;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace LiMan.UI.Components
{
public partial class AggregateStats : IDisposable
{
#region Public Methods
public void Dispose()
{
LMDService.EnrollMessPipe.EA_NewMessage -= async (sender, e) => await EnrollMessPipe_EA_NewMessage(sender, e);
LMDService.UpdActMessPipe.EA_NewMessage -= async (sender, e) => await UpdActMessPipe_EA_NewMessage(sender, e);
MServ.EA_SelCodImp -= async () => await MServ_EA_SelCodImp();
MServ.EA_SelCodInst -= async () => await MServ_EA_SelCodInst();
}
#endregion Public Methods
#region Protected Properties
/// <summary>
/// Codice installazione selezionata
/// </summary>
protected string CodInstSel
{
get => codInstSel;
set
{
if (codInstSel != value)
{
codInstSel = value;
isLoading = true;
var pUpd = Task.Run(async () =>
{
await ReloadData();
MServ.UsrParamSet("CodInst", value);
MServ.ReportSelCodInst();
});
pUpd.Wait();
isLoading = false;
}
}
}
/// <summary>
/// Tipo app selezionato
/// </summary>
protected string CodTipoApp
{
get => codTipoApp;
set
{
if (codTipoApp != value)
{
codTipoApp = value;
isLoading = true;
var pUpd = Task.Run(async () =>
{
await ReloadData();
// resetto eventuale App
MServ.UsrParamSet("CodApp", "");
MServ.ReportSelCodApp();
});
pUpd.Wait();
isLoading = false;
}
}
}
[Inject]
protected LiManDataService LMDService { get; set; } = null!;
[Inject]
protected MessageService MServ { get; set; } = null!;
#endregion Protected Properties
#region Protected Methods
protected override async Task OnInitializedAsync()
{
LMDService.EnrollMessPipe.EA_NewMessage += async (sender, e) => await EnrollMessPipe_EA_NewMessage(sender, e);
LMDService.UpdActMessPipe.EA_NewMessage += async (sender, e) => await UpdActMessPipe_EA_NewMessage(sender, e);
MServ.EA_SelCodImp += async () => await MServ_EA_SelCodImp();
MServ.EA_SelCodInst += async () => await MServ_EA_SelCodInst();
await ReloadLicData();
await ReloadData();
}
#endregion Protected Methods
#region Private Fields
private string CodDeviceSel = "";
private string codInstSel = "";
private Dictionary<string, string> DetailFilt = new Dictionary<string, string>();
private bool HasFiltClienti = false;
private bool HasFiltImpiego = false;
private List<InstallazioneModel> ListInstall;
/// <summary>
/// Lista Task in stato DONE
/// </summary>
private Dictionary<string, string> TaskDone = new Dictionary<string, string>();
/// <summary>
/// Lista Task in stato REQUESTED
/// </summary>
private Dictionary<string, string> TaskReq = new Dictionary<string, string>();
/// <summary>
/// Lista Task in stato RUNNING
/// </summary>
private Dictionary<string, string> TaskRun = new Dictionary<string, string>();
/// <summary>
/// Status degli udpater da mostrare
/// </summary>
private Core.Enum.UpdStatus UpdStatusReq = Core.Enum.UpdStatus.None;
#endregion Private Fields
#region Private Properties
/// <summary>
/// Impianto selezionato
/// </summary>
private string CodImpSel { get; set; } = "";
private string codTipoApp { get; set; } = "";
private string DevName { get; set; } = "";
private DateTime DtFine { get; set; } = DateTime.Today.AddHours(DateTime.Now.Hour + 1);
private DateTime DtInizio { get; set; } = DateTime.Today.AddMonths(-1);
private InstallStatusDTO InfoStats { get; set; } = new InstallStatusDTO();
private bool isLoading { get; set; } = false;
[Inject]
private IJSRuntime JSRuntime { get; set; }
private List<DeviceDTO> ListDev2Show { get; set; } = new List<DeviceDTO>();
private Dictionary<string, DeviceDTO> ListDevices { get; set; } = new Dictionary<string, DeviceDTO>();
#endregion Private Properties
#region Private Methods
private string BtnState(int numRec, string cssAct = "text-success")
{
return numRec > 0 ? cssAct : "text-secondary";
}
/// <summary>
/// lancio richiesta udpate app info x ogni IMP gestito...
/// </summary>
private async Task DoMassiveRequest(Core.Enum.EgwAccTask reqType)
{
if (!await JSRuntime.InvokeAsync<bool>("confirm", $"Sicuro di voler inviare un comando di {reqType} a tutti i devices?"))
return;
if (!await JSRuntime.InvokeAsync<bool>("confirm", $"Sicuro di voler proseguire? Verranno interesati {InfoStats.TotalUpdaterAct} devices."))
return;
var listCodImp = InfoStats.InstDevices.Select(x => x.Value.CodImp).ToList();
LMDService.TaskReqAdd(listCodImp, reqType, $"{DateTime.Now:yyyy-MM-dd HH:mm:ss}");
}
/// <summary>
/// Evento refresh legato a ricezione evento da MessagePipe
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async Task EnrollMessPipe_EA_NewMessage(object sender, EventArgs e)
{
PubSubEventArgs currArgs = (PubSubEventArgs)e;
// qualsiasi messaggio fa scattare reload data
if (!string.IsNullOrEmpty(currArgs.newMessage))
{
isLoading = true;
await InvokeAsync(StateHasChanged);
await ReloadLicData();
await ReloadData();
isLoading = false;
await InvokeAsync(StateHasChanged);
}
}
/// <summary>
/// Selezionato CodImp (PC)
/// </summary>
/// <exception cref="NotImplementedException"></exception>
private async Task MServ_EA_SelCodImp()
{
isLoading = true;
// se trovo salvate in preferenze utente le info necessarie...
CodImpSel = MServ.UsrParamGet("CodImp");
HasFiltImpiego = !string.IsNullOrEmpty(CodImpSel);
DetailFilt.Clear();
DetailFilt.Add("Cliente", MServ.UsrParamGet("Cliente"));
DevName = MServ.UsrParamGet("PcInst");
DetailFilt.Add("PC", DevName);
DetailFilt.Add("SW ver.", MServ.UsrParamGet("Version"));
DetailFilt.Add("IP", MServ.UsrParamGet("PcIP"));
DetailFilt.Add("Started", MServ.UsrParamGet("PcRestart"));
ReloadTaskStatus();
isLoading = false;
await InvokeAsync(StateHasChanged);
}
/// <summary>
/// Selezionato CodInstall (Cliente)
/// </summary>
/// <exception cref="NotImplementedException"></exception>
private async Task MServ_EA_SelCodInst()
{
isLoading = true;
CodInstSel = MServ.UsrParamGet("CodInst");
HasFiltClienti = !string.IsNullOrEmpty(CodInstSel);
ReloadDevices();
isLoading = false;
await InvokeAsync(StateHasChanged);
}
private async Task ReloadData()
{
isLoading = true;
InfoStats = await LMDService.InstallStatusGetInfo(DtInizio, DtFine, CodInstSel, CodTipoApp);
ReloadDevices();
ReloadTaskStatus();
isLoading = false;
}
private void ReloadDevices()
{
ListDevices = new Dictionary<string, DeviceDTO>();
if (InfoStats != null)
{
// se necessario filtro...
if (HasFiltClienti)
{
ListDevices = InfoStats
.InstDevices
.Where(x => x.Value.CodInst == CodInstSel)
.OrderBy(x => x.Value.DevName)
.ToDictionary(x => x.Key, x => x.Value);
}
else
{
ListDevices = InfoStats
.InstDevices
.OrderBy(x => x.Value.DevName)
.ToDictionary(x => x.Key, x => x.Value);
}
}
}
private async Task ReloadLicData()
{
ListInstall = await LMDService.InstallazioniNextGetAll();
}
/// <summary>
/// Esegue update stato task
/// </summary>
private void ReloadTaskStatus()
{
TaskReq = LMDService.TaskListReqGet();
TaskRun = LMDService.TaskListRunGet();
TaskDone = LMDService.TaskListDoneGet();
}
/// <summary>
/// Imposta status visualizzazione dettaglio
/// </summary>
/// <param name="newStatus"></param>
private async void ShowDetail(Core.Enum.UpdStatus newStatus)
{
UpdStatusReq = newStatus;
ListDev2Show = new List<DeviceDTO>();
Dictionary<string, string> dictCurr = new Dictionary<string, string>();
switch (UpdStatusReq)
{
case Core.Enum.UpdStatus.Pending:
dictCurr = TaskReq;
break;
case Core.Enum.UpdStatus.Running:
dictCurr = TaskRun;
break;
case Core.Enum.UpdStatus.Done:
dictCurr = TaskDone;
break;
case Core.Enum.UpdStatus.None:
default:
break;
}
// filtro x stato richiesto...
List<DeviceDTO> list2show = new List<DeviceDTO>();
foreach (var item in ListDevices)
{
if (dictCurr.ContainsKey(item.Key))
{
// sistemo dataora in obj...
string rawDate = dictCurr[item.Key];
if (DateTime.TryParse(rawDate, out var date))
{
item.Value.LastUpdate = date;
}
list2show.Add(item.Value);
}
}
ListDev2Show = list2show;
await InvokeAsync(StateHasChanged);
}
/// <summary>
/// Evento refresh legato a ricezione evento da MessagePipe
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async Task UpdActMessPipe_EA_NewMessage(object sender, EventArgs e)
{
PubSubEventArgs currArgs = (PubSubEventArgs)e;
// qualsiasi messaggio fa scattare reload data
if (!string.IsNullOrEmpty(currArgs.newMessage))
{
isLoading = true;
ReloadTaskStatus();
isLoading = false;
await InvokeAsync(StateHasChanged);
}
}
#endregion Private Methods
}
}