1. 更改告警日志收集逻辑,只保存最近一个小时范围内的日志
2. 只监控连续无固定解
This commit is contained in:
parent
69da1b6c94
commit
55849e2e76
@ -210,7 +210,7 @@ public class WarningServiceImpl implements WarningService {
|
||||
curStatus.setWarningcode(curStatus.getWarningcode() | warningType);
|
||||
|
||||
// 新告警出现后,生成对应设备的 warning 日志文件
|
||||
//generate_warning_logs(curStatus.getDeviceid(),warningType,auxInfo);
|
||||
generate_warning_logs(curStatus.getDeviceid(),warningType,warningName);
|
||||
}
|
||||
isUpdated = true;
|
||||
}
|
||||
@ -258,7 +258,8 @@ public class WarningServiceImpl implements WarningService {
|
||||
}
|
||||
|
||||
public void generate_warning_logs(String device_id,int warning_type,String warning_type_name){
|
||||
if (warning_type == WarningCfg.TYPE_LOW_VOLTAGE) {
|
||||
// 连续无固定解
|
||||
if (warning_type == WarningCfg.TYPE_NO_FIXED_RESULT) {
|
||||
WarningLogExecutor warningLogExecutor = new WarningLogExecutor(logDirectory);
|
||||
warningLogExecutor.generateWarningLogs(device_id, warning_type_name);
|
||||
}
|
||||
|
||||
@ -1,21 +1,15 @@
|
||||
package com.imdroid.common.util;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.*;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import java.io.*;
|
||||
import java.time.*;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class WarningLogExecutor {
|
||||
|
||||
private final String logDirectory;
|
||||
private final ExecutorService executor;
|
||||
// 记录已初始化的文件
|
||||
private final ConcurrentHashMap<String, Boolean> initializedFiles = new ConcurrentHashMap<>();
|
||||
|
||||
public WarningLogExecutor(String logDirectory) {
|
||||
this.logDirectory = logDirectory;
|
||||
@ -23,68 +17,69 @@ public class WarningLogExecutor {
|
||||
}
|
||||
|
||||
public void generateWarningLogs(String keyword, String warningTypeName) {
|
||||
// 单线程处理,防止数据写入混乱
|
||||
executor.submit(() -> searchLogsForDeviceID(this.logDirectory, keyword, warningTypeName));
|
||||
}
|
||||
|
||||
public static void searchLogsForDeviceID(String dirPath, String deviceId, String warningType) {
|
||||
try {
|
||||
String date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm"));
|
||||
String warningFileName = "Warning-" + keyword + "-" + date + ".log";
|
||||
Path warningFile = Paths.get(logDirectory, warningFileName);
|
||||
|
||||
// 如果是新文件,先写入初始化信息
|
||||
if (initializedFiles.putIfAbsent(warningFileName, true) == null) {
|
||||
initializeWarningFile(warningFile, keyword, warningTypeName);
|
||||
// 判断目录是否存在,一般写在 application.properties 中
|
||||
File dir = new File(dirPath);
|
||||
if (!dir.exists() || !dir.isDirectory()) {
|
||||
return;
|
||||
}
|
||||
|
||||
try (Stream<Path> logFilesStream = Files.list(Paths.get(logDirectory))) {
|
||||
List<Path> logFiles = logFilesStream
|
||||
.filter(Files::isRegularFile)
|
||||
.filter(path -> path.getFileName().toString().matches("sideslopertcm(\\.\\d{4}-\\d{2}-\\d{2}\\.\\d+)?\\.log"))
|
||||
.collect(Collectors.toList());
|
||||
// 只收集一小时内的日志,否则太影响性能
|
||||
long oneHourAgo = System.currentTimeMillis() - 3600000;
|
||||
File[] files = dir.listFiles((d, name) -> name.startsWith("sideslopertcm"));
|
||||
|
||||
for (Path logFile : logFiles) {
|
||||
executor.submit(() -> processFile(logFile, keyword, warningFile));
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
executor.shutdown();
|
||||
|
||||
if (files != null && files.length > 0) {
|
||||
Arrays.sort(files, (f1, f2) -> Long.compare(f2.lastModified(), f1.lastModified()));
|
||||
|
||||
StringBuilder processedFiles = new StringBuilder();
|
||||
for (File file : files) {
|
||||
if (file.lastModified() >= oneHourAgo) {
|
||||
processedFiles.append(String.format("%s\n", file.getName()));
|
||||
}
|
||||
}
|
||||
|
||||
private void initializeWarningFile(Path warningFile, String keyword, String warningTypeName) {
|
||||
try (BufferedWriter writer = Files.newBufferedWriter(warningFile,
|
||||
StandardOpenOption.CREATE, StandardOpenOption.APPEND)) {
|
||||
writer.write("=== Warning Log File ===");
|
||||
writer.newLine();
|
||||
writer.write("Created Time: " + LocalDateTime.now());
|
||||
writer.newLine();
|
||||
writer.write("Warning Type Name: " + warningTypeName);
|
||||
writer.newLine();
|
||||
writer.write("Keyword: " + keyword);
|
||||
writer.newLine();
|
||||
writer.write("==============================");
|
||||
writer.newLine();
|
||||
writer.newLine();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (processedFiles.length() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
private void processFile(Path logFile, String keyword, Path warningFile) {
|
||||
try (BufferedReader reader = Files.newBufferedReader(logFile);
|
||||
BufferedWriter writer = Files.newBufferedWriter(warningFile,
|
||||
StandardOpenOption.CREATE, StandardOpenOption.APPEND)) {
|
||||
// 文件名的日期只保存年月日,当天重复触发会新的覆盖旧的
|
||||
String date = DateTimeFormatter.ofPattern("yyyyMMdd")
|
||||
.format(LocalDateTime.now());
|
||||
String outputFileName = String.format("%s/Warning_%s_%s_%s.log", dirPath,deviceId,date, warningType);
|
||||
|
||||
writer.write("Processing source file: " + logFile.getFileName());
|
||||
writer.newLine();
|
||||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(outputFileName))) {
|
||||
writer.write("=== 搜索信息 ===\n");
|
||||
writer.write("处理时间: " + LocalDateTime.now() + "\n");
|
||||
writer.write("搜索目录: " + dirPath + "\n");
|
||||
writer.write("设备ID: " + deviceId + "\n");
|
||||
writer.write("警告类型: " + warningType + "\n\n");
|
||||
|
||||
writer.write("=== 处理文件列表 ===\n");
|
||||
writer.write(processedFiles.toString());
|
||||
writer.write("\n=== 匹配结果 ===\n");
|
||||
|
||||
for (File file : files) {
|
||||
if (file.lastModified() >= oneHourAgo) {
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
if (line.contains(keyword)) {
|
||||
writer.write(line);
|
||||
writer.newLine();
|
||||
if (line.contains(deviceId)) {
|
||||
writer.write(String.format("[%s] %s\n",
|
||||
file.getName(), line));
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user