fix: [xxljob开关]

This commit is contained in:
ovo 2025-03-30 22:11:59 +08:00
parent 0ce8b27eea
commit 80275af210
4 changed files with 45 additions and 21 deletions

View File

@ -5,16 +5,13 @@ import com.guwan.backend.elasticsearch.document.LogstashLog;
import com.guwan.backend.service.LogstashLogService; import com.guwan.backend.service.LogstashLogService;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat; import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@RestController @RestController
@RequestMapping("/api/logs") @RequestMapping("/api/common/logs")
@RequiredArgsConstructor @RequiredArgsConstructor
public class LogController { public class LogController {
@ -40,4 +37,11 @@ public class LogController {
@RequestParam(defaultValue = "10") int pageSize) { @RequestParam(defaultValue = "10") int pageSize) {
return logstashLogService.getRecentErrorLogs(pageNum, 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);
}
} }

View File

@ -30,17 +30,17 @@ public class LogstashLog {
@IndexField(fieldType = FieldType.TEXT) @IndexField(fieldType = FieldType.TEXT)
private String message; private String message;
@IndexField(fieldType = FieldType.TEXT) @IndexField(fieldType = FieldType.KEYWORD)
private String level; private String level;
@IndexField(fieldType = FieldType.LONG) @IndexField(fieldType = FieldType.LONG)
private Long level_value; private Long level_value;
@IndexField(fieldType = FieldType.TEXT) @IndexField(fieldType = FieldType.KEYWORD)
private String logger_name; private String loggerName;
@IndexField(fieldType = FieldType.TEXT) @IndexField(fieldType = FieldType.KEYWORD)
private String thread_name; private String threadName;
@IndexField(fieldType = FieldType.TEXT) @IndexField(fieldType = FieldType.TEXT)
private String stack_trace; private String stack_trace;
@ -48,7 +48,7 @@ public class LogstashLog {
@IndexField(fieldType = FieldType.TEXT) @IndexField(fieldType = FieldType.TEXT)
private String app_name; private String app_name;
@IndexField(fieldType = FieldType.TEXT) @IndexField(fieldType = FieldType.KEYWORD)
private String host; private String host;
@IndexField(fieldType = FieldType.LONG) @IndexField(fieldType = FieldType.LONG)

View File

@ -9,6 +9,12 @@ import org.springframework.stereotype.Service;
import java.util.Date; import java.util.Date;
import java.util.List; 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 @Service
@RequiredArgsConstructor @RequiredArgsConstructor
@ -51,4 +57,18 @@ public class LogstashLogService {
return logstashLogEsMapper.pageQuery(wrapper, pageNum, pageSize); 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);
}
} }

View File

@ -160,26 +160,26 @@
</template> </template>
<script> <script>
let currentPage = 0; let currentPage = 1;
async function loadLogs() { async function loadLogs() {
const pageSize = document.getElementById('pageSize').value; const pageSize = document.getElementById('pageSize').value;
const response = await fetch(`/api/common/logs?page=${currentPage}&size=${pageSize}`); 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'); const container = document.getElementById('logsContainer');
container.innerHTML = ''; container.innerHTML = '';
logs.forEach(log => { result.records.forEach(log => {
const template = document.getElementById('logTemplate'); const template = document.getElementById('logTemplate');
const logElement = template.content.cloneNode(true); 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').textContent = log.level;
logElement.querySelector('.level').classList.add(`level-${log.level}`); logElement.querySelector('.level').classList.add(`level-${log.level}`);
logElement.querySelector('.thread-name').textContent = log.thread_name; logElement.querySelector('.thread-name').textContent = log.threadName;
logElement.querySelector('.logger-name').textContent = log.logger_name; logElement.querySelector('.logger-name').textContent = log.loggerName;
// 填充JSON数据 // 填充JSON数据
logElement.querySelector('.json-viewer').innerHTML = formatJSON(log); logElement.querySelector('.json-viewer').innerHTML = formatJSON(log);
@ -187,11 +187,11 @@
container.appendChild(logElement); container.appendChild(logElement);
}); });
updatePageInfo(); updatePageInfo(result.current, result.pages);
} }
function prevPage() { function prevPage() {
if (currentPage > 0) { if (currentPage > 1) {
currentPage--; currentPage--;
loadLogs(); loadLogs();
} }
@ -202,8 +202,8 @@
loadLogs(); loadLogs();
} }
function updatePageInfo() { function updatePageInfo(current, total) {
document.getElementById('pageInfo').textContent = `第${currentPage + 1}页`; document.getElementById('pageInfo').textContent = `第${current}页/共${total}页`;
} }
// 格式化JSON显示 // 格式化JSON显示