|
@@ -2,14 +2,11 @@ package com.scbfkj.uni.service;
|
|
|
|
|
|
import com.scbfkj.uni.library.DataFormatUtil;
|
|
|
import com.scbfkj.uni.library.UniReturnUtil;
|
|
|
-import com.scbfkj.uni.library.script.DatabaseScriptUtil;
|
|
|
import com.scbfkj.uni.process.DataBase;
|
|
|
-import com.scbfkj.uni.process.Elasticsearch;
|
|
|
-import com.scbfkj.uni.process.Kafka;
|
|
|
import com.scbfkj.uni.system.Config;
|
|
|
+import com.zaxxer.hikari.pool.HikariPool;
|
|
|
|
|
|
import java.io.File;
|
|
|
-import java.io.FileNotFoundException;
|
|
|
import java.util.*;
|
|
|
import java.util.regex.Matcher;
|
|
|
import java.util.regex.Pattern;
|
|
@@ -18,6 +15,26 @@ import java.util.stream.Stream;
|
|
|
|
|
|
public class LoggerService {
|
|
|
|
|
|
+ public enum LogType {
|
|
|
+
|
|
|
+ USER("userlog"),
|
|
|
+ INTERFACE("interfacelog"),
|
|
|
+ SYSTEM("systemerrlog"),
|
|
|
+ SERVICE("servicelog"),
|
|
|
+ SERVICE_ERR("serviceerrlog");
|
|
|
+
|
|
|
+ private String name;
|
|
|
+
|
|
|
+ LogType(String name) {
|
|
|
+ this.name = name;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getName() {
|
|
|
+ return name;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
private static final String connection = """
|
|
|
{
|
|
|
"jdbcUrl": "jdbc:sqlite:%s",
|
|
@@ -25,15 +42,16 @@ public class LoggerService {
|
|
|
"enableCache":false
|
|
|
}
|
|
|
""";
|
|
|
- private static long filename = 0;
|
|
|
+ private static String currentFileName;
|
|
|
|
|
|
private final static Set<Long> fileNames = new HashSet<>();
|
|
|
- private static final String dbFileName = "logs/%s.sqlite";
|
|
|
+ private static final String dir = "logs/";
|
|
|
|
|
|
- public static void log(String target, Map<String, Object> data) {
|
|
|
- filename = System.currentTimeMillis() / Config.splitCount;
|
|
|
-
|
|
|
- String filePath = dbFileName.formatted(filename);
|
|
|
+ public static void log(LogType type, Map<String, Object> data) {
|
|
|
+
|
|
|
+ long filename = System.currentTimeMillis() / Config.splitCount;
|
|
|
+ currentFileName = filename + ".sqlite";
|
|
|
+ String filePath = dir + currentFileName;
|
|
|
String connectionStr = connection.formatted(filePath);
|
|
|
synchronized (fileNames) {
|
|
|
if (!fileNames.contains(filename)) {
|
|
@@ -46,7 +64,8 @@ public class LoggerService {
|
|
|
primary key autoincrement,
|
|
|
target TEXT,
|
|
|
currentfile TEXT,
|
|
|
- targetconnection TEXT,
|
|
|
+ datasourceid TEXT,
|
|
|
+ expression TEXT,
|
|
|
datacontent TEXT
|
|
|
)""");
|
|
|
} catch (Exception e) {
|
|
@@ -59,50 +78,47 @@ public class LoggerService {
|
|
|
}
|
|
|
try {
|
|
|
List<Object[]> datas = new ArrayList<>();
|
|
|
- List<String> targets = Config.targets;
|
|
|
- for (String targetconnection : targets) {
|
|
|
- datas.add(new Object[]{target, filename, targetconnection, DataFormatUtil.toString(data)});
|
|
|
+
|
|
|
+ List<Map<String, Object>> targets = DataBase.query(Config.centerConnectionStr, "select * from systeminfo where keyname=?", Collections.singletonList(new Object[]{type.getName()}));
|
|
|
+ for (Map<String, Object> targetconnection : targets) {
|
|
|
+ datas.add(new Object[]{type.getName(), filePath, targetconnection.get("datasourceid"), targetconnection.get("expression"), DataFormatUtil.toString(data)});
|
|
|
}
|
|
|
-
|
|
|
- DataBase.updateBatch(connectionStr, "insert into logs (target,currentfile,targetconnection,datacontent) values(?,?)", datas);
|
|
|
+ DataBase.updateBatch(connectionStr, "insert into logs (target,currentfile,datasourceid,expression,datacontent) values(?,?,?,?,?)", datas);
|
|
|
} catch (Exception e) {
|
|
|
System.out.println(UniReturnUtil.getMessage(e));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static void sendMessage(String dirPath) throws FileNotFoundException {
|
|
|
+ public static void sendMessage() {
|
|
|
|
|
|
- if (Objects.isNull(dirPath)) {
|
|
|
- dirPath = "logs";
|
|
|
- }
|
|
|
- File file = new File(dirPath);
|
|
|
- if (file.isFile()) {
|
|
|
- throw new FileNotFoundException("%s不是一个目录".formatted(dirPath));
|
|
|
- }
|
|
|
+
|
|
|
+ File file = new File(dir);
|
|
|
+
|
|
|
|
|
|
List<File> files = Arrays.stream(file.listFiles()).toList();
|
|
|
|
|
|
List<File> logsFiles = files.parallelStream()
|
|
|
.filter(logFile -> {
|
|
|
String fileName = logFile.getName();
|
|
|
- String sqliteFileName = fileName.substring(0, fileName.indexOf(".sqlite"));
|
|
|
- return !sqliteFileName.equals(filename);
|
|
|
+ return !fileName.equals(currentFileName) && fileName.endsWith("sqlite");
|
|
|
}).toList();
|
|
|
|
|
|
List<String> errorFileNames = new ArrayList<>();
|
|
|
- logsFiles.stream()
|
|
|
+ logsFiles.parallelStream()
|
|
|
.map(logFile -> {
|
|
|
String fileName = logFile.getName();
|
|
|
|
|
|
- return connection.formatted(fileName);
|
|
|
+ return connection.formatted(dir + fileName);
|
|
|
})
|
|
|
.flatMap(connectionStr -> {
|
|
|
|
|
|
try {
|
|
|
return DataBase.query(connectionStr, "select * from logs where 1=?", Collections.singletonList(new Object[]{1})).stream();
|
|
|
} catch (Exception e) {
|
|
|
- Matcher matcher = Pattern.compile("\\d+").matcher(connectionStr);
|
|
|
- errorFileNames.add(matcher.group());
|
|
|
+ Matcher matcher = Pattern.compile("logs/\\d+\\.sqlite").matcher(connectionStr);
|
|
|
+ if (matcher.find()) {
|
|
|
+ errorFileNames.add(matcher.group());
|
|
|
+ }
|
|
|
return Stream.empty();
|
|
|
}
|
|
|
})
|
|
@@ -113,20 +129,42 @@ public class LoggerService {
|
|
|
|
|
|
List<Map<String, Object>> value = stringListEntry.getValue();
|
|
|
|
|
|
- Map<String, List<Map<String, Object>>> targetconnection = value.stream().collect(Collectors.groupingBy(data -> data.get("targetconnection").toString()));
|
|
|
- targetconnection.forEach((connectionStr, value1) -> {
|
|
|
+ Map<String, List<Map<String, Object>>> targetconnection = value.stream().collect(Collectors.groupingBy(data -> data.get("datasourceid").toString()));
|
|
|
+
|
|
|
+ targetconnection.forEach((datasourceid, value1) -> {
|
|
|
|
|
|
Stream<String> datacontentStream = value1.stream().map(data -> data.get("datacontent").toString());
|
|
|
|
|
|
|
|
|
- Map<String, Object> config = ((Map<String, Object>) DataFormatUtil.toMap(connectionStr));
|
|
|
- String type = config.getOrDefault("type", "DB").toString();
|
|
|
try {
|
|
|
+ List<Map<String, Object>> dataSourceList = DataBase.query(Config.centerConnectionStr, "select * from datasource where datasourceid=?", Collections.singletonList(new Object[]{datasourceid}));
|
|
|
+ if (dataSourceList.isEmpty()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Map<String, Object> config = dataSourceList.get(0);
|
|
|
+ String type = config.getOrDefault("datasourcetype", "DB").toString();
|
|
|
+ String connectionStr = config.get("connectset").toString();
|
|
|
+ String expression = value1.get(0).get("expression").toString();
|
|
|
+ List<Object> parameters = new ArrayList<>();
|
|
|
+
|
|
|
switch (type.toUpperCase()) {
|
|
|
- case "ES" -> Elasticsearch.sendMessage(connectionStr, targetName, datacontentStream.toList());
|
|
|
- case "KAFKA" -> Kafka.sendMessage(connectionStr, targetName, datacontentStream.toList());
|
|
|
- case "DB" ->
|
|
|
- DatabaseScriptUtil.exec(connectionStr, targetName, datacontentStream.map(DataFormatUtil::toMap).map(dataContent -> ((Map<String, Object>) dataContent)).toList(), "1", null, null);
|
|
|
+ case "ES", "KAFKA" -> {
|
|
|
+ parameters.add(DataFormatUtil.toMap(connectionStr));
|
|
|
+ parameters.add(expression);
|
|
|
+ parameters.add(connectionStr);
|
|
|
+ parameters.add(targetName);
|
|
|
+ parameters.add(datacontentStream.toList());
|
|
|
+ DataProcessService.processByAlgorithm("1", parameters);
|
|
|
+ }
|
|
|
+ case "DB" -> {
|
|
|
+ parameters.add(connectionStr);
|
|
|
+ parameters.add(targetName);
|
|
|
+ parameters.add(datacontentStream.map(DataFormatUtil::toMap).map(dataContent -> ((Map<String, Object>) dataContent)).toList());
|
|
|
+ parameters.add("1");
|
|
|
+ parameters.add(null);
|
|
|
+ parameters.add(null);
|
|
|
+ DataProcessService.processByAlgorithm("3", parameters);
|
|
|
+ }
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
|
|
@@ -134,15 +172,24 @@ public class LoggerService {
|
|
|
errorFileNames.add(currentfile);
|
|
|
System.out.println(UniReturnUtil.getMessage(e));
|
|
|
}
|
|
|
-
|
|
|
});
|
|
|
});
|
|
|
-
|
|
|
- if (logsFiles.removeIf(f -> errorFileNames.stream().anyMatch(errorFileName -> errorFileName.contains(f.getName())))) {
|
|
|
- for (File logsFile : logsFiles) {
|
|
|
- logsFile.delete();
|
|
|
+ logsFiles.stream().filter(f ->
|
|
|
+ errorFileNames.stream().filter(Objects::nonNull).noneMatch(errorFileName ->
|
|
|
+ errorFileName.contains(f.getName()))
|
|
|
+ ).forEach(f -> {
|
|
|
+ try {
|
|
|
+ String connectionStr = connection.formatted(dir + f.getName());
|
|
|
+ DataBase.exec(connectionStr, "delete from logs");
|
|
|
+ HikariPool hikariPool = DataBase.dataSourcePools.remove(connectionStr);
|
|
|
+ if (Objects.nonNull(hikariPool))
|
|
|
+ hikariPool.shutdown();
|
|
|
+
|
|
|
+ f.delete();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
- }
|
|
|
+ });
|
|
|
|
|
|
}
|
|
|
}
|