Browse Source

添加 ChartController 提供图表相关数据

reghao 7 months ago
parent
commit
1f75660054

+ 28 - 11
content/content-service/src/main/java/cn/reghao/tnb/content/app/exam/controller/ExamStatisticController.java → content/content-service/src/main/java/cn/reghao/tnb/content/app/data/controller/ChartController.java

@@ -1,10 +1,11 @@
-package cn.reghao.tnb.content.app.exam.controller;
+package cn.reghao.tnb.content.app.data.controller;
 
 import cn.reghao.jutil.web.WebResult;
+import cn.reghao.tnb.common.auth.AuthUser;
+import cn.reghao.tnb.content.app.data.service.ChartService;
 import cn.reghao.tnb.content.app.exam.model.chart.LineChartData;
-import cn.reghao.tnb.content.app.exam.service.ChartService;
-import io.swagger.v3.oas.annotations.tags.Tag;
 import io.swagger.v3.oas.annotations.Operation;
+import io.swagger.v3.oas.annotations.tags.Tag;
 import org.springframework.http.MediaType;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -15,20 +16,20 @@ import java.util.List;
 
 /**
  * @author reghao
- * @date 2024-08-29 09:47:56
+ * @date 2025-08-19 21:44:54
  */
-@Tag(name = "考试数据统计接口")
+@Tag(name = "图表数据接口")
 @RestController
-@RequestMapping("/api/content/exam/statistic")
-public class ExamStatisticController {
+@RequestMapping("/api/content/chart")
+public class ChartController {
     private final ChartService chartService;
 
-    public ExamStatisticController(ChartService chartService) {
+    public ChartController(ChartService chartService) {
         this.chartService = chartService;
     }
 
     @Operation(summary = "获取用户考试结果的统计数据", description = "N")
-    @GetMapping(value = "/count", produces = MediaType.APPLICATION_JSON_VALUE)
+    @GetMapping(value = "/exam/count", produces = MediaType.APPLICATION_JSON_VALUE)
     public String getExamCount() {
         List<String> list = new ArrayList<>();
         list.add("试卷1,试卷2,试卷3,试卷4,试卷4,试卷6,试卷7");
@@ -37,7 +38,7 @@ public class ExamStatisticController {
     }
 
     @Operation(summary = "获取用户的考试通过率", description = "N")
-    @GetMapping(value = "/rate", produces = MediaType.APPLICATION_JSON_VALUE)
+    @GetMapping(value = "/exam/rate", produces = MediaType.APPLICATION_JSON_VALUE)
     public String getExamPassRate() {
         List<String> list = new ArrayList<>();
         list.add("试卷1,试卷2,试卷3,试卷4,试卷4,试卷6,试卷7");
@@ -46,9 +47,25 @@ public class ExamStatisticController {
     }
 
     @Operation(summary = "获取折线图数据", description = "N")
-    @GetMapping(value = "/linechart", produces = MediaType.APPLICATION_JSON_VALUE)
+    @GetMapping(value = "/exam/linechart", produces = MediaType.APPLICATION_JSON_VALUE)
     public String getLineChart() {
         LineChartData lineChartData = chartService.getLineChartData();
         return WebResult.success(lineChartData);
     }
+
+    @AuthUser
+    @Operation(summary = "用户最近一周观看视频时长统计", description = "N")
+    @GetMapping(value = "/video/watch", produces = MediaType.APPLICATION_JSON_VALUE)
+    public String getWatchLineChart() {
+        List<String> list = chartService.getWatchLineChart();
+        return WebResult.success(list);
+    }
+
+    @AuthUser
+    @Operation(summary = "用户最近一周观看视频分区统计", description = "N")
+    @GetMapping(value = "/video/region", produces = MediaType.APPLICATION_JSON_VALUE)
+    public String getRegionPieChart() {
+        List<String> list = chartService.getRegionPieChart();
+        return WebResult.success(list);
+    }
 }

+ 72 - 9
content/content-service/src/main/java/cn/reghao/tnb/content/app/vod/service/WatchAnalysisService.java → content/content-service/src/main/java/cn/reghao/tnb/content/app/data/service/ChartService.java

@@ -1,31 +1,94 @@
-package cn.reghao.tnb.content.app.vod.service;
+package cn.reghao.tnb.content.app.data.service;
 
 import cn.reghao.jutil.jdk.converter.DateTimeConverter;
+import cn.reghao.jutil.jdk.text.TextFile;
 import cn.reghao.tnb.common.auth.UserContext;
+import cn.reghao.tnb.content.app.exam.model.chart.LineChartData;
 import cn.reghao.tnb.content.app.vod.model.po.VideoCategory;
 import cn.reghao.tnb.content.app.vod.model.query.CategoryQuery;
+import cn.reghao.tnb.content.app.vod.service.CategoryService;
 import org.springframework.stereotype.Service;
 
 import java.security.SecureRandom;
 import java.time.LocalDateTime;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Random;
+import java.util.*;
 import java.util.stream.Collectors;
 
 /**
  * @author reghao
- * @date 2023-10-11 14:35:46
+ * @date 2025-08-14 09:31:22
  */
 @Service
-public class WatchAnalysisService {
+public class ChartService {
+    private final TextFile textFile = new TextFile();
     private final CategoryService categoryService;
 
-    public WatchAnalysisService(CategoryService categoryService) {
+    public ChartService(CategoryService categoryService) {
         this.categoryService = categoryService;
     }
 
-    public List<String> getLineChartData() {
+    public LineChartData getLineChartData() {
+        String filePath = "/home/reghao/Downloads/2010四川高考一分一段.txt";
+        List<String> lines = textFile.read(filePath);
+        Map<Integer, Integer> map = new TreeMap<>();
+        for (String line : lines) {
+            String[] arr = line.split("\\s+");
+            int score = Integer.parseInt(arr[0].replace("分", ""));
+            int num = Integer.parseInt(arr[1]);
+            int total = Integer.parseInt(arr[2]);
+            map.put(score, num);
+        }
+
+        int level1 = 512;
+        int level2 = 441;
+        int level3 = 413;
+        int level4 = 361;
+        int level0 = level1+100;
+        int level5 = level4-50;
+
+        Map<String, Integer> map1 = new TreeMap<>();
+        int num0 = getNum(map, level0, 750);
+        map1.put("level0", num0);
+
+        int num1 = getNum(map, level1, level0);
+        map1.put("level1", num1);
+
+        int num2 = getNum(map, level2, level1);
+        map1.put("level2", num2);
+
+        int num3 = getNum(map, level3, level2);
+        map1.put("level3", num3);
+
+        int num4 = getNum(map, level4, level3);
+        map1.put("level4", num4);
+
+        int num5 = getNum(map, level5, level4);
+        map1.put("level5", num5);
+
+        int num6 = getNum(map, 1, level5);
+        map1.put("level6", num6);
+
+        int max = map1.values().stream().mapToInt(i -> i).max().orElse(-1);
+        LineChartData chartData = new LineChartData("人数", 0, max, 10, "分数段");
+        chartData.setXLabel(map1.keySet().toArray(new String[0]));
+        chartData.setXValue(map1.values().toArray(new Integer[0]));
+        return chartData;
+    }
+
+    private int getNum(Map<Integer, Integer> map, int start, int end) {
+        int num = 0;
+        for (int i = start; i < end; i++) {
+            if (map.get(i) != null) {
+                num += map.get(i);
+            } else {
+                System.out.println(i + " get null");
+            }
+        }
+
+        return num;
+    }
+
+    public List<String> getWatchLineChart() {
         long loginUser = UserContext.getUser();
 
         List<String> xAxis = new ArrayList<>();
@@ -50,7 +113,7 @@ public class WatchAnalysisService {
         return list;
     }
 
-    public List<String> getPieChartData() {
+    public List<String> getRegionPieChart() {
         long loginUser = UserContext.getUser();
 
         CategoryQuery categoryQuery = new CategoryQuery.Builder().pid(1).build();

+ 1 - 1
content/content-service/src/main/java/cn/reghao/tnb/content/app/data/spider/TaskContext.java

@@ -27,7 +27,7 @@ public class TaskContext {
         this.richTextMongo = richTextMongo;
     }
 
-    @PostConstruct
+    //@PostConstruct
     public void startTask() {
         GetNewsDetailTask getNewsDetailTask = new GetNewsDetailTask(webRequest, richTextMongo);
         scheduler.scheduleAtFixedRate(getNewsDetailTask, 0, 1, TimeUnit.HOURS);

+ 0 - 79
content/content-service/src/main/java/cn/reghao/tnb/content/app/exam/service/ChartService.java

@@ -1,79 +0,0 @@
-package cn.reghao.tnb.content.app.exam.service;
-
-import cn.reghao.jutil.jdk.text.TextFile;
-import cn.reghao.tnb.content.app.exam.model.chart.LineChartData;
-import org.springframework.stereotype.Service;
-
-import java.util.List;
-import java.util.Map;
-import java.util.TreeMap;
-
-/**
- * @author reghao
- * @date 2025-08-14 09:31:22
- */
-@Service
-public class ChartService {
-    private final TextFile textFile = new TextFile();
-
-    public LineChartData getLineChartData() {
-        String filePath = "/home/reghao/Downloads/2010四川高考一分一段.txt";
-        List<String> lines = textFile.read(filePath);
-        Map<Integer, Integer> map = new TreeMap<>();
-        for (String line : lines) {
-            String[] arr = line.split("\\s+");
-            int score = Integer.parseInt(arr[0].replace("分", ""));
-            int num = Integer.parseInt(arr[1]);
-            int total = Integer.parseInt(arr[2]);
-            map.put(score, num);
-        }
-
-        int level1 = 512;
-        int level2 = 441;
-        int level3 = 413;
-        int level4 = 361;
-        int level0 = level1+100;
-        int level5 = level4-50;
-
-        Map<String, Integer> map1 = new TreeMap<>();
-        int num0 = getNum(map, level0, 750);
-        map1.put("level0", num0);
-
-        int num1 = getNum(map, level1, level0);
-        map1.put("level1", num1);
-
-        int num2 = getNum(map, level2, level1);
-        map1.put("level2", num2);
-
-        int num3 = getNum(map, level3, level2);
-        map1.put("level3", num3);
-
-        int num4 = getNum(map, level4, level3);
-        map1.put("level4", num4);
-
-        int num5 = getNum(map, level5, level4);
-        map1.put("level5", num5);
-
-        int num6 = getNum(map, 1, level5);
-        map1.put("level6", num6);
-
-        int max = map1.values().stream().mapToInt(i -> i).max().orElse(-1);
-        LineChartData chartData = new LineChartData("人数", 0, max, 10, "分数段");
-        chartData.setXLabel(map1.keySet().toArray(new String[0]));
-        chartData.setXValue(map1.values().toArray(new Integer[0]));
-        return chartData;
-    }
-
-    private int getNum(Map<Integer, Integer> map, int start, int end) {
-        int num = 0;
-        for (int i = start; i < end; i++) {
-            if (map.get(i) != null) {
-                num += map.get(i);
-            } else {
-                System.out.println(i + " get null");
-            }
-        }
-
-        return num;
-    }
-}

+ 0 - 42
content/content-service/src/main/java/cn/reghao/tnb/content/app/vod/controller/WatchAnalysisController.java

@@ -1,42 +0,0 @@
-package cn.reghao.tnb.content.app.vod.controller;
-
-import cn.reghao.jutil.web.WebResult;
-import cn.reghao.tnb.common.auth.AuthUser;
-import cn.reghao.tnb.content.app.vod.service.WatchAnalysisService;
-import io.swagger.v3.oas.annotations.tags.Tag;
-import io.swagger.v3.oas.annotations.Operation;
-import org.springframework.http.MediaType;
-import org.springframework.web.bind.annotation.*;
-
-import java.util.List;
-
-/**
- * @author reghao
- * @date 2023-08-27 19:12:45
- */
-@Tag(name = "用户观看视频分析接口")
-@RestController
-@RequestMapping("/api/content/video/analysis")
-public class WatchAnalysisController {
-    private final WatchAnalysisService watchAnalysisService;
-
-    public WatchAnalysisController(WatchAnalysisService watchAnalysisService) {
-        this.watchAnalysisService = watchAnalysisService;
-    }
-
-    @AuthUser
-    @Operation(summary = "用户最近一周观看视频时长统计", description = "N")
-    @GetMapping(value = "/watch", produces = MediaType.APPLICATION_JSON_VALUE)
-    public String getExamCount() {
-        List<String> list = watchAnalysisService.getLineChartData();
-        return WebResult.success(list);
-    }
-
-    @AuthUser
-    @Operation(summary = "用户最近一周观看视频分区统计", description = "N")
-    @GetMapping(value = "/watch/region", produces = MediaType.APPLICATION_JSON_VALUE)
-    public String getExamPassRate() {
-        List<String> list = watchAnalysisService.getPieChartData();
-        return WebResult.success(list);
-    }
-}