wukai 7 months ago
parent
commit
fb8ec4efa7

+ 38 - 4
jjt-biz/src/main/java/com/jjt/biz/util/DataUtil.java

@@ -2,10 +2,8 @@ package com.jjt.biz.util;
 
 import com.jjt.biz.vo.ScoreVO;
 
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
+import java.util.stream.Collectors;
 
 /**
  * DataUtil$
@@ -84,4 +82,40 @@ public class DataUtil {
         }
     }
 
+    /**
+     * 补数据
+     *
+     * @param standTime
+     * @param result
+     */
+    public static void standTimeNet(List<Date> standTime, List<Map<String, Object>> result) {
+        for (Map<String, Object> map : result) {
+            // 创建一个新的 times 和 scores 列表,以确保它们与标准时间序列对齐
+            List<Date> alignedTimes = new ArrayList<>();
+            List<Float> alignedScores = new ArrayList<>();
+            // 使用一个Map来存储原始数据,以便快速查找
+            Map<Date, Float> timeScoreMap = new HashMap<>();
+            List<Map<String, Object>> trendList = (List<Map<String, Object>>) map.get("data");
+            List<Map<String, Object>> newTrend = new ArrayList<>();
+            Map<Date, Float> map1 = trendList.stream().collect(Collectors.toMap(mp -> (Date) mp.get("time"), mp -> (Float) mp.get("value")));
+            for (Date d : map1.keySet()) {
+                timeScoreMap.put(d, map1.get(d));
+            }
+            // 遍历标准时间序列,并根据需要添加空值或实际值
+            for (Date time : standTime) {
+                Map<String, Object> map2 = new HashMap<>(16);
+                map2.put("time", time);
+                map2.put("value", timeScoreMap.getOrDefault(time, 0F));
+                newTrend.add(map2);
+            }
+            map.remove("data");
+            map.put("data", newTrend);
+            // 更新原有的 times 和 scores 列表
+//            trendList.clear();
+//            trendList.addAll(newTrend);
+//            scores.clear();
+//            scores.addAll(alignedScores);
+        }
+    }
+
 }

+ 9 - 0
jjt-biz/src/main/java/com/jjt/risk/controller/RiskOtherController.java

@@ -156,6 +156,8 @@ public class RiskOtherController extends BaseController {
     @GetMapping(value = "/network/{modelId}/{metricsId}")
     public AjaxResult network(@PathVariable("modelId") Long modelId, @PathVariable("metricsId") Long metricsId) {
         List<Map<String, Object>> result = new ArrayList<>();
+        final int[] len = {0};
+        final List<Date>[] standTime = new List[]{new ArrayList<>()};
         modelDetailService.selectBizModelDetailList4ModelId(modelId).forEach(md -> {
             BizObjMetrics bom = new BizObjMetrics();
             bom.setObjId(md.getObjId());
@@ -174,18 +176,25 @@ public class RiskOtherController extends BaseController {
                 q.setObjMetricsId(om.getObjMetricsId());
                 q.setParams(params);
                 List<BizObjMetricsData> list = dataService.selectBizObjMetricsDataList(q);
+                List<Date> times = new ArrayList<>();
                 list.forEach(data -> {
                     Map<String, Object> map = new HashMap<>(16);
+                    times.add(data.getCreateTime());
                     map.put("time", data.getCreateTime());
                     map.put("value", data.getdValue().floatValue());
                     trendList.add(map);
                 });
+                if (list.size() > len[0]) {
+                    len[0] = list.size();
+                    standTime[0] = times;
+                }
                 objectMap.put("data", trendList);
                 result.add(objectMap);
             }
         });
 
 
+        DataUtil.standTimeNet(standTime[0], result);
 //        String[] objs = {"node1", "node2", "node3", "ecs4", "业务对象5", "虚拟主机6", "cluster7"};
 //        for (String obj : objs) {
 //            Map<String, Object> map = new HashMap<>(16);