using global::Microsoft.AspNetCore.Components; using Microsoft.JSInterop; using MP.Core.DTO; using MP.Core.Objects; using MP.SPEC.Data; using NLog; using System.Diagnostics; using static EgwCoreLib.Utils.DtUtils; namespace MP.SPEC.Pages { public partial class FluxLogStatus { #region Protected Fields protected double currVal = 0; protected Enums.DataInterval IntReq = Enums.DataInterval.hour; protected double nextVal = 0; protected int numRecPage = 10; protected int pageNum = 1; protected int totalCount = 0; protected int totRecords = 0; protected Enums.ValSelection ValMode = Enums.ValSelection.First; #endregion Protected Fields #region Protected Properties protected Dictionary actDbMaintStats { get; set; } = new Dictionary(); protected List actProcDedupStats { get; set; } = new List(); protected List fluxList { get; set; } = new List(); [Inject] protected IJSRuntime JSRuntime { get; set; } = null!; protected Dictionary ListMacchineAll { get; set; } = new Dictionary(); protected int MaxVal { get => fluxList.Count; } [Inject] protected MpDataService MDataServ { get; set; } = null!; protected int NumItem { get => numItem; set { if (numItem != value) { // controllo valori ammissibili numItem = value >= 1 ? value : 1; // aggiorno tempi updateExpTime(); } } } protected string strDbMaintTimeExp { get; set; } = "-"; protected string strPrTimeExp { get; set; } = "-"; #endregion Protected Properties #region Protected Methods /// /// Esegue cleanup dati /// /// protected async Task DoCleanup() { if (!await JSRuntime.InvokeAsync("confirm", "Attenzione! il task di Data Cleanup eliminerà in modo definitivo i dati in eccesso secondo lo schema impostato, sei sicuro di voler procedere?")) return; isProcessing = true; lastDedupExecTime = "..."; Stopwatch sw = Stopwatch.StartNew(); int currStep = 1; int stepVal = 1; foreach (var item in fluxList) { // aggiorno valori currVal = (currStep - 1) * stepVal; nextVal = currStep * stepVal; await InvokeAsync(StateHasChanged); // periodo: fix per imporre fine alla mezzanotte di oggi (= processo tutto ieri) if (CurrPeriodo.Fine > DateTime.Today) { CurrPeriodo.Fine = DateTime.Today; } // processo i flussi 1:1 x mandare DoUpdate ad avanzamento await MDataServ.FluxLogDataRedux(idxMaccSel, new List { item }, CurrPeriodo, ValMode, IntReq, NumItem); currStep++; } //await MDataServ.FluxLogDataRedux(idxMaccSel, fluxList, CurrPeriodo, ValMode, IntReq, maxItem); sw.Stop(); lastDedupExecTime = $"{sw.Elapsed.Minutes}m {sw.Elapsed.Seconds}s"; isProcessing = false; } /// /// Esegue cleanup dati /// /// protected async Task IdxRebuild() { if (!await JSRuntime.InvokeAsync("confirm", "Manutenzione Database: si tratta di un operazione che può richiedere un tempo levato sei sicuro di voler procedere?")) return; isReindexing = true; Stopwatch sw = Stopwatch.StartNew(); lastDbMaintTime = "..."; await InvokeAsync(StateHasChanged); await MDataServ.ForceDbMaint(true, true, true, 1000, 10, 50); sw.Stop(); lastDbMaintTime = $"{sw.Elapsed.Minutes}m {sw.Elapsed.Seconds}s"; isReindexing = false; } protected override void OnInitialized() { ReloadStats(); ReloadData(); } protected void ReloadMacchine() { if (ListMacchineAll == null || ListMacchineAll.Count == 0) { var rawData = MDataServ.MacchineGetFilt("*"); // trasformo! if (rawData != null) { ListMacchineAll = rawData.ToDictionary(x => x.IdxMacchina, x => $"{x.IdxMacchina} | {x.Nome}"); } } } protected async Task SaveFluxList(List newList) { fluxList = newList; await Task.Delay(1); } protected void SaveNumRec(int newNum) { if (numRecPage != newNum) { numRecPage = newNum; } } protected void SavePage(int newNum) { if (pageNum != newNum) { pageNum = newNum; } } protected async Task SetPeriodo(Periodo newPeriodo) { if (!CurrPeriodo.Equals(newPeriodo)) { CurrPeriodo = newPeriodo; } await Task.Delay(1); } protected async Task SetTotCount(int numRec) { totalCount = numRec; await Task.Delay(1); } protected async Task SetTotRec(int numRec) { totRecords = numRec; await Task.Delay(1); updateExpTime(); } #endregion Protected Methods #region Private Fields private static NLog.Logger Log = LogManager.GetCurrentClassLogger(); /// /// Tempo atteso processing /// - da calcolare in base al num eventi e alla tab logProcessing... /// private int expTimeMsec = 3000; private string lastDbMaintTime = ""; private string lastDedupExecTime = ""; #endregion Private Fields #region Private Properties private Periodo CurrPeriodo { get; set; } = new Periodo(); private string idxMaccSel { get; set; } = ""; private bool isProcessing { get; set; } = false; private bool isReindexing { get; set; } = false; private double msProcEst { get; set; } = 0.2; private int numItem { get; set; } = 1; #endregion Private Properties #region Private Methods private void ReloadData() { ReloadMacchine(); DateTime dtEnd = DateTime.Today.AddDays(1); DateTime dtStart = dtEnd.AddMonths(-1); CurrPeriodo = new Periodo(dtStart, dtEnd); } private void ReloadStats() { actProcDedupStats = MDataServ.ProcFLStats(); actDbMaintStats = MDataServ.DbDedupStats(); } private void updateExpTime() { // calcolo tempo stimato e mostro... double msProcEst = 0.15 * numItem; // recupero statistiche x tipoInt e num eventi... var calcStats = actProcDedupStats.Where(x => x.Interval == IntReq && x.Num4Int == numItem); //var calcStats = actStats.Where(x => x.IdxMacchina == idxMaccSel && x.Interval == IntReq && x.Num4Int == numItem); if (calcStats != null && calcStats.Count() > 0) { var totSec = calcStats.Sum(x => x.ProcTime); var totalRec = calcStats.Sum(x => x.NumRec); msProcEst = totSec * 1000 / (totalRec > 1 ? totalRec : 1); } int numFlux = fluxList.Count > 1 ? fluxList.Count : 1; expTimeMsec = (int)Math.Ceiling(msProcEst * totRecords / numFlux); strPrTimeExp = "-"; if (totRecords > 0) { var TotalTime = TimeSpan.FromMilliseconds(msProcEst * totRecords); if (TotalTime.TotalMinutes > 60) { strPrTimeExp = $"{TotalTime.Hours}h {TotalTime.Minutes}m"; } else { strPrTimeExp = $"{TotalTime.Minutes}m {TotalTime.Seconds}s"; } } // ora sistemo le statistiche DB maintenance double estimDbMaintSec = 150; var dbTotSec = actDbMaintStats.Sum(x => x.Value); var dbNumItem = actDbMaintStats.Count(); if (dbNumItem > 0) { estimDbMaintSec = dbTotSec / dbNumItem; } var TotalTimeDb = TimeSpan.FromSeconds(estimDbMaintSec); if (TotalTimeDb.TotalMinutes > 60) { strDbMaintTimeExp = $"{TotalTimeDb.Hours}h {TotalTimeDb.Minutes}m"; } else { strDbMaintTimeExp = $"{TotalTimeDb.Minutes}m {TotalTimeDb.Seconds}s"; } } #endregion Private Methods } }