|
@@ -1,7 +1,10 @@
|
|
|
package com.tzld.piaoquan.ad.engine.service.score.strategy;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
|
|
|
import com.tzld.piaoquan.ad.engine.commons.dto.AdPlatformCreativeDTO;
|
|
|
+import com.tzld.piaoquan.ad.engine.commons.util.DateUtils;
|
|
|
+import com.tzld.piaoquan.ad.engine.service.entity.GuaranteeView;
|
|
|
import com.tzld.piaoquan.ad.engine.commons.enums.RedisPrefixEnum;
|
|
|
import com.tzld.piaoquan.ad.engine.commons.param.RankRecommendRequestParam;
|
|
|
import com.tzld.piaoquan.ad.engine.commons.redis.AdRedisHelper;
|
|
@@ -10,8 +13,10 @@ import com.tzld.piaoquan.ad.engine.service.feature.Feature;
|
|
|
import com.tzld.piaoquan.ad.engine.service.feature.FeatureService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
|
+import org.apache.commons.collections4.MapUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
@@ -25,11 +30,18 @@ public abstract class RankStrategyBasic implements RankStrategy {
|
|
|
@ApolloJsonValue("${creative.model.score.coefficient:{}}")
|
|
|
private Map<Long, Double> creativeScoreCoefficient;
|
|
|
|
|
|
+ @Value("${guarantee.exp:742}")
|
|
|
+ protected String guaranteeExp;
|
|
|
+ @ApolloJsonValue("${alpha:1.0}")
|
|
|
+ protected Double alpha;
|
|
|
@Autowired
|
|
|
private FeatureService featureService;
|
|
|
@Autowired
|
|
|
protected AdRedisHelper adRedisHelper;
|
|
|
|
|
|
+ String adPlatformGuaranteeKey = "ad:platform:guarantee:data:{date}:{adverId}";
|
|
|
+
|
|
|
+
|
|
|
protected Feature getFeature(ScoreParam param, RankRecommendRequestParam request) {
|
|
|
List<AdPlatformCreativeDTO> adIdList = request.getAdIdList();
|
|
|
List<String> cidList = adIdList.stream()
|
|
@@ -98,4 +110,84 @@ public abstract class RankStrategyBasic implements RankStrategy {
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+ protected Map<String, GuaranteeView> getGuaranteeViewMap(RankRecommendRequestParam request, ScoreParam scoreParam) {
|
|
|
+ Map<String, GuaranteeView> map = new HashMap<>();
|
|
|
+ try {
|
|
|
+ if (scoreParam.getExpCodeSet().contains(guaranteeExp)) {
|
|
|
+ String thatDayDateString = DateUtils.getThatDayDateString();
|
|
|
+ String redisKey = adPlatformGuaranteeKey.replace("{date}", thatDayDateString);
|
|
|
+ List<String> adVerIds = request.getAdIdList().stream().map(AdPlatformCreativeDTO::getAdVerId).distinct()
|
|
|
+ .filter(Objects::nonNull).collect(Collectors.toList());
|
|
|
+ List<String> redisKeys = adVerIds.stream().map(e -> redisKey.replace("{adverId}", e)).collect(Collectors.toList());
|
|
|
+ List<String> values = adRedisHelper.mget(redisKeys);
|
|
|
+ if (CollectionUtils.isNotEmpty(values)) {
|
|
|
+ for (int i = 0; i < redisKeys.size(); i++) {
|
|
|
+ String value = values.get(i);
|
|
|
+ if (value != null) {
|
|
|
+ GuaranteeView guaranteeView = JSONObject.parseObject(value, GuaranteeView.class);
|
|
|
+ map.put(adVerIds.get(i), guaranteeView);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("getGuaranteeViewMap error", e);
|
|
|
+ }
|
|
|
+
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected void setGuaranteeWeight(Map<String, GuaranteeView> map, String adVerId, Map<String, Object> ext) {
|
|
|
+ if (MapUtils.isNotEmpty(map)) {
|
|
|
+ GuaranteeView guaranteeView = map.get(adVerId);
|
|
|
+ if (guaranteeView != null) {
|
|
|
+ double guaranteeWeight = calculateGuaranteedWeight(guaranteeView);
|
|
|
+ ext.put("guaranteeView", guaranteeView.toString());
|
|
|
+ ext.put("guaranteeWeight", guaranteeWeight);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ protected double calculateGuaranteedWeight(GuaranteeView guaranteeView) {
|
|
|
+ if (guaranteeView == null) {
|
|
|
+ return 1.0;
|
|
|
+ }
|
|
|
+ double guaranteeWeight;
|
|
|
+ if (guaranteeView.getGuaranteeNum() == null || guaranteeView.getGuaranteeNum() == 0
|
|
|
+ || guaranteeView.getGuaranteeRate() == null || guaranteeView.getGuaranteeRate() == 0.0) {
|
|
|
+ guaranteeWeight = 1.0;
|
|
|
+ } else {
|
|
|
+ if (guaranteeView.getAdrAlgoViewNum() != null &&
|
|
|
+ guaranteeView.getGuaranteeNum() <= guaranteeView.getAdrAlgoViewNum()) {
|
|
|
+ guaranteeWeight = 0.0;
|
|
|
+ } else {
|
|
|
+ int allViewNum = guaranteeView.getAllAlgoViewNum() == null || guaranteeView.getAllAlgoViewNum() == 0 ?
|
|
|
+ 1 : guaranteeView.getAllAlgoViewNum();
|
|
|
+ int adrAlogViewNum = guaranteeView.getAdrAlgoViewNum() == null || guaranteeView.getAdrAlgoViewNum() == 0 ?
|
|
|
+ 1 : guaranteeView.getAdrAlgoViewNum();
|
|
|
+ //guaranteeView.getGuaranteeRate() 是百分之几 要乘0.01
|
|
|
+ guaranteeWeight = guaranteeView.getGuaranteeRate() * 0.01 * allViewNum / adrAlogViewNum;
|
|
|
+ if (guaranteeWeight < 0.5) {
|
|
|
+ guaranteeWeight = 0.5;
|
|
|
+ }
|
|
|
+ if (guaranteeWeight > 2) {
|
|
|
+ guaranteeWeight = 2;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return guaranteeWeight;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected double getGuaranteeScoreCoefficient(ScoreParam scoreParam, Map<String, Object> ext) {
|
|
|
+ if (scoreParam.getExpCodeSet().contains(guaranteeExp)) {
|
|
|
+ if (ext.get("guaranteeWeight") == null) {
|
|
|
+ return 1.0;
|
|
|
+ } else {
|
|
|
+ return Math.pow((double) ext.get("guaranteeWeight"), alpha);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return 1.0;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|