Merge remote-tracking branch 'refs/remotes/origin/master' into feature/REQ-3282

This commit is contained in:
zhanghonghao 2024-12-25 17:53:56 +08:00
commit b76cc3a066
2 changed files with 30 additions and 20 deletions

View File

@ -1,5 +1,6 @@
package cn.axzo.log.platform.server.controller.web;
import cn.axzo.framework.auth.annotation.PreBuildContext;
import cn.axzo.log.platform.client.model.req.LogAddReq;
import cn.axzo.log.platform.client.model.req.LogBatchAddReq;
import cn.axzo.log.platform.client.model.req.LogBatchDeleteReq;
@ -7,13 +8,10 @@ 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.dto.FindLogResp;
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;
@ -30,6 +28,7 @@ import java.util.List;
* @version 1.0
* @date 2024/9/11 18:16
*/
@PreBuildContext
@RestController
@RequestMapping("/webApi/log")
@RequiredArgsConstructor

View File

@ -1,6 +1,8 @@
package cn.axzo.log.platform.server.service.impl;
import cn.axzo.basics.common.util.NumberUtil;
import cn.axzo.framework.auth.domain.ContextInfo;
import cn.axzo.framework.auth.domain.ContextInfoHolder;
import cn.axzo.framework.domain.ServiceException;
import cn.axzo.log.platform.client.model.Condition;
import cn.axzo.log.platform.client.model.req.LogAddReq;
@ -180,9 +182,10 @@ public class LogServiceImpl implements LogService {
@Override
public FindLogResp findLogs(FindLogDto req) {
log.info("raw find logs conditions: {}", JSONObject.toJSONString(req));
Document filterDoc = new Document();
int skip = 0;
int limit = 0;
int limit = 1000;
Document sortDoc = new Document();
MongoCollection<Document> logCollection = mongoTemplate.getCollection("log");
@ -205,6 +208,7 @@ public class LogServiceImpl implements LogService {
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)
.allowDiskUse(true)
.sort(sortDoc)
.skip(skip)
.limit(limit)
@ -216,8 +220,8 @@ public class LogServiceImpl implements LogService {
}
}
FindLogResp findLogResp = new FindLogResp();
findLogResp.setPageSize(req.getPageSize());
findLogResp.setPageNum(req.getPageNum());
findLogResp.setPageSize(limit);
findLogResp.setPageNum(NumberUtil.isPositiveNumber(req.getPageNum()) ? req.getPageNum() : 1);
findLogResp.setTotal(0L);
findLogResp.setData(logEntityList);
if (CollUtil.isEmpty(logEntityList)) {
@ -428,17 +432,17 @@ public class LogServiceImpl implements LogService {
* @return true合法false非法
*/
public static Boolean isValidJsonObject(String str) {
if (JSONUtil.isTypeJSON(str)) {
if (JSONUtil.isTypeJSONObject(str)) {
try {
JSONUtil.parseObj(str);
JSONObject.parseObject(str);
return true;
} catch (Exception e1) {
try {
JSONUtil.parseArray(str);
return true;
} catch (Exception e2) {
return false;
}
// try {
// JSONUtil.parseArray(str);
// return true;
// } catch (Exception e2) {
// return false;
// }
}
}
return false;
@ -447,16 +451,23 @@ public class LogServiceImpl implements LogService {
@NotNull
private LogEntity buildToLogEntity(LogAddReq req) {
String message = req.getMessage();
if (JSONUtil.isTypeJSONArray(req.getMessage())) {
message = "{" + MESSAGE_FIELD_ARRAY_DEFAULT_KEY + ":" + req.getMessage() + "}";
}
// 判断req.message字段是否是一个合法的json字符串
// if (JSONUtil.isTypeJSONArray(req.getMessage())) {
// message = "{" + MESSAGE_FIELD_ARRAY_DEFAULT_KEY + ":" + req.getMessage() + "}";
// }
// 判断req.message字段是否是一个合法的jsonObject字符串
if (Boolean.TRUE.equals(!isValidJsonObject(message))) {
throw new ServiceException("message is not a valid json string, message: " + message);
throw new ServiceException("message is not a valid json object string, message: " + message);
}
LogEntity logEntity = BeanUtil.copyProperties(req, LogEntity.class, "message");
logEntity.setTimestamp(System.currentTimeMillis());
logEntity.setMessage(Document.parse(message));
Document msgDoc = Document.parse(message);
// 将用户上下文信息添加到message字段中
ContextInfo contextInfo = ContextInfoHolder.get();
if (Objects.nonNull(contextInfo)) {
msgDoc.append("userContextInfo", Document.parse(JSONObject.toJSONString(contextInfo)));
}
logEntity.setMessage(msgDoc);
return logEntity;
}
}