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

582 lines
19 KiB
C#

using EgwCoreLib.Razor.Data;
using LiMan.DB;
using LiMan.DB.DTO;
using LiMan.UI.Data;
using Microsoft.AspNetCore.Components;
using Microsoft.IdentityModel.Tokens;
using Microsoft.JSInterop;
using Newtonsoft.Json;
using Org.BouncyCastle.Ocsp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using static Core.Enum;
using static EgwCoreLib.Razor.Sorter;
namespace LiMan.UI.Components
{
public partial class InstAppPareto : IDisposable
{
#region Public Properties
[Parameter]
public List<AppRelStatusDTO> AppDetList { get; set; } = new List<AppRelStatusDTO>();
[Parameter]
public AppStatusDTO CurrRecord { get; set; } = null!;
#endregion Public Properties
#region Public Methods
public void Dispose()
{
LMDService.TaskMessPipe.EA_NewMessage -= async (sender, e) => await TaskMessPipe_EA_NewMessage(sender, e);
}
#endregion Public Methods
#region Protected Properties
[Inject]
protected IJSRuntime JSRuntime { get; set; } = null!;
protected List<AppRelStatusDTO> ListRecord { get; set; } = new List<AppRelStatusDTO>();
[Inject]
protected LiManDataService LMDService { get; set; } = null!;
[Inject]
protected MessageService MServ { get; set; } = null!;
protected List<AppRelStatusDTO> SearchRecord { get; set; } = new List<AppRelStatusDTO>();
#endregion Protected Properties
#region Protected Methods
protected override void OnInitialized()
{
LMDService.TaskMessPipe.EA_NewMessage += async (sender, e) => await TaskMessPipe_EA_NewMessage(sender, e);
}
protected override void OnParametersSet()
{
ReloadData();
ReloadTaskStatus();
ReloadTaskResults();
ReloadCharts();
}
/// <summary>
/// Reset filtro cliente...
/// </summary>
/// <param name="currRec"></param>
protected void ResetCliente()
{
HasFlitCli = false;
CodInstSel = "";
// salvo nelle preferenze i valori CodImp e PcInst
MServ.UsrParamSet("Cliente", "");
MServ.UsrParamSet("CodInst", "");
MServ.UsrParamSet("CodImp", "");
MServ.UsrParamSet("PcInst", "");
MServ.UsrParamSet("PcRestart", "");
MServ.UsrParamSet("PcIP", "");
MServ.UsrParamSet("Version", "");
// Richiedo azione x gestione dettaglio info
MServ.ReportSelCodInst();
}
/// <summary>
/// Reset filtro PC/Impiego...
/// </summary>
/// <param name="currRec"></param>
protected void ResetCodImp()
{
HasFiltImp = false;
CodImpSel = "";
// salvo nelle preferenze i valori CodImp e PcInst
MServ.UsrParamSet("CodImp", "");
MServ.UsrParamSet("PcInst", "");
MServ.UsrParamSet("PcRestart", "");
MServ.UsrParamSet("PcIP", "");
MServ.UsrParamSet("Version", "");
// Richiedo azione x gestione dettaglio info
MServ.ReportSelCodImp();
}
/// <summary>
/// Set filtro cliente...
/// </summary>
/// <param name="currRec"></param>
protected void SetCliente(AppRelStatusDTO currRec)
{
HasFlitCli = true;
CodInstSel = currRec.CodInst;
// salvo nelle preferenze i valori CodImp e PcInst
MServ.UsrParamSet("Cliente", currRec.Cliente);
MServ.UsrParamSet("CodInst", currRec.CodInst);
MServ.UsrParamSet("CodImp", "");
MServ.UsrParamSet("PcInst", "");
MServ.UsrParamSet("PcRestart", "");
MServ.UsrParamSet("PcIP", "");
MServ.UsrParamSet("Version", "");
// Richiedo azione x gestione dettaglio info
MServ.ReportSelCodInst();
// se era selezionato impianto lo deseleziona...
if (HasFiltImp)
{
CodImpSel = "";
HasFiltImp = false;
MServ.ReportSelCodImp();
}
}
/// <summary>
/// Set filtro PC/Impiego...
/// </summary>
/// <param name="currRec"></param>
protected void SetCodImp(AppRelStatusDTO currRec)
{
HasFiltImp = true;
CodImpSel = currRec.CodImp;
AppInstList = AppDetList
.Where(x => x.CodImp == currRec.CodImp && x.IdxSubLic == currRec.IdxSubLic)
.ToList();
// se era selezionato cliente lo riseleziono...
if (HasFlitCli)
{
CodInstSel = currRec.CodInst;
MServ.ReportSelCodInst();
}
// salvo nelle preferenze i valori CodImp e PcInst
MServ.UsrParamSet("Cliente", currRec.Cliente);
MServ.UsrParamSet("CodInst", currRec.CodInst);
MServ.UsrParamSet("CodImp", currRec.CodImp);
MServ.UsrParamSet("PcInst", currRec.PcInst);
MServ.UsrParamSet("PcRestart", "");
MServ.UsrParamSet("PcIP", "");
MServ.UsrParamSet("Version", "");
ReloadTaskStatus();
ReloadTaskResults();
// cerco in stats info
if (RunStats != null && RunStats.Count > 0)
{
if (RunStats.ContainsKey("Startup"))
{
MServ.UsrParamSet("PcRestart", RunStats["Startup"]);
}
if (RunStats.ContainsKey("Version"))
{
MServ.UsrParamSet("Version", RunStats["Version"]);
}
}
// cerco nei device info
if (DevInfo != null && DevInfo.Count > 0)
{
string ipKey = "Int.0.Ipv4";
var recFound = DevInfo.Where(x => x.Key.StartsWith(ipKey)).ToList();
if (recFound.Count > 0)
{
var recIp = recFound.FirstOrDefault();
if (recIp.Key.StartsWith(ipKey))
{
MServ.UsrParamSet("PcIP", recIp.Value);
}
}
}
// Richiedo azione x gestione dettaglio info
MServ.ReportSelCodImp();
}
protected void setNumPage(int newNum)
{
currPage = newNum;
ReloadData();
isLoading = false;
}
protected void setNumRec(int newNum)
{
currPage = 1;
numRecord = newNum;
ReloadData();
isLoading = false;
}
protected void SortRequested(SortCallBack e)
{
isLoading = true;
if (sortField == e.ParamName)
{
sortAsc = e.IsAscending;
}
sortField = e.ParamName;
ReloadData();
isLoading = false;
}
#endregion Protected Methods
#region Private Fields
private List<AppRelStatusDTO> AppInstList = new List<AppRelStatusDTO>();
private List<DoughnutStyling> ChartColor = new List<DoughnutStyling>();
private double[] ChartData = new double[1];
private string[] ChartLabels = new string[1];
private string CodImpSel = "";
private string CodInstSel = "";
private int currPage = 1;
private Dictionary<string, string> DevInfo = new Dictionary<string, string>();
private bool HasFiltImp = false;
private bool HasFlitCli = false;
private bool isLoading = false;
private Dictionary<string, string> LicInfo = new Dictionary<string, string>();
private int numRecord = 10;
private Dictionary<string, string> RunStats = new Dictionary<string, string>();
private bool showDetail = false;
private string sKey = "InstAppPareto";
/// <summary>
/// Elenco task eseguiti
/// </summary>
private Dictionary<string, string> TaskDone = new Dictionary<string, string>();
private int totalCount = 0;
#endregion Private Fields
#region Private Properties
private string CssDivDetail
{
get => HasFiltImp ? "col-9" : "col-5";
}
private string CssDivMain
{
get => HasFiltImp ? "col-3" : "col-7";
}
private string CssResetCli
{
get => HasFlitCli ? "" : "disabled";
}
private string CssResetImp
{
get => HasFiltImp ? "" : "disabled";
}
private bool sortAsc
{
get
{
bool answ = false;
var sVal = MServ.UsrParamGet($"{sKey}_sort");
if (!string.IsNullOrEmpty(sVal))
{
bool.TryParse(sVal, out answ);
}
return answ;
}
set => MServ.UsrParamSet($"{sKey}_sort", $"{value}");
}
private string sortField
{
get => MServ.UsrParamGet($"{sKey}_field");
set => MServ.UsrParamSet($"{sKey}_field", $"{value}");
}
#endregion Private Properties
#region Private Methods
private string CssBtnImp(string CodImp)
{
return HasFiltImp && CodImpSel == CodImp ? "btn-primary" : "btn-outline-primary";
}
private string CssBtnInst(string CodInst)
{
return HasFlitCli && CodInstSel == CodInst ? "btn-dark" : "btn-outline-dark";
}
private string cssVers(AppRelStatusDTO rec)
{
string answ = "";
switch (rec.UpToDateStatus)
{
case 4:
answ = "text-success fw-bold";
break;
case 3:
answ = "text-success";
break;
case 2:
answ = "text-warning";
break;
case 1:
answ = "text-danger";
break;
case 0:
answ = "text-danger fw-bold";
break;
default:
break;
}
return answ;
}
private void MServ_EA_TaskUpdate()
{
ReloadTaskStatus();
ReloadTaskResults();
}
/// <summary>
/// Registrazione richiesta update app remota
/// </summary>
/// <param name="selRec"></param>
/// <returns></returns>
private async Task ProcReqUpdate(AppRelStatusDTO selRec)
{
if (!await JSRuntime.InvokeAsync<bool>("confirm", $"Sicuro di voler forzare update applicativo {selRec.CodApp} sul Device? Eventuali configurazioni presenti saranno ripristinate, altrimenti andrà verificato manualmente l'esito dell'operazione."))
return;
// invio task update applicazione
LMDService.TaskReqAdd(CodImpSel, EgwAccTask.TargetAppUpdate, selRec.CodApp);
ReloadTaskStatus();
ReloadTaskResults();
// invio notifica update...
LMDService.UpdActMessPipe.sendMessage(selRec.CodApp);
}
private void ReloadCharts()
{
// sistemo info x grafico... se ho impieghi uso quello, altrimenti istanze...
if (CurrRecord.NumImp > 0)
{
ChartData = SearchRecord
.GroupBy(x => x.VersNumInstall)
.Select(g => (double)g.Sum(x => x.NumImp))
.ToArray();
}
else
{
ChartData = SearchRecord
.GroupBy(x => x.VersNumInstall)
.Select(g => (double)g.Count())
.ToArray();
}
ChartLabels = SearchRecord
.GroupBy(x => x.VersNumInstall)
.Select(g => g.Key)
.ToArray();
ChartColor.Clear();
var listStatus = SearchRecord
.GroupBy(x => x.VersNumInstall)
.Select(g => g.First().UpToDateStatus)
.ToList();
foreach (var item in listStatus)
{
switch (item)
{
case 4:
ChartColor.Add(new DoughnutStyling("#12FF06", "#006900"));
break;
case 3:
ChartColor.Add(new DoughnutStyling("#ABFF69", "#11AA11"));
break;
case 2:
ChartColor.Add(new DoughnutStyling("#FFE493", "#AAFF00"));
break;
case 1:
ChartColor.Add(new DoughnutStyling("#FF6900", "#953500"));
break;
case 0:
ChartColor.Add(new DoughnutStyling("#AA3600", "#AA0000"));
break;
default:
break;
}
}
}
private void ReloadData()
{
SearchRecord = CurrRecord.DetailInstalled;
totalCount = SearchRecord.Count;
switch (sortField)
{
case "NumImp":
if (sortAsc)
{
SearchRecord = SearchRecord.OrderBy(x => x.NumImp).ThenByDescending(x => x.VersNumInstall).ToList();
}
else
{
SearchRecord = SearchRecord.OrderByDescending(x => x.NumImp).ThenByDescending(x => x.VersNumInstall).ToList();
}
break;
case "VersNumInstall":
if (sortAsc)
{
SearchRecord = SearchRecord.OrderBy(x => x.VersNumInstall).ThenBy(x => x.PcInst).ToList();
}
else
{
SearchRecord = SearchRecord.OrderByDescending(x => x.VersNumInstall).ThenBy(x => x.PcInst).ToList();
}
break;
case "PcInst":
if (sortAsc)
{
SearchRecord = SearchRecord.OrderBy(x => x.PcInst).ThenByDescending(x => x.VersNumInstall).ToList();
}
else
{
SearchRecord = SearchRecord.OrderByDescending(x => x.PcInst).ThenByDescending(x => x.VersNumInstall).ToList();
}
break;
case "CodInst":
if (sortAsc)
{
SearchRecord = SearchRecord.OrderBy(x => x.CodInst).ThenByDescending(x => x.VersNumInstall).ToList();
}
else
{
SearchRecord = SearchRecord.OrderByDescending(x => x.CodInst).ThenByDescending(x => x.VersNumInstall).ToList();
}
break;
default:
SearchRecord = SearchRecord
.OrderByDescending(x => x.VersNumInstall)
.ThenBy(x => x.CodInst)
.ThenBy(x => x.PcInst)
.ToList();
break;
}
// paginazione
ListRecord = SearchRecord
.Skip((currPage - 1) * numRecord)
.Take(numRecord)
.ToList();
}
/// <summary>
/// Rilegge risultato task come obj specifici in area DONE...
/// </summary>
/// <returns></returns>
private void ReloadTaskResults()
{
DevInfo = new Dictionary<string, string>();
RunStats = new Dictionary<string, string>();
LicInfo = new Dictionary<string, string>();
if (TaskDone != null && TaskDone.Count > 0)
{
// cerco Device Info specifiche
string devInfoKey = $"{EgwAccTask.DeviceInfoGet}";
if (TaskDone.ContainsKey(devInfoKey))
{
string rawData = TaskDone[devInfoKey];
if (!string.IsNullOrEmpty(rawData) && rawData.Count() > 2)
{
DevInfo = JsonConvert.DeserializeObject<Dictionary<string, string>>(rawData);
}
}
// cerco Info Stats
if (TaskDone.ContainsKey("RunStats"))
{
string rawData = TaskDone["RunStats"];
if (!string.IsNullOrEmpty(rawData) && rawData.Count() > 2)
{
RunStats = JsonConvert.DeserializeObject<Dictionary<string, string>>(rawData);
}
}
// cerco Info License
if (TaskDone.ContainsKey("LicInfo"))
{
string rawData = TaskDone["LicInfo"];
if (!string.IsNullOrEmpty(rawData) && rawData.Count() > 2)
{
LicInfo = JsonConvert.DeserializeObject<Dictionary<string, string>>(rawData);
}
}
}
}
/// <summary>
/// Esegue update stato task
/// </summary>
private void ReloadTaskStatus()
{
TaskDone = new Dictionary<string, string>();
if (!string.IsNullOrEmpty(CodImpSel))
{
TaskDone = LMDService.TaskDoneGet(CodImpSel);
}
}
/// <summary>
/// Evento refresh legato a ricezione evento da MessagePipe
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async Task TaskMessPipe_EA_NewMessage(object sender, EventArgs e)
{
PubSubEventArgs currArgs = (PubSubEventArgs)e;
// qualsiasi messaggio fa scattare reload data
if (!string.IsNullOrEmpty(currArgs.newMessage))
{
// verifico se sia il cod impianto selezionato, nel faso forzo refreesh...
if (!string.IsNullOrEmpty(CodImpSel) && CodImpSel.Equals(currArgs.newMessage))
{
isLoading = true;
ReloadTaskStatus();
ReloadTaskResults();
isLoading = false;
await InvokeAsync(StateHasChanged);
}
}
}
#endregion Private Methods
}
}