Commit ec76952a by 黄明步

修改/stat/getBrokenLineData接口参数

parent 6f7d1c5b
......@@ -32,13 +32,15 @@ public class StatController {
@ApiOperation(value = "获取调用信息数据")
@ApiImplicitParams(value = {
@ApiImplicitParam(name = "interval",value = "时间间隔,可选值[year|month|day|hour|minute|second]",dataType = "String", required = false),
@ApiImplicitParam(name = "startDate",value = "开始日期范围,非必填,为空则默认当日",dataType = "String"),
@ApiImplicitParam(name = "endDate",value = "结束日期范围,非必填",dataType = "String"),
@ApiImplicitParam(name = "displayType",value = "展示类型,总体量或各法院使用量可选值[detailed|full]",dataType = "String",required = false),
})
@GetMapping("/getBrokenLineData")
public Result getBrokenLineData(@RequestParam(value = "interval", defaultValue = "minute") String interval,
public Result getBrokenLineData(@RequestParam(value = "startDate", required = false) String startDate,
@RequestParam(value = "endDate", required = false) String endDate,
@RequestParam(value = "displayType", defaultValue = "full") String displayType) {
return statService.getBrokenLineData(interval, displayType);
return statService.getBrokenLineData(startDate, endDate, "interval", displayType);
}
......
package com.gxmailu.ocrCloudPlatform.service.impl;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
......@@ -46,11 +47,11 @@ public class StatService {
private static final String APP_ABILITY_RECORD_INDEX = "app_ability_record";
public Result getBrokenLineData(String interval, String displayType) {
public Result getBrokenLineData(String startDate, String endDate, String interval, String displayType) {
try {
List<BrokenLineData> brokenLineData = new ArrayList<>();
if (displayType.equals("full")) {
return Result.success("获取折线数据成功", getLineData(interval, null));
return Result.success("获取折线数据成功", getLineData(startDate, endDate, interval, null));
} else {
List<Court> courtList = courtService.getCourtList()
......@@ -59,7 +60,7 @@ public class StatService {
.collect(Collectors.toList());
Map<String, Object> map = new HashMap<>();
for (Court court : courtList) {
List<BrokenLineData> lineData = getLineData(interval, court);
List<BrokenLineData> lineData = getLineData(startDate, endDate, interval, court);
map.put(court.getForSort(), lineData);
}
......@@ -73,6 +74,121 @@ public class StatService {
}
}
public List<BrokenLineData> getLineData(String startDate, String endDate, String interval, Court court) {
List<BrokenLineData> resultList = new ArrayList<>();
// String startTime = DateUtil.beginOfDay(DateUtil.date()).toString("yyyy-MM-dd HH:mm:ss");
// String endTime = DateUtil.beginOfDay(DateUtil.date()).toString("yyyy-MM-dd HH:mm:ss");
String startTime = DateUtil.beginOfDay(DateUtil.parseDate("2024-01-23")).toString("yyyy-MM-dd HH:mm:ss");
String endTime = DateUtil.endOfDay(DateUtil.parseDate("2024-01-23")).toString("yyyy-MM-dd HH:mm:ss");
CalendarInterval defaultInterval = CalendarInterval.Minute;
// switch (interval) {
// case "year":
// startTime = DateUtil.beginOfYear(DateUtil.date()).toString("yyyy-MM-dd HH:mm:ss");
// endTime = DateUtil.endOfYear(DateUtil.date()).toString("yyyy-MM-dd HH:mm:ss");
// defaultInterval = CalendarInterval.Month;
// break;
// case "month":
// // startTime = DateUtil.beginOfMonth(DateUtil.date()).toString("yyyy-MM-dd HH:mm:ss");
// // endTime = DateUtil.endOfMonth(DateUtil.date()).toString("yyyy-MM-dd HH:mm:ss");
// startTime = DateUtil.beginOfMonth(DateUtil.parseDate("2024-01-23")).toString("yyyy-MM-dd HH:mm:ss");
// endTime = DateUtil.endOfMonth(DateUtil.parseDate("2024-01-23")).toString("yyyy-MM-dd HH:mm:ss");
// defaultInterval = CalendarInterval.Day;
// break;
// case "day":
// defaultInterval = CalendarInterval.Hour;
// break;
// case "hour":
// break;
// case "minute":
// break;
// case "second":
// defaultInterval = CalendarInterval.Second;
// break;
// }
String newEndDate = "";
if (StrUtil.isNotBlank(startDate)) {
int count = startDate.split("-").length;
switch (count) {
case 1:
DateTime year = DateUtil.parse(startDate, "yyyy");
startDate = DateUtil.beginOfYear(year).toString();
newEndDate = DateUtil.endOfYear(year).toString();
defaultInterval = CalendarInterval.Month;
break;
case 2:
DateTime yearMonth = DateUtil.parse(startDate, "yyyy-MM");
startDate = DateUtil.beginOfMonth(yearMonth).toString();
newEndDate = DateUtil.endOfMonth(yearMonth).toString();
defaultInterval = CalendarInterval.Day;
break;
case 3:
DateTime yearMonthDayDateTime = DateUtil.parse(startDate, "yyyy-MM-dd");
startDate = DateUtil.beginOfDay(yearMonthDayDateTime).toString();
newEndDate = DateUtil.endOfDay(yearMonthDayDateTime).toString();
defaultInterval = CalendarInterval.Hour;
break;
}
} else {
// startTime = DateUtil.beginOfDay(DateUtil.date()).toString("yyyy-MM-dd HH:mm:ss");
// newEndDate = DateUtil.beginOfDay(DateUtil.date()).toString("yyyy-MM-dd HH:mm:ss");
startDate = DateUtil.beginOfDay(DateUtil.parseDate("2024-01-23")).toString("yyyy-MM-dd HH:mm:ss");
newEndDate = DateUtil.endOfDay(DateUtil.parseDate("2024-01-23")).toString("yyyy-MM-dd HH:mm:ss");
defaultInterval = CalendarInterval.Minute;
}
endDate = StrUtil.isBlank(endDate) ? newEndDate : endDate;
try {
String finalStartTime = startDate;
String finalEndTime = endDate;
CalendarInterval finalDefaultInterval = defaultInterval;
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(finalStartTime))
.lte(JsonData.of(finalEndTime))
));
boolQuery.must(mustQuery -> mustQuery.bool(bq -> {
if (ObjUtil.isNotNull(court)) {
List<String> ipScopes = StrUtil.splitTrim(court.getIpScope(), "-");
try {
List<String> ipRange = ipScopes.size() != 2 ? Arrays.asList("255.255.255.255", "255.255.255.255") : IpUtils.getIPRange(ipScopes.get(0), ipScopes.get(1), 3);
ipRange.forEach(ip -> bq.should(q -> q.prefix(pq -> pq.field("ip.keyword").value(ip))));
} catch (Exception e) {
log.error("IP段解析错误", e);
}
}
return bq;
}));
return boolQuery;
// 聚合查询,按每一分钟为间隔查询每分钟的调用量
})).aggregations("record", agg -> agg.dateHistogram(DateHistogramAggregation.of(s -> s.field("callTime").calendarInterval(finalDefaultInterval)))
.aggregations("fileCount", fileCountAgg -> fileCountAgg.sum(SumAggregation.of(s -> s.field("fileCount")))))
.trackTotalHits(tb -> tb.enabled(true)), Void.class);
List<DateHistogramBucket> recordBcketList = search.aggregations().get("record").dateHistogram().buckets().array();
recordBcketList.forEach(recordBucket -> {
BrokenLineData brokenLineData = new BrokenLineData();
String time = StrUtil.isBlank(recordBucket.keyAsString()) ? "" : recordBucket.keyAsString();
brokenLineData.setTime(time);
long value = (long) recordBucket.aggregations().get("fileCount").sum().value();
brokenLineData.setValue(value);
resultList.add(brokenLineData);
});
return resultList;
} catch (IOException e) {
log.error("获取折线图数据失败", e);
return resultList;
}
}
public List<BrokenLineData> getLineData(String interval, Court court) {
List<BrokenLineData> resultList = new ArrayList<>();
......
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