|
@@ -1,7 +1,6 @@
|
|
|
package com.tzld.piaoquan.ad.engine.commons.helper;
|
|
package com.tzld.piaoquan.ad.engine.commons.helper;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSON;
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.alibaba.fastjson.TypeReference;
|
|
import com.alibaba.fastjson.TypeReference;
|
|
|
import com.tzld.piaoquan.ad.engine.commons.redis.AlgorithmRedisHelper;
|
|
import com.tzld.piaoquan.ad.engine.commons.redis.AlgorithmRedisHelper;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
@@ -12,7 +11,6 @@ import org.springframework.stereotype.Component;
|
|
|
import javax.annotation.PostConstruct;
|
|
import javax.annotation.PostConstruct;
|
|
|
import java.util.Collections;
|
|
import java.util.Collections;
|
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
@Slf4j
|
|
@Slf4j
|
|
@@ -23,6 +21,7 @@ public class ModelUserLayerDataHelper {
|
|
|
|
|
|
|
|
private static final String redisKey = "ad:engine:strategy:model_ctcvr_calibration";
|
|
private static final String redisKey = "ad:engine:strategy:model_ctcvr_calibration";
|
|
|
private static final String keyFormat = "%s:%s:%s:%s:%s:%s";
|
|
private static final String keyFormat = "%s:%s:%s:%s:%s:%s";
|
|
|
|
|
+ private static final String SUM = "sum";
|
|
|
|
|
|
|
|
private volatile static Map<String, Double> dataMap = Collections.emptyMap();
|
|
private volatile static Map<String, Double> dataMap = Collections.emptyMap();
|
|
|
|
|
|
|
@@ -30,31 +29,38 @@ public class ModelUserLayerDataHelper {
|
|
|
@PostConstruct
|
|
@PostConstruct
|
|
|
public void init() {
|
|
public void init() {
|
|
|
Map<String, Double> map = updateDataMap();
|
|
Map<String, Double> map = updateDataMap();
|
|
|
- dataMap = Collections.unmodifiableMap(map);
|
|
|
|
|
|
|
+ if (map != null) {
|
|
|
|
|
+ dataMap = Collections.unmodifiableMap(map);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 每5分钟更新一次数据
|
|
// 每5分钟更新一次数据
|
|
|
@Scheduled(fixedRate = 5 * 60 * 1000)
|
|
@Scheduled(fixedRate = 5 * 60 * 1000)
|
|
|
public void scheduledUpdate() {
|
|
public void scheduledUpdate() {
|
|
|
Map<String, Double> map = updateDataMap();
|
|
Map<String, Double> map = updateDataMap();
|
|
|
- dataMap = Collections.unmodifiableMap(map);
|
|
|
|
|
|
|
+ if (map != null) {
|
|
|
|
|
+ dataMap = Collections.unmodifiableMap(map);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public static Double getCopc(String model, String landingPageType, String targetingConversion, String layer, String profession, String customerId) {
|
|
public static Double getCopc(String model, String landingPageType, String targetingConversion, String layer, String profession, String customerId) {
|
|
|
- log.info("ModelUserLayerDataHelper dataMap={}", JSON.toJSONString(dataMap));
|
|
|
|
|
if (null != dataMap) {
|
|
if (null != dataMap) {
|
|
|
|
|
+ model = nullToSum(model);
|
|
|
|
|
+ landingPageType = nullToSum(landingPageType);
|
|
|
|
|
+ targetingConversion = nullToSum(targetingConversion);
|
|
|
|
|
+ layer = nullToSum(layer);
|
|
|
|
|
+ profession = nullToSum(profession);
|
|
|
|
|
+ customerId = nullToSum(customerId);
|
|
|
|
|
+
|
|
|
String key = String.format(keyFormat, model, landingPageType, targetingConversion, layer, profession, customerId);
|
|
String key = String.format(keyFormat, model, landingPageType, targetingConversion, layer, profession, customerId);
|
|
|
- log.info("ModelUserLayerDataHelper key={}", key);
|
|
|
|
|
if (dataMap.containsKey(key)) {
|
|
if (dataMap.containsKey(key)) {
|
|
|
return dataMap.get(key);
|
|
return dataMap.get(key);
|
|
|
}
|
|
}
|
|
|
- key = String.format(keyFormat, model, landingPageType, targetingConversion, layer, profession, "sum");
|
|
|
|
|
- log.info("ModelUserLayerDataHelper key={}", key);
|
|
|
|
|
|
|
+ key = String.format(keyFormat, model, landingPageType, targetingConversion, layer, profession, SUM);
|
|
|
if (dataMap.containsKey(key)) {
|
|
if (dataMap.containsKey(key)) {
|
|
|
return dataMap.get(key);
|
|
return dataMap.get(key);
|
|
|
}
|
|
}
|
|
|
- key = String.format(keyFormat, model, landingPageType, targetingConversion, layer, "sum", "sum");
|
|
|
|
|
- log.info("ModelUserLayerDataHelper key={}", key);
|
|
|
|
|
|
|
+ key = String.format(keyFormat, model, landingPageType, targetingConversion, layer, SUM, SUM);
|
|
|
if (dataMap.containsKey(key)) {
|
|
if (dataMap.containsKey(key)) {
|
|
|
return dataMap.get(key);
|
|
return dataMap.get(key);
|
|
|
}
|
|
}
|
|
@@ -62,29 +68,40 @@ public class ModelUserLayerDataHelper {
|
|
|
return null;
|
|
return null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private static String nullToSum(String value) {
|
|
|
|
|
+ if (value == null || value.isEmpty() || "null".equalsIgnoreCase(value)) {
|
|
|
|
|
+ return SUM;
|
|
|
|
|
+ }
|
|
|
|
|
+ return value;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @return 新数据;失败返回 null,调用方保留旧 dataMap
|
|
|
|
|
+ */
|
|
|
private Map<String, Double> updateDataMap() {
|
|
private Map<String, Double> updateDataMap() {
|
|
|
try {
|
|
try {
|
|
|
String value = algRedisHelper.get(redisKey);
|
|
String value = algRedisHelper.get(redisKey);
|
|
|
|
|
|
|
|
if (value == null || value.trim().isEmpty()) {
|
|
if (value == null || value.trim().isEmpty()) {
|
|
|
- log.warn("update model layer copc skipped: redis value is empty");
|
|
|
|
|
- return new HashMap<>();
|
|
|
|
|
|
|
+ log.warn("update model layer copc skipped: redis value is empty, keep old dataMap size={}", dataMap.size());
|
|
|
|
|
+ return null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- Map<String, Double> dataMap = JSON.parseObject(
|
|
|
|
|
|
|
+ Map<String, Double> newDataMap = JSON.parseObject(
|
|
|
value,
|
|
value,
|
|
|
new TypeReference<Map<String, Double>>() {}
|
|
new TypeReference<Map<String, Double>>() {}
|
|
|
);
|
|
);
|
|
|
|
|
|
|
|
- if (dataMap == null) {
|
|
|
|
|
- dataMap = new HashMap<>();
|
|
|
|
|
|
|
+ if (newDataMap == null) {
|
|
|
|
|
+ log.warn("update model layer copc skipped: parsed map is null, keep old dataMap size={}", dataMap.size());
|
|
|
|
|
+ return null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- log.info("update model layer copc success size={}", dataMap.size());
|
|
|
|
|
- return dataMap;
|
|
|
|
|
|
|
+ log.info("update model layer copc success size={}", newDataMap.size());
|
|
|
|
|
+ return newDataMap;
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
- log.error("update model layer copc error", e);
|
|
|
|
|
- return new HashMap<>();
|
|
|
|
|
|
|
+ log.error("update model layer copc error, keep old dataMap size={}", dataMap.size(), e);
|
|
|
|
|
+ return null;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|