🔨 添加策略类型.

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -77,9 +77,10 @@ public interface AlarmPolicyService {
/** /**
* 通过缓存查询监控告警策略 * 通过缓存查询监控告警策略
* *
* @param type type
* @return rows * @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.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.dromara.visor.common.constant.Const;
import org.dromara.visor.common.constant.ErrorMessage; import org.dromara.visor.common.constant.ErrorMessage;
import org.dromara.visor.common.utils.Assert; import org.dromara.visor.common.utils.Assert;
import org.dromara.visor.framework.biz.operator.log.core.utils.OperatorLogs; import org.dromara.visor.framework.biz.operator.log.core.utils.OperatorLogs;
@@ -110,8 +111,11 @@ public class AlarmPolicyServiceImpl implements AlarmPolicyService {
Long id = record.getId(); Long id = record.getId();
// 设置告警通知 // 设置告警通知
alarmPolicyNotifyService.setAlarmPolicyNotify(id, request.getNotifyIdList()); alarmPolicyNotifyService.setAlarmPolicyNotify(id, request.getNotifyIdList());
// 删除缓存 // 重新加载上下文
alarmEngineContext.reloadPolicy(id); alarmEngineContext.reloadPolicy(id);
// 删除缓存
RedisMaps.delete(AlarmPolicyCacheKeyDefine.ALARM_POLICY.format(record.getType()),
AlarmPolicyCacheKeyDefine.ALARM_POLICY.format(Const.ALL));
// 设置日志参数 // 设置日志参数
OperatorLogs.add(OperatorLogs.ID, id); OperatorLogs.add(OperatorLogs.ID, id);
log.info("AlarmPolicyService-createAlarmPolicy id: {}, effect: {}", id, effect); log.info("AlarmPolicyService-createAlarmPolicy id: {}, effect: {}", id, effect);
@@ -130,6 +134,11 @@ public class AlarmPolicyServiceImpl implements AlarmPolicyService {
Long newId = this.createAlarmPolicy(request); Long newId = this.createAlarmPolicy(request);
// 复制策略规则 // 复制策略规则
alarmPolicyRuleService.copyAlarmPolicyRule(id, newId); alarmPolicyRuleService.copyAlarmPolicyRule(id, newId);
// 重新加载上下文
alarmEngineContext.reloadPolicy(id);
// 删除缓存
RedisMaps.delete(AlarmPolicyCacheKeyDefine.ALARM_POLICY.format(record.getType()),
AlarmPolicyCacheKeyDefine.ALARM_POLICY.format(Const.ALL));
return newId; return newId;
} }
@@ -143,6 +152,7 @@ public class AlarmPolicyServiceImpl implements AlarmPolicyService {
Assert.notNull(record, ErrorMessage.DATA_ABSENT); Assert.notNull(record, ErrorMessage.DATA_ABSENT);
// 转换 // 转换
AlarmPolicyDO updateRecord = AlarmPolicyConvert.MAPPER.to(request); AlarmPolicyDO updateRecord = AlarmPolicyConvert.MAPPER.to(request);
updateRecord.setType(record.getType());
// 查询数据是否冲突 // 查询数据是否冲突
this.checkAlarmPolicyPresent(updateRecord); this.checkAlarmPolicyPresent(updateRecord);
// 更新 // 更新
@@ -152,8 +162,11 @@ public class AlarmPolicyServiceImpl implements AlarmPolicyService {
alarmPolicyNotifyService.setAlarmPolicyNotify(id, request.getNotifyIdList()); alarmPolicyNotifyService.setAlarmPolicyNotify(id, request.getNotifyIdList());
} }
log.info("AlarmPolicyService-updateAlarmPolicyById effect: {}", effect); log.info("AlarmPolicyService-updateAlarmPolicyById effect: {}", effect);
// 删除缓存 // 重新加载上下文
alarmEngineContext.reloadPolicy(id); alarmEngineContext.reloadPolicy(id);
// 删除缓存
RedisMaps.delete(AlarmPolicyCacheKeyDefine.ALARM_POLICY.format(record.getType()),
AlarmPolicyCacheKeyDefine.ALARM_POLICY.format(Const.ALL));
return effect; return effect;
} }
@@ -171,16 +184,21 @@ public class AlarmPolicyServiceImpl implements AlarmPolicyService {
} }
@Override @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()) { 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); 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); CacheBarriers.removeBarrier(list);
@@ -259,6 +277,7 @@ public class AlarmPolicyServiceImpl implements AlarmPolicyService {
public LambdaQueryWrapper<AlarmPolicyDO> buildQueryWrapper(AlarmPolicyQueryRequest request) { public LambdaQueryWrapper<AlarmPolicyDO> buildQueryWrapper(AlarmPolicyQueryRequest request) {
return alarmPolicyDAO.wrapper() return alarmPolicyDAO.wrapper()
.eq(AlarmPolicyDO::getId, request.getId()) .eq(AlarmPolicyDO::getId, request.getId())
.eq(AlarmPolicyDO::getType, request.getType())
.like(AlarmPolicyDO::getName, request.getName()) .like(AlarmPolicyDO::getName, request.getName())
.like(AlarmPolicyDO::getDescription, request.getDescription()); .like(AlarmPolicyDO::getDescription, request.getDescription());
} }
@@ -274,6 +293,7 @@ public class AlarmPolicyServiceImpl implements AlarmPolicyService {
// 更新时忽略当前记录 // 更新时忽略当前记录
.ne(AlarmPolicyDO::getId, domain.getId()) .ne(AlarmPolicyDO::getId, domain.getId())
// 用其他字段做重复校验 // 用其他字段做重复校验
.eq(AlarmPolicyDO::getType, domain.getType())
.eq(AlarmPolicyDO::getName, domain.getName()); .eq(AlarmPolicyDO::getName, domain.getName());
// 检查是否存在 // 检查是否存在
boolean present = alarmPolicyDAO.of(wrapper).present(); boolean present = alarmPolicyDAO.of(wrapper).present();

View File

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

View File

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

View File

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

View File

@@ -177,8 +177,8 @@ export default defineStore('cache', {
}, },
// 查询监控告警策略列表 // 查询监控告警策略列表
async loadMonitorAlarmPolicy(force = false) { async loadMonitorAlarmPolicy(type: string = 'all', force = false) {
return await this.load('alarmPolicy', getAlarmPolicyList, ['monitor:alarm-policy:query'], force); 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' | 'authorizedHostKeys' | 'authorizedHostIdentities'
| 'commandSnippetGroups' | 'pathBookmarkGroups' | 'commandSnippetGroups' | 'pathBookmarkGroups'
| 'commandSnippets' | 'pathBookmarks' | 'commandSnippets' | 'pathBookmarks'
| 'alarmPolicy' | 'monitorMetrics' | 'alarmPolicy_*' | 'monitorMetrics'
| 'systemSetting' | 'notifyTemplate*' | 'systemSetting' | 'notifyTemplate*'
| '*_Tags' | 'preference_*' | '*_Tags' | 'preference_*'
| string | string

View File

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

View File

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

View File

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

View File

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