From b664ad784e2366d277542c128abfe3bb3b157ac5 Mon Sep 17 00:00:00 2001 From: "Samuele E. Locatelli (W11-AI)" Date: Fri, 10 Apr 2026 08:50:55 +0200 Subject: [PATCH] Aggiunta metodi gestione valori weight da memoria/redis (da testare) --- MP.IOC/Services/IWeightProvider.cs | 19 +++++++++ MP.IOC/Services/InMemoryWeightProvider.cs | 27 ++++++++++++ MP.IOC/Services/RedisWeightProvider.cs | 51 +++++++++++++++++++++++ 3 files changed, 97 insertions(+) diff --git a/MP.IOC/Services/IWeightProvider.cs b/MP.IOC/Services/IWeightProvider.cs index 1f565705..cc074941 100644 --- a/MP.IOC/Services/IWeightProvider.cs +++ b/MP.IOC/Services/IWeightProvider.cs @@ -1,5 +1,11 @@ namespace MP.IOC.Services { + public class WeightDTO + { + public string Method { get; set; } = ""; + public int OldWeight { get; set; } = 100; + public int NewWeight { get; set; } = 0; + } public interface IWeightProvider { @@ -7,6 +13,19 @@ /// Ritorna la coppia (oldWeight, newWeight) per scegliere dove instradare il metodo tra i 2 sistemi API. /// (int oldWeight, int newWeight) GetWeightsFor(string method); + + /// + /// Ritorna l'intero elenco dei weight attivi nel formato WeightDTO + /// + /// + List GetAllWeights(); + + /// + /// Aggiorna/Aggiuinge il valore del weight richiesto + /// + /// + /// + bool UpsertWeight(WeightDTO updRecord); } } diff --git a/MP.IOC/Services/InMemoryWeightProvider.cs b/MP.IOC/Services/InMemoryWeightProvider.cs index 9a951557..03fc3e28 100644 --- a/MP.IOC/Services/InMemoryWeightProvider.cs +++ b/MP.IOC/Services/InMemoryWeightProvider.cs @@ -25,6 +25,33 @@ namespace MP.IOC.Services { _map[method] = (Math.Clamp(oldWeight, 0, 100), Math.Clamp(newWeight, 0, 100)); } + + public List GetAllWeights() + { + var result = new List(); + + foreach (var kvp in _map) + { + result.Add(new WeightDTO + { + Method = kvp.Key, + OldWeight = Math.Clamp(kvp.Value.oldW, 0, 100), + NewWeight = Math.Clamp(kvp.Value.newW, 0, 100) + }); + } + + return result; + } + + public bool UpsertWeight(WeightDTO updRecord) + { + if (updRecord == null || string.IsNullOrEmpty(updRecord.Method)) + return false; + + _map[updRecord.Method] = (Math.Clamp(updRecord.OldWeight, 0, 100), Math.Clamp(updRecord.NewWeight, 0, 100)); + + return true; + } } diff --git a/MP.IOC/Services/RedisWeightProvider.cs b/MP.IOC/Services/RedisWeightProvider.cs index 5cca6b61..3127c36e 100644 --- a/MP.IOC/Services/RedisWeightProvider.cs +++ b/MP.IOC/Services/RedisWeightProvider.cs @@ -76,6 +76,57 @@ namespace MP.IOC.Services }); } + public List GetAllWeights() + { + var result = new List(); + var keys = _db.KeyScan($"{_keyPrefix}*"); + + foreach (var key in keys) + { + var methodName = KeyToString(key.ToString()); + if (string.IsNullOrEmpty(methodName)) continue; + + var oldVal = _db.HashGet(key, "old"); + var newVal = _db.HashGet(key, "new"); + + int oldW = 100; + int newW = 0; + + if (!oldVal.IsNull && int.TryParse(oldVal.ToString(), out var parsedOld)) + oldW = Math.Clamp(parsedOld, 0, 100); + + if (!newVal.IsNull && int.TryParse(newVal.ToString(), out var parsedNew)) + newW = Math.Clamp(parsedNew, 0, 100); + + result.Add(new WeightDTO { Method = methodName, OldWeight = oldW, NewWeight = newW }); + } + + return result; + } + + public bool UpsertWeight(WeightDTO updRecord) + { + if (updRecord == null || string.IsNullOrEmpty(updRecord.Method)) + return false; + + var key = _keyPrefix + updRecord.Method; + _db.HashSet(key, new HashEntry[] { + new HashEntry("old", Math.Clamp(updRecord.OldWeight, 0, 100)), + new HashEntry("new", Math.Clamp(updRecord.NewWeight, 0, 100)) + }); + + return true; + } + + private string KeyToString(string key) + { + if (string.IsNullOrEmpty(key)) return ""; + var prefix = _keyPrefix ?? ""; + if (key.StartsWith(prefix)) + return key.Substring(prefix.Length); + return key; + } + #endregion Public Methods #region Private Fields