diff --git a/MP.SPEC/Data/MpDataService.cs b/MP.SPEC/Data/MpDataService.cs index 0a2f1715..4f4a43c1 100644 --- a/MP.SPEC/Data/MpDataService.cs +++ b/MP.SPEC/Data/MpDataService.cs @@ -419,32 +419,13 @@ namespace MP.SPEC.Data /// public async Task> ConfigGetAllAsync() { - using var activity = ActivitySource.StartActivity("ConfigGetAllAsync"); - string source = "REDIS"; - List? result = new List(); - // cerco in redis... - RedisValue rawData = await redisDb.StringGetAsync(Utils.redisConfKey); - if (!string.IsNullOrEmpty($"{rawData}")) - { - result = JsonConvert.DeserializeObject>($"{rawData}"); - source = "REDIS"; - } - else - { - result = await dbController.ConfigGetAllAsync(); - // serializzo e salvo... - rawData = JsonConvert.SerializeObject(result); - await redisDb.StringSetAsync(Utils.redisConfKey, rawData, getRandTOut(redisLongTimeCache)); - } - if (result == null) - { - result = new List(); - } - activity?.SetTag("data.source", source); - activity?.SetTag("result.count", result.Count); - activity?.Stop(); - LogTrace($"ConfigGetAllAsync Read from {source}: {activity?.Duration.TotalMilliseconds}ms"); - return result; + return await GetOrFetchAsync( + operationName: "ConfigGetAllAsync", + cacheKey: Utils.redisConfKey, + expiration: TimeSpan.FromMinutes(redisLongTimeCache), + fetchFunc: async () => await dbController.ConfigGetAllAsync() ?? new List(), + tagList: [Utils.redisConfKey] + ); } /// @@ -461,13 +442,6 @@ namespace MP.SPEC.Data LogTrace($"ConfigResetCache Read from {source}: {activity?.Duration.TotalMilliseconds}ms"); } - private async Task ResetConfigCache() - { - await redisDb.StringSetAsync(Utils.redisConfKey, ""); - List tags2del = new List() { Utils.redisConfKey }; - await FlushCacheByTagsAsync(tags2del); - } - /// /// Restituisce valore della stringa (SE disponibile) /// @@ -593,33 +567,14 @@ namespace MP.SPEC.Data /// public async Task> DossiersGetLastFiltAsync(string IdxMacchina, string CodArticolo, DateTime DtStart, DateTime DtEnd, int MaxRec) { - using var activity = ActivitySource.StartActivity("DossiersGetLastFiltAsync"); - List? result = new List(); - string source = "DB"; string currKey = $"{Utils.redisDossByMac}:{IdxMacchina}:{CodArticolo}:{DtStart:yyyyMMddHHmm}:{DtEnd:yyyyMMddHHmm}:{MaxRec}"; - // cerco in redis dato valore sel idxMaccSel... - RedisValue rawData = await redisDb.StringGetAsync(currKey); - if (rawData.HasValue) - { - result = JsonConvert.DeserializeObject>($"{rawData}"); - source = "REDIS"; - } - else - { - result = await dbController.DossiersGetLastFiltAsync(IdxMacchina, CodArticolo, DtStart, DtEnd, MaxRec); - // serializzo e salvo... - rawData = JsonConvert.SerializeObject(result); - await redisDb.StringSetAsync(currKey, rawData, getRandTOut(redisLongTimeCache / 5)); - } - if (result == null) - { - result = new List(); - } - activity?.SetTag("data.source", source); - activity?.SetTag("result.count", result.Count); - activity?.Stop(); - LogTrace($"DossiersGetLastFiltAsync | Read from {source}: {activity?.Duration.TotalMilliseconds}ms"); - return result; + return await GetOrFetchAsync( + operationName: "DossiersGetLastFiltAsync", + cacheKey: currKey, + expiration: TimeSpan.FromMinutes(redisLongTimeCache / 5), + fetchFunc: async () => await dbController.DossiersGetLastFiltAsync(IdxMacchina, CodArticolo, DtStart, DtEnd, MaxRec) ?? new List(), + tagList: [Utils.redisDossByMac] + ); } /// @@ -1312,42 +1267,6 @@ namespace MP.SPEC.Data return mongoController.InitRecipe(confPath, idxPODL, CalcArgs); } - /// - /// Recupero info IOB x TAB (da info registrate IOB-WIN--> MP-IO) - /// - /// - /// - public async Task IobInfo(string IdxMacchina) - { - using var activity = ActivitySource.StartActivity("IobInfo"); - string source = "DB"; - IOB_data? result = new IOB_data(); - // cerco in redis... - string currKey = redHashMpIO($"hM2IOB:{IdxMacchina}"); - RedisValue rawData = await redisDb.StringGetAsync(currKey); - //if (!string.IsNullOrEmpty($"{rawData}")) - if (rawData.HasValue) - { - result = JsonConvert.DeserializeObject($"{rawData}"); - source = "REDIS"; - } - else - { - Log.Error($"Errore: non trovato valore valido in REDIS | key: {currKey}"); - Log.Info($"REDIS | conf: {redisConn.Configuration}"); - Log.Info($" --> Valore trovato:{Environment.NewLine}{rawData}"); - } - if (result == null) - { - result = new IOB_data(); - LogTrace($"Init valore default | IdxMacchina: {IdxMacchina}"); - } - activity?.SetTag("data.source", source); - activity?.Stop(); - LogTrace($"IobInfo per {IdxMacchina} | {source} | {activity?.Duration.TotalMilliseconds}ms"); - return result; - } - /// /// Elimina record + svuotamento cache /// @@ -1923,28 +1842,6 @@ namespace MP.SPEC.Data return dbResult; } - /// - /// ODL correnti (tutti) - /// - /// - /// - /// - /// elenco TUTTI gli ODL - /// - /// - /// - public List OdlListAll() - { - using var activity = ActivitySource.StartActivity("OdlListAll"); - List? result = new List(); - string source = "DB"; - result = dbController.OdlListAll(); - activity?.SetTag("data.source", "DB"); - activity?.Stop(); - LogTrace($"OdlListAll | Read from {source}: {activity?.Duration.TotalMilliseconds}ms"); - return result; - } - /// /// Elenco ODL filtrati x stato, articolo, KeyRich (che contiene stato) /// @@ -2544,28 +2441,6 @@ namespace MP.SPEC.Data return answ; } - /// - /// Effettua reset microstato macchina - /// - /// - public async Task ResetMicrostatoMacchina(string idxMacchina) - { - using var activity = ActivitySource.StartActivity("ResetMicrostatoMacchina"); - string source = "DB"; - // salvo microstato 0... - MicroStatoMacchinaModel newRecMS = new MicroStatoMacchinaModel() - { - IdxMacchina = idxMacchina, - InizioStato = DateTime.Now, - IdxMicroStato = 0, - Value = "FER" - }; - var result = await dbController.MicroStatoMacchinaUpsert(newRecMS); - activity?.SetTag("data.source", source); - activity?.Stop(); - LogTrace($"ResetMicrostatoMacchina | Read from {source}: {activity?.Duration.TotalMilliseconds}ms"); - } - /// /// Statistiche ODL calcolate (da stored stp_STAT_ODL) /// @@ -2617,37 +2492,6 @@ namespace MP.SPEC.Data return result; } - /// - /// Restituisce il valore da REDIS associato al tag richiesto - /// - /// Chiave in cui cercare il valore - /// - public string TagConfGetKey(string redKey) - { - string outVal = ""; - using var activity = ActivitySource.StartActivity("TagConfGetKey"); - string source = "REDIS"; - // cerco in REDIS la conf x l'IOB - var rawData = redisDb.StringGet(redKey); - if (!string.IsNullOrEmpty(rawData)) - { - outVal = $"{rawData}"; - } - activity?.SetTag("data.source", source); - activity?.Stop(); - LogTrace($"TagConfGetKey | {source} | {activity?.Duration.TotalMilliseconds}ms"); - return outVal; - } - - /// - /// Elenco setup dei tag conf correnti - /// - /// - public Dictionary> TagsGetAll() - { - return currTagConf; - } - /// /// Elimina record + svuotamento cache /// @@ -2777,54 +2621,6 @@ namespace MP.SPEC.Data return answ; } - /// - /// Update valore Dossier - /// - /// - /// - /// - public async Task updateDossierValue(DossierModel currDoss, FluxLogDTO editFL) - { - using var activity = ActivitySource.StartActivity("updateDossierValue"); - string source = "DB"; - bool answ = false; - // recupero intero set valori dossier deserializzando... - var fluxLogList = FluxLogDtoGetByFlux(currDoss.Valore); - // se tutto ok - if (fluxLogList != null) - { - // da provare...!!!! - - // elimino vecchio record - var currRec = fluxLogList.FirstOrDefault(x => x.CodFlux == editFL.CodFlux && x.dtEvento == editFL.dtEvento); - if (currRec != null) - { - fluxLogList.Remove(currRec); - // aggiungo nuovo - fluxLogList.Add(editFL); - } - - // serializzo nuovamente valore - DossierFluxLogDTO? result = new DossierFluxLogDTO(); - var ODLflux = result.ODL.ToList(); - foreach (var item in fluxLogList) - { - ODLflux.Add(item); - } - - DossierFluxLogDTO updatedResult = new DossierFluxLogDTO() { ODL = ODLflux }; - - string rawVal = JsonConvert.SerializeObject(updatedResult); - currDoss.Valore = rawVal; - // aggiorno record sul DB - await dbController.DossiersUpdateValore(currDoss); - } - activity?.SetTag("data.source", source); - activity?.Stop(); - LogTrace($"updateDossierValue | {source} | {activity?.Duration.TotalMilliseconds}ms"); - return answ; - } - /// /// Elenco completo tabella Vocabolario /// @@ -2878,25 +2674,6 @@ namespace MP.SPEC.Data return fatto; } - /// - /// Elimina i record associati al keyFilt indicato - /// - /// - public bool WipKitDeleteGroup(string KeyFilt) - { - using var activity = ActivitySource.StartActivity("WipKitDeleteGroup"); - string source = "DB"; - bool fatto = false; - // salvo - fatto = dbController.WipKitDeleteGroup(KeyFilt); - // svuoto cache - EmptyWipCache(); - activity?.SetTag("data.source", source); - activity?.Stop(); - LogTrace($"WipKitDeleteGroup Read from {source}: {activity?.Duration.TotalMilliseconds}ms"); - return fatto; - } - /// /// Elimina i record più vecchi della data-ora indicata /// @@ -2973,18 +2750,6 @@ namespace MP.SPEC.Data #endregion Public Methods - #region Protected Fields - - protected Random rand = new Random(); - - #endregion Protected Fields - - #region Protected Properties - - protected string canCacheParametri { get; set; } = ""; - - #endregion Protected Properties - #region Protected Methods /// @@ -3084,6 +2849,8 @@ namespace MP.SPEC.Data // Cache per controllo eliminazione articoli (Smart HashSet approach) private HashSet _usedArtIdsCache = new(); + private string canCacheParametri = ""; + private string MpIoNS = ""; /// @@ -3091,6 +2858,8 @@ namespace MP.SPEC.Data /// private List ObjVocabolario = new List(); + private Random rand = new Random(); + /// /// Oggetto per connessione a REDIS /// @@ -3274,6 +3043,83 @@ namespace MP.SPEC.Data activity?.SetTag("data.source", "REDIS"); } + private async Task ResetConfigCache() + { + await redisDb.StringSetAsync(Utils.redisConfKey, ""); + List tags2del = new List() { Utils.redisConfKey }; + await FlushCacheByTagsAsync(tags2del); + } + +#if false + /// + /// Update valore Dossier + /// + /// + /// + /// + public async Task updateDossierValue(DossierModel currDoss, FluxLogDTO editFL) + { + using var activity = ActivitySource.StartActivity("updateDossierValue"); + string source = "DB"; + bool answ = false; + // recupero intero set valori dossier deserializzando... + var fluxLogList = FluxLogDtoGetByFlux(currDoss.Valore); + // se tutto ok + if (fluxLogList != null) + { + // da provare...!!!! + + // elimino vecchio record + var currRec = fluxLogList.FirstOrDefault(x => x.CodFlux == editFL.CodFlux && x.dtEvento == editFL.dtEvento); + if (currRec != null) + { + fluxLogList.Remove(currRec); + // aggiungo nuovo + fluxLogList.Add(editFL); + } + + // serializzo nuovamente valore + DossierFluxLogDTO? result = new DossierFluxLogDTO(); + var ODLflux = result.ODL.ToList(); + foreach (var item in fluxLogList) + { + ODLflux.Add(item); + } + + DossierFluxLogDTO updatedResult = new DossierFluxLogDTO() { ODL = ODLflux }; + + string rawVal = JsonConvert.SerializeObject(updatedResult); + currDoss.Valore = rawVal; + // aggiorno record sul DB + await dbController.DossiersUpdateValore(currDoss); + } + activity?.SetTag("data.source", source); + activity?.Stop(); + LogTrace($"updateDossierValue | {source} | {activity?.Duration.TotalMilliseconds}ms"); + return answ; + } +#endif +#if false + /// + /// Elimina i record associati al keyFilt indicato + /// + /// + public bool WipKitDeleteGroup(string KeyFilt) + { + using var activity = ActivitySource.StartActivity("WipKitDeleteGroup"); + string source = "DB"; + bool fatto = false; + // salvo + fatto = dbController.WipKitDeleteGroup(KeyFilt); + // svuoto cache + EmptyWipCache(); + activity?.SetTag("data.source", source); + activity?.Stop(); + LogTrace($"WipKitDeleteGroup Read from {source}: {activity?.Duration.TotalMilliseconds}ms"); + return fatto; + } +#endif + /// /// Reset macchine e gruppi ///