Browse Source

日志记录

wangyunpeng 9 months ago
parent
commit
6efd21485d

+ 10 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/repository/crawler/PublishContentSortLogRepository.java

@@ -0,0 +1,10 @@
+package com.tzld.longarticle.recommend.server.repository.crawler;
+
+import com.tzld.longarticle.recommend.server.repository.entity.crawler.PublishContentSortLog;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.stereotype.Repository;
+
+@Repository
+public interface PublishContentSortLogRepository extends JpaRepository<PublishContentSortLog, Long> {
+
+}

+ 31 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/repository/entity/crawler/PublishContentSortLog.java

@@ -0,0 +1,31 @@
+package com.tzld.longarticle.recommend.server.repository.entity.crawler;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+@Entity
+@Table(name = "publish_content_sort_log")
+public class PublishContentSortLog {
+    @Id
+    private Long id;
+    @Column(name = "gh_id")
+    private String ghId;
+    @Column(name = "account_name")
+    private String accountName;
+    @Column(name = "strategy")
+    private String strategy;
+    @Column(name = "publish_content_id")
+    private String publishContentId;
+    @Column(name = "create_timestamp")
+    private Long createTimestamp;
+
+}

+ 30 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/RecommendService.java

@@ -1,7 +1,10 @@
 package com.tzld.longarticle.recommend.server.service;
 
+import com.alibaba.fastjson.JSONObject;
 import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
 import com.tzld.longarticle.recommend.server.model.*;
+import com.tzld.longarticle.recommend.server.repository.crawler.PublishContentSortLogRepository;
+import com.tzld.longarticle.recommend.server.repository.entity.crawler.PublishContentSortLog;
 import com.tzld.longarticle.recommend.server.service.rank.RankParam;
 import com.tzld.longarticle.recommend.server.service.rank.RankResult;
 import com.tzld.longarticle.recommend.server.service.rank.RankService;
@@ -13,6 +16,7 @@ import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.collections4.CollectionUtils;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import org.springframework.util.StringUtils;
 
@@ -20,6 +24,7 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
+import java.util.stream.Collectors;
 
 /**
  * @author dyp
@@ -32,9 +37,13 @@ public class RecommendService {
     private RecallService recallService;
     @Autowired
     private RankService rankService;
+    @Autowired
+    private PublishContentSortLogRepository publishContentSortLogRepository;
 
     @ApolloJsonValue("${accountStrategyConfig:{}}")
     private Map<String, String> accountStrategyConfigMap;
+    @Value("${spring.profiles.active}")
+    private String env;
 
 
     public RecommendResponse recommend(RecommendRequest request) {
@@ -52,6 +61,8 @@ public class RecommendService {
         RankResult rankResult = rankService.rank(convertToRankParam(param, recallResult));
         log.info("rankResult {}", JSONUtils.toJson(rankResult));
 
+        saveSortLog(param, rankResult);
+
         return buildRecommendResponse(recallResult, rankResult, param.getPublishNum());
     }
 
@@ -123,4 +134,23 @@ public class RecommendService {
         return rankParam;
     }
 
+    /**
+     * 记录 账号、排序策略、结果。。。
+     * @param param
+     * @param rankResult
+     */
+    private void saveSortLog(RecommendParam param, RankResult rankResult) {
+        if (!"prod".equals(env)) {
+            return;
+        }
+        PublishContentSortLog log = new PublishContentSortLog();
+        log.setGhId(param.getGhId());
+        log.setAccountName(param.getAccountName());
+        log.setStrategy(param.getStrategy());
+        List<String> publishContentIds = rankResult.getContents().stream().map(Content::getId).collect(Collectors.toList());
+        log.setPublishContentId(JSONObject.toJSONString(publishContentIds));
+        log.setCreateTimestamp(System.currentTimeMillis());
+        publishContentSortLogRepository.save(log);
+    }
+
 }