From 076495494d2c184b4bea962880e320c11aa622de Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Mon, 12 Dec 2022 20:09:06 +0100 Subject: [PATCH] Bilanciamento condizioni reload pagina intera: - solo ogni 10 min - reset timers al cambio pagina --- MP.SPEC/Components/ListPARAMS.razor.cs | 51 ++++++++------ MP.SPEC/Data/MpDataService.cs | 32 +++++++++ MP.SPEC/MP.SPEC.csproj | 2 +- MP.SPEC/Pages/PARAMS.razor.cs | 93 +++++++++++++++++++++++++- MP.SPEC/Resources/ChangeLog.html | 2 +- MP.SPEC/Resources/VersNum.txt | 2 +- MP.SPEC/Resources/manifest.xml | 2 +- 7 files changed, 158 insertions(+), 26 deletions(-) diff --git a/MP.SPEC/Components/ListPARAMS.razor.cs b/MP.SPEC/Components/ListPARAMS.razor.cs index bbc70671..12593937 100644 --- a/MP.SPEC/Components/ListPARAMS.razor.cs +++ b/MP.SPEC/Components/ListPARAMS.razor.cs @@ -61,24 +61,34 @@ namespace MP.SPEC.Components public void ElapsedTimer(object? source, System.Timers.ElapsedEventArgs e) { - if (!isLoading && LiveUpdate) + // controlo se URL diverso da apgina params... + if (!NavManager.Uri.Contains("PARAMS")) { - aTimer.Stop(); - // inizio misura esecuzione - Stopwatch stopWatch = new Stopwatch(); - stopWatch.Start(); - var pUpd = Task.Run(async () => + aTimer.Enabled = false; + } + else + { + aTimer.Enabled = true; + if (!isLoading && LiveUpdate) { - await Task.Delay(1); - await InvokeAsync(() => reloadData(true)); - }); - pUpd.Wait(); - // misuro tempo esecuzione - stopWatch.Stop(); - TimeSpan ts = stopWatch.Elapsed; - int deltaTime = RefreshPeriod - (int)ts.TotalMilliseconds; - aTimer.Interval = deltaTime > 100 ? deltaTime : 100; - aTimer.Start(); + // inizio misura esecuzione + Stopwatch stopWatch = new Stopwatch(); + stopWatch.Start(); + var pUpd = Task.Run(async () => + { + await Task.Delay(1); + await InvokeAsync(() => reloadData(true)); + }); + pUpd.Wait(); + // misuro tempo esecuzione + stopWatch.Stop(); + TimeSpan ts = stopWatch.Elapsed; + double deltaTime = RefreshPeriod - ts.TotalMilliseconds; + // aggiungo fattore di disturbo random... + double mFact = 0.01 * random.Next(80, 120); + aTimer.Interval = mFact * (deltaTime > 100 ? deltaTime : 100); + //aTimer.Start(); + } } } @@ -98,7 +108,7 @@ namespace MP.SPEC.Components dataTo = (DateTime)SelDtMax; } - SearchRecords = await MDService.FluxLogGetLastFilt(dataTo, dataFrom, SelMacchina, SelFlux, MaxRecord, 1.1 * RefreshPeriod / 1000); + SearchRecords = await MDService.FluxLogGetLastFilt(dataTo, dataFrom, SelMacchina, SelFlux, MaxRecord, RefreshPeriod / 1000); totalCount = SearchRecords.Count; ListRecords = SearchRecords.Skip(numRecord * (currPage - 1)).Take(numRecord).ToList(); await Task.Delay(1); @@ -114,6 +124,7 @@ namespace MP.SPEC.Components aTimer = new System.Timers.Timer(RefreshPeriod); aTimer.Elapsed += ElapsedTimer; aTimer.Enabled = true; + aTimer.AutoReset = true; aTimer.Start(); } @@ -121,6 +132,8 @@ namespace MP.SPEC.Components #region Protected Fields + protected Random random = new Random(); + #endregion Protected Fields #region Protected Properties @@ -195,10 +208,8 @@ namespace MP.SPEC.Components #region Private Fields - private System.Timers.Timer aTimer = null!; - private int _totalCount = 0; - + private System.Timers.Timer aTimer = null!; private FluxLog? currRecord = null; private List? ListRecords; diff --git a/MP.SPEC/Data/MpDataService.cs b/MP.SPEC/Data/MpDataService.cs index 53070b2e..ddc1321b 100644 --- a/MP.SPEC/Data/MpDataService.cs +++ b/MP.SPEC/Data/MpDataService.cs @@ -575,6 +575,37 @@ namespace MP.SPEC.Data return await dbController.EvListInsert(newRec); } + /// + /// Imposta in redis la scadenza della pagina x il reload + /// + /// + /// + public DateTime ExpiryReloadParamGet() + { + DateTime dtRif = DateTime.Now; + string currKey = $"{redisParamPageExp}"; + RedisValue rawData = redisDb.StringGet(currKey); + if (rawData.HasValue) + { + dtRif = JsonConvert.DeserializeObject($"{rawData}"); + } + return dtRif; + } + + /// + /// Imposta in redis la scadenza della pagina x il reload + /// + /// + /// + public bool ExpiryReloadParamSet(DateTime expTime) + { + bool fatto = false; + string currKey = $"{redisParamPageExp}"; + string rawData = JsonConvert.SerializeObject(expTime); + fatto = redisDb.StringSet(currKey, rawData); + return fatto; + } + public async Task FlushRedisCache() { await Task.Delay(1); @@ -1429,6 +1460,7 @@ namespace MP.SPEC.Data private const string redisOdlByBatch = redisXdlData + "OdlByBatch"; private const string redisOdlCurrByMac = redisXdlData + "OdlByMac"; private const string redisOdlList = redisXdlData + "OdlList"; + private const string redisParamPageExp = redisBaseAddrSpec + "Cache:ParamPage"; private const string redisPOdlByOdl = redisXdlData + "POdlByOdl"; private const string redisPOdlByPOdl = redisXdlData + "POdlByPOdl"; private const string redisPOdlList = redisXdlData + "POdlList"; diff --git a/MP.SPEC/MP.SPEC.csproj b/MP.SPEC/MP.SPEC.csproj index 93010083..644ec40e 100644 --- a/MP.SPEC/MP.SPEC.csproj +++ b/MP.SPEC/MP.SPEC.csproj @@ -5,7 +5,7 @@ enable enable MP.SPEC - 6.16.2212.1214 + 6.16.2212.1220 diff --git a/MP.SPEC/Pages/PARAMS.razor.cs b/MP.SPEC/Pages/PARAMS.razor.cs index e537924d..e4c1cdcb 100644 --- a/MP.SPEC/Pages/PARAMS.razor.cs +++ b/MP.SPEC/Pages/PARAMS.razor.cs @@ -1,6 +1,8 @@ -using MP.Data.DatabaseModels; +using Microsoft.AspNetCore.Components; +using MP.Data.DatabaseModels; using MP.SPEC.Components; using MP.SPEC.Data; +using NLog; namespace MP.SPEC.Pages { @@ -10,19 +12,70 @@ namespace MP.SPEC.Pages public void Dispose() { + aTimer.Elapsed -= ElapsedTimer; + aTimer.Stop(); + aTimer.Close(); + aTimer.Dispose(); GC.Collect(); } + public void ElapsedTimer(object? source, System.Timers.ElapsedEventArgs e) + { + // controllo se sia scaduto tempo massimo (in redis) x ricaricare pagina in modo completo... + var dtRif = MDService.ExpiryReloadParamGet(); + if (dtRif > DateTime.Now) + { + Log.Trace("----- Elapsed check PARAMS.cs -----"); + } + else + { + var pUpd = Task.Run(async () => + { + MDService.ExpiryReloadParamSet(DateTime.Now.AddSeconds(intForceReload)); + aTimer.Elapsed -= ElapsedTimer; + aTimer.Stop(); + aTimer.Close(); + aTimer.Dispose(); + await Task.Delay(1); + await InvokeAsync(() => forceReloadCache()); + }); + pUpd.Wait(); + } + } + + public void StartTimer() + { + Random random = new Random(); + double multPer = 0.01 * random.Next(50, 300); + aTimer = new System.Timers.Timer(RefreshPeriod * multPer); + aTimer.Elapsed += ElapsedTimer; + aTimer.Enabled = true; + aTimer.AutoReset = true; + aTimer.Start(); + } + #endregion Public Methods #region Protected Fields protected int CurrCounter = 0; - + protected int intForceReload = 600; protected DataPager? pagerODL = null!; #endregion Protected Fields + #region Protected Properties + + [Inject] + protected MpDataService MDService { get; set; } = null!; + + protected int RefreshPeriod + { + get => currFilter.TempoAgg; + } + + #endregion Protected Properties + #region Protected Methods protected async Task detailSel(FluxLog newRec) @@ -52,6 +105,19 @@ namespace MP.SPEC.Pages numRecord = newNum; } + /// + /// Esegue svuotamento forzato cache + reload pagina ogni minuto... + /// + protected async Task forceReloadCache() + { + Log.Debug("----- forceReloadCache on PARAMS.cs -----"); + await Task.Delay(1); + await MDService.FlushRedisCache(); + await Task.Delay(1); + // rimando a pagina corrente + NavManager.NavigateTo(NavManager.Uri, true, true); + } + protected void ForceReloadPage(int newNum) { currPage = newNum; @@ -76,6 +142,8 @@ namespace MP.SPEC.Pages modFilter.LiveUpdate = (currPage == 1); currFilter = modFilter; await Task.Delay(1); + setExpiryReload(); + StartTimer(); isFiltering = false; } @@ -103,6 +171,13 @@ namespace MP.SPEC.Pages #endregion Protected Methods + #region Private Fields + + private static Logger Log = LogManager.GetCurrentClassLogger(); + private System.Timers.Timer aTimer = null!; + + #endregion Private Fields + #region Private Properties private SelectFluxParams currFilter { get; set; } = new SelectFluxParams(); @@ -114,8 +189,12 @@ namespace MP.SPEC.Pages } private bool isFiltering { get; set; } = false; + private bool isLoading { get; set; } = true; + [Inject] + private NavigationManager NavManager { get; set; } = null!; + private int numRecord { get => currFilter.NumRec; @@ -128,6 +207,16 @@ namespace MP.SPEC.Pages #region Private Methods + private void setExpiryReload() + { + // verifico se ho una scadenza expiry del periodo desiderato, sennò imposto nuova... + var dtRif = MDService.ExpiryReloadParamGet(); + if (dtRif <= DateTime.Now) + { + MDService.ExpiryReloadParamSet(DateTime.Now.AddSeconds(intForceReload)); + } + } + private async Task updateFilter(SelectFluxParams newParams) { isFiltering = false; diff --git a/MP.SPEC/Resources/ChangeLog.html b/MP.SPEC/Resources/ChangeLog.html index 09159d86..86ee3164 100644 --- a/MP.SPEC/Resources/ChangeLog.html +++ b/MP.SPEC/Resources/ChangeLog.html @@ -1,6 +1,6 @@ Modulo MAPOSPEC -

Versione: 6.16.2212.1214

+

Versione: 6.16.2212.1220


Note di rilascio:
  • diff --git a/MP.SPEC/Resources/VersNum.txt b/MP.SPEC/Resources/VersNum.txt index 77ac8965..00223c89 100644 --- a/MP.SPEC/Resources/VersNum.txt +++ b/MP.SPEC/Resources/VersNum.txt @@ -1 +1 @@ -6.16.2212.1214 +6.16.2212.1220 diff --git a/MP.SPEC/Resources/manifest.xml b/MP.SPEC/Resources/manifest.xml index d6afba70..c877e1e4 100644 --- a/MP.SPEC/Resources/manifest.xml +++ b/MP.SPEC/Resources/manifest.xml @@ -1,6 +1,6 @@ - 6.16.2212.1214 + 6.16.2212.1220 https://nexus.steamware.net/repository/SWS/MP-SPEC/stable/LAST/MP.SPEC.zip https://nexus.steamware.net/repository/SWS/MP-SPEC/stable/LAST/ChangeLog.html false