feat(REQ-2997): 新增findLogs接口
This commit is contained in:
parent
0861a77a78
commit
0d2feaffaa
@ -5,9 +5,14 @@ import cn.axzo.log.platform.client.model.req.LogBatchAddReq;
|
||||
import cn.axzo.log.platform.client.model.req.LogBatchDeleteReq;
|
||||
import cn.axzo.log.platform.client.model.req.LogFindReq;
|
||||
import cn.axzo.log.platform.client.model.resp.LogResp;
|
||||
import cn.axzo.log.platform.server.dto.FindLogDto;
|
||||
import cn.axzo.log.platform.server.entity.LogEntity;
|
||||
import cn.axzo.log.platform.server.service.LogService;
|
||||
import cn.azxo.framework.common.model.CommonResponse;
|
||||
import com.mongodb.BasicDBObject;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
@ -31,6 +36,9 @@ public class WebLogController {
|
||||
|
||||
private final LogService logService;
|
||||
|
||||
@Value("${log-plat.direct-query-valid:false}")
|
||||
private boolean directQueryValid;
|
||||
|
||||
/**
|
||||
* 通过筛查条件查询日志列表
|
||||
* <p>
|
||||
@ -88,4 +96,20 @@ public class WebLogController {
|
||||
CommonResponse<Boolean> batchDeleteLogs(@RequestBody LogBatchDeleteReq req) {
|
||||
return CommonResponse.success(logService.batchDeleteLogs(req));
|
||||
}
|
||||
|
||||
/**
|
||||
* 该接口只用于原生 mongoDB 查询,其余场景均不建议使用
|
||||
* <p>
|
||||
* 1. 该接口本不应该存在
|
||||
* 2. 接口不做任何特殊逻辑处理,直接查询mongodb数据库数据
|
||||
* </p>
|
||||
* @return 日志列表
|
||||
*/
|
||||
@PostMapping(value = "/findLogs")
|
||||
CommonResponse<List<LogEntity>> findLogs(@RequestBody @Valid FindLogDto req){
|
||||
if (!directQueryValid) {
|
||||
return CommonResponse.fail("direct query is not allowed");
|
||||
}
|
||||
return CommonResponse.success(logService.findLogs(req));
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,37 @@
|
||||
package cn.axzo.log.platform.server.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @author chenwenjian
|
||||
* @version 1.0
|
||||
* @date 2024/10/15 15:02
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class FindLogDto {
|
||||
|
||||
/**
|
||||
* 查询条件, 原生查询filter内语句,示例:{"logLevel":{"$ge":"INFO"}}
|
||||
*/
|
||||
private String filter;
|
||||
|
||||
/**
|
||||
* 分页大小
|
||||
*/
|
||||
private Integer pageSize;
|
||||
|
||||
/**
|
||||
* 当前页码
|
||||
*/
|
||||
private Integer pageNum;
|
||||
|
||||
/**
|
||||
* 排序,原生查询sort内语句,例如:{"timestamp":-1}
|
||||
*/
|
||||
private String sort;
|
||||
|
||||
}
|
||||
@ -5,6 +5,8 @@ import cn.axzo.log.platform.client.model.req.LogBatchAddReq;
|
||||
import cn.axzo.log.platform.client.model.req.LogBatchDeleteReq;
|
||||
import cn.axzo.log.platform.client.model.req.LogFindReq;
|
||||
import cn.axzo.log.platform.client.model.resp.LogResp;
|
||||
import cn.axzo.log.platform.server.dto.FindLogDto;
|
||||
import cn.axzo.log.platform.server.entity.LogEntity;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ -24,4 +26,6 @@ public interface LogService {
|
||||
List<String> batchAddLogs(LogBatchAddReq req);
|
||||
|
||||
Boolean batchDeleteLogs(LogBatchDeleteReq req);
|
||||
|
||||
List<LogEntity> findLogs(FindLogDto req);
|
||||
}
|
||||
|
||||
@ -8,6 +8,7 @@ import cn.axzo.log.platform.client.model.req.LogBatchAddReq;
|
||||
import cn.axzo.log.platform.client.model.req.LogBatchDeleteReq;
|
||||
import cn.axzo.log.platform.client.model.req.LogFindReq;
|
||||
import cn.axzo.log.platform.client.model.resp.LogResp;
|
||||
import cn.axzo.log.platform.server.dto.FindLogDto;
|
||||
import cn.axzo.log.platform.server.entity.LogEntity;
|
||||
import cn.axzo.log.platform.server.mapper.LogMapper;
|
||||
import cn.axzo.log.platform.server.service.LogService;
|
||||
@ -16,8 +17,13 @@ import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.text.CharSequenceUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.mongodb.client.MongoCollection;
|
||||
import com.mongodb.client.MongoCursor;
|
||||
import com.mongodb.client.result.DeleteResult;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.bson.Document;
|
||||
import org.bson.types.ObjectId;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@ -30,6 +36,7 @@ import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@ -42,6 +49,7 @@ import java.util.stream.Collectors;
|
||||
* @version 1.0
|
||||
* @date 2024/9/11 11:04
|
||||
*/
|
||||
@Slf4j
|
||||
@RefreshScope
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@ -160,6 +168,59 @@ public class LogServiceImpl implements LogService {
|
||||
return result.wasAcknowledged();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<LogEntity> findLogs(FindLogDto req) {
|
||||
Document filterDoc = new Document();
|
||||
int skip = 0;
|
||||
int limit = 0;
|
||||
Document sortDoc = new Document();
|
||||
MongoCollection<Document> logCollection = mongoTemplate.getCollection("log");
|
||||
|
||||
// 筛选条件
|
||||
if (StringUtils.isNotBlank(req.getFilter())) {
|
||||
filterDoc = JSONObject.parseObject(req.getFilter(), Document.class);
|
||||
}
|
||||
|
||||
// 分页
|
||||
if (NumberUtil.isPositiveNumber(req.getPageSize()) && NumberUtil.isPositiveNumber(req.getPageNum())) {
|
||||
skip = (req.getPageNum() - 1) * req.getPageSize();
|
||||
limit = req.getPageSize();
|
||||
}
|
||||
|
||||
// 排序
|
||||
if (StringUtils.isNotBlank(req.getSort())) {
|
||||
sortDoc = JSONObject.parseObject(req.getSort(), Document.class);
|
||||
}
|
||||
|
||||
log.info("findLogs filter: {}, skip: {}, limit: {}, sort: {}", filterDoc.toString(), skip, limit, sortDoc.toString());
|
||||
ArrayList<LogEntity> logEntityList = new ArrayList<>();
|
||||
try (MongoCursor<Document> cursor = logCollection.find(filterDoc)
|
||||
.sort(sortDoc)
|
||||
.skip(skip)
|
||||
.limit(limit)
|
||||
.iterator()) {
|
||||
while (cursor.hasNext()) {
|
||||
Document doc = cursor.next();
|
||||
LogEntity logEntity = DocToLogEntity(doc);
|
||||
logEntityList.add(logEntity);
|
||||
}
|
||||
}
|
||||
return logEntityList;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static LogEntity DocToLogEntity(Document doc) {
|
||||
LogEntity logEntity = new LogEntity();
|
||||
logEntity.setId(doc.getObjectId("_id").toString());
|
||||
logEntity.setScene(doc.getString("scene"));
|
||||
logEntity.setLevel(doc.getString("level"));
|
||||
logEntity.setTimestamp(doc.getLong("timestamp"));
|
||||
logEntity.setTags(doc.getList("tags", String.class));
|
||||
logEntity.setMessage(doc.get("message", Document.class));
|
||||
return logEntity;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 构建查询条件
|
||||
*
|
||||
|
||||
Loading…
Reference in New Issue
Block a user