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 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);
}
}

View File

@ -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)

View File

@ -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);
}
}

View File

@ -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显示