fix init vocabolario

This commit is contained in:
zaccaria.majid
2023-04-04 11:07:50 +02:00
parent b410d4dd0c
commit 6fa2b6eae2
6 changed files with 233 additions and 27 deletions
@@ -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;
}
/// <summary>
/// Getting door data by orderId
/// </summary>
@@ -473,6 +471,30 @@ namespace WebDoorCreator.Data.Controllers
return fatto;
}
/// <summary>
/// Estraggo tutte le lingue disponibili per questa applicazione
/// </summary>
/// <returns></returns>
public List<LanguageModel> LanguageGetAll()
{
List<LanguageModel> dbResult = new List<LanguageModel>();
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;
}
/// <summary>
/// ListValues list (All)
/// </summary>
@@ -801,7 +823,6 @@ namespace WebDoorCreator.Data.Controllers
}
return dbResult;
}
public Dictionary<string, Dictionary<string, string>> VocLemmaGetAll()
{
Dictionary<string, Dictionary<string, string>> DTOResult = new Dictionary<string, Dictionary<string, string>>();
@@ -824,7 +845,7 @@ namespace WebDoorCreator.Data.Controllers
foreach (var lingua in lingue)
{
Dictionary<string, string> dict = new Dictionary<string, string>();
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
@@ -19,7 +19,7 @@ namespace WebDoorCreator.UI.Components.Gen
public EventCallback<string> E_UserRole { get; set; }
public EventCallback<string> E_UserId { get; set; }
public EventCallback<List<Dictionary<string, Dictionary<string, string>>>> E_VocLemmas { get; set; }
public EventCallback<Dictionary<string, Dictionary<string, string>>> E_VocLemmas { get; set; }
public List<CompanyModel> listCompanies { get; set; } = new List<CompanyModel>();
@@ -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<string, Dictionary<string, string>>();
// 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<string>();
listCompanies = new List<CompanyModel>();
listVocLemmas = new Dictionary<string, Dictionary<string, string>>();
//await WDCService.FlushRedisCache();
listVocLemmas = await WDCService.VocLemmaGetAll();
var user = await _userManager.FindByNameAsync(currUser);
if (user != null)
@@ -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;
}
+23 -2
View File
@@ -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<string>? _userClaims { get; set; } = null;
private string _userRole { get; set; } = "";
private int _userCurrComp { get; set; } //= -1;
private string? _currLanguage { get; set; } = null;
#endregion Private Properties
}
+118 -8
View File
@@ -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<string, Dictionary<string, string>>? VocabularyLemmas
{
@@ -14,11 +57,18 @@ namespace WebDoorCreator.UI.Data
if (_vocabularyLemmas != value)
{
_vocabularyLemmas = value;
reportVocabularyLemmas();
}
}
}
private Dictionary<string, Dictionary<string, string>>? _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<Dictionary<string, Dictionary<string, string>>?> VocLemmaGetAll()
{
if (EA_VocabularyLemmas != null)
string source = "DB";
Dictionary<string, Dictionary<string, string>>? dbResult = new Dictionary<string, Dictionary<string, string>>();
// 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<Dictionary<string, Dictionary<string, string>>>(rawData);
if (tempResult == null)
{
dbResult = new Dictionary<string, Dictionary<string, string>>();
}
else
{
dbResult = tempResult;
}
}
else
{
dbResult = dbController.VocLemmaGetAll();
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
await redisDb.StringSetAsync(currKey, rawData, UltraLongCache);
}
if (dbResult == null)
{
dbResult = new Dictionary<string, Dictionary<string, string>>();
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Debug($"VocLemmaGetAll | {source} in: {ts.TotalMilliseconds} ms");
return dbResult;
}
private Dictionary<string, Dictionary<string, string>>? _vocabularyLemmas { get; set; } = null;
/// <summary>
/// Durata cache lunga IN SECONDI
/// </summary>
private int cacheTtlLong = 60 * 5;
/// <summary>
/// Oggetto per connessione a REDIS
/// </summary>
private IConnectionMultiplexer redisConn;
/// <summary>
/// Oggetto DB redis da impiegare x chiamate R/W
/// </summary>
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; } = "";
}
}
}
@@ -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<bool> VocLemmaInsert(string rootPath)
{
bool fatto = false;
List<VocabularyTempModel> VocLemmas = new List<VocabularyTempModel>();
@@ -506,6 +504,7 @@ namespace WebDoorCreator.UI.Data
Log.Debug($"DoorOpGetByDoorId | {source} in: {ts.TotalMilliseconds} ms");
return dbResult;
}
public async Task<Dictionary<string, Dictionary<string, string>>?> 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;
}
/// <summary>
/// Update or add door
/// </summary>
@@ -770,6 +768,49 @@ namespace WebDoorCreator.UI.Data
return dbResult;
}
/// <summary>
/// Estraggo tutte le lingue disponibili per questa applicazione
/// </summary>
/// <returns></returns>
public async Task<List<LanguageModel>?> LanguageGetAll()
{
string source = "DB";
List<LanguageModel>? dbResult = new List<LanguageModel>();
// 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<List<LanguageModel>>(rawData);
if (tempResult == null)
{
dbResult = new List<LanguageModel>();
}
else
{
dbResult = tempResult;
}
}
else
{
dbResult = dbController.LanguageGetAll();
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
await redisDb.StringSetAsync(currKey, rawData, UltraLongCache);
}
if (dbResult == null)
{
dbResult = new List<LanguageModel>();
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Debug($"LanguageGetAll | {source} in: {ts.TotalMilliseconds} ms");
return dbResult;
}
/// <summary>
/// Add new order
/// </summary>
@@ -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