Selaa lähdekoodia

ADD: Redis Json 解析

sunxy 1 vuosi sitten
vanhempi
commit
a5dad3df40

+ 33 - 4
recommend-feature-service/src/main/java/com/tzld/piaoquan/recommend/feature/grpcservice/FeatureGrpcService.java

@@ -1,16 +1,16 @@
 package com.tzld.piaoquan.recommend.feature.grpcservice;
 
 import com.tzld.piaoquan.recommend.feature.client.ProtobufUtils;
+import com.tzld.piaoquan.recommend.feature.model.common.Result;
 import com.tzld.piaoquan.recommend.feature.model.feature.*;
-import com.tzld.piaoquan.recommend.feature.service.AdItemFeatureService;
-import com.tzld.piaoquan.recommend.feature.service.UserAdFeatureService;
-import com.tzld.piaoquan.recommend.feature.service.UserFeatureService;
-import com.tzld.piaoquan.recommend.feature.service.VideoFeatureService;
+import com.tzld.piaoquan.recommend.feature.service.*;
 import io.grpc.stub.StreamObserver;
 import lombok.extern.slf4j.Slf4j;
 import net.devh.boot.grpc.server.service.GrpcService;
 import org.springframework.beans.factory.annotation.Autowired;
 
+import java.util.List;
+
 /**
  * @author dyp
  */
@@ -28,6 +28,8 @@ public class FeatureGrpcService extends FeatureServiceGrpc.FeatureServiceImplBas
     @Autowired
     private UserAdFeatureService userAdFeatureService;
 
+    @Autowired
+    private UserAndVideoFeatureService userAndVideoFeatureService;
 
     @Override
     public void getUserFeature(GetUserFeatureRequest request, StreamObserver<GetUserFeatureResponse> responseObserver) {
@@ -87,4 +89,31 @@ public class FeatureGrpcService extends FeatureServiceGrpc.FeatureServiceImplBas
         responseObserver.onNext(response);
         responseObserver.onCompleted();
     }
+
+    @Override
+    public void getUserFeatureInfo(GetUserFeatureInfoRequest request, StreamObserver<GetUserFeatureInfoResponse> responseObserver) {
+        log.info("FeatureGrpcService getUserFeatureInfo request={}", ProtobufUtils.toJson(request));
+        List<GetUserFeatureInfo> getUserFeatureInfoList = userAndVideoFeatureService.queryUserFeature(request.getMidsList());
+        GetUserFeatureInfoResponse response = GetUserFeatureInfoResponse.newBuilder()
+                .setResult(Result.newBuilder().setCode(1))
+                .addAllUserFeatureInfo(getUserFeatureInfoList)
+                .build();
+        log.info("FeatureGrpcService getUserFeatureInfo response={}", ProtobufUtils.toJson(response));
+        responseObserver.onNext(response);
+        responseObserver.onCompleted();
+
+    }
+
+    @Override
+    public void getVideoFeatureInfo(GetVideoFeatureInfoRequest request, StreamObserver<GetVideoFeatureInfoResponse> responseObserver) {
+        log.info("FeatureGrpcService getVideoFeatureInfo request={}", ProtobufUtils.toJson(request));
+        List<GetVideoFeatureInfo> getVideoFeatureInfoList = userAndVideoFeatureService.queryVideoFeature(request.getVideoIdsList());
+        GetVideoFeatureInfoResponse response = GetVideoFeatureInfoResponse.newBuilder()
+                .setResult(Result.newBuilder().setCode(1))
+                .addAllVideoFeatureInfo(getVideoFeatureInfoList)
+                .build();
+        log.info("FeatureGrpcService getVideoFeatureInfo response={}", ProtobufUtils.toJson(response));
+        responseObserver.onNext(response);
+        responseObserver.onCompleted();
+    }
 }

+ 277 - 0
recommend-feature-service/src/main/java/com/tzld/piaoquan/recommend/feature/service/UserAndVideoFeatureConverter.java

@@ -0,0 +1,277 @@
+package com.tzld.piaoquan.recommend.feature.service;
+
+
+import com.alibaba.fastjson.JSONObject;
+import com.tzld.piaoquan.recommend.feature.model.feature.GetUserFeatureInfo;
+import com.tzld.piaoquan.recommend.feature.model.feature.GetVideoFeatureInfo;
+
+/**
+ * @author sunxy
+ */
+public class UserAndVideoFeatureConverter {
+
+    public static GetUserFeatureInfo convertUserFeatureInfoToProto(JSONObject jsonObject) {
+        if (jsonObject == null) {
+            return null;
+        }
+        GetUserFeatureInfo.Builder builder = GetUserFeatureInfo.newBuilder();
+        if (jsonObject.containsKey("u1hour_exp_cnt")) {
+            builder.setU1HourExpCnt(jsonObject.getLong("u1hour_exp_cnt"));
+        }
+        if (jsonObject.containsKey("u1hour_click_cnt")) {
+            builder.setU1HourClickCnt(jsonObject.getLong("u1hour_click_cnt"));
+        }
+        if (jsonObject.containsKey("u1hour_share_cnt")) {
+            builder.setU1HourShareCnt(jsonObject.getLong("u1hour_share_cnt"));
+        }
+        if (jsonObject.containsKey("u1hour_return_cnt")) {
+            builder.setU1HourReturnCnt(jsonObject.getLong("u1hour_return_cnt"));
+        }
+        if (jsonObject.containsKey("u_ctr1hour")) {
+            builder.setUCtr1Hour(jsonObject.getDouble("u_ctr1hour"));
+        }
+        if (jsonObject.containsKey("u_str1hour")) {
+            builder.setUStr1Hour(jsonObject.getDouble("u_str1hour"));
+        }
+        if (jsonObject.containsKey("u_rov1hour")) {
+            builder.setURov1Hour(jsonObject.getDouble("u_rov1hour"));
+        }
+        if (jsonObject.containsKey("u_ros1hour")) {
+            builder.setURos1Hour(jsonObject.getDouble("u_ros1hour"));
+        }
+        if (jsonObject.containsKey("u_today_exp_cnt")) {
+            builder.setUTodayExpCnt(jsonObject.getLong("u_today_exp_cnt"));
+        }
+        if (jsonObject.containsKey("u_today_click_cnt")) {
+            builder.setUTodayClickCnt(jsonObject.getLong("u_today_click_cnt"));
+        }
+        if (jsonObject.containsKey("u_today_share_cnt")) {
+            builder.setUTodayShareCnt(jsonObject.getLong("u_today_share_cnt"));
+        }
+        if (jsonObject.containsKey("u_today_return_cnt")) {
+            builder.setUTodayReturnCnt(jsonObject.getLong("u_today_return_cnt"));
+        }
+        if (jsonObject.containsKey("u_ctr_today")) {
+            builder.setUCtrToday(jsonObject.getDouble("u_ctr_today"));
+        }
+        if (jsonObject.containsKey("u_str_today")) {
+            builder.setUStrToday(jsonObject.getDouble("u_str_today"));
+        }
+        if (jsonObject.containsKey("u_rov_today")) {
+            builder.setURovToday(jsonObject.getDouble("u_rov_today"));
+        }
+        if (jsonObject.containsKey("u_ros_today")) {
+            builder.setURosToday(jsonObject.getDouble("u_ros_today"));
+        }
+        if (jsonObject.containsKey("u1day_exp_cnt")) {
+            builder.setU1DayExpCnt(jsonObject.getLong("u1day_exp_cnt"));
+        }
+        if (jsonObject.containsKey("u1day_click_cnt")) {
+            builder.setU1DayClickCnt(jsonObject.getLong("u1day_click_cnt"));
+        }
+        if (jsonObject.containsKey("u1day_share_cnt")) {
+            builder.setU1DayShareCnt(jsonObject.getLong("u1day_share_cnt"));
+        }
+        if (jsonObject.containsKey("u1day_return_cnt")) {
+            builder.setU1DayReturnCnt(jsonObject.getLong("u1day_return_cnt"));
+        }
+        if (jsonObject.containsKey("u_ctr1day")) {
+            builder.setUCtr1Day(jsonObject.getDouble("u_ctr1day"));
+        }
+        if (jsonObject.containsKey("u_str1day")) {
+            builder.setUStr1Day(jsonObject.getDouble("u_str1day"));
+        }
+        if (jsonObject.containsKey("u_rov1day")) {
+            builder.setURov1Day(jsonObject.getDouble("u_rov1day"));
+        }
+        if (jsonObject.containsKey("u_ros1day")) {
+            builder.setURos1Day(jsonObject.getDouble("u_ros1day"));
+        }
+        if (jsonObject.containsKey("u3day_exp_cnt")) {
+            builder.setU3DayExpCnt(jsonObject.getLong("u3day_exp_cnt"));
+        }
+        if (jsonObject.containsKey("u3day_click_cnt")) {
+            builder.setU3DayClickCnt(jsonObject.getLong("u3day_click_cnt"));
+        }
+        if (jsonObject.containsKey("u3day_share_cnt")) {
+            builder.setU3DayShareCnt(jsonObject.getLong("u3day_share_cnt"));
+        }
+        if (jsonObject.containsKey("u3day_return_cnt")) {
+            builder.setU3DayReturnCnt(jsonObject.getLong("u3day_return_cnt"));
+        }
+        if (jsonObject.containsKey("u_ctr3day")) {
+            builder.setUCtr3Day(jsonObject.getDouble("u_ctr3day"));
+        }
+        if (jsonObject.containsKey("u_str3day")) {
+            builder.setUStr3Day(jsonObject.getDouble("u_str3day"));
+        }
+        if (jsonObject.containsKey("u_rov3day")) {
+            builder.setURov3Day(jsonObject.getDouble("u_rov3day"));
+        }
+        if (jsonObject.containsKey("u_ros3day")) {
+            builder.setURos3Day(jsonObject.getDouble("u_ros3day"));
+        }
+        if (jsonObject.containsKey("mid")) {
+            builder.setMid(jsonObject.getString("mid"));
+        }
+        if (jsonObject.containsKey("machineinfo_brand")) {
+            builder.setMachineinfoBrand(jsonObject.getString("machineinfo_brand"));
+        }
+        if (jsonObject.containsKey("machineinfo_model")) {
+            builder.setMachineinfoModel(jsonObject.getString("machineinfo_model"));
+        }
+        if (jsonObject.containsKey("machineinfo_system")) {
+            builder.setMachineinfoSystem(jsonObject.getString("machineinfo_system"));
+        }
+        if (jsonObject.containsKey("machineinfo_platform")) {
+            builder.setMachineinfoPlatform(jsonObject.getString("machineinfo_platform"));
+        }
+
+        return builder.build();
+    }
+
+    public static GetVideoFeatureInfo convertVideoFeatureInfoToProtoVideo(JSONObject jsonObject) {
+        if (jsonObject == null) {
+            return null;
+        }
+        GetVideoFeatureInfo.Builder builder = GetVideoFeatureInfo.newBuilder();
+        if (jsonObject.containsKey("video_id")) {
+            builder.setVideoId(jsonObject.getLong("video_id"));
+        }
+        if (jsonObject.containsKey("play_count_total")) {
+            builder.setPlayCountTotal(jsonObject.getLong("play_count_total"));
+        }
+        if (jsonObject.containsKey("total_time")) {
+            builder.setTotalTime(jsonObject.getInteger("total_time"));
+        }
+        if (jsonObject.containsKey("gmt_create")) {
+            builder.setGmtCreate(jsonObject.getString("gmt_create"));
+        }
+        if (jsonObject.containsKey("uid")) {
+            builder.setUid(jsonObject.getLong("uid"));
+        }
+        if (jsonObject.containsKey("i15min_exp_cnt")) {
+            builder.setI15MinExpCnt(jsonObject.getLong("i15min_exp_cnt"));
+        }
+        if (jsonObject.containsKey("i15min_click_cnt")) {
+            builder.setI15MinClickCnt(jsonObject.getLong("i15min_click_cnt"));
+        }
+        if (jsonObject.containsKey("i15min_share_cnt")) {
+            builder.setI15MinShareCnt(jsonObject.getLong("i15min_share_cnt"));
+        }
+        if (jsonObject.containsKey("i15min_return_cnt")) {
+            builder.setI15MinReturnCnt(jsonObject.getLong("i15min_return_cnt"));
+        }
+        if (jsonObject.containsKey("i_ctr15min")) {
+            builder.setICtr15Min(jsonObject.getDouble("i_ctr15min"));
+        }
+        if (jsonObject.containsKey("i_str15min")) {
+            builder.setIStr15Min(jsonObject.getDouble("i_str15min"));
+        }
+        if (jsonObject.containsKey("i_rov15min")) {
+            builder.setIRov15Min(jsonObject.getDouble("i_rov15min"));
+        }
+        if (jsonObject.containsKey("i_ros15min")) {
+            builder.setIRos15Min(jsonObject.getDouble("i_ros15min"));
+        }
+        if (jsonObject.containsKey("i1hour_exp_cnt")) {
+            builder.setI1HourExpCnt(jsonObject.getLong("i1hour_exp_cnt"));
+        }
+        if (jsonObject.containsKey("i1hour_click_cnt")) {
+            builder.setI1HourClickCnt(jsonObject.getLong("i1hour_click_cnt"));
+        }
+        if (jsonObject.containsKey("i1hour_share_cnt")) {
+            builder.setI1HourShareCnt(jsonObject.getLong("i1hour_share_cnt"));
+        }
+        if (jsonObject.containsKey("i1hour_return_cnt")) {
+            builder.setI1HourReturnCnt(jsonObject.getLong("i1hour_return_cnt"));
+        }
+        if (jsonObject.containsKey("i_ctr1hour")) {
+            builder.setICtr1Hour(jsonObject.getDouble("i_ctr1hour"));
+        }
+        if (jsonObject.containsKey("i_str1hour")) {
+            builder.setIStr1Hour(jsonObject.getDouble("i_str1hour"));
+        }
+        if (jsonObject.containsKey("i_rov1hour")) {
+            builder.setIRov1Hour(jsonObject.getDouble("i_rov1hour"));
+        }
+        if (jsonObject.containsKey("i_ros1hour")) {
+            builder.setIRos1Hour(jsonObject.getDouble("i_ros1hour"));
+        }
+        if (jsonObject.containsKey("i_today_exp_cnt")) {
+            builder.setITodayExpCnt(jsonObject.getLong("i_today_exp_cnt"));
+        }
+        if (jsonObject.containsKey("i_today_click_cnt")) {
+            builder.setITodayClickCnt(jsonObject.getLong("i_today_click_cnt"));
+        }
+        if (jsonObject.containsKey("i_today_share_cnt")) {
+            builder.setITodayShareCnt(jsonObject.getLong("i_today_share_cnt"));
+        }
+        if (jsonObject.containsKey("i_today_return_cnt")) {
+            builder.setITodayReturnCnt(jsonObject.getLong("i_today_return_cnt"));
+        }
+        if (jsonObject.containsKey("i_ctr_today")) {
+            builder.setICtrToday(jsonObject.getDouble("i_ctr_today"));
+        }
+        if (jsonObject.containsKey("i_str_today")) {
+            builder.setIStrToday(jsonObject.getDouble("i_str_today"));
+        }
+        if (jsonObject.containsKey("i_rov_today")) {
+            builder.setIRovToday(jsonObject.getDouble("i_rov_today"));
+        }
+        if (jsonObject.containsKey("i_ros_today")) {
+            builder.setIRosToday(jsonObject.getDouble("i_ros_today"));
+        }
+        if (jsonObject.containsKey("i1day_exp_cnt")) {
+            builder.setI1DayExpCnt(jsonObject.getLong("i1day_exp_cnt"));
+        }
+        if (jsonObject.containsKey("i1day_click_cnt")) {
+            builder.setI1DayClickCnt(jsonObject.getLong("i1day_click_cnt"));
+        }
+        if (jsonObject.containsKey("i1day_share_cnt")) {
+            builder.setI1DayShareCnt(jsonObject.getLong("i1day_share_cnt"));
+        }
+        if (jsonObject.containsKey("i1day_return_cnt")) {
+            builder.setI1DayReturnCnt(jsonObject.getLong("i1day_return_cnt"));
+        }
+        if (jsonObject.containsKey("i_ctr1day")) {
+            builder.setICtr1Day(jsonObject.getDouble("i_ctr1day"));
+        }
+        if (jsonObject.containsKey("i_str1day")) {
+            builder.setIStr1Day(jsonObject.getDouble("i_str1day"));
+        }
+        if (jsonObject.containsKey("i_rov1day")) {
+            builder.setIRov1Day(jsonObject.getDouble("i_rov1day"));
+        }
+        if (jsonObject.containsKey("i_ros1day")) {
+            builder.setIRos1Day(jsonObject.getDouble("i_ros1day"));
+        }
+        if (jsonObject.containsKey("i3day_exp_cnt")) {
+            builder.setI3DayExpCnt(jsonObject.getLong("i3day_exp_cnt"));
+        }
+        if (jsonObject.containsKey("i3day_click_cnt")) {
+            builder.setI3DayClickCnt(jsonObject.getLong("i3day_click_cnt"));
+        }
+        if (jsonObject.containsKey("i3day_share_cnt")) {
+            builder.setI3DayShareCnt(jsonObject.getLong("i3day_share_cnt"));
+        }
+        if (jsonObject.containsKey("i3day_return_cnt")) {
+            builder.setI3DayReturnCnt(jsonObject.getLong("i3day_return_cnt"));
+        }
+        if (jsonObject.containsKey("i_ctr3day")) {
+            builder.setICtr3Day(jsonObject.getDouble("i_ctr3day"));
+        }
+        if (jsonObject.containsKey("i_str3day")) {
+            builder.setIStr3Day(jsonObject.getDouble("i_str3day"));
+        }
+        if (jsonObject.containsKey("i_rov3day")) {
+            builder.setIRov3Day(jsonObject.getDouble("i_rov3day"));
+        }
+        if (jsonObject.containsKey("i_ros3day")) {
+            builder.setIRos3Day(jsonObject.getDouble("i_ros3day"));
+        }
+
+        return builder.build();
+    }
+
+}

+ 13 - 8
recommend-feature-service/src/main/java/com/tzld/piaoquan/recommend/feature/service/UserAndVideoFeatureService.java

@@ -1,8 +1,8 @@
 package com.tzld.piaoquan.recommend.feature.service;
 
 import com.alibaba.fastjson.JSONObject;
-import com.tzld.piaoquan.recommend.feature.domain.video.base.UserFeatureInfo;
-import com.tzld.piaoquan.recommend.feature.domain.video.base.VideoFeatureInfo;
+import com.tzld.piaoquan.recommend.feature.model.feature.GetUserFeatureInfo;
+import com.tzld.piaoquan.recommend.feature.model.feature.GetVideoFeatureInfo;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.data.redis.core.RedisTemplate;
@@ -33,7 +33,7 @@ public class UserAndVideoFeatureService {
     @Resource(name = "redisTemplate")
     private RedisTemplate<String, Object> redisTemplate;
 
-    public List<UserFeatureInfo> queryUserFeature(List<String> mids) {
+    public List<GetUserFeatureInfo> queryUserFeature(List<String> mids) {
         if (CollectionUtils.isEmpty(mids)) {
             return Collections.emptyList();
         }
@@ -47,7 +47,7 @@ public class UserAndVideoFeatureService {
         if (CollectionUtils.isEmpty(resultObjList)) {
             return Collections.emptyList();
         }
-        List<UserFeatureInfo> result = Collections.synchronizedList(new ArrayList<>(resultObjList.size()));
+        List<GetUserFeatureInfo> result = Collections.synchronizedList(new ArrayList<>(resultObjList.size()));
         // 提交 Callable 任务
         List<Callable<Void>> callableList = new ArrayList<>(resultObjList.size());
         for (Object resultObj : resultObjList) {
@@ -58,7 +58,9 @@ public class UserAndVideoFeatureService {
                     if (resultJson == null) {
                         return null;
                     }
-//                    result.add();
+                    // convertToProto
+                    GetUserFeatureInfo getUserFeatureInfo = UserAndVideoFeatureConverter.convertUserFeatureInfoToProto(resultJson);
+                    result.add(getUserFeatureInfo);
                 } catch (Exception e) {
                     log.error("queryUserFeature error, resultJson:{}", resultJsonStr, e);
                 }
@@ -76,7 +78,7 @@ public class UserAndVideoFeatureService {
         return result;
     }
 
-    public List<VideoFeatureInfo> queryVideoFeature(List<Long> videoIds) {
+    public List<GetVideoFeatureInfo> queryVideoFeature(List<Long> videoIds) {
         if (CollectionUtils.isEmpty(videoIds)) {
             return Collections.emptyList();
         }
@@ -91,7 +93,7 @@ public class UserAndVideoFeatureService {
             return Collections.emptyList();
         }
         // 提交 Callable 任务
-        List<VideoFeatureInfo> result = Collections.synchronizedList(new ArrayList<>(resultObjList.size()));
+        List<GetVideoFeatureInfo> result = Collections.synchronizedList(new ArrayList<>(resultObjList.size()));
         List<Callable<Void>> callableList = new ArrayList<>(resultObjList.size());
         for (Object resultObj : resultObjList) {
             callableList.add(() -> {
@@ -101,7 +103,9 @@ public class UserAndVideoFeatureService {
                     if (resultJson == null) {
                         return null;
                     }
-//                    result.add();
+                    // convertToProto
+                    GetVideoFeatureInfo getVideoFeatureInfo = UserAndVideoFeatureConverter.convertVideoFeatureInfoToProtoVideo(resultJson);
+                    result.add(getVideoFeatureInfo);
                 } catch (Exception e) {
                     log.error("queryVideoFeature error, resultJson:{}", resultJsonStr, e);
                 }
@@ -118,4 +122,5 @@ public class UserAndVideoFeatureService {
 
         return result;
     }
+
 }

+ 0 - 32
recommend-feature-service/src/main/java/com/tzld/piaoquan/recommend/feature/web/UserAndVideoFeatureController.java

@@ -1,32 +0,0 @@
-package com.tzld.piaoquan.recommend.feature.web;
-
-import com.tzld.piaoquan.recommend.feature.domain.video.base.UserFeatureInfo;
-import com.tzld.piaoquan.recommend.feature.domain.video.base.VideoFeatureInfo;
-import com.tzld.piaoquan.recommend.feature.service.UserAndVideoFeatureService;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
-
-import javax.annotation.Resource;
-import java.util.List;
-
-/**
- * @author sunxy
- */
-@RestController
-@RequestMapping("/feature")
-public class UserAndVideoFeatureController {
-
-    @Resource
-    private UserAndVideoFeatureService userAndVideoFeatureService;
-
-    @RequestMapping("/getUserFeature")
-    public List<UserFeatureInfo> getUserFeature(List<String> mids) {
-        return userAndVideoFeatureService.queryUserFeature(mids);
-    }
-
-    @RequestMapping("/getVideoFeature")
-    public List<VideoFeatureInfo> getVideoFeature(List<Long> videoIds) {
-        return userAndVideoFeatureService.queryVideoFeature(videoIds);
-    }
-
-}