// 页标
sourceBuilder.from((interfaceDTO.getPageIndex() - 1) * interfaceDTO.getPageSize());
// 页数
sourceBuilder.size(interfaceDTO.getPageSize());
// 获取超过1w条数据,需要加上track_total_hits: true ,不然只能显示出9999条
sourceBuilder.trackTotalHits(true);
@Override
public RequestResult getRequestLogs(InterfaceDTO interfaceDTO) throws Exception {
Date queryRequestLogStartTime = DateUtil.toDate(interfaceDTO.getQueryRequestLogStartTime(), "yyyy-MM-dd HH:mm:ss");
Date queryRequestLogEndTime = DateUtil.toDate(interfaceDTO.getQueryRequestLogEndTime(), "yyyy-MM-dd HH:mm:ss");
List<Date> betweenDates = DateUtil.getBetweenDates(queryRequestLogStartTime, queryRequestLogEndTime);
BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
if (StringUtil.isNotEmpty(interfaceDTO.getPartnerId())) {
queryBuilder.must(QueryBuilders.termQuery("partnerId", interfaceDTO.getPartnerId().toLowerCase()));
}
if (StringUtil.isNotEmpty(interfaceDTO.getAppId())) {
queryBuilder.must(QueryBuilders.termQuery("appId", interfaceDTO.getAppId().toLowerCase()));
}
if (interfaceDTO.getServiceId() != null) {
queryBuilder.must(QueryBuilders.termQuery("serviceId", interfaceDTO.getServiceId()));
}
if (interfaceDTO.getId() != null) {
queryBuilder.must(QueryBuilders.termQuery("interfaceId", interfaceDTO.getId()));
}
if (interfaceDTO.getResponseState() != null) {
queryBuilder.must(QueryBuilders.termQuery("responseState", interfaceDTO.getResponseState()));
}
if (interfaceDTO.getRequestDuration() != null) {
queryBuilder.must(QueryBuilders.rangeQuery("downStreamRequestDuration").gte(interfaceDTO.getRequestDuration()));
}
queryBuilder.must(QueryBuilders.rangeQuery("downStreamRequestTime").from(queryRequestLogStartTime.getTime()).to(queryRequestLogEndTime.getTime()));
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(queryBuilder);
sourceBuilder.from((interfaceDTO.getPageIndex() - 1) * interfaceDTO.getPageSize());
sourceBuilder.size(interfaceDTO.getPageSize());
sourceBuilder.trackTotalHits(true);
sourceBuilder.sort("downStreamRequestTime", SortOrder.DESC);
sourceBuilder.fetchSource(new String[] {}, new String[] {});
SearchRequest searchRequest = new SearchRequest(getEsIndices(betweenDates));
searchRequest.indicesOptions(IndicesOptions.fromOptions(true, true, true, false));
searchRequest.types("_doc");
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = eslClient.search(searchRequest, ElasticsearchConfig.COMMON_OPTIONS);
SearchHits hits = searchResponse.getHits();
TotalHits totalHits = hits.getTotalHits();
SearchHit[] searchHits = hits.getHits();
PageUtil<InterfaceLogDTO> interfaceLogs = pageInterfaceLogs(totalHits, searchHits, interfaceDTO);
return RequestResult.success(interfaceLogs);
}
private PageUtil<InterfaceLogDTO> pageInterfaceLogs(TotalHits totalHits, SearchHit[] searchHits, InterfaceDTO interfaceDTO) {
List<InterfaceLogDTO> interfaceLogDTOS = new ArrayList<>();
for (SearchHit hit: searchHits) {
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
JSONObject jsonObject = new JSONObject();
jsonObject.putAll(sourceAsMap);
InterfaceLogDTO interfaceLogDTO = jsonObject.toJavaObject(InterfaceLogDTO.class);
interfaceLogDTOS.add(interfaceLogDTO);
}
int totalPage = Math.toIntExact(totalHits.value / interfaceDTO.getPageSize()) + 1;
return new PageUtil<>(interfaceDTO.getPageIndex(), interfaceDTO.getPageSize(), Long.valueOf(totalHits.value).intValue(), totalPage, interfaceLogDTOS);
}