1、预警信息新增api预警的状态

pull/254/head
xjs 4 years ago
parent f2a6a46d29
commit 827e8c521f

@ -4,24 +4,33 @@ import java.io.Serializable;
import java.util.Date; import java.util.Date;
/** /**
* api
*
* @author xiejs * @author xiejs
* @desc api * @since 2021-12-31
* @create 2021-12-31
*/ */
public class ApiRecord implements Serializable { public class ApiRecord implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
/** 主键id */ /**
* id
*/
private Long id; private Long id;
/** api名称 */ /**
* api
*/
private String apiName; private String apiName;
/** api地址 */ /**
* api
*/
private String apiUrl; private String apiUrl;
/** api每天请求次数 */ /**
* api
*/
private Long dayCount; private Long dayCount;
/** /**
@ -29,16 +38,32 @@ public class ApiRecord implements Serializable {
*/ */
private Integer requestTime; private Integer requestTime;
/** api总请求次数 */ /**
* api
*/
private Long totalCount; private Long totalCount;
/** api限制请求次数每天 */ private Integer status;
/**
* api
*/
private Long limitCount; private Long limitCount;
private Date createTime; private Date createTime;
private Date updateTime; private Date updateTime;
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public Long getId() { public Long getId() {
return id; return id;
} }

@ -15,6 +15,23 @@
:value="index"/> :value="index"/>
</el-select> </el-select>
</el-form-item> </el-form-item>
<el-form-item label="状态" prop="status">
<el-select
v-model="queryParams.status"
placeholder="请输入"
clearable
size="small"
@change="handleQuery"
style="width: 150px">
<el-option
v-for="dict in dict.type.request_status"
:key="dict.value"
:label="dict.label"
:value="dict.value"/>
</el-select>
</el-form-item>
<el-form-item> <el-form-item>
<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"></el-button> <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"></el-button>
<el-button icon="el-icon-refresh" size="mini" @click="resetQuery"></el-button> <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"></el-button>
@ -58,11 +75,21 @@
<span>{{ scope.row.requestTime + "ms" }}</span> <span>{{ scope.row.requestTime + "ms" }}</span>
</template> </template>
</el-table-column> </el-table-column>
<el-table-column label="API每天限制请求次数" align="center" prop="limitCount" :show-overflow-tooltip="true"/> <el-table-column label="每天限制次数" align="center" prop="limitCount" :show-overflow-tooltip="true"/>
<el-table-column label="API每天请求次数" align="center" prop="dayCount"/> <el-table-column label="API每天请求次数" align="center" prop="dayCount"/>
<el-table-column label="调用时间" align="center" prop="updateTime" width="180"/> <el-table-column label="调用时间" align="center" prop="updateTime" width="180"/>
<el-table-column label="创建时间" align="center" prop="createTime" width="180" :show-overflow-tooltip="true">
<el-table-column label="API状态" align="center" prop="status" width="100">
<template slot-scope="scope">
<el-tag :type="scope.row.status===1?'success':'danger'">
{{ scope.row.status === 1 ? '正常' : '异常' }}
</el-tag>
</template>
</el-table-column>
<el-table-column label="创建时间" align="center" prop="createTime" width="150" :show-overflow-tooltip="true">
<template slot-scope="scope"> <template slot-scope="scope">
<span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span> <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
</template> </template>
@ -112,6 +139,9 @@ import {
export default { export default {
name: "Apiwarning", name: "Apiwarning",
dicts: ['request_status'],
data() { data() {
return { return {
// //
@ -137,6 +167,7 @@ export default {
pageNum: 1, pageNum: 1,
pageSize: 10, pageSize: 10,
apiName: null, apiName: null,
status: null
}, },
// //
form: {}, form: {},

@ -18,7 +18,7 @@ public class MyMetaObjectHandler implements MetaObjectHandler {
@Override @Override
public void insertFill(MetaObject metaObject) { public void insertFill(MetaObject metaObject) {
log.info("start insert fill ...."); //log.info("start insert fill ....");
//this.setFieldValByName("createTime", new Date(), metaObject); //this.setFieldValByName("createTime", new Date(), metaObject);
this.strictInsertFill(metaObject, "createTime", Date.class,new Date()); // 起始版本 3.3.3(推荐) this.strictInsertFill(metaObject, "createTime", Date.class,new Date()); // 起始版本 3.3.3(推荐)
this.strictInsertFill(metaObject, "updateTime", Date.class,new Date()); this.strictInsertFill(metaObject, "updateTime", Date.class,new Date());

@ -28,12 +28,12 @@ import java.lang.annotation.Annotation;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import java.util.Date; import java.util.*;
import java.util.List;
import java.util.Objects;
import static com.xjs.consts.ApiConst.DEMOTE_ERROR; import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
import static com.xjs.consts.ApiWarnHandleConst.NO; import static com.xjs.consts.ApiWarnHandleConst.NO;
import static com.xjs.consts.ReqConst.ERROR;
import static com.xjs.consts.ReqConst.SUCCESS;
/** /**
* api * api
@ -76,14 +76,19 @@ public class ApiLogAspect {
//执行预警切入逻辑(降级不预警) //执行预警切入逻辑(降级不预警)
if (obj instanceof JSONObject) { if (obj instanceof JSONObject) {
JSONObject jsonObject = (JSONObject) obj; JSONObject jsonObject = (JSONObject) obj;
if (!jsonObject.containsKey(DEMOTE_ERROR)) { if (!jsonObject.containsKey(DEMOTE_ERROR)) {
warning(between, joinPoint); this.warning(between, joinPoint);
} else {
//如果降级,接口状态修改为异常
this.demoteHandle(joinPoint);
} }
} }
//返回值为String情况 //返回值为String情况
if (obj instanceof String) { if (obj instanceof String) {
if (StringUtils.isNotEmpty(String.valueOf(obj))) { if (StringUtils.isNotEmpty(String.valueOf(obj))) {
warning(between, joinPoint); this.warning(between, joinPoint);
} }
} }
@ -94,6 +99,7 @@ public class ApiLogAspect {
} }
} }
/** /**
* *
* *
@ -148,11 +154,12 @@ public class ApiLogAspect {
if (e != null || StringUtils.isEmpty(response)) { if (e != null || StringUtils.isEmpty(response)) {
entity.setIsSuccess(ReqConst.ERROR); entity.setIsSuccess(ReqConst.ERROR);
} else { } else {
entity.setIsSuccess(ReqConst.SUCCESS); entity.setIsSuccess(SUCCESS);
} }
remoteLogFeign.saveApiLog(entity); remoteLogFeign.saveApiLog(entity);
} }
/** /**
* *
* *
@ -160,20 +167,16 @@ public class ApiLogAspect {
* @param joinPoint aop * @param joinPoint aop
*/ */
private void warning(long between, ProceedingJoinPoint joinPoint) { private void warning(long between, ProceedingJoinPoint joinPoint) {
//获取目标类名及方法名
Signature signature = joinPoint.getSignature(); Map<String, String> annotationInfo = this.getAnnotationInfo(joinPoint);
String method = signature.getName();
Class aclass = signature.getDeclaringType(); if (CollUtil.isEmpty(annotationInfo)) {
Method[] methods = aclass.getMethods(); return;
//根据目标的方法名判断当前方法 }
for (Method thisMethod : methods) {
if (method.equals(thisMethod.getName())) { String name = annotationInfo.get("name");
//拿到当前方法的注解判断是否为apiLog注解 String url = annotationInfo.get("url");
Annotation[] declaredAnnotations = thisMethod.getDeclaredAnnotations();
for (Annotation annotation : declaredAnnotations) {
if (annotation instanceof ApiLog) {
String name = ((ApiLog) annotation).name();
String url = ((ApiLog) annotation).url();
//根据拿到的url和name查询数据库是否存在存在则count+1不存在则add //根据拿到的url和name查询数据库是否存在存在则count+1不存在则add
ApiRecord apiRecord = new ApiRecord(); ApiRecord apiRecord = new ApiRecord();
apiRecord.setApiName(name); apiRecord.setApiName(name);
@ -187,10 +190,12 @@ public class ApiLogAspect {
apiRecord.setTotalCount(1L); apiRecord.setTotalCount(1L);
apiRecord.setDayCount(1L); apiRecord.setDayCount(1L);
apiRecord.setLimitCount(30L); apiRecord.setLimitCount(30L);
apiRecord.setStatus(SUCCESS);
remoteWarningCRUDFeign.saveApiRecordForRPC(apiRecord); remoteWarningCRUDFeign.saveApiRecordForRPC(apiRecord);
} else { } else {
ApiRecord haveApiRecord = data.get(0); ApiRecord haveApiRecord = data.get(0);
haveApiRecord.setStatus(SUCCESS);
haveApiRecord.setRequestTime((int) between); haveApiRecord.setRequestTime((int) between);
haveApiRecord.setTotalCount(haveApiRecord.getTotalCount() + 1L); haveApiRecord.setTotalCount(haveApiRecord.getTotalCount() + 1L);
//统计当前的请求次数,隔天清零 //统计当前的请求次数,隔天清零
@ -234,11 +239,73 @@ public class ApiLogAspect {
} }
} }
} }
/**
*
* @param joinPoint
*/
private void demoteHandle(ProceedingJoinPoint joinPoint) {
Map<String, String> map = this.getAnnotationInfo(joinPoint);
if (CollUtil.isEmpty(map)) {
return;
}
String name = map.get("name");
String url = map.get("url");
ApiRecord apiRecord = new ApiRecord();
apiRecord.setApiName(name);
apiRecord.setApiUrl(url);
R<List<ApiRecord>> listR = remoteWarningCRUDFeign.selectApiRecordListForRPC(apiRecord);
if (listR.getCode() == R.SUCCESS) {
List<ApiRecord> data = listR.getData();
if (CollUtil.isNotEmpty(data)) {
ApiRecord haveApiRecord = data.get(0);
//置为空让mp自动填充
haveApiRecord.setUpdateTime(null);
haveApiRecord.setStatus(ERROR);
remoteWarningCRUDFeign.updateApiRecordForRPC(haveApiRecord);
} }
} }
} }
/**
*
*
* @param joinPoint
* @return map
*/
private Map<String, String> getAnnotationInfo(ProceedingJoinPoint joinPoint) {
//获取目标类名及方法名
Signature signature = joinPoint.getSignature();
String method = signature.getName();
Class aclass = signature.getDeclaringType();
Method[] methods = aclass.getMethods();
//根据目标的方法名判断当前方法
for (Method thisMethod : methods) {
if (method.equals(thisMethod.getName())) {
//拿到当前方法的注解判断是否为apiLog注解
Annotation[] declaredAnnotations = thisMethod.getDeclaredAnnotations();
for (Annotation annotation : declaredAnnotations) {
if (annotation instanceof ApiLog) {
String name = ((ApiLog) annotation).name();
String url = ((ApiLog) annotation).url();
Map<String, String> hashMap = new HashMap<>();
hashMap.put("name", name);
hashMap.put("url", url);
return hashMap;
}
}
}
}
return null;
} }
} }

@ -50,6 +50,7 @@ public class ApiWarningController extends BaseController {
/** /**
* *
*
* @param id id * @param id id
* @return R * @return R
*/ */
@ -70,7 +71,7 @@ public class ApiWarningController extends BaseController {
@RequiresPermissions("warning:warning:list") @RequiresPermissions("warning:warning:list")
@GetMapping("/apiwarnlist") @GetMapping("/apiwarnlist")
@ApiOperation("查询api预警列表") @ApiOperation("查询api预警列表")
public TableDataInfo list(ApiWarning apiWarning) { public TableDataInfo list(@Validated({SelectGroup.class}) ApiWarning apiWarning) {
startPage(); startPage();
List<ApiWarning> list = apiWarningService.list(new QueryWrapper<ApiWarning>() List<ApiWarning> list = apiWarningService.list(new QueryWrapper<ApiWarning>()
.orderByDesc("create_time") .orderByDesc("create_time")
@ -113,6 +114,7 @@ public class ApiWarningController extends BaseController {
/** /**
* Api * Api
*
* @return api * @return api
*/ */
@GetMapping("getApiName") @GetMapping("getApiName")
@ -173,6 +175,7 @@ public class ApiWarningController extends BaseController {
/** /**
* Api * Api
*
* @return api * @return api
*/ */
@GetMapping("getApiNameForRPC") @GetMapping("getApiNameForRPC")
@ -183,7 +186,6 @@ public class ApiWarningController extends BaseController {
} }
/** /**
* apiwebsocket * apiwebsocket
* *
@ -224,8 +226,6 @@ public class ApiWarningController extends BaseController {
} }
//-------------------------代码生成------------------------------------ //-------------------------代码生成------------------------------------

@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import com.ruoyi.common.core.annotation.Excel; import com.ruoyi.common.core.annotation.Excel;
import com.xjs.validation.annotation.CheckNumber;
import com.xjs.validation.group.SelectGroup; import com.xjs.validation.group.SelectGroup;
import com.xjs.validation.group.UpdateGroup; import com.xjs.validation.group.UpdateGroup;
import lombok.Data; import lombok.Data;
@ -56,6 +57,13 @@ public class ApiRecord implements Serializable {
@Excel(name = "api总请求次数") @Excel(name = "api总请求次数")
private Long totalCount; private Long totalCount;
/**
*
*/
@Excel(name = "接口状态", readConverterExp = "1=正常,2=异常")
@CheckNumber(num= {1, 2}, groups = { SelectGroup.class})
private Integer status;
/** /**
* api * api
*/ */

@ -4,8 +4,10 @@ import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import com.ruoyi.common.core.annotation.Excel; import com.ruoyi.common.core.annotation.Excel;
import com.xjs.validation.group.SelectGroup;
import lombok.Data; import lombok.Data;
import javax.validation.constraints.Size;
import java.io.Serializable; import java.io.Serializable;
import java.util.Date; import java.util.Date;
@ -22,6 +24,7 @@ public class ApiWarning implements Serializable {
/** api名称 */ /** api名称 */
@Excel(name = "api名称") @Excel(name = "api名称")
@Size(max = 20, message = "请控制api名称长度在20字符", groups = {SelectGroup.class})
private String apiName; private String apiName;
/** 预警类型 */ /** 预警类型 */

@ -14,10 +14,11 @@
<result property="createTime" column="create_time" /> <result property="createTime" column="create_time" />
<result property="dayCount" column="day_count" /> <result property="dayCount" column="day_count" />
<result property="updateTime" column="update_time" /> <result property="updateTime" column="update_time" />
<result property="status" column="status" />
</resultMap> </resultMap>
<sql id="selectApiRecordVo"> <sql id="selectApiRecordVo">
select id, api_name, api_url, total_count, limit_count, create_time, update_time,day_count,request_time select id, api_name, api_url, total_count, limit_count, create_time, update_time,day_count,request_time,status
from api_record from api_record
</sql> </sql>
@ -25,6 +26,7 @@
<include refid="selectApiRecordVo"/> <include refid="selectApiRecordVo"/>
<where> <where>
<if test="apiName != null and apiName != ''"> and api_name like concat('%', #{apiName}, '%')</if> <if test="apiName != null and apiName != ''"> and api_name like concat('%', #{apiName}, '%')</if>
<if test="status != null">status = #{status}</if>
</where> </where>
order by create_time desc order by create_time desc
</select> </select>
@ -44,6 +46,7 @@
<if test="limitCount != null">limit_count = #{limitCount},</if> <if test="limitCount != null">limit_count = #{limitCount},</if>
<if test="createTime != null">create_time = #{createTime},</if> <if test="createTime != null">create_time = #{createTime},</if>
<if test="updateTime != null">update_time = #{updateTime},</if> <if test="updateTime != null">update_time = #{updateTime},</if>
<if test="status != null">status = #{status},</if>
</trim> </trim>
where id = #{id} where id = #{id}
</update> </update>

@ -106,18 +106,36 @@ public class _36wallpaperProcessor implements PageProcessor {
private void initParameter() { private void initParameter() {
//判断redis中是否存在 //判断redis中是否存在
Boolean hasKey = redisService.hasKey(REDIS_KEY); Boolean hasKey = redisService.hasKey(REDIS_KEY);
JSONObject json;
String downloadImg = "downloadImg";
String path = "path";
String init = "init";
if (hasKey) { if (hasKey) {
String cacheObject = redisService.getCacheObject(REDIS_KEY); String cacheObject = redisService.getCacheObject(REDIS_KEY);
JSONObject json = JSONObject.parseObject(cacheObject); try {
this.init = json.getBoolean("init"); json = JSONObject.parseObject(cacheObject);
this.downloadImg = json.getBoolean("downloadImg"); if(json.containsKey(init) && json.containsKey(downloadImg) && json.containsKey(path)){
this.path = json.getString("path"); this.init = json.getBoolean(init);
this.downloadImg = json.getBoolean(downloadImg);
this.path = json.getString(path);
}
} catch (Exception e) {
log.error("JSON转换异常:"+e.getMessage());
}
} else if (StringUtils.isNotEmpty(remoteConfigService.getConfigKeyForRPC(CONFIG_KEY).getData())) { } else if (StringUtils.isNotEmpty(remoteConfigService.getConfigKeyForRPC(CONFIG_KEY).getData())) {
String data = remoteConfigService.getConfigKeyForRPC(CONFIG_KEY).getData(); String data = remoteConfigService.getConfigKeyForRPC(CONFIG_KEY).getData();
JSONObject json = JSONObject.parseObject(data); try {
this.init = json.getBoolean("init"); json = JSONObject.parseObject(data);
this.downloadImg = json.getBoolean("downloadImg"); if(json.containsKey(init) && json.containsKey(downloadImg) && json.containsKey(path)){
this.path = json.getString("path"); this.init = json.getBoolean(init);
this.downloadImg = json.getBoolean(downloadImg);
this.path = json.getString(path);
}
} catch (Exception e) {
log.error("JSON转换异常:"+e.getMessage());
}
} }
} }

Loading…
Cancel
Save