From 6fa2b6eae2bfbfd19084f4ac436f23a13843bae7 Mon Sep 17 00:00:00 2001 From: "zaccaria.majid" Date: Tue, 4 Apr 2023 11:07:50 +0200 Subject: [PATCH] fix init vocabolario --- .../Controllers/WebDoorCreatorController.cs | 32 ++++- .../Components/Gen/NavMenuHorizontal.razor.cs | 19 ++- .../Components/Hardware/HardwareList.razor.cs | 5 +- WebDoorCreator.UI/Data/WDCUserService.cs | 25 +++- .../Data/WDCVocabularyService.cs | 126 ++++++++++++++++-- .../Data/WebDoorCreatorService.cs | 53 +++++++- 6 files changed, 233 insertions(+), 27 deletions(-) diff --git a/WebDoorCreator.Data/Controllers/WebDoorCreatorController.cs b/WebDoorCreator.Data/Controllers/WebDoorCreatorController.cs index 7bd00ac..157cbf4 100644 --- a/WebDoorCreator.Data/Controllers/WebDoorCreatorController.cs +++ b/WebDoorCreator.Data/Controllers/WebDoorCreatorController.cs @@ -2,12 +2,9 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using NLog; -using System.Collections.Generic; -using System.ComponentModel.Design; using System.Data; using WebDoorCreator.Data.DbModels; using WebDoorCreator.Data.DTO; -using static WebDoorCreator.Core.Enum; namespace WebDoorCreator.Data.Controllers { @@ -404,6 +401,7 @@ namespace WebDoorCreator.Data.Controllers await Task.Delay(1); return dbResult; } + /// /// Getting door data by orderId /// @@ -473,6 +471,30 @@ namespace WebDoorCreator.Data.Controllers return fatto; } + /// + /// Estraggo tutte le lingue disponibili per questa applicazione + /// + /// + public List LanguageGetAll() + { + List dbResult = new List(); + using (WDCDataContext localDbCtx = new WDCDataContext(_configuration)) + { + try + { + // extracting entire set + dbResult = localDbCtx + .DbSetLanguages + .ToList(); + } + catch (Exception exc) + { + Log.Error($"Error in LanguageGetAll:{Environment.NewLine}{exc}"); + } + } + return dbResult; + } + /// /// ListValues list (All) /// @@ -801,7 +823,6 @@ namespace WebDoorCreator.Data.Controllers } return dbResult; } - public Dictionary> VocLemmaGetAll() { Dictionary> DTOResult = new Dictionary>(); @@ -824,7 +845,7 @@ namespace WebDoorCreator.Data.Controllers foreach (var lingua in lingue) { Dictionary dict = new Dictionary(); - foreach (var lemma in allVoc.Where(x=>x.Lingua == lingua)) + foreach (var lemma in allVoc.Where(x => x.Lingua == lingua)) { dict.Add(lemma.Lemma, lemma.Traduzione); } @@ -880,6 +901,7 @@ namespace WebDoorCreator.Data.Controllers } return fatto; } + #endregion Public Methods #region Private Fields diff --git a/WebDoorCreator.UI/Components/Gen/NavMenuHorizontal.razor.cs b/WebDoorCreator.UI/Components/Gen/NavMenuHorizontal.razor.cs index 646db5b..be64d9f 100644 --- a/WebDoorCreator.UI/Components/Gen/NavMenuHorizontal.razor.cs +++ b/WebDoorCreator.UI/Components/Gen/NavMenuHorizontal.razor.cs @@ -19,7 +19,7 @@ namespace WebDoorCreator.UI.Components.Gen public EventCallback E_UserRole { get; set; } public EventCallback E_UserId { get; set; } - public EventCallback>>> E_VocLemmas { get; set; } + public EventCallback>> E_VocLemmas { get; set; } public List listCompanies { get; set; } = new List(); @@ -101,7 +101,6 @@ namespace WebDoorCreator.UI.Components.Gen WDCUService.EA_UserClaims -= OnNewUserClaims; WDCUService.EA_UserCurrCompany -= OnNewUserCurrComp; WDCUService.EA_UserId -= OnNewUserId; - WDCVService.EA_VocabularyLemmas -= OnNewVocLemma; } public async void OnNewUserClaims() @@ -196,9 +195,19 @@ namespace WebDoorCreator.UI.Components.Gen WDCUService.EA_UserClaims += OnNewUserClaims; WDCUService.EA_UserCurrCompany += OnNewUserCurrComp; WDCUService.EA_UserId += OnNewUserId; - WDCVService.EA_VocabularyLemmas += OnNewVocLemma; + //WDCVService.EA_VocabularyLemmas += OnNewVocLemma; } + //protected override async Task OnAfterRenderAsync(bool firstRender) + //{ + // if (firstRender) + // { + // listVocLemmas = new Dictionary>(); + + // listVocLemmas = await WDCService.VocLemmaGetAll(); + // } + //} + protected override async Task OnParametersSetAsync() { await Task.Delay(1); @@ -221,10 +230,6 @@ namespace WebDoorCreator.UI.Components.Gen // reset preliminare listRecord = new List(); listCompanies = new List(); - listVocLemmas = new Dictionary>(); - //await WDCService.FlushRedisCache(); - - listVocLemmas = await WDCService.VocLemmaGetAll(); var user = await _userManager.FindByNameAsync(currUser); if (user != null) diff --git a/WebDoorCreator.UI/Components/Hardware/HardwareList.razor.cs b/WebDoorCreator.UI/Components/Hardware/HardwareList.razor.cs index f860b7d..0074d0e 100644 --- a/WebDoorCreator.UI/Components/Hardware/HardwareList.razor.cs +++ b/WebDoorCreator.UI/Components/Hardware/HardwareList.razor.cs @@ -1,7 +1,9 @@ using Microsoft.AspNetCore.Components; +using System.Globalization; using WebDoorCreator.Data.DbModels; using WebDoorCreator.UI.Data; + namespace WebDoorCreator.UI.Components.Hardware { public partial class HardwareList @@ -24,11 +26,12 @@ namespace WebDoorCreator.UI.Components.Hardware } } + //CultureInfo ci = CultureInfo.InstalledUICulture; protected string translate(string lemma) { string answ = ""; - answ = WDVService.traduci("IT",lemma); + answ = WDVService.traduci("EN", lemma); return answ; } diff --git a/WebDoorCreator.UI/Data/WDCUserService.cs b/WebDoorCreator.UI/Data/WDCUserService.cs index 7431177..1af9414 100644 --- a/WebDoorCreator.UI/Data/WDCUserService.cs +++ b/WebDoorCreator.UI/Data/WDCUserService.cs @@ -12,6 +12,9 @@ public event Action EA_UserCurrCompany = null!; + public event Action EA_CurrLanguage = null!; + + #endregion Public Events #region Public Properties @@ -67,7 +70,18 @@ } } } - + public string? currLanguage + { + get => _currLanguage; + set + { + if (_currLanguage != value) + { + _currLanguage = value; + reportCurrentLanguage(); + } + } + } #endregion Public Properties #region Protected Methods @@ -103,7 +117,13 @@ EA_UserCurrCompany?.Invoke(); } } - + protected void reportCurrentLanguage() + { + if (EA_CurrLanguage != null) + { + EA_CurrLanguage?.Invoke(); + } + } #endregion Protected Methods #region Private Properties @@ -112,6 +132,7 @@ private List? _userClaims { get; set; } = null; private string _userRole { get; set; } = ""; private int _userCurrComp { get; set; } //= -1; + private string? _currLanguage { get; set; } = null; #endregion Private Properties } diff --git a/WebDoorCreator.UI/Data/WDCVocabularyService.cs b/WebDoorCreator.UI/Data/WDCVocabularyService.cs index 88e36e2..5c3c33c 100644 --- a/WebDoorCreator.UI/Data/WDCVocabularyService.cs +++ b/WebDoorCreator.UI/Data/WDCVocabularyService.cs @@ -1,10 +1,53 @@ -using Microsoft.EntityFrameworkCore.Storage.ValueConversion.Internal; +using Microsoft.AspNetCore.Identity.UI.Services; +using Newtonsoft.Json; +using NLog; +using Org.BouncyCastle.Asn1.X500; +using StackExchange.Redis; +using System.Diagnostics; +using WebDoorCreator.Data.Controllers; namespace WebDoorCreator.UI.Data { public class WDCVocabularyService { - public event Action EA_VocabularyLemmas = null!; + public static WebDoorCreatorController dbController = null!; + + public WDCVocabularyService(IConfiguration configuration, IConnectionMultiplexer redisConnMult, IEmailSender emailSender) + { + _configuration = configuration; + _emailSender = emailSender; + // Conf cache + redisConn = redisConnMult; + redisDb = this.redisConn.GetDatabase(); + + // json serializer... FIX errore loop circolare https://www.ryadel.com/en/jsonserializationexception-self-referencing-loop-detected-error-fix-entity-framework-asp-net-core/ + JSSettings = new JsonSerializerSettings() + { + ReferenceLoopHandling = ReferenceLoopHandling.Ignore + }; + + // cod app + CodApp = _configuration["CodApp"]; + // Conf DB + string connStr = _configuration.GetConnectionString("WDC.DB"); + if (string.IsNullOrEmpty(connStr)) + { + Log.Info("ConnString empty!"); + } + else + { + dbController = new WebDoorCreatorController(configuration); + } + + if(VocabularyLemmas == null) + { + VocabularyLemmas = Task.Run(async () => await VocLemmaGetAll()).Result; + } + + Log.Info("Avviata classe WDCVocabularyService"); + } + + public Dictionary>? VocabularyLemmas { @@ -14,11 +57,18 @@ namespace WebDoorCreator.UI.Data if (_vocabularyLemmas != value) { _vocabularyLemmas = value; - reportVocabularyLemmas(); } } } + + private Dictionary>? _vocabularyLemmas { get; set; } = null; + + private TimeSpan UltraLongCache + { + get => TimeSpan.FromSeconds(cacheTtlLong * 10 * rnd.Next(900, 1100) / 1000); + } + public string traduci(string lingua, string lemma) { string answ = $"[{lemma}]"; @@ -34,14 +84,74 @@ namespace WebDoorCreator.UI.Data return answ; } - protected void reportVocabularyLemmas() + public async Task>?> VocLemmaGetAll() { - if (EA_VocabularyLemmas != null) + string source = "DB"; + Dictionary>? dbResult = new Dictionary>(); + // cerco da cache + Stopwatch stopWatch = new Stopwatch(); + stopWatch.Start(); + string currKey = $"{rKeyVocLemma}"; + string? rawData = await redisDb.StringGetAsync(currKey); + if (!string.IsNullOrEmpty(rawData)) { - EA_VocabularyLemmas?.Invoke(); + source = "REDIS"; + var tempResult = JsonConvert.DeserializeObject>>(rawData); + if (tempResult == null) + { + dbResult = new Dictionary>(); + } + else + { + dbResult = tempResult; + } } + else + { + dbResult = dbController.VocLemmaGetAll(); + rawData = JsonConvert.SerializeObject(dbResult, JSSettings); + await redisDb.StringSetAsync(currKey, rawData, UltraLongCache); + } + if (dbResult == null) + { + dbResult = new Dictionary>(); + } + stopWatch.Stop(); + TimeSpan ts = stopWatch.Elapsed; + Log.Debug($"VocLemmaGetAll | {source} in: {ts.TotalMilliseconds} ms"); + return dbResult; } - private Dictionary>? _vocabularyLemmas { get; set; } = null; + + /// + /// Durata cache lunga IN SECONDI + /// + private int cacheTtlLong = 60 * 5; + + /// + /// Oggetto per connessione a REDIS + /// + private IConnectionMultiplexer redisConn; + + /// + /// Oggetto DB redis da impiegare x chiamate R/W + /// + private IDatabase redisDb = null!; + + protected const string redisBaseAddr = "WDC"; + + protected const string rKeyVocLemma = $"{redisBaseAddr}:Cache:VocLemma"; + + protected Random rnd = new Random(); + + private static IConfiguration _configuration = null!; + + private static JsonSerializerSettings? JSSettings; + + private static NLog.Logger Log = LogManager.GetCurrentClassLogger(); + + private readonly IEmailSender _emailSender; + + public string CodApp { get; set; } = ""; } -} +} \ No newline at end of file diff --git a/WebDoorCreator.UI/Data/WebDoorCreatorService.cs b/WebDoorCreator.UI/Data/WebDoorCreatorService.cs index 3dbecf0..97245e5 100644 --- a/WebDoorCreator.UI/Data/WebDoorCreatorService.cs +++ b/WebDoorCreator.UI/Data/WebDoorCreatorService.cs @@ -1,6 +1,5 @@ using EgwCoreLib.Razor.Data; using Microsoft.AspNetCore.Identity.UI.Services; -using Microsoft.CodeAnalysis.FlowAnalysis.DataFlow.ValueContentAnalysis; using Microsoft.EntityFrameworkCore; using Newtonsoft.Json; using NLog; @@ -293,7 +292,6 @@ namespace WebDoorCreator.UI.Data public async Task VocLemmaInsert(string rootPath) { - bool fatto = false; List VocLemmas = new List(); @@ -506,6 +504,7 @@ namespace WebDoorCreator.UI.Data Log.Debug($"DoorOpGetByDoorId | {source} in: {ts.TotalMilliseconds} ms"); return dbResult; } + public async Task>?> VocLemmaGetAll() { string source = "DB"; @@ -571,7 +570,7 @@ namespace WebDoorCreator.UI.Data } } else - { + { rawData = JsonConvert.SerializeObject(dbResult, JSSettings); await redisDb.StringSetAsync(currKey, rawData, UltraLongCache); } @@ -650,7 +649,6 @@ namespace WebDoorCreator.UI.Data return dbResult; } - /// /// Update or add door /// @@ -770,6 +768,49 @@ namespace WebDoorCreator.UI.Data return dbResult; } + /// + /// Estraggo tutte le lingue disponibili per questa applicazione + /// + /// + public async Task?> LanguageGetAll() + { + string source = "DB"; + + List? dbResult = new List(); + // cerco da cache + string currKey = $"{rKeyLanguage}"; + Stopwatch stopWatch = new Stopwatch(); + stopWatch.Start(); + string? rawData = await redisDb.StringGetAsync(currKey); + if (!string.IsNullOrEmpty(rawData)) + { + source = "REDIS"; + var tempResult = JsonConvert.DeserializeObject>(rawData); + if (tempResult == null) + { + dbResult = new List(); + } + else + { + dbResult = tempResult; + } + } + else + { + dbResult = dbController.LanguageGetAll(); + rawData = JsonConvert.SerializeObject(dbResult, JSSettings); + await redisDb.StringSetAsync(currKey, rawData, UltraLongCache); + } + if (dbResult == null) + { + dbResult = new List(); + } + stopWatch.Stop(); + TimeSpan ts = stopWatch.Elapsed; + Log.Debug($"LanguageGetAll | {source} in: {ts.TotalMilliseconds} ms"); + return dbResult; + } + /// /// Add new order /// @@ -1146,7 +1187,11 @@ namespace WebDoorCreator.UI.Data protected const string rKeyUsers = $"{redisBaseAddr}:Cache:Users"; protected const string rKeyUsersView = $"{redisBaseAddr}:Cache:UsersView"; + protected const string rKeyVocLemma = $"{redisBaseAddr}:Cache:VocLemma"; + + protected const string rKeyLanguage = $"{redisBaseAddr}:Cache:Languages"; + protected Random rnd = new Random(); #endregion Protected Fields