From 80275af21008d61f98817c0be857e6c39b2f8fad Mon Sep 17 00:00:00 2001
From: ovo <ovo@ovo.com>
Date: Sun, 30 Mar 2025 22:11:59 +0800
Subject: [PATCH] =?UTF-8?q?fix:=20[xxljob=E5=BC=80=E5=85=B3]?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../backend/controller/LogController.java     | 14 ++++++++-----
 .../elasticsearch/document/LogstashLog.java   | 12 +++++------
 .../backend/service/LogstashLogService.java   | 20 +++++++++++++++++++
 src/main/resources/static/log-viewer.html     | 20 +++++++++----------
 4 files changed, 45 insertions(+), 21 deletions(-)

diff --git a/src/main/java/com/guwan/backend/controller/LogController.java b/src/main/java/com/guwan/backend/controller/LogController.java
index 91b06c1..46534b7 100644
--- a/src/main/java/com/guwan/backend/controller/LogController.java
+++ b/src/main/java/com/guwan/backend/controller/LogController.java
@@ -5,16 +5,13 @@ import com.guwan.backend.elasticsearch.document.LogstashLog;
 import com.guwan.backend.service.LogstashLogService;
 import lombok.RequiredArgsConstructor;
 import org.springframework.format.annotation.DateTimeFormat;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
 import java.util.Date;
 import java.util.List;
 
 @RestController
-@RequestMapping("/api/logs")
+@RequestMapping("/api/common/logs")
 @RequiredArgsConstructor
 public class LogController {
 
@@ -40,4 +37,11 @@ public class LogController {
             @RequestParam(defaultValue = "10") int pageSize) {
         return logstashLogService.getRecentErrorLogs(pageNum, pageSize);
     }
+
+    @GetMapping
+    public EsPageInfo<LogstashLog> getLogs(
+            @RequestParam(defaultValue = "1") int page,
+            @RequestParam(defaultValue = "20") int size) {
+        return logstashLogService.getLogsList(page, size);
+    }
 } 
\ No newline at end of file
diff --git a/src/main/java/com/guwan/backend/elasticsearch/document/LogstashLog.java b/src/main/java/com/guwan/backend/elasticsearch/document/LogstashLog.java
index 37cb7e0..1158ed9 100644
--- a/src/main/java/com/guwan/backend/elasticsearch/document/LogstashLog.java
+++ b/src/main/java/com/guwan/backend/elasticsearch/document/LogstashLog.java
@@ -30,17 +30,17 @@ public class LogstashLog {
     @IndexField(fieldType = FieldType.TEXT)
     private String message;
 
-    @IndexField(fieldType = FieldType.TEXT)
+    @IndexField(fieldType = FieldType.KEYWORD)
     private String level;
 
     @IndexField(fieldType = FieldType.LONG)
     private Long level_value;
 
-    @IndexField(fieldType = FieldType.TEXT)
-    private String logger_name;
+    @IndexField(fieldType = FieldType.KEYWORD)
+    private String loggerName;
 
-    @IndexField(fieldType = FieldType.TEXT)
-    private String thread_name;
+    @IndexField(fieldType = FieldType.KEYWORD)
+    private String threadName;
 
     @IndexField(fieldType = FieldType.TEXT)
     private String stack_trace;
@@ -48,7 +48,7 @@ public class LogstashLog {
     @IndexField(fieldType = FieldType.TEXT)
     private String app_name;
 
-    @IndexField(fieldType = FieldType.TEXT)
+    @IndexField(fieldType = FieldType.KEYWORD)
     private String host;
 
     @IndexField(fieldType = FieldType.LONG)
diff --git a/src/main/java/com/guwan/backend/service/LogstashLogService.java b/src/main/java/com/guwan/backend/service/LogstashLogService.java
index e19a699..7735456 100644
--- a/src/main/java/com/guwan/backend/service/LogstashLogService.java
+++ b/src/main/java/com/guwan/backend/service/LogstashLogService.java
@@ -9,6 +9,12 @@ import org.springframework.stereotype.Service;
 
 import java.util.Date;
 import java.util.List;
+import java.time.LocalDate;
+import java.time.format.DateTimeFormatter;
+import java.util.ArrayList;
+import java.lang.StringBuilder;
+import java.time.Instant;
+import java.time.temporal.ChronoUnit;
 
 @Service
 @RequiredArgsConstructor
@@ -51,4 +57,18 @@ public class LogstashLogService {
 
         return logstashLogEsMapper.pageQuery(wrapper, pageNum, pageSize);
     }
+
+    public EsPageInfo<LogstashLog> getLogsList(int page, int size) {
+        LambdaEsQueryWrapper<LogstashLog> wrapper = new LambdaEsQueryWrapper<>();
+        
+        // 设置时间范围为最近7天
+        Instant now = Instant.now();
+        Instant sevenDaysAgo = now.minus(7, ChronoUnit.DAYS);
+        
+        wrapper.index("logstash-logs-*")
+               .between(LogstashLog::getTimestamp, sevenDaysAgo, now)
+               .orderByDesc(LogstashLog::getTimestamp);
+        
+        return logstashLogEsMapper.pageQuery(wrapper, page, size);
+    }
 } 
\ No newline at end of file
diff --git a/src/main/resources/static/log-viewer.html b/src/main/resources/static/log-viewer.html
index 80b4b78..68d1138 100644
--- a/src/main/resources/static/log-viewer.html
+++ b/src/main/resources/static/log-viewer.html
@@ -160,26 +160,26 @@
     </template>
 
     <script>
-        let currentPage = 0;
+        let currentPage = 1;
         
         async function loadLogs() {
             const pageSize = document.getElementById('pageSize').value;
             const response = await fetch(`/api/common/logs?page=${currentPage}&size=${pageSize}`);
-            const logs = await response.json();
+            const result = await response.json();
             
             const container = document.getElementById('logsContainer');
             container.innerHTML = '';
             
-            logs.forEach(log => {
+            result.records.forEach(log => {
                 const template = document.getElementById('logTemplate');
                 const logElement = template.content.cloneNode(true);
                 
                 // 填充基础信息
-                logElement.querySelector('.timestamp').textContent = log['@timestamp'];
+                logElement.querySelector('.timestamp').textContent = log.timestamp;
                 logElement.querySelector('.level').textContent = log.level;
                 logElement.querySelector('.level').classList.add(`level-${log.level}`);
-                logElement.querySelector('.thread-name').textContent = log.thread_name;
-                logElement.querySelector('.logger-name').textContent = log.logger_name;
+                logElement.querySelector('.thread-name').textContent = log.threadName;
+                logElement.querySelector('.logger-name').textContent = log.loggerName;
                 
                 // 填充JSON数据
                 logElement.querySelector('.json-viewer').innerHTML = formatJSON(log);
@@ -187,11 +187,11 @@
                 container.appendChild(logElement);
             });
             
-            updatePageInfo();
+            updatePageInfo(result.current, result.pages);
         }
         
         function prevPage() {
-            if (currentPage > 0) {
+            if (currentPage > 1) {
                 currentPage--;
                 loadLogs();
             }
@@ -202,8 +202,8 @@
             loadLogs();
         }
         
-        function updatePageInfo() {
-            document.getElementById('pageInfo').textContent = `第${currentPage + 1}页`;
+        function updatePageInfo(current, total) {
+            document.getElementById('pageInfo').textContent = `第${current}页/共${total}页`;
         }
         
         // 格式化JSON显示