新增 特定设备出现告警时,统计日志记录于 Warning 文件

- 新增当检测到特定站点出现警告时,统计此站点的日志
This commit is contained in:
fengyarnom 2024-10-30 18:22:52 +08:00
parent f45ade6dfa
commit a826d34f9d
2 changed files with 77 additions and 0 deletions

View File

@ -0,0 +1,67 @@
package com.imdroid.sideslope.bd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.*;
import java.nio.file.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.*;
import java.util.stream.Collectors;
@Component
public class WarningLogExecutor {
// 警告日志生成
@Value("${log.directory}")
private String logDirectory;
private final ExecutorService executor;
public WarningLogExecutor() {
this.executor = Executors.newFixedThreadPool(4);
}
public void generateWarningLogs(String keyword) {
try {
List<Path> logFiles = Files.list(Paths.get(logDirectory))
.filter(Files::isRegularFile)
.filter(path -> path.getFileName().toString().matches("sideslopertcm(\\.\\d{4}-\\d{2}-\\d{2}\\.\\d+)?\\.log"))
.collect(Collectors.toList());
for (Path logFile : logFiles) {
executor.submit(() -> processFile(logFile, keyword));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
executor.shutdown();
}
}
private void processFile(Path logFile, String keyword) {
String date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
String warningFileName = "Warning-" + keyword + "-" + date + ".log";
Path warningFile = Paths.get(logDirectory, warningFileName);
try (BufferedReader reader = Files.newBufferedReader(logFile);
BufferedWriter writer = Files.newBufferedWriter(warningFile, StandardOpenOption.CREATE, StandardOpenOption.APPEND)) {
String line;
while ((line = reader.readLine()) != null) {
// 行如果包含了特定的关键词比如站点的编号则记录这一行
if (line.contains(keyword)) {
writer.write(line);
writer.newLine();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@ -3,6 +3,7 @@ package com.imdroid.sideslope.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.imdroid.common.util.NumberUtils; import com.imdroid.common.util.NumberUtils;
import com.imdroid.secapi.dto.*; import com.imdroid.secapi.dto.*;
import com.imdroid.sideslope.bd.WarningLogExecutor;
import com.imdroid.sideslope.sal.Device; import com.imdroid.sideslope.sal.Device;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -21,6 +22,8 @@ public class WarningServiceImpl implements WarningService {
WarningMsgMapper warningMsgMapper; WarningMsgMapper warningMsgMapper;
@Autowired @Autowired
GnssStatusMapper gnssStatusMapper; GnssStatusMapper gnssStatusMapper;
@Autowired
private WarningLogExecutor warningLogExecutor;
// warning type <-> level & value // warning type <-> level & value
Map<Integer, int[]> cfgMap = new ConcurrentHashMap<>(); Map<Integer, int[]> cfgMap = new ConcurrentHashMap<>();
@ -174,6 +177,9 @@ public class WarningServiceImpl implements WarningService {
warningMsgMapper.insert(warningMsg); warningMsgMapper.insert(warningMsg);
//告警级别 //告警级别
curStatus.setWarningcode(curStatus.getWarningcode() | warningType); curStatus.setWarningcode(curStatus.getWarningcode() | warningType);
// 新告警出现后生成对于设备的 warning 日志文件
generate_warning_logs(curStatus.getDeviceid());
} }
isUpdated = true; isUpdated = true;
} }
@ -219,4 +225,8 @@ public class WarningServiceImpl implements WarningService {
} }
} }
} }
public void generate_warning_logs(String device_id){
warningLogExecutor.generateWarningLogs(device_id);
}
} }