Browse Source

Merge branch 'project-jmga-2.2.0' into project-jmga-dev

dengsixing 1 week ago
parent
commit
63a86c124d

+ 13 - 61
src/main/java/com/fdkankan/contro/service/impl/JmgaServiceImpl.java

@@ -21,6 +21,7 @@ import com.fdkankan.contro.constant.ModelingControlRespCode;
 import com.fdkankan.contro.entity.*;
 import com.fdkankan.contro.entity.*;
 import com.fdkankan.contro.mq.service.IBuildLogService;
 import com.fdkankan.contro.mq.service.IBuildLogService;
 import com.fdkankan.contro.service.*;
 import com.fdkankan.contro.service.*;
+import com.fdkankan.contro.util.LogUtil;
 import com.fdkankan.contro.util.SceneSourceUtil;
 import com.fdkankan.contro.util.SceneSourceUtil;
 import com.fdkankan.contro.vo.SendCallAlgorithmParam;
 import com.fdkankan.contro.vo.SendCallAlgorithmParam;
 import com.fdkankan.contro.vo.UploadSceneOrigParamVo;
 import com.fdkankan.contro.vo.UploadSceneOrigParamVo;
@@ -37,8 +38,13 @@ import java.io.File;
 import java.io.IOException;
 import java.io.IOException;
 import java.nio.charset.StandardCharsets;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Files;
+import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.nio.file.Paths;
+import java.time.LocalDate;
+import java.time.format.DateTimeFormatter;
 import java.util.*;
 import java.util.*;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 import java.util.stream.Collectors;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 import java.util.stream.Stream;
 
 
@@ -483,33 +489,15 @@ public class JmgaServiceImpl implements IJmgaService {
         }
         }
     }
     }
 
 
-    // 递归收集文件列表
-    private static void collectFiles(File dir, List<File> fileList) {
-        File[] files = dir.listFiles();
-        if (files == null) return;
-
-        for (File file : files) {
-            if (file.isDirectory()) {
-                collectFiles(file, fileList);
-            } else {
-                fileList.add(file);
-            }
-        }
-    }
-
     private void copyLog(String serverPath, String tmpPath, String... levels) throws IOException {
     private void copyLog(String serverPath, String tmpPath, String... levels) throws IOException {
-        level: for (String level : levels) {
-            String infoPath = serverPath + File.separator + level + File.separator;
-            List<File> fileList = FileUtil.loopFiles(infoPath);
-            if(CollUtil.isEmpty(fileList)){
+        for (String level : levels) {
+            String levelPath = serverPath + File.separator + level + File.separator;
+            List<Path> latestDaysLogs = LogUtil.getLatestDaysLogs(Paths.get(levelPath), level, 7);
+            if(CollUtil.isEmpty(latestDaysLogs)){
                 continue;
                 continue;
             }
             }
-            CollUtil.reverse(fileList);
-            for (File file : fileList) {
-                int i = fileList.indexOf(file);
-                if(i > 7){
-                    continue level;
-                }
+            for (Path path : latestDaysLogs) {
+                File file = path.toFile();
                 String targetPath = file.getAbsolutePath().replace(FileUtil.getParent(serverPath, 1) + File.separator, tmpPath);
                 String targetPath = file.getAbsolutePath().replace(FileUtil.getParent(serverPath, 1) + File.separator, tmpPath);
                 File targetLog = new File(targetPath);
                 File targetLog = new File(targetPath);
 //                targetLog.createNewFile();
 //                targetLog.createNewFile();
@@ -524,44 +512,8 @@ public class JmgaServiceImpl implements IJmgaService {
     }
     }
 
 
     public static void main(String[] args) throws IOException {
     public static void main(String[] args) throws IOException {
-        String logBasePath = "D:\\Downloads\\";
-        File baseDir = new File(logBasePath);
-        File[] modelingDirs = baseDir.listFiles(f ->
-                f.isDirectory() && f.getName().startsWith("modeling-control")
-        );
-        if (modelingDirs != null) {
-            for (File dir : modelingDirs) {
-                String serverPath = dir.getAbsolutePath();
-                String tmpPath = "D:\\Downloads\\temp\\";
-                String[] levels = {"info","error","sql"};
-                level: for (String level : levels) {
-                    String infoPath = serverPath + File.separator + level + File.separator;
-                    List<File> fileList = FileUtil.loopFiles(infoPath);
-                    if(CollUtil.isEmpty(fileList)){
-                        continue;
-                    }
-                    CollUtil.reverse(fileList);
-                    for (File file : fileList) {
-                        int i = fileList.indexOf(file);
-                        if(i > 7){
-                            continue level;
-                        }
-                        String targetPath = file.getAbsolutePath().replace(FileUtil.getParent(serverPath, 1) + File.separator, tmpPath);
-                        File targetLog = new File(targetPath);
-//                targetLog.createNewFile();
-                        try (Stream<String> lines = Files.lines(Paths.get(file.getAbsolutePath()), StandardCharsets.UTF_8)) {
-                            lines.map(line -> line.replace("公安", "xx").replaceAll(ipv4Pattern, "ip"))
-                                    .forEach(line -> FileUtil.appendUtf8String(line, targetLog)); // 或者进行其他处理
-                        } catch (IOException e) {
-                            e.printStackTrace();
-                        }
-                    }
-                }
-            }
-        }
-
-
 
 
     }
     }
 
 
+
 }
 }

+ 88 - 0
src/main/java/com/fdkankan/contro/util/LogUtil.java

@@ -0,0 +1,88 @@
+package com.fdkankan.contro.util;
+
+import com.fdkankan.common.util.DateExtUtil;
+import com.fdkankan.contro.service.impl.JmgaServiceImpl;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.time.LocalDate;
+import java.time.format.DateTimeFormatter;
+import java.util.*;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+public class LogUtil {
+
+    // info-2026-01-08-0.log
+    private static final Pattern LOG_PATTERN =
+            Pattern.compile("info-(\\d{4}-\\d{2}-\\d{2})-(\\d+)\\.log");
+
+    public static List<Path> getLatestDaysLogs(Path logDir, String level, int days) throws IOException {
+        String currentDayLogName = level.concat(".log");
+        Map<LocalDate, List<Path>> logsByDate = new HashMap<>();
+        Path infoLog = null;
+
+        try (Stream<Path> stream = Files.list(logDir)) {
+            for (Path path : stream.collect(Collectors.toList())) {
+                String fileName = path.getFileName().toString();
+
+                // info.log
+                if (currentDayLogName.equals(fileName)) {
+                    infoLog = path;
+                    continue;
+                }
+
+                Matcher matcher = LOG_PATTERN.matcher(fileName);
+                if (matcher.matches()) {
+                    LocalDate date = LocalDate.parse(matcher.group(1), DateTimeFormatter.ofPattern(DateExtUtil.dateStyle4));
+                    logsByDate
+                            .computeIfAbsent(date, d -> new ArrayList<>())
+                            .add(path);
+                }
+            }
+        }
+
+        // 取最近N天
+        List<LocalDate> latestDates = logsByDate.keySet().stream()
+                .sorted(Comparator.reverseOrder())
+                .limit(days)
+                .collect(Collectors.toList());
+
+        List<Path> result = new ArrayList<>();
+
+        // 同一天的日志可以按切片下标排序(可选)
+        for (LocalDate date : latestDates) {
+            List<Path> dayLogs = logsByDate.get(date);
+            dayLogs.sort(Comparator.comparingInt(LogUtil::extractIndex));
+            result.addAll(dayLogs);
+        }
+
+        // 一定包含 info.log
+        if (infoLog != null) {
+            result.add(infoLog);
+        }
+
+        return result;
+    }
+
+    // 提取切片下标 N
+    private static int extractIndex(Path path) {
+        Matcher matcher = LOG_PATTERN.matcher(path.getFileName().toString());
+        if (matcher.matches()) {
+            return Integer.parseInt(matcher.group(2));
+        }
+        return 0;
+    }
+
+    public static void main(String[] args) throws IOException {
+        List<Path> info = getLatestDaysLogs(Paths.get("D:\\Downloads\\modeling-control-1\\info"), "info", 7);
+        for (Path path : info) {
+            System.out.println(path.toFile().getAbsolutePath());
+        }
+    }
+
+}