74 lines
2.6 KiB
Java
74 lines
2.6 KiB
Java
package com.guwan.backend.service;
|
|
|
|
import cn.easyes.core.biz.EsPageInfo;
|
|
import cn.easyes.core.conditions.LambdaEsQueryWrapper;
|
|
import com.guwan.backend.elasticsearch.document.LogstashLog;
|
|
import com.guwan.backend.elasticsearch.mapper.LogstashLogEsMapper;
|
|
import lombok.RequiredArgsConstructor;
|
|
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
|
|
public class LogstashLogService {
|
|
|
|
private final LogstashLogEsMapper logstashLogEsMapper;
|
|
|
|
/**
|
|
* 按时间范围和日志级别查询
|
|
*/
|
|
public List<LogstashLog> queryLogsByTimeRangeAndLevel(Date startTime, Date endTime, String level) {
|
|
LambdaEsQueryWrapper<LogstashLog> wrapper = new LambdaEsQueryWrapper<>();
|
|
wrapper.between(LogstashLog::getTimestamp, startTime, endTime)
|
|
.eq(LogstashLog::getLevel, level)
|
|
.orderByDesc(LogstashLog::getTimestamp);
|
|
|
|
return logstashLogEsMapper.selectList(wrapper);
|
|
}
|
|
|
|
/**
|
|
* 按关键字搜索日志内容
|
|
*/
|
|
public List<LogstashLog> searchLogsByKeyword(String keyword, Date startTime, Date endTime) {
|
|
LambdaEsQueryWrapper<LogstashLog> wrapper = new LambdaEsQueryWrapper<>();
|
|
wrapper.between(LogstashLog::getTimestamp, startTime, endTime)
|
|
.match(LogstashLog::getMessage, keyword)
|
|
.orderByDesc(LogstashLog::getTimestamp);
|
|
|
|
return logstashLogEsMapper.selectList(wrapper);
|
|
}
|
|
|
|
/**
|
|
* 分页查询最近的错误日志
|
|
*/
|
|
public EsPageInfo<LogstashLog> getRecentErrorLogs(int pageNum, int pageSize) {
|
|
LambdaEsQueryWrapper<LogstashLog> wrapper = new LambdaEsQueryWrapper<>();
|
|
wrapper.orderByDesc(LogstashLog::getTimestamp)
|
|
.from((pageNum - 1) * pageSize)
|
|
.size(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);
|
|
}
|
|
} |