Przeglądaj źródła

cps修改权重 用法,按照等比例随机命中权重

dodo hold 1 rok temu
rodzic
commit
2b704d986c

Plik diff jest za duży
+ 0 - 0
molilian.api/Properties/PublishProfiles/latest.pubxml.user


+ 52 - 0
molilian.core/Core/cps/CpsPoolCore.cs

@@ -9,6 +9,8 @@ namespace molilian.core
         private static string _end_point;
         private static Dictionary<string, decimal> _incomeAmt = new();
         private static Dictionary<string, int> _calls = new();
+
+        static Random _random = new Random();
         static CpsPoolCore()
         {
             _end_point = Environment.GetEnvironmentVariable("EndPoint");
@@ -29,6 +31,56 @@ namespace molilian.core
             var links = LinksList();
             if (!links.Any()) return null;
 
+            // 获取符合条件的链接
+            var eligibleLinks = links.Where(e => IsEligible(e, channel)).ToList();
+            if (!eligibleLinks.Any()) return null;
+
+            // 计算总权重
+            int totalWeight = eligibleLinks.Sum(item => item.priority_weight);
+            if (totalWeight == 0) return null;
+
+            int randomValue = _random.Next(totalWeight);
+
+            // 按权重查找
+            int accumulatedWeight = 0;
+            foreach (var link in eligibleLinks.OrderByDescending(e => e.priority_weight))
+            {
+                accumulatedWeight += link.priority_weight;
+                if (randomValue < accumulatedWeight)
+                    return link;
+            }
+
+            // 保险起见返回第一个
+            return eligibleLinks.First();
+        }
+
+
+
+        public static CpsLinksDTO? GetOne2(string channel)
+        {
+            var list = List();
+            if (!list.Any()) return null;
+
+            var pool = list.Where(e => e.channel_code == channel && e.status).FirstOrDefault();
+            if (pool == null) return null;
+
+            var links = LinksList();
+            if (!links.Any()) return null;
+
+            //int totalWeight = links.Sum(item => item.priority_weight);
+
+            //int randomValue = _random.Next(0,totalWeight);
+
+            //// 累积权重直到找到选中项
+            //double accumulatedWeight = 0;
+            //foreach (var item in items)
+            //{
+            //    accumulatedWeight += Math.Pow(2, weightSelector(item));
+            //    if (randomValue <= accumulatedWeight)
+            //        return item;
+            //}
+
+
             return links.Where(e => IsEligible(e, channel))
                 .OrderByDescending(e => e.priority_weight)
                 .ThenBy(l => Guid.NewGuid())

Niektóre pliki nie zostały wyświetlone z powodu dużej ilości zmienionych plików