Commit 5c732a67 by 黄明步

修复调用数据对比折线图显示异常

parent 2fb10394
#!/bin/bash
#执行命令 nohup sh check_api.sh > /dev/null 2>&1 &
#设置变量,url为你需要检测的目标网站的网址(IP或域名)
url=http://147.1.5.81:9027/api/checkHealth
#每隔多少秒检查一次
cheack_time=15
#定义函数check_http:
#使用curl命令检查http服务器的状态
#-m设置curl不管访问成功或失败,最大消耗的时间为5秒,5秒连接服务为相应则视为无法连接
#-s设置静默连接,不显示连接时的连接速度、时间消耗等信息
#-o将curl下载的页面内容导出到/dev/null(默认会在屏幕显示页面内容)d
#-w设置curl命令需要显示的内容%{http_code},指定curl返回服务器的状态码
check_http(){
status_code=$(curl -m 5 -s -o /dev/null -w %{http_code} $url)
}
restart_process(){
echo "当前时间为:$date
$url服务器异常,状态码为${status_code}.
,即将执行重启,请尽快排查异常." > /var/log/http.log
docker restart ocr-api-9027
}
error_count=0
while :
do
check_http
date=$(date +%Y%m%d-%H:%M:%S)
#生成报警邮件的内容
#echo "当前时间为:$date
#$url服务器异常,状态码为${status_code}.
#,即将执行重启,请尽快排查异常." > /tmp/http$$.pid
#指定测试服务器状态的函数,并根据返回码决定是发送邮件报警,重启服务,还是将正常信息写入日志
#error_count用来记录服务错误次数,目前只有第一次检测有问题会重启。
if [ $status_code -ne 200 ];then
error_count=$(( $error_count + 1 ))
if [ $error_count -eq 5 ];then
restart_process
fi
echo " $(date +%Y/%m/%d %H:%M:%S) 错误的次数为: $error_count" >> /var/log/http.log
else
error_count=0
echo "$(date +%Y/%m/%d %H:%M:%S) $url连接正常" >> /var/log/http.log
fi
sleep $cheack_time
done
package com.gxmailu.ocrCloudPlatform.controller; package com.gxmailu.ocrCloudPlatform.controller;
import cn.hutool.json.JSONObject; import cn.hutool.json.JSONObject;
import com.gxmailu.ocrCloudPlatform.mapper.AppAbilityRecord1Mapper;
import com.gxmailu.ocrCloudPlatform.mapper.AppAbilityRecordAllMapper;
import com.gxmailu.ocrCloudPlatform.service.impl.ElasticSearchService;
import com.gxmailu.ocrCloudPlatform.service.impl.RetransmissionService; import com.gxmailu.ocrCloudPlatform.service.impl.RetransmissionService;
import com.gxmailu.ocrCloudPlatform.vo.OcrResult; import com.gxmailu.ocrCloudPlatform.vo.OcrResult;
import com.gxmailu.ocrCloudPlatform.vo.Result;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
...@@ -24,12 +22,10 @@ public class RetransmissionController { ...@@ -24,12 +22,10 @@ public class RetransmissionController {
@Autowired @Autowired
private RetransmissionService retransmissionService; private RetransmissionService retransmissionService;
@Autowired @GetMapping("/api/checkHealth")
private ElasticSearchService elasticSearchService; public Result checkHealth() {
@Autowired return Result.success();
private AppAbilityRecord1Mapper record1Mapper; }
@Autowired
private AppAbilityRecordAllMapper recordAllMapper;
@GetMapping("/ofs/api/sync/getClientOcrTask") @GetMapping("/ofs/api/sync/getClientOcrTask")
public OcrResult getClientOcrTask() { public OcrResult getClientOcrTask() {
......
...@@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollUtil; ...@@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjUtil; import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray; import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
...@@ -343,7 +344,7 @@ public class AppAbilityRecordAllServiceImpl extends ServiceImpl<AppAbilityRecord ...@@ -343,7 +344,7 @@ public class AppAbilityRecordAllServiceImpl extends ServiceImpl<AppAbilityRecord
DateTime nowDateTime = DateTime.now(); DateTime nowDateTime = DateTime.now();
String newDateStr = nowDateTime.toDateStr(); String newDateStr = nowDateTime.toDateStr();
if (StringUtils.isEmpty(date)) { if (StrUtil.isBlank(date)) {
date = newDateStr; date = newDateStr;
} else { } else {
if (date.equals("yesterday")) { if (date.equals("yesterday")) {
...@@ -352,27 +353,23 @@ public class AppAbilityRecordAllServiceImpl extends ServiceImpl<AppAbilityRecord ...@@ -352,27 +353,23 @@ public class AppAbilityRecordAllServiceImpl extends ServiceImpl<AppAbilityRecord
date = DateUtil.format(DateUtil.parseDate(date), "yyyy-MM-dd"); date = DateUtil.format(DateUtil.parseDate(date), "yyyy-MM-dd");
} }
} }
List<BrokenLineData> brokenLineData = null; List<BrokenLineData> brokenLineData;
if (date.equals(newDateStr)) { if (date.equals(newDateStr)) {
brokenLineData = elasticSearchService.getBrokenLineData(date); brokenLineData = elasticSearchService.getBrokenLineData(date);
// brokenLineData = recordAllMapper.selectBrokenLineData(date);
} else { } else {
Object dateDataObj = redisService.getValue("brokenLineData-" + date); Object dateDataObj = redisService.getValue("brokenLineData-" + date);
if (dateDataObj == null) { if (dateDataObj == null) {
brokenLineData = elasticSearchService.getBrokenLineData(date); brokenLineData = elasticSearchService.getBrokenLineData(date);
// brokenLineData = recordAllMapper.selectBrokenLineData(date);
redisService.set("brokenLineData-" + date, brokenLineData, 1, TimeUnit.DAYS); redisService.set("brokenLineData-" + date, brokenLineData, 1, TimeUnit.DAYS);
} else { } else {
brokenLineData = com.alibaba.fastjson.JSONArray.parseArray(dateDataObj.toString(), BrokenLineData.class); brokenLineData = JSONUtil.toList(dateDataObj.toString(), BrokenLineData.class);
if (CollUtil.isNotEmpty(brokenLineData)) { if (CollUtil.isNotEmpty(brokenLineData)) {
if (!brokenLineData.get(0).getTime().startsWith(newDateStr)) { if (!brokenLineData.get(0).getTime().startsWith(newDateStr)) {
brokenLineData = elasticSearchService.getBrokenLineData(date); brokenLineData = elasticSearchService.getBrokenLineData(date);
// brokenLineData = recordAllMapper.selectBrokenLineData(date);
redisService.set("brokenLineData-" + date, brokenLineData, 1, TimeUnit.DAYS); redisService.set("brokenLineData-" + date, brokenLineData, 1, TimeUnit.DAYS);
} }
} else { } else {
brokenLineData = elasticSearchService.getBrokenLineData(date); brokenLineData = elasticSearchService.getBrokenLineData(date);
// brokenLineData = recordAllMapper.selectBrokenLineData(date);
redisService.set("brokenLineData-" + date, brokenLineData, 1, TimeUnit.DAYS); redisService.set("brokenLineData-" + date, brokenLineData, 1, TimeUnit.DAYS);
} }
} }
......
...@@ -11,10 +11,12 @@ import cn.hutool.json.JSONObject; ...@@ -11,10 +11,12 @@ import cn.hutool.json.JSONObject;
import co.elastic.clients.elasticsearch.ElasticsearchClient; import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.aggregations.*; import co.elastic.clients.elasticsearch._types.aggregations.*;
import co.elastic.clients.elasticsearch.core.*; import co.elastic.clients.elasticsearch.core.*;
import co.elastic.clients.elasticsearch.core.bulk.BulkResponseItem;
import co.elastic.clients.elasticsearch.core.search.TotalHits; import co.elastic.clients.elasticsearch.core.search.TotalHits;
import co.elastic.clients.elasticsearch.indices.DeleteIndexResponse; import co.elastic.clients.elasticsearch.indices.DeleteIndexResponse;
import co.elastic.clients.json.JsonData; import co.elastic.clients.json.JsonData;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gxmailu.ocrCloudPlatform.entity.AppAbilityRecord;
import com.gxmailu.ocrCloudPlatform.entity.AppAbilityRecordAll; import com.gxmailu.ocrCloudPlatform.entity.AppAbilityRecordAll;
import com.gxmailu.ocrCloudPlatform.entity.Court; import com.gxmailu.ocrCloudPlatform.entity.Court;
import com.gxmailu.ocrCloudPlatform.mapper.*; import com.gxmailu.ocrCloudPlatform.mapper.*;
...@@ -92,6 +94,31 @@ public class ElasticSearchService { ...@@ -92,6 +94,31 @@ public class ElasticSearchService {
return response.source(); return response.source();
} }
public List<Map.Entry<String, String>> recordDataImport(List<AppAbilityRecord> appAbilityRecordList) {
if (CollUtil.isNotEmpty(appAbilityRecordList)) {
BulkRequest.Builder br = new BulkRequest.Builder();
appAbilityRecordList.forEach(abilityRecord -> br.operations(bo -> bo.index(io -> io.index(APP_ABILITY_RECORD_INDEX)
.id(abilityRecord.getId().toString())
.document(abilityRecord))));
BulkResponse response = null;
try {
response = client.bulk(br.build());
} catch (IOException e) {
throw new RuntimeException(e);
}
if (response.errors()) {
log.error("AppAbilityRecord数据导入失败,错误信息:{}", response);
return new ArrayList<>();
}
List<BulkResponseItem> items = response.items();
Map<String, String> map = items.stream().collect(Collectors.toMap(BulkResponseItem::id, BulkResponseItem::result));
return new ArrayList<>(map.entrySet());
}
return new ArrayList<>();
}
public void deleteIndex(String index) throws IOException { public void deleteIndex(String index) throws IOException {
DeleteIndexResponse deleteIndexResponse = client.indices().delete(s -> s.index(index)); DeleteIndexResponse deleteIndexResponse = client.indices().delete(s -> s.index(index));
...@@ -199,16 +226,21 @@ public class ElasticSearchService { ...@@ -199,16 +226,21 @@ public class ElasticSearchService {
*/ */
public List<BrokenLineData> getBrokenLineData(String dateStr) { public List<BrokenLineData> getBrokenLineData(String dateStr) {
List<BrokenLineData> resultList = new ArrayList<>(); List<BrokenLineData> resultList = new ArrayList<>();
DateTime dateTime = DateUtil.parse(dateStr, "yyyy-MM-dd"); // 获取指定日期的开始时间 假设日期为2022-12-23 则结果为(2022-12-23 00:00:00)
dateTime = DateUtil.parse(dateTime.toDateStr() + " 00:00:00", "yyyy-MM-dd HH:mm:ss"); String startTime = DateUtil.beginOfDay(DateUtil.parse(dateStr)).toString("yyyy-MM-dd HH:mm:ss");
// 获取指定日期的结束时间 假设日期为2022-12-23 则结果为 (2022-12-23 23:59:59)
String endTime = DateUtil.endOfDay(DateUtil.parse(dateStr)).toString("yyyy-MM-dd HH:mm:ss");
try { try {
DateTime finalDateTime = dateTime;
SearchResponse<Void> search = client.search(srBuilder -> srBuilder.index(APP_ABILITY_RECORD_INDEX).query(queryBuilder -> queryBuilder.bool(boolQuery -> { SearchResponse<Void> search = client.search(srBuilder -> srBuilder.index(APP_ABILITY_RECORD_INDEX).query(queryBuilder -> queryBuilder.bool(boolQuery -> {
// 日期范围查询 // 日期范围查询
boolQuery.must(q -> q.range(rangeQueryBuilder -> rangeQueryBuilder boolQuery.must(q -> q.range(rangeQueryBuilder -> rangeQueryBuilder
.field("callTime") .field("callTime")
.gte(JsonData.of(DateUtil.beginOfDay(finalDateTime))) // .from(startTime)
.lt(JsonData.of(DateUtil.endOfDay(finalDateTime))))); // .to(endTime)
.gte(JsonData.of(startTime))
.lt(JsonData.of(endTime))
));
return boolQuery; return boolQuery;
// 聚合查询,按每一分钟为间隔查询每分钟的调用量 // 聚合查询,按每一分钟为间隔查询每分钟的调用量
})).aggregations("record", agg -> agg.dateHistogram(DateHistogramAggregation.of(s -> s.field("callTime") })).aggregations("record", agg -> agg.dateHistogram(DateHistogramAggregation.of(s -> s.field("callTime")
...@@ -216,7 +248,8 @@ public class ElasticSearchService { ...@@ -216,7 +248,8 @@ public class ElasticSearchService {
List<DateHistogramBucket> bucketList = search.aggregations().get("record").dateHistogram().buckets().array(); List<DateHistogramBucket> bucketList = search.aggregations().get("record").dateHistogram().buckets().array();
bucketList.forEach(bucket -> { bucketList.forEach(bucket -> {
BrokenLineData brokenLineData = new BrokenLineData(); BrokenLineData brokenLineData = new BrokenLineData();
brokenLineData.setTime(bucket.keyAsString()); String time = StrUtil.isBlank(bucket.keyAsString()) ? "" : bucket.keyAsString();
brokenLineData.setTime(time.replace(dateStr, DateUtil.date().toString("yyyy-MM-dd")));
brokenLineData.setValue(bucket.docCount()); brokenLineData.setValue(bucket.docCount());
resultList.add(brokenLineData); resultList.add(brokenLineData);
}); });
......
...@@ -384,6 +384,7 @@ public class RetransmissionService { ...@@ -384,6 +384,7 @@ public class RetransmissionService {
log.error("从查询不到ID为 {} 的文件数据", id); log.error("从查询不到ID为 {} 的文件数据", id);
return; return;
} }
elasticSearchService.recordDataImport(CollUtil.newArrayList(tmpRecordByTrigger));
appAbilityRecordAll = new AppAbilityRecordAll(); appAbilityRecordAll = new AppAbilityRecordAll();
BeanUtil.copyProperties(tmpRecordByTriggerMapper.selectById(id), appAbilityRecordAll); BeanUtil.copyProperties(tmpRecordByTriggerMapper.selectById(id), appAbilityRecordAll);
} }
...@@ -455,11 +456,13 @@ public class RetransmissionService { ...@@ -455,11 +456,13 @@ public class RetransmissionService {
log.error("从数据库中无法获取ID为 {} 的文件数据", id); log.error("从数据库中无法获取ID为 {} 的文件数据", id);
return; return;
} }
elasticSearchService.recordDataImport(CollUtil.newArrayList(tmpRecordByTrigger));
appAbilityRecordAll = new AppAbilityRecordAll();
BeanUtil.copyProperties(tmpRecordByTriggerMapper.selectById(id), appAbilityRecordAll); BeanUtil.copyProperties(tmpRecordByTriggerMapper.selectById(id), appAbilityRecordAll);
} }
// 如果再次查询后结果仍为空,中止下载处理 // 如果再次查询后结果仍为空,中止下载处理
if (appAbilityRecordAll == null || StrUtil.isBlank(appAbilityRecordAll.getServerIp()) if (StrUtil.isBlank(appAbilityRecordAll.getServerIp())
|| StrUtil.isBlank(appAbilityRecordAll.getDstUrl()) || StrUtil.isBlank(appAbilityRecordAll.getDstUrl())
|| StrUtil.isBlank(appAbilityRecordAll.getPdf())) { || StrUtil.isBlank(appAbilityRecordAll.getPdf())) {
log.error("无法获取ID为 {} 的完整文件信息,中止下载处理", id); log.error("无法获取ID为 {} 的完整文件信息,中止下载处理", id);
...@@ -834,6 +837,9 @@ public class RetransmissionService { ...@@ -834,6 +837,9 @@ public class RetransmissionService {
} }
record.setIp(clientIP); record.setIp(clientIP);
mapper.updateById(record); mapper.updateById(record);
// 保存到ES
elasticSearchService.recordDataImport(Collections.singletonList(record));
// log.info("修改id:{}的真实客户端请求ip结果:{}", id, mapper.updateById(record)); // log.info("修改id:{}的真实客户端请求ip结果:{}", id, mapper.updateById(record));
AppAbilityRecordAll recordAll = recordAllMapper.selectById(id); AppAbilityRecordAll recordAll = recordAllMapper.selectById(id);
if (recordAll == null) { if (recordAll == null) {
......
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