🔨 添加策略类型.

This commit is contained in:
lijiahangmax
2025-10-13 18:23:07 +08:00
parent 8929aa2f74
commit 9651354317
18 changed files with 80 additions and 20 deletions

View File

@@ -104,9 +104,10 @@ public class AlarmPolicyController {
@IgnoreLog(IgnoreLogMode.RET)
@GetMapping("/list")
@Operation(summary = "查询全部监控告警策略")
@Parameter(name = "type", description = "type", required = true)
@PreAuthorize("@ss.hasPermission('monitor:alarm-policy:query')")
public List<AlarmPolicyVO> getAlarmPolicyList() {
return alarmPolicyService.getAlarmPolicyListByCache();
public List<AlarmPolicyVO> getAlarmPolicyList(@RequestParam("type") String type) {
return alarmPolicyService.getAlarmPolicyListByCache(type);
}
@IgnoreLog(IgnoreLogMode.RET)

View File

@@ -39,8 +39,8 @@ import java.util.concurrent.TimeUnit;
public interface AlarmPolicyCacheKeyDefine {
CacheKeyDefine ALARM_POLICY = new CacheKeyBuilder()
.key("alarm:policy:list")
.desc("告警策略")
.key("alarm:policy:list:{}")
.desc("告警策略 ${type}")
.type(AlarmPolicyCacheDTO.class)
.struct(RedisCacheStruct.HASH)
.timeout(8, TimeUnit.HOURS)

View File

@@ -50,6 +50,10 @@ public class AlarmPolicyDO extends BaseDO {
private static final long serialVersionUID = 1L;
@Schema(description = "策略类型")
@TableField("type")
private String type;
@Schema(description = "策略名称")
@TableField("name")
private String name;

View File

@@ -47,6 +47,10 @@ public class AlarmPolicyCreateRequest implements Serializable {
private static final long serialVersionUID = 1L;
@NotBlank
@Schema(description = "策略类型")
private String type;
@NotBlank
@Size(max = 64)
@Schema(description = "策略名称")

View File

@@ -26,6 +26,7 @@ import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import org.dromara.visor.common.entity.BaseQueryRequest;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
/**
@@ -46,6 +47,10 @@ public class AlarmPolicyQueryRequest extends BaseQueryRequest {
@Schema(description = "id")
private Long id;
@NotBlank
@Schema(description = "策略类型")
private String type;
@Size(max = 64)
@Schema(description = "策略名称")
private String name;

View File

@@ -51,6 +51,9 @@ public class AlarmPolicyVO implements Serializable {
@Schema(description = "id")
private Long id;
@Schema(description = "策略类型")
private String type;
@Schema(description = "策略名称")
private String name;

View File

@@ -48,6 +48,9 @@ public class AlarmEnginePolicy {
@Schema(description = "策略id")
private Long id;
@Schema(description = "策略类型")
private String type;
@Schema(description = "策略名称")
private String name;

View File

@@ -77,9 +77,10 @@ public interface AlarmPolicyService {
/**
* 通过缓存查询监控告警策略
*
* @param type type
* @return rows
*/
List<AlarmPolicyVO> getAlarmPolicyListByCache();
List<AlarmPolicyVO> getAlarmPolicyListByCache(String type);
/**
* 分页查询监控告警策略

View File

@@ -27,6 +27,7 @@ import cn.orionsec.kit.lang.utils.Booleans;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.dromara.visor.common.constant.Const;
import org.dromara.visor.common.constant.ErrorMessage;
import org.dromara.visor.common.utils.Assert;
import org.dromara.visor.framework.biz.operator.log.core.utils.OperatorLogs;
@@ -110,8 +111,11 @@ public class AlarmPolicyServiceImpl implements AlarmPolicyService {
Long id = record.getId();
// 设置告警通知
alarmPolicyNotifyService.setAlarmPolicyNotify(id, request.getNotifyIdList());
// 删除缓存
// 重新加载上下文
alarmEngineContext.reloadPolicy(id);
// 删除缓存
RedisMaps.delete(AlarmPolicyCacheKeyDefine.ALARM_POLICY.format(record.getType()),
AlarmPolicyCacheKeyDefine.ALARM_POLICY.format(Const.ALL));
// 设置日志参数
OperatorLogs.add(OperatorLogs.ID, id);
log.info("AlarmPolicyService-createAlarmPolicy id: {}, effect: {}", id, effect);
@@ -130,6 +134,11 @@ public class AlarmPolicyServiceImpl implements AlarmPolicyService {
Long newId = this.createAlarmPolicy(request);
// 复制策略规则
alarmPolicyRuleService.copyAlarmPolicyRule(id, newId);
// 重新加载上下文
alarmEngineContext.reloadPolicy(id);
// 删除缓存
RedisMaps.delete(AlarmPolicyCacheKeyDefine.ALARM_POLICY.format(record.getType()),
AlarmPolicyCacheKeyDefine.ALARM_POLICY.format(Const.ALL));
return newId;
}
@@ -143,6 +152,7 @@ public class AlarmPolicyServiceImpl implements AlarmPolicyService {
Assert.notNull(record, ErrorMessage.DATA_ABSENT);
// 转换
AlarmPolicyDO updateRecord = AlarmPolicyConvert.MAPPER.to(request);
updateRecord.setType(record.getType());
// 查询数据是否冲突
this.checkAlarmPolicyPresent(updateRecord);
// 更新
@@ -152,8 +162,11 @@ public class AlarmPolicyServiceImpl implements AlarmPolicyService {
alarmPolicyNotifyService.setAlarmPolicyNotify(id, request.getNotifyIdList());
}
log.info("AlarmPolicyService-updateAlarmPolicyById effect: {}", effect);
// 删除缓存
// 重新加载上下文
alarmEngineContext.reloadPolicy(id);
// 删除缓存
RedisMaps.delete(AlarmPolicyCacheKeyDefine.ALARM_POLICY.format(record.getType()),
AlarmPolicyCacheKeyDefine.ALARM_POLICY.format(Const.ALL));
return effect;
}
@@ -171,16 +184,21 @@ public class AlarmPolicyServiceImpl implements AlarmPolicyService {
}
@Override
public List<AlarmPolicyVO> getAlarmPolicyListByCache() {
public List<AlarmPolicyVO> getAlarmPolicyListByCache(String type) {
String cacheKey = AlarmPolicyCacheKeyDefine.ALARM_POLICY.format(type);
// 查询缓存
List<AlarmPolicyCacheDTO> list = RedisMaps.valuesJson(AlarmPolicyCacheKeyDefine.ALARM_POLICY);
List<AlarmPolicyCacheDTO> list = RedisMaps.valuesJson(cacheKey, AlarmPolicyCacheKeyDefine.ALARM_POLICY);
if (list.isEmpty()) {
// 查询数据库
list = alarmPolicyDAO.of().list(AlarmPolicyConvert.MAPPER::toCache);
list = alarmPolicyDAO.of()
.createWrapper()
.eq(!Const.ALL.equals(type), AlarmPolicyDO::getType, type)
.then()
.list(AlarmPolicyConvert.MAPPER::toCache);
// 设置屏障 防止穿透
CacheBarriers.checkBarrier(list, AlarmPolicyCacheDTO::new);
// 设置缓存
RedisMaps.putAllJson(AlarmPolicyCacheKeyDefine.ALARM_POLICY, s -> s.getId().toString(), list);
RedisMaps.putAllJson(cacheKey, AlarmPolicyCacheKeyDefine.ALARM_POLICY, s -> s.getId().toString(), list);
}
// 删除屏障
CacheBarriers.removeBarrier(list);
@@ -259,6 +277,7 @@ public class AlarmPolicyServiceImpl implements AlarmPolicyService {
public LambdaQueryWrapper<AlarmPolicyDO> buildQueryWrapper(AlarmPolicyQueryRequest request) {
return alarmPolicyDAO.wrapper()
.eq(AlarmPolicyDO::getId, request.getId())
.eq(AlarmPolicyDO::getType, request.getType())
.like(AlarmPolicyDO::getName, request.getName())
.like(AlarmPolicyDO::getDescription, request.getDescription());
}
@@ -274,6 +293,7 @@ public class AlarmPolicyServiceImpl implements AlarmPolicyService {
// 更新时忽略当前记录
.ne(AlarmPolicyDO::getId, domain.getId())
// 用其他字段做重复校验
.eq(AlarmPolicyDO::getType, domain.getType())
.eq(AlarmPolicyDO::getName, domain.getName());
// 检查是否存在
boolean present = alarmPolicyDAO.of(wrapper).present();

View File

@@ -5,6 +5,7 @@
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="org.dromara.visor.module.monitor.entity.domain.AlarmPolicyDO">
<id column="id" property="id"/>
<result column="type" property="type"/>
<result column="name" property="name"/>
<result column="description" property="description"/>
<result column="create_time" property="createTime"/>
@@ -16,7 +17,7 @@
<!-- 通用查询结果列 -->
<sql id="Base_Column_List">
id, name, description, create_time, update_time, creator, updater, deleted
id, type, name, description, create_time, update_time, creator, updater, deleted
</sql>
</mapper>

View File

@@ -7,6 +7,7 @@ import axios from 'axios';
*/
export interface AlarmPolicyCreateRequest {
name?: string;
type?: string;
description?: string;
notifyIdList?: Array<number>;
}
@@ -24,6 +25,7 @@ export interface AlarmPolicyUpdateRequest extends AlarmPolicyCreateRequest {
*/
export interface AlarmPolicyQueryRequest extends Pagination, OrderDirection {
id?: number;
type?: string;
name?: string;
description?: string;
}
@@ -33,6 +35,7 @@ export interface AlarmPolicyQueryRequest extends Pagination, OrderDirection {
*/
export interface AlarmPolicyQueryResponse extends TableData {
id: number;
type: string;
name: string;
description: string;
notifyIdList: Array<number>;
@@ -73,8 +76,8 @@ export function getAlarmPolicy(id: number) {
/**
* 查询全部监控告警策略
*/
export function getAlarmPolicyList() {
return axios.get<Array<AlarmPolicyQueryResponse>>('/monitor/alarm-policy/list');
export function getAlarmPolicyList(type: string) {
return axios.get<Array<AlarmPolicyQueryResponse>>('/monitor/alarm-policy/list', { params: { type } });
}
/**

View File

@@ -19,6 +19,12 @@
import { useCacheStore } from '@/store';
import useLoading from '@/hooks/loading';
const props = withDefaults(defineProps<Partial<{
type?: string;
}>>(), {
type: 'all',
});
const modelValue = defineModel({ type: Number });
const { loading, setLoading } = useLoading();
@@ -30,7 +36,7 @@
const initOptions = async () => {
setLoading(true);
try {
const values = await cacheStore.loadMonitorAlarmPolicy();
const values = await cacheStore.loadMonitorAlarmPolicy(props.type);
optionData.value = values.map(s => {
return {
label: s.name,

View File

@@ -177,8 +177,8 @@ export default defineStore('cache', {
},
// 查询监控告警策略列表
async loadMonitorAlarmPolicy(force = false) {
return await this.load('alarmPolicy', getAlarmPolicyList, ['monitor:alarm-policy:query'], force);
async loadMonitorAlarmPolicy(type: string = 'all', force = false) {
return await this.load(`alarmPolicy_${type}`, () => getAlarmPolicyList(type), ['monitor:alarm-policy:query'], force);
},
// 查询监控指标列表

View File

@@ -7,7 +7,7 @@ export type CacheType = 'users' | 'menus' | 'roles'
| 'authorizedHostKeys' | 'authorizedHostIdentities'
| 'commandSnippetGroups' | 'pathBookmarkGroups'
| 'commandSnippets' | 'pathBookmarks'
| 'alarmPolicy' | 'monitorMetrics'
| 'alarmPolicy_*' | 'monitorMetrics'
| 'systemSetting' | 'notifyTemplate*'
| '*_Tags' | 'preference_*'
| string

View File

@@ -57,8 +57,9 @@
import formRules from '../types/form.rules';
import { assignOmitRecord } from '@/utils';
import { createAlarmPolicy, updateAlarmPolicy, copyAlarmPolicy, getAlarmPolicy } from '@/api/monitor/alarm-policy';
import { Message } from '@arco-design/web-vue';
import { useToggle } from '@vueuse/core';
import { Message } from '@arco-design/web-vue';
import { AlarmPolicyType } from '../types/const';
import NotifyTemplateSelector from '@/components/system/notify-template/selector/index.vue';
const emits = defineEmits(['added', 'updated']);
@@ -75,6 +76,7 @@
const defaultForm = (): AlarmPolicyUpdateRequest => {
return {
id: undefined,
type: AlarmPolicyType.HOST,
name: undefined,
description: undefined,
notifyIdList: [],

View File

@@ -145,7 +145,7 @@
import useLoading from '@/hooks/loading';
import columns from '../types/table.columns';
import { useRouter } from 'vue-router';
import { TableName } from '../types/const';
import { AlarmPolicyType, TableName } from '../types/const';
import { useTablePagination, useTableColumns } from '@/hooks/table';
import { useQueryOrder, ASC } from '@/hooks/query-order';
import TableAdjust from '@/components/app/table-adjust/index.vue';
@@ -161,6 +161,7 @@
const tableRenderData = ref<Array<AlarmPolicyQueryResponse>>([]);
const formModel = reactive<AlarmPolicyQueryRequest>({
id: undefined,
type: AlarmPolicyType.HOST,
name: undefined,
description: undefined,
});

View File

@@ -1 +1,6 @@
export const TableName = 'monitor_alarm_policy';
// 告警策略类型
export const AlarmPolicyType = {
HOST: 'HOST',
};

View File

@@ -31,6 +31,7 @@
<!-- 告警策略 -->
<a-form-item field="policyId" label="告警策略">
<alarm-policy-selector v-model="formModel.policyId"
type="HOST"
placeholder="请选择告警策略"
allow-clear />
</a-form-item>