Browse Source

Merge branch 'dev-xym-report' of Server/long-article-recommend into master

xueyiming 8 months ago
parent
commit
f9741e5f90

+ 12 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/model/vo/ReportUvVo.java

@@ -0,0 +1,12 @@
+package com.tzld.longarticle.recommend.server.model.vo;
+
+import lombok.Data;
+
+@Data
+public class ReportUvVo {
+
+    private String ghId;
+
+    private Long uv;
+
+}

+ 4 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/exterior/ThirdPartyService.java

@@ -4,6 +4,7 @@ import com.tzld.longarticle.recommend.server.common.response.CommonResponse;
 import com.tzld.longarticle.recommend.server.model.param.CallbackParam;
 import com.tzld.longarticle.recommend.server.model.param.PushMessageParam;
 import com.tzld.longarticle.recommend.server.model.vo.PushMessageVo;
+import com.tzld.longarticle.recommend.server.model.vo.ReportUvVo;
 import org.springframework.stereotype.Service;
 
 import java.util.List;
@@ -11,4 +12,7 @@ import java.util.List;
 @Service
 public interface ThirdPartyService {
     CommonResponse<List<PushMessageVo>> getPushMessage(PushMessageParam param);
+
+
+    CommonResponse<List<ReportUvVo>> getReportUv(String date, String accessToken);
 }

+ 50 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/exterior/impl/ThirdPartyServiceImpl.java

@@ -1,6 +1,8 @@
 package com.tzld.longarticle.recommend.server.service.exterior.impl;
 
 import com.alibaba.fastjson.JSON;
+import com.aliyun.odps.data.Record;
+import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
 import com.tzld.longarticle.recommend.server.common.enums.SecretEnum;
 import com.tzld.longarticle.recommend.server.common.enums.cgi.ReplyStrategyServiceEnum;
 import com.tzld.longarticle.recommend.server.common.response.CommonResponse;
@@ -12,9 +14,12 @@ import com.tzld.longarticle.recommend.server.model.cgi.MsgData;
 import com.tzld.longarticle.recommend.server.model.cgi.ReplyBucketData;
 import com.tzld.longarticle.recommend.server.model.param.PushMessageParam;
 import com.tzld.longarticle.recommend.server.model.vo.PushMessageVo;
+import com.tzld.longarticle.recommend.server.model.vo.ReportUvVo;
 import com.tzld.longarticle.recommend.server.service.exterior.AccessTokenService;
 import com.tzld.longarticle.recommend.server.service.exterior.ThirdPartyService;
 import com.tzld.longarticle.recommend.server.service.strategy.reply.ReplyStrategyService;
+import com.tzld.longarticle.recommend.server.util.DateUtils;
+import lombok.Data;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.BeanUtils;
@@ -22,6 +27,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.ApplicationContext;
 import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
+import com.tzld.longarticle.recommend.server.remote.ODPSManager;
 
 import javax.annotation.PostConstruct;
 import java.util.ArrayList;
@@ -38,6 +44,12 @@ public class ThirdPartyServiceImpl implements ThirdPartyService {
     @Autowired
     private AccessTokenService accessTokenService;
 
+    @Autowired
+    private ODPSManager odpsManager;
+
+    @ApolloJsonValue("${canViewReportDate:2024-10-31}")
+    private String canViewReportDate;
+
     private Map<String, ReplyStrategyService> strategyServiceMap;
 
     @PostConstruct
@@ -85,6 +97,44 @@ public class ThirdPartyServiceImpl implements ThirdPartyService {
         return CommonResponse.success(pushMessageVoList);
     }
 
+    @Override
+    public CommonResponse<List<ReportUvVo>> getReportUv(String date, String accessToken) {
+        if (StringUtils.isEmpty(date) || !DateUtils.isValidDate(date) || StringUtils.isEmpty(accessToken)) {
+            return CommonResponse.create(ExceptionCodeEnum.PARAM_ERROR, "参数错误");
+        }
+        if (!accessTokenService.validateAccessToken(accessToken)) {
+            return CommonResponse.create(ExceptionCodeEnum.PARAM_ERROR, "accessToken错误或者已失效");
+        }
+        SecretEnum secretEnum = accessTokenService.getSecretEnum(accessToken);
+        if (secretEnum == null) {
+            return CommonResponse.create(ExceptionCodeEnum.PARAM_ERROR, "获取secret失败");
+        }
+        if (!DateUtils.isValidDate(canViewReportDate)) {
+            return CommonResponse.create(ExceptionCodeEnum.PARAM_ERROR, "系统异常");
+        }
+        String channel = secretEnum.channel;
+        long targetTime = DateUtils.dateStrToTimestamp(date, "yyyy-MM-dd");
+        long limitTime = DateUtils.dateStrToTimestamp(canViewReportDate, "yyyy-MM-dd");
+        if (targetTime > limitTime) {
+            return CommonResponse.create(500, "数据不存在");
+        }
+        String dt = date.replace("-", "").substring(0, 8);
+        String sql = String.format("SELECT * FROM alg_growth_3rd_gh_reply_uv_report WHERE dt = %s AND channel = '%s';",
+                dt, channel);
+        List<ReportUvVo> res = new ArrayList<>();
+        List<Record> recordList = odpsManager.query(sql);
+        if (CollectionUtils.isEmpty(recordList)) {
+            return CommonResponse.success(res);
+        }
+        for (Record record : recordList) {
+            ReportUvVo reportUvVo = new ReportUvVo();
+            reportUvVo.setGhId(record.getString(0));
+            reportUvVo.setUv(record.getBigint(1));
+            res.add(reportUvVo);
+        }
+        return CommonResponse.success(res);
+    }
+
     private ReplyBucketData getPushMessageData(PushMessageParam param, String channel) {
         log.info("strategyServiceMap={}", JSON.toJSONString(strategyServiceMap));
         for (Map.Entry<String, ReplyStrategyService> stringReplyStrategyServiceEntry : strategyServiceMap.entrySet()) {

+ 14 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/util/DateUtils.java

@@ -8,6 +8,7 @@ import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.ZoneId;
 import java.time.format.DateTimeFormatter;
+import java.time.format.DateTimeParseException;
 import java.time.temporal.ChronoUnit;
 import java.util.*;
 
@@ -75,6 +76,19 @@ public final class DateUtils {
         return dateTime.toEpochSecond(zone.getRules().getOffset(dateTime));
     }
 
+    public static boolean isValidDate(String dateString) {
+        // 定义日期格式
+        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
+
+        try {
+            // 尝试解析日期字符串
+            LocalDate date = LocalDate.parse(dateString, dateFormatter);
+            return true; // 如果解析成功,返回 true
+        } catch (DateTimeParseException e) {
+            return false; // 如果解析失败,返回 false
+        }
+    }
+
     public static String timestampToYMDStr(Long timestamp, String format) {
 
         // 定义北京时区

+ 7 - 4
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/web/ThirdPartyController.java

@@ -6,14 +6,12 @@ import com.tzld.longarticle.recommend.server.model.param.CallbackParam;
 import com.tzld.longarticle.recommend.server.model.param.PushMessageParam;
 import com.tzld.longarticle.recommend.server.model.vo.AccessTokenVo;
 import com.tzld.longarticle.recommend.server.model.vo.PushMessageVo;
+import com.tzld.longarticle.recommend.server.model.vo.ReportUvVo;
 import com.tzld.longarticle.recommend.server.mq.MessageCallbackProducer;
 import com.tzld.longarticle.recommend.server.service.exterior.AccessTokenService;
 import com.tzld.longarticle.recommend.server.service.exterior.ThirdPartyService;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
 import java.util.List;
 
@@ -44,4 +42,9 @@ public class ThirdPartyController {
     public CommonResponse<Void> pushMessageCallback(@RequestBody CallbackParam param) {
         return messageCallbackProducer.sendMessage(param);
     }
+
+    @GetMapping("/report/uv")
+    public CommonResponse<List<ReportUvVo>> getReportUv(@RequestParam String date, @RequestParam String accessToken) {
+        return thirdPartyService.getReportUv(date, accessToken);
+    }
 }