add(REQ-2696) 添加日志排序
This commit is contained in:
parent
a06ac5fb55
commit
cac769cf4e
@ -90,4 +90,10 @@ public class LogFindReq {
|
||||
* </p>
|
||||
*/
|
||||
private String lastId;
|
||||
|
||||
/**
|
||||
* 排序字段,如果为空,则会默认id排序,否则会按顺序排序
|
||||
* "sortFields":[{"key":"message.contents.personId","sort":"desc"},{"key":"timestamp","sort":"asc"}]
|
||||
*/
|
||||
private List<@Valid SortField> sortFields;
|
||||
}
|
||||
|
||||
@ -0,0 +1,26 @@
|
||||
package cn.axzo.log.platform.client.model.req;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
|
||||
/**
|
||||
* @Author zr
|
||||
* @Date 2024/11/15 09:44
|
||||
* @Description
|
||||
**/
|
||||
@Data
|
||||
public class SortField {
|
||||
/**
|
||||
* 文档字段名称
|
||||
* 最外层可使用 xxxx
|
||||
* 内部文档字段使用 message.xxxx
|
||||
*/
|
||||
@NotBlank(message = "sort key is required")
|
||||
private String key;
|
||||
/**
|
||||
* asc升序 -desc降序
|
||||
*/
|
||||
private String sort;
|
||||
}
|
||||
@ -7,6 +7,7 @@ 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;
|
||||
import cn.axzo.log.platform.client.model.req.LogFindReq;
|
||||
import cn.axzo.log.platform.client.model.req.SortField;
|
||||
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;
|
||||
@ -24,6 +25,7 @@ import com.mongodb.client.MongoCursor;
|
||||
import com.mongodb.client.result.DeleteResult;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.compress.utils.Lists;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.bson.Document;
|
||||
import org.bson.types.ObjectId;
|
||||
@ -286,6 +288,12 @@ public class LogServiceImpl implements LogService {
|
||||
}
|
||||
query.limit(req.getPageSize());
|
||||
}
|
||||
if(CollUtil.isNotEmpty(req.getSortFields())){
|
||||
List<Sort.Order> orders = buildSort(req.getSortFields());
|
||||
Sort sort = Sort.by(orders);
|
||||
query.with(sort);
|
||||
return query;
|
||||
}
|
||||
Sort sort = Sort.by(Sort.Direction.DESC, "_id");
|
||||
query.with(sort);
|
||||
return query;
|
||||
@ -346,6 +354,15 @@ public class LogServiceImpl implements LogService {
|
||||
|
||||
}
|
||||
|
||||
private static List<Sort.Order> buildSort( List<SortField> sortReqs){
|
||||
ArrayList<Sort.Order> orders = Lists.newArrayList();
|
||||
for (SortField sortReq : sortReqs) {
|
||||
Sort.Order order = new Sort.Order(Sort.Direction.fromString(sortReq.getSort()), sortReq.getKey());
|
||||
orders.add(order);
|
||||
}
|
||||
return orders;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询按timestamp字段指定顺序排序后的指定条数日志记录
|
||||
*
|
||||
|
||||
Loading…
Reference in New Issue
Block a user