|
|
@@ -30,7 +30,7 @@ public class LogHandler implements WebSocketHandler {
|
|
|
private final Map<String, WebSocketSession> pullSessions = new ConcurrentHashMap<>();
|
|
|
|
|
|
public LogHandler() {
|
|
|
- threadPool.submit(new PushTask());
|
|
|
+ //threadPool.submit(new PushTask());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -55,6 +55,21 @@ public class LogHandler implements WebSocketHandler {
|
|
|
log.info("WebSocket 建立连接");
|
|
|
}
|
|
|
|
|
|
+ public void pushAppLog(AppLog appLog) {
|
|
|
+ String app = appLog.getApp();
|
|
|
+ String host = appLog.getHost();
|
|
|
+ WebSocketSession pullSession = getPullSession(app, host);
|
|
|
+ if (pullSession != null) {
|
|
|
+ String jsonData = JsonConverter.objectToJson(appLog);
|
|
|
+ WebSocketMessage<String> message1 = new TextMessage(jsonData);
|
|
|
+ try {
|
|
|
+ pullSession.sendMessage(message1);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private Map<String, String> parseParams(String query) {
|
|
|
String[] params = query.split("&");
|
|
|
Map<String, String> map = new HashMap<>();
|
|
|
@@ -66,7 +81,7 @@ public class LogHandler implements WebSocketHandler {
|
|
|
return map;
|
|
|
}
|
|
|
|
|
|
- Map<Long, Integer> map = new TreeMap<>();
|
|
|
+ Map<Long, Integer> ngxLogMap = new TreeMap<>();
|
|
|
@Override
|
|
|
public void handleMessage(WebSocketSession webSocketSession, WebSocketMessage<?> webSocketMessage)
|
|
|
throws IOException {
|
|
|
@@ -80,19 +95,7 @@ public class LogHandler implements WebSocketHandler {
|
|
|
if (object instanceof AppLog) {
|
|
|
AppLog appLog = (AppLog) object;
|
|
|
String dateTimeStr = DateTimeConverter.format(appLog.getTimestamp());
|
|
|
-
|
|
|
- String app = appLog.getApp();
|
|
|
- String host = appLog.getHost();
|
|
|
- WebSocketSession pullSession = getPullSession(app, host);
|
|
|
- if (pullSession != null) {
|
|
|
- String jsonData = JsonConverter.objectToJson(appLog);
|
|
|
- WebSocketMessage<String> message1 = new TextMessage(jsonData);
|
|
|
- try {
|
|
|
- pullSession.sendMessage(message1);
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
+ pushAppLog(appLog);
|
|
|
} else if (object instanceof NginxLog) {
|
|
|
NginxLog nginxLog = (NginxLog) object;
|
|
|
String date = nginxLog.getTimeIso8601();
|
|
|
@@ -101,12 +104,12 @@ public class LogHandler implements WebSocketHandler {
|
|
|
LocalDateTime localDateTime = LocalDateTime.parse(date, formatter);
|
|
|
Long timestamp = localDateTime.toEpochSecond(ZoneOffset.of("+8"));
|
|
|
Long key = timestamp;
|
|
|
- Integer count = map.get(key);
|
|
|
+ Integer count = ngxLogMap.get(key);
|
|
|
if (count == null) {
|
|
|
- map.put(key, 1);
|
|
|
+ ngxLogMap.put(key, 1);
|
|
|
} else {
|
|
|
- int count1 = map.get(key) + 1;
|
|
|
- map.put(key, count1);
|
|
|
+ int count1 = ngxLogMap.get(key) + 1;
|
|
|
+ ngxLogMap.put(key, count1);
|
|
|
}
|
|
|
}
|
|
|
} else if (webSocketMessage instanceof PingMessage) {
|
|
|
@@ -167,12 +170,19 @@ public class LogHandler implements WebSocketHandler {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * NginxLog 在前端 echarts 中的可视化
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @return
|
|
|
+ * @date 2023-12-01 17:41:07
|
|
|
+ */
|
|
|
class PushTask implements Runnable {
|
|
|
@Override
|
|
|
public void run() {
|
|
|
while (!Thread.interrupted()) {
|
|
|
try {
|
|
|
- if (map.size() < 3) {
|
|
|
+ if (ngxLogMap.size() < 3) {
|
|
|
Thread.sleep(10_000);
|
|
|
continue;
|
|
|
}
|
|
|
@@ -183,15 +193,15 @@ public class LogHandler implements WebSocketHandler {
|
|
|
List<String> xList = new ArrayList<>();
|
|
|
List<Integer> yList = new ArrayList<>();
|
|
|
Set<Long> keys = new HashSet<>();
|
|
|
- for (Long key : map.keySet()) {
|
|
|
+ for (Long key : ngxLogMap.keySet()) {
|
|
|
if (key < baseKey) {
|
|
|
xList.add(DateTimeConverter.format(key*1000).split(" ")[1]);
|
|
|
- yList.add(map.get(key));
|
|
|
+ yList.add(ngxLogMap.get(key));
|
|
|
keys.add(key);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- keys.forEach(map::remove);
|
|
|
+ keys.forEach(ngxLogMap::remove);
|
|
|
keys.clear();
|
|
|
List results = new ArrayList();
|
|
|
results.add(xList.toArray());
|