This commit is contained in:
Samuele Locatelli
2026-04-10 08:52:15 +02:00
3 changed files with 97 additions and 0 deletions
+19
View File
@@ -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.
/// </summary>
(int oldWeight, int newWeight) GetWeightsFor(string method);
/// <summary>
/// Ritorna l'intero elenco dei weight attivi nel formato WeightDTO
/// </summary>
/// <returns></returns>
List<WeightDTO> GetAllWeights();
/// <summary>
/// Aggiorna/Aggiuinge il valore del weight richiesto
/// </summary>
/// <param name=""></param>
/// <returns></returns>
bool UpsertWeight(WeightDTO updRecord);
}
}
+27
View File
@@ -25,6 +25,33 @@ namespace MP.IOC.Services
{
_map[method] = (Math.Clamp(oldWeight, 0, 100), Math.Clamp(newWeight, 0, 100));
}
public List<WeightDTO> GetAllWeights()
{
var result = new List<WeightDTO>();
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;
}
}
+51
View File
@@ -76,6 +76,57 @@ namespace MP.IOC.Services
});
}
public List<WeightDTO> GetAllWeights()
{
var result = new List<WeightDTO>();
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