Przeglądaj źródła

创意用户分层校准

jch 4 dni temu
rodzic
commit
5544c93b3b

+ 23 - 61
ad-engine-commons/src/main/java/com/tzld/piaoquan/ad/engine/commons/helper/CreativeUserLayerDataHelper.java

@@ -1,26 +1,23 @@
 package com.tzld.piaoquan.ad.engine.commons.helper;
 
-import com.alibaba.fastjson.JSONObject;
-import com.aliyun.oss.OSS;
-import com.aliyun.oss.model.OSSObject;
+import com.tzld.piaoquan.ad.engine.commons.redis.AlgorithmRedisHelper;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.PostConstruct;
-import java.io.BufferedReader;
-import java.io.InputStreamReader;
-import java.util.*;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
 
 @Slf4j
 @Component
 public class CreativeUserLayerDataHelper {
     @Autowired
-    private OSS ossClient;
+    protected AlgorithmRedisHelper algRedisHelper;
 
-    private static final String BUCKET_NAME = "art-recommend";
-    private static final String filePath = "ad_engine_strategy/cid_layer_copc.txt";
+    private static final String redisKey = "ad:engine:strategy:clib:cid_layer";
     private static final String keyFormat = "%s:%s:%s";
 
     private volatile static Map<String, Double> cidMap = Collections.emptyMap();
@@ -28,19 +25,15 @@ public class CreativeUserLayerDataHelper {
     // 服务启动时初始化数据
     @PostConstruct
     public void init() {
-        Map<String, Double> map = readTextFromOss(filePath);
-        if (null != map) {
-            cidMap = Collections.unmodifiableMap(map);
-        }
+        Map<String, Double> map = updateCidMap();
+        cidMap = Collections.unmodifiableMap(map);
     }
 
     // 每5分钟更新一次数据
     @Scheduled(fixedRate = 5 * 60 * 1000)
     public void scheduledUpdate() {
-        Map<String, Double> map = readTextFromOss(filePath);
-        if (null != map) {
-            cidMap = Collections.unmodifiableMap(map);
-        }
+        Map<String, Double> map = updateCidMap();
+        cidMap = Collections.unmodifiableMap(map);
     }
 
     public static Double getCopc(String model, String cid, String layer) {
@@ -57,54 +50,23 @@ public class CreativeUserLayerDataHelper {
         return null;
     }
 
-    private HashMap<String, Double> readTextFromOss(String ossFilePath) {
+    private Map<String, Double> updateCidMap() {
+        Map<String, Double> tmpCidSMap = new HashMap<>();
         try {
-            List<JSONObject> list = readOssFile(ossFilePath);
-            return parseCidData(list);
-        } catch (Exception e) {
-            log.error("get modelVersionPath error", e);
-        }
-        return new HashMap<>();
-    }
-
-    private HashMap<String, Double> parseCidData(List<JSONObject> list) {
-        if (null != list) {
-            HashMap<String, Double> map = new HashMap<>();
-            for (JSONObject json : list) {
-                String model = json.getString("model");
-                String cid = json.getString("cid");
-                String layer = json.getString("layer");
-                Double copc = json.getDoubleValue("final_copc");
-                String key = String.format(keyFormat, model, cid, layer);
-                map.put(key, copc);
-            }
-            return map;
-        }
-        return null;
-    }
-
-    private List<JSONObject> readOssFile(String ossFilePath) {
-        if (!ossClient.doesObjectExist(BUCKET_NAME, ossFilePath)) {
-            log.info("ossFilePath not exist: {}", ossFilePath);
-            return null;
-        }
-        List<JSONObject> ossJson = new ArrayList<>();
-        try (OSSObject ossObj = ossClient.getObject(BUCKET_NAME, ossFilePath)) {
-            BufferedReader reader = new BufferedReader(new InputStreamReader(ossObj.getObjectContent()));
-            String line = reader.readLine();
-            String[] columns = line.split("\t");
-            while ((line = reader.readLine()) != null) {
-                String[] values = line.split("\t");
-                JSONObject jsonObj = new JSONObject();
-                for (int i = 0; i < columns.length; i++) {
-                    jsonObj.put(columns[i], values[i]);
+            String value = algRedisHelper.get(redisKey);
+            if (null != value && !value.isEmpty()) {
+                String[] cells = value.split(",");
+                for (String cell : cells) {
+                    String[] pair = cell.split("_");
+                    if (2 == pair.length) {
+                        tmpCidSMap.put(pair[0], Double.parseDouble(pair[1]));
+                    }
                 }
-                ossJson.add(jsonObj);
             }
+            log.info("update cid layer copc success size={}", tmpCidSMap.size());
         } catch (Exception e) {
-            log.error("load oss file error: {}, ", ossFilePath, e);
+            log.error("update cid layer copc error", e);
         }
-        log.info("load oss file: {}, size: {}", ossFilePath, ossJson.size());
-        return ossJson;
+        return tmpCidSMap;
     }
 }