Browse Source

抽取公共方法

xueyiming 1 day ago
parent
commit
c8427b090a

+ 87 - 45
ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/score/strategy/RankStrategyBasic.java

@@ -26,6 +26,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 
 import java.util.*;
+import java.util.function.Function;
 import java.util.stream.Collectors;
 
 @Slf4j
@@ -325,51 +326,31 @@ public abstract class RankStrategyBasic implements RankStrategy {
 
         Map<Long, CorrectCpaParam> resultMap = new HashMap<>();
         try {
-            List<Long> customerIds = request.getAdIdList().stream().map(AdPlatformCreativeDTO::getCustomerId).distinct().collect(Collectors.toList());
-            List<String> customerLayerHourKeys = customerIds.stream().map(e -> redisAdCustomerLayerHourKey.replace("{customerId}", String.valueOf(e))).collect(Collectors.toList());
-            List<String> customerLayerHourValues = adRedisHelper.mget(customerLayerHourKeys);
-            List<String> customerLayerDayKeys = customerIds.stream().map(e -> redisAadCustomerLayerDayKey.replace("{customerId}", String.valueOf(e))).collect(Collectors.toList());
-            List<String> customerLayerDayValues = adRedisHelper.mget(customerLayerDayKeys);
-
-            Map<Long, JSONObject> customerLayerMap = new HashMap<>();
-            for (int i = 0; i < customerIds.size(); i++) {
-                JSONObject jsonObject = new JSONObject();
-                if (StringUtils.isNotEmpty(customerLayerHourValues.get(i))) {
-                    JSONObject jsonObject1 = JSONObject.parseObject(customerLayerHourValues.get(i));
-                    jsonObject.put("realCtcvrHour", jsonObject1.get("ctcvr"));
-                    jsonObject.put("pCtcvrHour", jsonObject1.get("pCtcvr"));
-                    jsonObject.put("viewsHour", jsonObject1.get("views"));
-                }
-                if (StringUtils.isNotEmpty(customerLayerDayValues.get(i))) {
-                    JSONObject jsonObject1 = JSONObject.parseObject(customerLayerDayValues.get(i));
-                    jsonObject.put("realCtcvrDay", jsonObject1.get("ctcvr"));
-                    jsonObject.put("pCtcvrDay", jsonObject1.get("pCtcvr"));
-                    jsonObject.put("viewsDay", jsonObject1.get("views"));
-                }
-                customerLayerMap.put(customerIds.get(i), jsonObject);
-            }
-            List<String> adVerIds = request.getAdIdList().stream().map(AdPlatformCreativeDTO::getAdVerId).distinct().collect(Collectors.toList());
-            List<String> adVerLayerHourKeys = adVerIds.stream().map(e -> redisAdVerLayerHourKey.replace("{adverId}", e)).collect(Collectors.toList());
-            List<String> adVerLayerHourValues = adRedisHelper.mget(adVerLayerHourKeys);
-            List<String> adVerLayerDayKeys = adVerIds.stream().map(e -> redisAdVerLayerDayKey.replace("{adverId}", e)).collect(Collectors.toList());
-            List<String> adVerLayerDayValues = adRedisHelper.mget(adVerLayerDayKeys);
-            Map<String, JSONObject> adVerLayerMap = new HashMap<>();
-            for (int i = 0; i < adVerIds.size(); i++) {
-                JSONObject jsonObject = new JSONObject();
-                if (StringUtils.isNotEmpty(adVerLayerHourValues.get(i))) {
-                    JSONObject jsonObject1 = JSONObject.parseObject(adVerLayerHourValues.get(i));
-                    jsonObject.put("realCtcvrHour", jsonObject1.get("ctcvr"));
-                    jsonObject.put("pCtcvrHour", jsonObject1.get("pCtcvr"));
-                    jsonObject.put("viewsHour", jsonObject1.get("views"));
-                }
-                if (StringUtils.isNotEmpty(adVerLayerDayValues.get(i))) {
-                    JSONObject jsonObject1 = JSONObject.parseObject(adVerLayerDayValues.get(i));
-                    jsonObject.put("realCtcvrDay", jsonObject1.get("ctcvr"));
-                    jsonObject.put("pCtcvrDay", jsonObject1.get("pCtcvr"));
-                    jsonObject.put("viewsDay", jsonObject1.get("views"));
-                }
-                adVerLayerMap.put(adVerIds.get(i), jsonObject);
+            if (CollectionUtils.isEmpty(request.getAdIdList())) {
+                return resultMap;
             }
+
+            // 抽取公共方法获取数据
+            List<Long> customerIds = request.getAdIdList().stream()
+                    .map(AdPlatformCreativeDTO::getCustomerId)
+                    .distinct()
+                    .collect(Collectors.toList());
+            Map<Long, JSONObject> customerLayerMap = getRedisData(
+                    customerIds,
+                    id -> redisAdCustomerLayerHourKey.replace("{customerId}", String.valueOf(id)),
+                    id -> redisAadCustomerLayerDayKey.replace("{customerId}", String.valueOf(id))
+            );
+
+            List<String> adVerIds = request.getAdIdList().stream()
+                    .map(AdPlatformCreativeDTO::getAdVerId)
+                    .distinct()
+                    .collect(Collectors.toList());
+            Map<String, JSONObject> adVerLayerMap = getRedisData(
+                    adVerIds,
+                    id -> redisAdVerLayerHourKey.replace("{adverId}", id),
+                    id -> redisAdVerLayerDayKey.replace("{adverId}", id)
+            );
+
             for (AdPlatformCreativeDTO adPlatformCreativeDTO : request.getAdIdList()) {
                 Long creativeId = adPlatformCreativeDTO.getCreativeId();
                 Long customerId = adPlatformCreativeDTO.getCustomerId();
@@ -380,6 +361,11 @@ public abstract class RankStrategyBasic implements RankStrategy {
                     jsonObject = adVerLayerMap.get(adPlatformCreativeDTO.getAdVerId());
                 }
                 CorrectCpaParam correctCpaParam = new CorrectCpaParam();
+                if (jsonObject == null) {
+                    correctCpaParam.setCorrectionFactor(1.0);
+                    resultMap.put(creativeId, correctCpaParam);
+                    continue;
+                }
                 Integer views = jsonObject.getInteger("viewsHour");
                 Double realCtcvrHour = jsonObject.getDouble("realCtcvrHour");
                 Double pCtcvrHour = jsonObject.getDouble("pCtcvrHour");
@@ -390,7 +376,7 @@ public abstract class RankStrategyBasic implements RankStrategy {
                 correctCpaParam.setRealCtcvrDay(realCtcvrDay);
                 correctCpaParam.setPCtcvrDay(pCtcvrDay);
                 correctCpaParam.setView(views);
-                double correctionFactor = 1;
+                double correctionFactor = 1.0;
                 //曝光数小于目标曝光数,不进行修正
                 if (views == null || views < correctCpaView) {
                     correctCpaParam.setCorrectionFactor(correctionFactor);
@@ -419,6 +405,62 @@ public abstract class RankStrategyBasic implements RankStrategy {
         return resultMap;
     }
 
+    private <T> Map<T, JSONObject> getRedisData(
+            Collection<T> ids,
+            Function<T, String> hourKeyBuilder,
+            Function<T, String> dayKeyBuilder) {
+
+        Map<T, JSONObject> resultMap = new HashMap<>();
+        if (CollectionUtils.isEmpty(ids)) {
+            return resultMap;
+        }
+
+        // 构建Keys
+        List<String> hourKeys = ids.stream()
+                .map(hourKeyBuilder)
+                .collect(Collectors.toList());
+        List<String> dayKeys = ids.stream()
+                .map(dayKeyBuilder)
+                .collect(Collectors.toList());
+
+        // 批量获取Redis值
+        List<String> hourValues = adRedisHelper.mget(hourKeys);
+        List<String> dayValues = adRedisHelper.mget(dayKeys);
+
+        // 解析数据
+        Iterator<T> idIter = ids.iterator();
+        for (int i = 0; i < ids.size(); i++) {
+            T id = idIter.next();
+            JSONObject jsonObj = new JSONObject();
+
+            // 解析小时数据
+            parseRedisValue(hourValues.get(i), jsonObj, "Hour", "ctcvr", "pCtcvr", "views");
+            // 解析天数据
+            parseRedisValue(dayValues.get(i), jsonObj, "Day", "ctcvr", "pCtcvr", "views");
+
+            resultMap.put(id, jsonObj);
+        }
+        return resultMap;
+    }
+
+    private void parseRedisValue(String value, JSONObject target,
+                                 String suffix, String... keys) {
+        if (StringUtils.isEmpty(value)) return;
+
+        try {
+            JSONObject source = JSONObject.parseObject(value);
+            for (String key : keys) {
+                Object val = source.get(key);
+                if (val != null) {
+                    String newKey = (key.equals("ctcvr") ? "realCtcvr" : key) + suffix;
+                    target.put(newKey, val);
+                }
+            }
+        } catch (Exception e) {
+            log.warn("Parse redis value failed: {}", value, e);
+        }
+    }
+
 
     protected AdRankItem creativeCovertRankItem(AdPlatformCreativeDTO dto, RankRecommendRequestParam request, Set<String> noApiAdVerIds) {
         AdRankItem adRankItem = new AdRankItem();