Commit 2c7463cd by 黄明步

完成接口改造

parent 0f3eae12
package com.gxmailu.ocrCloudPlatform.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.TotalHits;
import co.elastic.clients.json.JsonData;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
......@@ -30,7 +25,10 @@ import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.util.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
......@@ -64,9 +62,8 @@ public class AppAbilityRecordAllServiceImpl extends ServiceImpl<AppAbilityRecord
private RedisService redisService;
@Resource
private ElasticsearchClient client;
private ElasticSearchService elasticSearchService;
private static final String APP_ABILITY_RECORD_INDEX = "app_ability_record";
@Override
......@@ -133,21 +130,75 @@ public class AppAbilityRecordAllServiceImpl extends ServiceImpl<AppAbilityRecord
courtList = courtMapper.selectList(new LambdaQueryWrapper<Court>().isNotNull(Court::getIpScope));
redisService.set("courtList", courtList, 5L, TimeUnit.MINUTES);
}
JSONArray array = new JSONArray();
courtList.forEach(court -> {
JSONObject object = new JSONObject();
Long sum = recordAllMapper.selectCount(new LambdaQueryWrapper<AppAbilityRecordAll>().likeRight(AppAbilityRecordAll::getIp, court.getIpScope()));
if (sum > 0) {
object.set("name", court.getName());
object.set("sortName", court.getForSort());
object.set("sum", sum);
array.add(object);
}
});
// 以各院总数降序排列
Comparator.comparing(obj -> ((JSONObject) obj).getInt("number")).reversed();
return array;
// JSONArray array = new JSONArray();
// try {
// SearchResponse<Void> searchResponse = client.search(srBuilder -> srBuilder.index(APP_ABILITY_RECORD_INDEX).query(queryBuilder -> queryBuilder.bool(boolQuery -> {
// courtList.forEach(court -> boolQuery.should(q -> q.prefix(pq -> pq.field("ip.keyword").value(court.getIpScope()))));
// return boolQuery;
// })).aggregations("count", agg -> agg.terms(TermsAggregation.of(s -> s.field("ip.keyword")))).trackTotalHits(tb -> tb.enabled(true)), Void.class);
//
// StringTermsAggregate stringTermsAggregate = searchResponse.aggregations()
// .get("count")
// .sterms();
//
// Map<String, JSONObject> resultMap = new HashMap<>();
// Buckets<StringTermsBucket> buckets = stringTermsAggregate.buckets();
// buckets.array().forEach(bucket -> {
// for (Court court : courtList) {
// if (bucket.key().stringValue().startsWith(court.getIpScope())) {
// // 若此 IP 范围已存在,则取出并更新 sum
// if (resultMap.containsKey(court.getIpScope())) {
// JSONObject existingObject = resultMap.get(court.getIpScope());
// existingObject.set("sum", existingObject.getInt("sum") + bucket.docCount());
// } else {
// // 否则,创建新的 JSONObject 并加入 resultMap
// JSONObject object = new JSONObject();
// object.set("name", court.getName());
// object.set("sortName", court.getForSort());
// object.set("sum", bucket.docCount());
// resultMap.put(court.getIpScope(), object);
// }
// break;
// }
// }
// });
//
// array = new JSONArray(resultMap.values());
// // 以各院总数降序排列
// array.sort(Comparator.comparing(obj -> ((JSONObject) obj).getInt("sum")).reversed());
// } catch (IOException e) {
// log.error("从ES中查询各法院调用量发生异常!", e);
// }
return elasticSearchService.getCourtUseSum(courtList);
}
// public Object getCourtUseSum() {
// List<Court> courtList;
// Object data = redisService.getValue("courtList");
// if (data != null) {
// JSONArray array = JSONUtil.parseArray(data);
// courtList = new ArrayList<>();
// for (Object o : array) {
// courtList.add(JSONUtil.parseObj(o).toBean(Court.class));
// }
// } else {
// courtList = courtMapper.selectList(new LambdaQueryWrapper<Court>().isNotNull(Court::getIpScope));
// redisService.set("courtList", courtList, 5L, TimeUnit.MINUTES);
// }
// JSONArray array = new JSONArray();
// courtList.forEach(court -> {
// JSONObject object = new JSONObject();
// Long sum = recordAllMapper.selectCount(new LambdaQueryWrapper<AppAbilityRecordAll>().likeRight(AppAbilityRecordAll::getIp, court.getIpScope()));
// if (sum > 0) {
// object.set("name", court.getName());
// object.set("sortName", court.getForSort());
// object.set("sum", sum);
// array.add(object);
// }
// });
// // 以各院总数降序排列
// Comparator.comparing(obj -> ((JSONObject) obj).getInt("number")).reversed();
// return array;
// }
// @Override
// public Object getCourtUseInYear(Integer year, String courtName) {
......@@ -184,7 +235,8 @@ public class AppAbilityRecordAllServiceImpl extends ServiceImpl<AppAbilityRecord
}
DateTime start = DateUtil.beginOfYear(date);
DateTime end = DateUtil.endOfYear(date);
return getCourtUseCount(courtName, start, end);
Court court = getCourtInfoByName(courtName);
return elasticSearchService.getCourtUseCount(court, start, end);
}
@Override
......@@ -195,7 +247,8 @@ public class AppAbilityRecordAllServiceImpl extends ServiceImpl<AppAbilityRecord
}
DateTime start = DateUtil.beginOfMonth(date);
DateTime end = DateUtil.endOfMonth(date);
return getCourtUseCount(courtName, start, end);
Court court = getCourtInfoByName(courtName);
return elasticSearchService.getCourtUseCount(court, start, end);
}
@Override
......@@ -206,38 +259,11 @@ public class AppAbilityRecordAllServiceImpl extends ServiceImpl<AppAbilityRecord
}
DateTime start = DateUtil.beginOfDay(date);
DateTime end = DateUtil.endOfDay(date);
return getCourtUseCount(courtName, start, end);
}
private Object getCourtUseCount(String courtName, DateTime start, DateTime end) {
JSONObject object = new JSONObject();
Court court = getCourtInfoByName(courtName);
SearchResponse<Map> searchResponse;
try {
searchResponse = client.search(srBuilder -> srBuilder
.index(APP_ABILITY_RECORD_INDEX)
.query(queryBuilder -> queryBuilder.bool(boolQuery -> {
// 时间范围查询
boolQuery.must(q -> q.range(rangeQueryBuilder -> rangeQueryBuilder
.field("callTime")
.gte(JsonData.of(start.toString()))
.lt(JsonData.of(end.toString()))));
// 法院查询
if (ObjUtil.isNotNull(court) && StrUtil.isNotBlank(court.getIpScope())) {
boolQuery.must(mustQuery -> mustQuery.prefix(pq -> pq.field("ip").value(court.getIpScope())));
}
return boolQuery;
}))
// 总命中数,与hits.size()不同的是trackTotalHits不会限制10000
.trackTotalHits(tb -> tb.enabled(true)), Map.class);
TotalHits totalHits = searchResponse.hits().total();
object.set("count", ObjUtil.isNull(totalHits) ? 0 : totalHits.value());
} catch (Exception e) {
log.error("从ES中查询[{}-{}]调用量发生异常!", start.toString(), end.toString(), e);
}
return object;
return elasticSearchService.getCourtUseCount(court, start, end);
}
@Override
public Result getCorrelationData(String day1, String day2) {
try {
......@@ -267,39 +293,86 @@ public class AppAbilityRecordAllServiceImpl extends ServiceImpl<AppAbilityRecord
}
}
// public Result getBrokenLineData(String date) {
// try {
// Date nowDate = new Date();
// String newDateStr = DateUtil.format(nowDate, "yyyy-MM-dd");
// if (StringUtils.isEmpty(date)) {
// date = DateUtil.format(nowDate, "yyyy-MM-dd");
// } else {
// if (date.equals("yesterday")) {
// Calendar cal = Calendar.getInstance();
// cal.setTime(nowDate);
// cal.add(Calendar.DATE, -1);
// date = DateUtil.format(cal.getTime(), "yyyy-MM-dd");
// } else {
// date = DateUtil.format(DateUtil.parse(date), "yyyy-MM-dd");
// }
// }
// List<BrokenLineData> brokenLineData = null;
// if (date.equals(newDateStr)) {
// brokenLineData = recordAllMapper.selectBrokenLineData(date);
// } else {
// Object dateDataObj = redisService.getValue("brokenLineData-" + date);
// if (dateDataObj == null) {
// brokenLineData = recordAllMapper.selectBrokenLineData(date);
// redisService.set("brokenLineData-" + date, brokenLineData, 1, TimeUnit.DAYS);
// } else {
// brokenLineData = com.alibaba.fastjson.JSONArray.parseArray(dateDataObj.toString(), BrokenLineData.class);
// if (brokenLineData.size() > 0) {
// if (!brokenLineData.get(0).getTime().startsWith(newDateStr)) {
// brokenLineData = recordAllMapper.selectBrokenLineData(date);
// redisService.set("brokenLineData-" + date, brokenLineData, 1, TimeUnit.DAYS);
// }
// } else {
// brokenLineData = recordAllMapper.selectBrokenLineData(date);
// redisService.set("brokenLineData-" + date, brokenLineData, 1, TimeUnit.DAYS);
// }
// }
// }
// // System.out.println(brokenLineData);
// return Result.success("获取折线数据成功", brokenLineData);
// } catch (Exception e) {
// log.error("获取折线数据失败", e);
// return Result.serverError("获取折线数据失败", e);
// }
// }
public Result getBrokenLineData(String date) {
try {
Date nowDate = new Date();
String newDateStr = DateUtil.format(nowDate, "yyyy-MM-dd");
DateTime nowDateTime = DateTime.now();
String newDateStr = nowDateTime.toDateStr();
if (StringUtils.isEmpty(date)) {
date = DateUtil.format(nowDate, "yyyy-MM-dd");
date = newDateStr;
} else {
if (date.equals("yesterday")) {
Calendar cal = Calendar.getInstance();
cal.setTime(nowDate);
cal.add(Calendar.DATE, -1);
date = DateUtil.format(cal.getTime(), "yyyy-MM-dd");
date = DateUtil.yesterday().toDateStr();
} else {
date = DateUtil.format(DateUtil.parse(date), "yyyy-MM-dd");
date = DateUtil.format(DateUtil.parseDate(date), "yyyy-MM-dd");
}
}
List<BrokenLineData> brokenLineData = null;
if (date.equals(newDateStr)) {
brokenLineData = recordAllMapper.selectBrokenLineData(date);
brokenLineData = elasticSearchService.getBrokenLineData(date);
// brokenLineData = recordAllMapper.selectBrokenLineData(date);
} else {
Object dateDataObj = redisService.getValue("brokenLineData-" + date);
if (dateDataObj == null) {
brokenLineData = recordAllMapper.selectBrokenLineData(date);
brokenLineData = elasticSearchService.getBrokenLineData(date);
// brokenLineData = recordAllMapper.selectBrokenLineData(date);
redisService.set("brokenLineData-" + date, brokenLineData, 1, TimeUnit.DAYS);
} else {
brokenLineData = com.alibaba.fastjson.JSONArray.parseArray(dateDataObj.toString(), BrokenLineData.class);
if (brokenLineData.size() > 0) {
if (CollUtil.isNotEmpty(brokenLineData)) {
if (!brokenLineData.get(0).getTime().startsWith(newDateStr)) {
brokenLineData = recordAllMapper.selectBrokenLineData(date);
brokenLineData = elasticSearchService.getBrokenLineData(date);
// brokenLineData = recordAllMapper.selectBrokenLineData(date);
redisService.set("brokenLineData-" + date, brokenLineData, 1, TimeUnit.DAYS);
}
} else {
brokenLineData = recordAllMapper.selectBrokenLineData(date);
brokenLineData = elasticSearchService.getBrokenLineData(date);
// brokenLineData = recordAllMapper.selectBrokenLineData(date);
redisService.set("brokenLineData-" + date, brokenLineData, 1, TimeUnit.DAYS);
}
}
......
......@@ -2,19 +2,25 @@ package com.gxmailu.ocrCloudPlatform.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.BulkRequest;
import co.elastic.clients.elasticsearch.core.BulkResponse;
import co.elastic.clients.elasticsearch.core.DeleteByQueryResponse;
import co.elastic.clients.elasticsearch.core.GetResponse;
import co.elastic.clients.elasticsearch._types.aggregations.*;
import co.elastic.clients.elasticsearch.core.*;
import co.elastic.clients.elasticsearch.core.search.TotalHits;
import co.elastic.clients.elasticsearch.indices.DeleteIndexResponse;
import co.elastic.clients.json.JsonData;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gxmailu.ocrCloudPlatform.entity.AppAbilityRecord3;
import com.gxmailu.ocrCloudPlatform.entity.AppAbilityRecordAll;
import com.gxmailu.ocrCloudPlatform.entity.Court;
import com.gxmailu.ocrCloudPlatform.mapper.AppAbilityRecord3Mapper;
import com.gxmailu.ocrCloudPlatform.mapper.AppAbilityRecord4Mapper;
import com.gxmailu.ocrCloudPlatform.mapper.AppAbilityRecordAllMapper;
import com.gxmailu.ocrCloudPlatform.vo.BrokenLineData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
......@@ -31,7 +37,7 @@ import java.util.stream.Collectors;
*/
@Slf4j
@Service
public class ElasticSearchService{
public class ElasticSearchService {
@Autowired
private ElasticsearchClient client;
......@@ -40,7 +46,7 @@ public class ElasticSearchService{
private static final Map<Character, String> SPECIAL_CHARACTERS_MAP = new HashMap<>();
private static String lastIndexTime = "2023-11-30 00:00:00";
private static String lastIndexTime = "2023-11-10 00:00:00";
@Resource
private AppAbilityRecordAllMapper recordAllMapper;
......@@ -93,7 +99,124 @@ public class ElasticSearchService{
public void deleteAllDoc(String index) throws IOException {
DeleteByQueryResponse deleteByQueryResponse = client.deleteByQuery(s -> s.index(index).query(q -> q.matchAll(m -> m)));
log.info("删除索引文档操作结果:{}",deleteByQueryResponse.deleted());
log.info("删除索引文档操作结果:{}", deleteByQueryResponse.deleted());
}
/**
* 按时间范围查询法院调用量
*
* @param court 法院
* @param start 开始时间
* @param end 结束时间
* @return {@link Object}
*/
public Object getCourtUseCount(Court court, DateTime start, DateTime end) {
JSONObject object = new JSONObject();
SearchResponse<Map> searchResponse;
try {
searchResponse = client.search(srBuilder -> srBuilder
.index(APP_ABILITY_RECORD_INDEX)
.query(queryBuilder -> queryBuilder.bool(boolQuery -> {
// 时间范围查询
boolQuery.must(q -> q.range(rangeQueryBuilder -> rangeQueryBuilder
.field("callTime")
.gte(JsonData.of(start.toString()))
.lt(JsonData.of(end.toString()))));
// 法院查询
if (ObjUtil.isNotNull(court) && StrUtil.isNotBlank(court.getIpScope())) {
boolQuery.must(mustQuery -> mustQuery.prefix(pq -> pq.field("ip").value(court.getIpScope())));
}
return boolQuery;
}))
// 总命中数,与hits.size()不同的是trackTotalHits不会限制10000
.trackTotalHits(tb -> tb.enabled(true)), Map.class);
TotalHits totalHits = searchResponse.hits().total();
object.set("count", ObjUtil.isNull(totalHits) ? 0 : totalHits.value());
} catch (Exception e) {
log.error("从ES中查询[{}-{}]调用量发生异常!", start.toString(), end.toString(), e);
}
return object;
}
/**
* 获取各法院调用量
*
* @param courtList 法院列表
* @return {@link Object}
*/
public Object getCourtUseSum(List<Court> courtList) {
JSONArray array = new JSONArray();
try {
SearchResponse<Void> searchResponse = client.search(srBuilder -> srBuilder.index(APP_ABILITY_RECORD_INDEX).query(queryBuilder -> queryBuilder.bool(boolQuery -> {
courtList.forEach(court -> boolQuery.should(q -> q.prefix(pq -> pq.field("ip.keyword").value(court.getIpScope()))));
return boolQuery;
})).aggregations("count", agg -> agg.terms(TermsAggregation.of(s -> s.field("ip.keyword")))).trackTotalHits(tb -> tb.enabled(true)), Void.class);
StringTermsAggregate stringTermsAggregate = searchResponse.aggregations()
.get("count")
.sterms();
Map<String, JSONObject> resultMap = new HashMap<>();
Buckets<StringTermsBucket> buckets = stringTermsAggregate.buckets();
buckets.array().forEach(bucket -> {
for (Court court : courtList) {
if (bucket.key().stringValue().startsWith(court.getIpScope())) {
// 若此 IP 范围已存在,则取出并更新 sum
if (resultMap.containsKey(court.getIpScope())) {
JSONObject existingObject = resultMap.get(court.getIpScope());
existingObject.set("sum", existingObject.getInt("sum") + bucket.docCount());
} else {
// 否则,创建新的 JSONObject 并加入 resultMap
JSONObject object = new JSONObject();
object.set("name", court.getName());
object.set("sortName", court.getForSort());
object.set("sum", bucket.docCount());
resultMap.put(court.getIpScope(), object);
}
break;
}
}
});
array = new JSONArray(resultMap.values());
// 以各院总数降序排列
array.sort(Comparator.comparing(obj -> ((JSONObject) obj).getInt("sum")).reversed());
} catch (IOException e) {
log.error("从ES中查询各法院调用量发生异常!", e);
}
return array;
}
/**
* 获取折线图数据
*
* @return {@link List}<{@link BrokenLineData}>
*/
public List<BrokenLineData> getBrokenLineData(String dateStr) {
List<BrokenLineData> resultList = new ArrayList<>();
DateTime dateTime = DateUtil.parse(dateStr, "yyyy-MM-dd");
dateTime = DateUtil.parse(dateTime.toDateStr() + " 00:00:00", "yyyy-MM-dd HH:mm:ss");
try {
DateTime finalDateTime = dateTime;
SearchResponse<Void> search = client.search(srBuilder -> srBuilder.index(APP_ABILITY_RECORD_INDEX).query(queryBuilder -> queryBuilder.bool(boolQuery -> {
boolQuery.must(q -> q.range(rangeQueryBuilder -> rangeQueryBuilder
.field("callTime")
.gte(JsonData.of(DateUtil.beginOfDay(finalDateTime)))
.lt(JsonData.of(DateUtil.endOfDay(finalDateTime)))));
return boolQuery;
})).aggregations("record", agg -> agg.dateHistogram(DateHistogramAggregation.of(s -> s.field("callTime")
.calendarInterval(CalendarInterval.Minute)))).trackTotalHits(tb -> tb.enabled(true)), Void.class);
List<DateHistogramBucket> bucketList = search.aggregations().get("record").dateHistogram().buckets().array();
bucketList.forEach(bucket -> {
BrokenLineData brokenLineData = new BrokenLineData();
brokenLineData.setTime(bucket.keyAsString());
brokenLineData.setValue(bucket.docCount());
resultList.add(brokenLineData);
});
return resultList;
} catch (IOException e) {
log.error("获取折线图数据失败", e);
return resultList;
}
}
......@@ -103,9 +226,9 @@ public class ElasticSearchService{
String endTime = "";
while (!DateUtil.parse(startTime, "yyyy-MM-dd HH:mm:ss").after(new Date())) {
endTime = DateUtil.offsetHour(DateUtil.parse(startTime, "yyyy-MM-dd HH:mm:ss"), 2).toString("yyyy-MM-dd HH:mm:ss");
// List<AppAbilityRecordAll> documentList = recordAllMapper.selectList(Wrappers.lambdaQuery(AppAbilityRecordAll.class).between(AppAbilityRecordAll::getCallTime, startTime, endTime));
List<AppAbilityRecord3> documentList = record3Mapper.selectList(Wrappers.lambdaQuery(AppAbilityRecord3.class).between(AppAbilityRecord3::getCallTime, startTime, endTime));
documentList = documentList.stream().sorted(Comparator.comparing(AppAbilityRecord3::getCallTime)).collect(Collectors.toList());
List<AppAbilityRecordAll> documentList = recordAllMapper.selectList(Wrappers.lambdaQuery(AppAbilityRecordAll.class).between(AppAbilityRecordAll::getCallTime, startTime, endTime));
// List<AppAbilityRecord3> documentList = record3Mapper.selectList(Wrappers.lambdaQuery(AppAbilityRecord3.class).between(AppAbilityRecord3::getCallTime, startTime, endTime));
documentList = documentList.stream().sorted(Comparator.comparing(AppAbilityRecordAll::getCallTime)).collect(Collectors.toList());
if (CollUtil.isNotEmpty(documentList)) {
BulkRequest.Builder br = new BulkRequest.Builder();
......
package com.gxmailu.ocrCloudPlatform;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.aggregations.*;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.TotalHits;
import co.elastic.clients.json.JsonData;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxmailu.ocrCloudPlatform.entity.Court;
import com.gxmailu.ocrCloudPlatform.mapper.AppAbilityRecordAllMapper;
import com.gxmailu.ocrCloudPlatform.mapper.CourtMapper;
import com.gxmailu.ocrCloudPlatform.service.AppAbilityRecordAllService;
import com.gxmailu.ocrCloudPlatform.service.impl.ElasticSearchService;
import com.gxmailu.ocrCloudPlatform.service.impl.RedisService;
import com.gxmailu.ocrCloudPlatform.vo.BrokenLineData;
import com.gxmailu.ocrCloudPlatform.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.StringUtils;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
/**
* @author Hmb
* @since 2023/12/14 10:18
*/
@Slf4j
@SpringBootTest
@RunWith(SpringRunner.class)
public class ESTest {
@Autowired
private ElasticsearchClient client;
@Autowired
private CourtMapper courtMapper;
private static final String APP_ABILITY_RECORD_INDEX = "app_ability_record";
@Autowired
private RedisService redisService;
@Autowired
private AppAbilityRecordAllMapper recordAllMapper;
@Autowired
private ElasticSearchService elasticSearchService;
@Autowired
private AppAbilityRecordAllService appAbilityRecordAllService;
@Test
public void getCourtUseByYear() throws IOException {
Date date = new Date();
Integer year = 2023;
if (year != null) {
date = DateUtil.parse(year + "-05-01 00:00:00");
}
DateTime start = DateUtil.beginOfMonth(date);
DateTime end = DateUtil.endOfMonth(date);
JSONObject object = new JSONObject();
Court court = getCourtInfoByName("广西壮族自治区南宁市中级人民法院");
// 使用es查询按年范围内的数据
SearchResponse<Map> searchResponse = client.search(srBuilder -> srBuilder
.index(APP_ABILITY_RECORD_INDEX)
.query(queryBuilder -> queryBuilder.bool(boolQuery -> {
boolQuery.must(q -> q.range(rangeQueryBuilder -> rangeQueryBuilder
.field("callTime")
.gte(JsonData.of(start.toString()))
.lt(JsonData.of(end.toString()))));
if (ObjUtil.isNotNull(court) && StrUtil.isNotBlank(court.getIpScope())) {
boolQuery.must(mustQuery -> mustQuery.prefix(pq -> pq.field("ip").value(court.getIpScope())));
}
return boolQuery;
}))
// 总命中数,与hits.size()不同的是trackTotalHits不会限制10000
.trackTotalHits(tb -> tb.enabled(true))
, Map.class);
TotalHits totalHits = searchResponse.hits().total();
System.out.println(totalHits.value());
}
@Test
public void getCourtUseSum() throws IOException {
List<Court> courtList;
Object data = redisService.getValue("courtList");
if (data != null) {
JSONArray array = JSONUtil.parseArray(data);
courtList = new ArrayList<>();
for (Object o : array) {
courtList.add(JSONUtil.parseObj(o).toBean(Court.class));
}
} else {
courtList = courtMapper.selectList(new LambdaQueryWrapper<Court>().isNotNull(Court::getIpScope));
redisService.set("courtList", courtList, 5L, TimeUnit.MINUTES);
}
JSONArray array = new JSONArray();
SearchResponse<Void> searchResponse = client.search(srBuilder -> srBuilder.index(APP_ABILITY_RECORD_INDEX).query(queryBuilder -> queryBuilder.bool(boolQuery -> {
courtList.forEach(court -> boolQuery.should(q -> q.prefix(pq -> pq.field("ip.keyword").value(court.getIpScope()))));
return boolQuery;
})).aggregations("count", agg -> agg.terms(TermsAggregation.of(s -> s.field("ip.keyword")))).trackTotalHits(tb -> tb.enabled(true)), Void.class);
StringTermsAggregate stringTermsAggregate = searchResponse.aggregations()
.get("count")
.sterms();
Map<String, JSONObject> resultMap = new HashMap<>();
Buckets<StringTermsBucket> buckets = stringTermsAggregate.buckets();
buckets.array().forEach(bucket -> {
for (Court court : courtList) {
if (bucket.key().stringValue().startsWith(court.getIpScope())) {
// 若此 IP 范围已存在,则取出并更新 sum
if (resultMap.containsKey(court.getIpScope())) {
JSONObject existingObject = resultMap.get(court.getIpScope());
existingObject.set("sum", existingObject.getInt("sum") + bucket.docCount());
} else {
// 否则,创建新的 JSONObject 并加入 resultMap
JSONObject object = new JSONObject();
object.set("name", court.getName());
object.set("sortName", court.getForSort());
object.set("sum", bucket.docCount());
resultMap.put(court.getIpScope(), object);
}
break;
}
}
});
array = new JSONArray(resultMap.values());
array.forEach(System.out::println);
// System.out.println(mapList);
// 以各院总数降序排列
// Comparator.comparing(obj -> ((JSONObject) obj).getInt("number")).reversed();
}
@Test
public void afds() {
Result brokenLineData = appAbilityRecordAllService.getCorrelationData("2023-12-10", "2023-12-09");
Result brokenLineData1 = appAbilityRecordAllService.getBrokenLineData("2023-12-10");
System.out.println(brokenLineData);
}
@Test
public void getBrokenLineDataO() {
String date = "2023-12-10 00:00:00";
try {
DateTime nowDateTime = DateTime.now();
String newDateStr = nowDateTime.toDateStr();
if (StringUtils.isEmpty(date)) {
date = newDateStr;
} else {
if (date.equals("yesterday")) {
date = DateUtil.yesterday().toDateStr();
} else {
date = DateUtil.format(DateUtil.parseDate(date), "yyyy-MM-dd");
}
}
List<BrokenLineData> brokenLineData = null;
if (date.equals(newDateStr)) {
brokenLineData = elasticSearchService.getBrokenLineData(date);
// brokenLineData = recordAllMapper.selectBrokenLineData(date);
} else {
Object dateDataObj = redisService.getValue("brokenLineData-" + date);
if (dateDataObj == null) {
brokenLineData = elasticSearchService.getBrokenLineData(date);
// brokenLineData = recordAllMapper.selectBrokenLineData(date);
redisService.set("brokenLineData-" + date, brokenLineData, 1, TimeUnit.DAYS);
} else {
brokenLineData = com.alibaba.fastjson.JSONArray.parseArray(dateDataObj.toString(), BrokenLineData.class);
if (CollUtil.isEmpty(brokenLineData)) {
if (!brokenLineData.get(0).getTime().startsWith(newDateStr)) {
brokenLineData = elasticSearchService.getBrokenLineData(date);
// brokenLineData = recordAllMapper.selectBrokenLineData(date);
redisService.set("brokenLineData-" + date, brokenLineData, 1, TimeUnit.DAYS);
}
} else {
brokenLineData = elasticSearchService.getBrokenLineData(date);
// brokenLineData = recordAllMapper.selectBrokenLineData(date);
redisService.set("brokenLineData-" + date, brokenLineData, 1, TimeUnit.DAYS);
}
}
}
System.out.println(brokenLineData);
} catch (Exception e) {
log.error("获取折线数据失败", e);
}
}
@Test
public void getBrokenLineData1() {
String date = "2023-12-10 00:00:00";
try {
DateTime nowDateTime = DateTime.now();
String newDateStr = nowDateTime.toDateStr();
if (StringUtils.isEmpty(date)) {
date = newDateStr;
} else {
if (date.equals("yesterday")) {
date = DateUtil.yesterday().toDateStr();
}
}
List<BrokenLineData> brokenLineData = null;
// 创建一个公共方法获取数据
String finalDate = date;
Supplier<List<BrokenLineData>> fetchData = () -> {
List<BrokenLineData> data = recordAllMapper.selectBrokenLineData(finalDate);
try {
client.search(srBuilder -> srBuilder.index(APP_ABILITY_RECORD_INDEX).query(queryBuilder -> queryBuilder.bool(boolQuery -> {
boolQuery.must(q -> q.range(rangeQueryBuilder -> rangeQueryBuilder
.field("callTime")
.gte(JsonData.of(DateUtil.beginOfDay(nowDateTime).toString()))
.lt(JsonData.of(DateUtil.endOfDay(nowDateTime).toString()))));
return boolQuery;
})).aggregations("count", agg -> agg.dateHistogram(DateHistogramAggregation.of(s -> s.field("callTime")
.calendarInterval(CalendarInterval.Minute)))).trackTotalHits(tb -> tb.enabled(true)), Void.class);
} catch (IOException e) {
throw new RuntimeException(e);
}
// redisService.set("brokenLineData-" + date, data, 1, TimeUnit.DAYS);
return data;
};
if (!date.equals(newDateStr)) {
Object dateDataObj = redisService.getValue("brokenLineData-" + date);
if (dateDataObj == null) {
brokenLineData = fetchData.get();
} else {
brokenLineData = com.alibaba.fastjson.JSONArray.parseArray(dateDataObj.toString(), BrokenLineData.class);
if (brokenLineData.isEmpty() || !brokenLineData.get(0).getTime().startsWith(newDateStr)) {
brokenLineData = fetchData.get();
}
}
} else {
brokenLineData = fetchData.get();
}
System.out.println(brokenLineData);
} catch (Exception e) {
log.error("获取折线数据失败", e);
}
}
@Test
public void getBrokenLineData() {
List<Object> resultList = new ArrayList<>();
String nowDateTimeStr = "2023-12-10 00:00:00";
DateTime nowDateTime = DateUtil.parse(nowDateTimeStr, "yyyy-MM-dd HH:mm:ss");
try {
SearchResponse<Void> search = client.search(srBuilder -> srBuilder.index(APP_ABILITY_RECORD_INDEX).query(queryBuilder -> queryBuilder.bool(boolQuery -> {
boolQuery.must(q -> q.range(rangeQueryBuilder -> rangeQueryBuilder
.field("callTime")
.gte(JsonData.of(DateUtil.beginOfDay(nowDateTime).toString()))
.lt(JsonData.of(DateUtil.endOfDay(nowDateTime).toString()))));
return boolQuery;
})).aggregations("record", agg -> agg.dateHistogram(DateHistogramAggregation.of(s -> s.field("callTime")
.calendarInterval(CalendarInterval.Minute)))).trackTotalHits(tb -> tb.enabled(true)), Void.class);
List<DateHistogramBucket> bucketList = search.aggregations().get("record").dateHistogram().buckets().array();
bucketList.forEach(bucket -> {
BrokenLineData brokenLineData = new BrokenLineData();
brokenLineData.setTime(bucket.keyAsString());
brokenLineData.setValue(bucket.docCount());
resultList.add(brokenLineData);
});
resultList.forEach(System.out::println);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private Court getCourtInfoByName(String courtName) {
if (courtName.equals("全部")) {
return null;
}
return courtMapper.selectOne(new LambdaQueryWrapper<Court>().eq(Court::getName, courtName));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment