🔨 优化标签查询逻辑.

This commit is contained in:
lijiahangmax
2025-10-07 14:26:40 +08:00
parent 25082b9ea1
commit 2103698417
17 changed files with 296 additions and 223 deletions

View File

@@ -312,6 +312,19 @@ public class RedisMaps extends RedisUtils {
return getJson(key.getKey(), hashKey, (Class<V>) key.getType());
}
/**
* 获取值 json
*
* @param define define
* @param key key
* @param hashKey hashKey
* @param <V> V
* @return value
*/
public static <V> V getJson(String key, CacheKeyDefine define, Object hashKey) {
return getJson(key, hashKey, (Class<V>) define.getType());
}
/**
* 获取值 json
*
@@ -355,9 +368,9 @@ public class RedisMaps extends RedisUtils {
/**
* 获取值 json
*
* @param key key
* @param hashKeys hashKeys
* @param <V> V
* @param key key
* @param hashKeys hashKeys
* @param <V> V
* @return values
*/
public static <V> List<V> multiGetJson(CacheKeyDefine key, List<?> hashKeys) {
@@ -367,10 +380,10 @@ public class RedisMaps extends RedisUtils {
/**
* 获取值 json
*
* @param key key
* @param hashKeys hashKeys
* @param clazz clazz
* @param <V> V
* @param key key
* @param hashKeys hashKeys
* @param clazz clazz
* @param <V> V
* @return values
*/
public static <V> List<V> multiGetJson(String key, List<?> hashKeys, Class<V> clazz) {

View File

@@ -93,11 +93,11 @@ public class AlarmPolicyRuleController {
@GetMapping("/list")
@Operation(summary = "查询全部监控告警规则")
@Parameter(name = "policyId", description = "policyId", required = true)
@Parameter(name = "metricsMeasurement", description = "metricsMeasurement")
@Parameter(name = "measurement", description = "measurement")
@PreAuthorize("@ss.hasPermission('monitor:alarm-policy:query')")
public List<AlarmPolicyRuleVO> getAlarmPolicyRuleList(@RequestParam("policyId") Long policyId,
@RequestParam(value = "metricsMeasurement", required = false) String metricsMeasurement) {
return alarmPolicyRuleService.getAlarmPolicyRuleList(policyId, metricsMeasurement);
@RequestParam(value = "measurement", required = false) String measurement) {
return alarmPolicyRuleService.getAlarmPolicyRuleList(policyId, measurement);
}
@DemoDisableApi

View File

@@ -106,6 +106,17 @@ public class MonitorHostController {
return monitorHostService.getMonitorHostChart(request);
}
@IgnoreLog(IgnoreLogMode.RET)
@GetMapping("/host-tags")
@Operation(summary = "查询监控告警标签")
@Parameter(name = "policyId", description = "policyId", required = true)
@Parameter(name = "measurement", description = "measurement")
@PreAuthorize("@ss.hasPermission('monitor:monitor-host:query')")
public List<String> getMonitorHostPolicyRuleTags(@RequestParam("policyId") Long policyId,
@RequestParam(value = "measurement", required = false) String measurement) {
return monitorHostService.getMonitorHostPolicyRuleTags(policyId, measurement);
}
@DemoDisableApi
@OperatorLog(MonitorHostOperatorType.UPDATE)
@PutMapping("/update")

View File

@@ -95,6 +95,20 @@ public interface MonitorHostDAO extends IMapper<MonitorHostDO> {
return this.delete(Conditions.in(MonitorHostDO::getHostId, hostIdList));
}
/**
* 通过 policyId 查询
*
* @param policyId policyId
* @return row
*/
default List<MonitorHostDO> selectByPolicyId(Long policyId) {
return this.of()
.createWrapper()
.eq(MonitorHostDO::getPolicyId, policyId)
.then()
.list();
}
/**
* 设置 policyId 为 null
*

View File

@@ -38,7 +38,7 @@ import java.util.concurrent.TimeUnit;
*/
public interface AlarmPolicyCacheKeyDefine {
CacheKeyDefine MONITOR_ALARM_POLICY = new CacheKeyBuilder()
CacheKeyDefine ALARM_POLICY = new CacheKeyBuilder()
.key("alarm:policy:list")
.desc("告警策略")
.type(AlarmPolicyCacheDTO.class)
@@ -49,6 +49,7 @@ public interface AlarmPolicyCacheKeyDefine {
CacheKeyDefine ALARM_RULE_SILENCE = new CacheKeyBuilder()
.key("alarm:silence:{}:{}")
.desc("告警规则沉默标志 ${agentKey} ${ruleId}")
.type(Long.class)
.struct(RedisCacheStruct.STRING)
.build();

View File

@@ -0,0 +1,48 @@
/*
* Copyright (c) 2023 - present Dromara, All rights reserved.
*
* https://visor.dromara.org
* https://visor.dromara.org.cn
* https://visor.orionsec.cn
*
* Members:
* Jiahang Li - ljh1553488six@139.com - author
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.visor.module.monitor.define.cache;
import cn.orionsec.kit.lang.define.cache.key.CacheKeyBuilder;
import cn.orionsec.kit.lang.define.cache.key.CacheKeyDefine;
import cn.orionsec.kit.lang.define.cache.key.struct.RedisCacheStruct;
import java.util.concurrent.TimeUnit;
/**
* 监控主机缓存 key
*
* @author Jiahang Li
* @version 1.0.0
* @since 2025-9-14 00:12
*/
public interface MonitorHostCacheKeyDefine {
CacheKeyDefine MONITOR_HOST_POLICY_HOST_TAGS = new CacheKeyBuilder()
.key("monitor:host:policy:host-tags:{}")
.desc("告警规则沉默标志 ${policyId}")
.type(String.class)
.struct(RedisCacheStruct.HASH)
.timeout(8, TimeUnit.HOURS)
.build();
}

View File

@@ -43,7 +43,7 @@ public enum MeasurementEnum {
/**
* cpu
*/
CPU("cpu", (s) -> {
CPU("cpu", true, (s) -> {
s.accept(MetricsConst.CPU_USER_SECONDS_TOTAL, double.class);
s.accept(MetricsConst.CPU_SYSTEM_SECONDS_TOTAL, double.class);
s.accept(MetricsConst.CPU_TOTAL_SECONDS_TOTAL, double.class);
@@ -52,7 +52,7 @@ public enum MeasurementEnum {
/**
* 内存
*/
MEMORY("memory", s -> {
MEMORY("memory", false, s -> {
s.accept(MetricsConst.MEM_USED_BYTES_TOTAL, long.class);
s.accept(MetricsConst.MEM_USED_PERCENT, double.class);
s.accept(MetricsConst.MEM_SWAP_USED_BYTES_TOTAL, long.class);
@@ -62,7 +62,7 @@ public enum MeasurementEnum {
/**
* 负载
*/
LOAD("load", s -> {
LOAD("load", false, s -> {
s.accept(MetricsConst.LOAD1, double.class);
s.accept(MetricsConst.LOAD5, double.class);
s.accept(MetricsConst.LOAD15, double.class);
@@ -74,7 +74,7 @@ public enum MeasurementEnum {
/**
* 磁盘
*/
DISK("disk", s -> {
DISK("disk", true, s -> {
s.accept(MetricsConst.DISK_FS_USED_BYTES_TOTAL, long.class);
s.accept(MetricsConst.DISK_FS_USED_PERCENT, double.class);
s.accept(MetricsConst.DISK_FS_INODES_USED_PERCENT, double.class);
@@ -83,7 +83,7 @@ public enum MeasurementEnum {
/**
* io
*/
IO("io", s -> {
IO("io", false, s -> {
s.accept(MetricsConst.DISK_IO_READ_BYTES_TOTAL, long.class);
s.accept(MetricsConst.DISK_IO_WRITE_BYTES_TOTAL, long.class);
s.accept(MetricsConst.DISK_IO_READS_TOTAL, long.class);
@@ -97,7 +97,7 @@ public enum MeasurementEnum {
/**
* 网络
*/
NETWORK("network", s -> {
NETWORK("network", true, s -> {
s.accept(MetricsConst.NET_SENT_BYTES_TOTAL, long.class);
s.accept(MetricsConst.NET_RECV_BYTES_TOTAL, long.class);
s.accept(MetricsConst.NET_SENT_PACKETS_TOTAL, long.class);
@@ -111,7 +111,7 @@ public enum MeasurementEnum {
/**
* 连接数
*/
CONNECTIONS("connections", s -> {
CONNECTIONS("connections", false, s -> {
s.accept(MetricsConst.NET_TCP_CONNECTIONS, int.class);
s.accept(MetricsConst.NET_UDP_CONNECTIONS, int.class);
s.accept(MetricsConst.NET_INET_CONNECTIONS, int.class);
@@ -121,10 +121,14 @@ public enum MeasurementEnum {
;
private final String measurement;
private final boolean hasTags;
private final Map<String, Class<?>> fields;
MeasurementEnum(String measurement, Consumer<BiConsumer<String, Class<?>>> register) {
MeasurementEnum(String measurement, boolean hasTags, Consumer<BiConsumer<String, Class<?>>> register) {
this.measurement = measurement;
this.hasTags = hasTags;
this.fields = new HashedMap<>();
register.accept(this.fields::put);
}

View File

@@ -1,159 +0,0 @@
/*
* Copyright (c) 2023 - present Dromara, All rights reserved.
*
* https://visor.dromara.org
* https://visor.dromara.org.cn
* https://visor.orionsec.cn
*
* Members:
* Jiahang Li - ljh1553488six@139.com - author
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.visor.module.monitor.enums;
import lombok.Getter;
import org.apache.commons.collections4.map.HashedMap;
import org.dromara.visor.module.monitor.constant.MetricsConst;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
/**
* 指标度量类型
*
* @author Jiahang Li
* @version 1.0.0
* @since 2025/8/14 10:27
*/
@Getter
public enum MeasurementFieldEnum {
/**
* cpu
*/
CPU("cpu", (s) -> {
s.accept(MetricsConst.CPU_USER_SECONDS_TOTAL, double.class);
s.accept(MetricsConst.CPU_SYSTEM_SECONDS_TOTAL, double.class);
s.accept(MetricsConst.CPU_TOTAL_SECONDS_TOTAL, double.class);
}),
/**
* 内存
*/
MEMORY("memory", s -> {
s.accept(MetricsConst.MEM_USED_BYTES_TOTAL, long.class);
s.accept(MetricsConst.MEM_USED_PERCENT, double.class);
s.accept(MetricsConst.MEM_SWAP_USED_BYTES_TOTAL, long.class);
s.accept(MetricsConst.MEM_SWAP_USED_PERCENT, double.class);
}),
/**
* 负载
*/
LOAD("load", s -> {
s.accept(MetricsConst.LOAD1, double.class);
s.accept(MetricsConst.LOAD5, double.class);
s.accept(MetricsConst.LOAD15, double.class);
s.accept(MetricsConst.LOAD1_CORE_RATIO, double.class);
s.accept(MetricsConst.LOAD5_CORE_RATIO, double.class);
s.accept(MetricsConst.LOAD15_CORE_RATIO, double.class);
}),
/**
* 磁盘
*/
DISK("disk", s -> {
s.accept(MetricsConst.DISK_FS_USED_BYTES_TOTAL, long.class);
s.accept(MetricsConst.DISK_FS_USED_PERCENT, double.class);
s.accept(MetricsConst.DISK_FS_INODES_USED_PERCENT, double.class);
}),
/**
* io
*/
IO("io", s -> {
s.accept(MetricsConst.DISK_IO_READ_BYTES_TOTAL, long.class);
s.accept(MetricsConst.DISK_IO_WRITE_BYTES_TOTAL, long.class);
s.accept(MetricsConst.DISK_IO_READS_TOTAL, long.class);
s.accept(MetricsConst.DISK_IO_WRITES_TOTAL, long.class);
s.accept(MetricsConst.DISK_IO_READ_BYTES_PER_SECOND, double.class);
s.accept(MetricsConst.DISK_IO_WRITE_BYTES_PER_SECOND, double.class);
s.accept(MetricsConst.DISK_IO_READS_PER_SECOND, double.class);
s.accept(MetricsConst.DISK_IO_WRITES_PER_SECOND, double.class);
}),
/**
* 网络
*/
NETWORK("network", s -> {
s.accept(MetricsConst.NET_SENT_BYTES_TOTAL, long.class);
s.accept(MetricsConst.NET_RECV_BYTES_TOTAL, long.class);
s.accept(MetricsConst.NET_SENT_PACKETS_TOTAL, long.class);
s.accept(MetricsConst.NET_RECV_PACKETS_TOTAL, long.class);
s.accept(MetricsConst.NET_SENT_BYTES_PER_SECOND, double.class);
s.accept(MetricsConst.NET_RECV_BYTES_PER_SECOND, double.class);
s.accept(MetricsConst.NET_SENT_PACKETS_PER_SECOND, double.class);
s.accept(MetricsConst.NET_RECV_PACKETS_PER_SECOND, double.class);
}),
/**
* 连接数
*/
CONNECTIONS("connections", s -> {
s.accept(MetricsConst.NET_TCP_CONNECTIONS, int.class);
s.accept(MetricsConst.NET_UDP_CONNECTIONS, int.class);
s.accept(MetricsConst.NET_INET_CONNECTIONS, int.class);
s.accept(MetricsConst.NET_ALL_CONNECTIONS, int.class);
}),
;
private final String measurement;
private final Map<String, Class<?>> fields;
MeasurementFieldEnum(String measurement, Consumer<BiConsumer<String, Class<?>>> register) {
this.measurement = measurement;
this.fields = new HashedMap<>();
register.accept(this.fields::put);
}
public static MeasurementFieldEnum of(String measurement) {
if (measurement == null) {
return null;
}
for (MeasurementFieldEnum e : values()) {
if (e.measurement.equals(measurement)) {
return e;
}
}
return null;
}
/**
* 获取度量值类型
*
* @param measurement measurement
* @param field field
* @return type
*/
public static Class<?> getMetricsValueType(String measurement, String field) {
MeasurementFieldEnum m = of(measurement);
if (m == null) {
return null;
}
return m.getFields().get(field);
}
}

View File

@@ -73,11 +73,11 @@ public interface AlarmPolicyRuleService {
/**
* 查询全部监控告警规则
*
* @param policyId policyId
* @param metricsMeasurement metricsMeasurement
* @param policyId policyId
* @param measurement measurement
* @return rows
*/
List<AlarmPolicyRuleVO> getAlarmPolicyRuleList(Long policyId, String metricsMeasurement);
List<AlarmPolicyRuleVO> getAlarmPolicyRuleList(Long policyId, String measurement);
/**
* 通过 metricsId 删除监控告警规则

View File

@@ -66,6 +66,15 @@ public interface MonitorHostService {
*/
List<TimeChartSeries> getMonitorHostChart(MonitorHostChartRequest request);
/**
* 查询监控告警规则主机标签
*
* @param policyId policyId
* @param measurement measurement
* @return tags
*/
List<String> getMonitorHostPolicyRuleTags(Long policyId, String measurement);
/**
* 更新监控主机
*

View File

@@ -160,13 +160,13 @@ public class AlarmPolicyRuleServiceImpl implements AlarmPolicyRuleService {
}
@Override
public List<AlarmPolicyRuleVO> getAlarmPolicyRuleList(Long policyId, String metricsMeasurement) {
public List<AlarmPolicyRuleVO> getAlarmPolicyRuleList(Long policyId, String measurement) {
// 查询
return alarmPolicyRuleDAO.of()
.createWrapper()
.eq(AlarmPolicyRuleDO::getPolicyId, policyId)
.eq(Strings.isNotBlank(metricsMeasurement), AlarmPolicyRuleDO::getMetricsMeasurement, metricsMeasurement)
// 同的指标在一起
.eq(Strings.isNotBlank(measurement), AlarmPolicyRuleDO::getMetricsMeasurement, measurement)
// 同的指标在一起
.orderByAsc(AlarmPolicyRuleDO::getMetricsId)
// 通过 p0 > p1 排序
.orderByAsc(AlarmPolicyRuleDO::getLevel)

View File

@@ -173,14 +173,14 @@ public class AlarmPolicyServiceImpl implements AlarmPolicyService {
@Override
public List<AlarmPolicyVO> getAlarmPolicyListByCache() {
// 查询缓存
List<AlarmPolicyCacheDTO> list = RedisMaps.valuesJson(AlarmPolicyCacheKeyDefine.MONITOR_ALARM_POLICY);
List<AlarmPolicyCacheDTO> list = RedisMaps.valuesJson(AlarmPolicyCacheKeyDefine.ALARM_POLICY);
if (list.isEmpty()) {
// 查询数据库
list = alarmPolicyDAO.of().list(AlarmPolicyConvert.MAPPER::toCache);
// 设置屏障 防止穿透
CacheBarriers.checkBarrier(list, AlarmPolicyCacheDTO::new);
// 设置缓存
RedisMaps.putAllJson(AlarmPolicyCacheKeyDefine.MONITOR_ALARM_POLICY, s -> s.getId().toString(), list);
RedisMaps.putAllJson(AlarmPolicyCacheKeyDefine.ALARM_POLICY, s -> s.getId().toString(), list);
}
// 删除屏障
CacheBarriers.removeBarrier(list);

View File

@@ -35,10 +35,12 @@ import org.dromara.visor.common.constant.ErrorMessage;
import org.dromara.visor.common.utils.Assert;
import org.dromara.visor.common.utils.LockerUtils;
import org.dromara.visor.framework.influxdb.core.utils.InfluxdbUtils;
import org.dromara.visor.framework.redis.core.utils.RedisMaps;
import org.dromara.visor.module.asset.api.HostApi;
import org.dromara.visor.module.asset.entity.dto.host.HostDTO;
import org.dromara.visor.module.infra.api.SystemUserApi;
import org.dromara.visor.module.monitor.dao.MonitorHostDAO;
import org.dromara.visor.module.monitor.define.cache.MonitorHostCacheKeyDefine;
import org.dromara.visor.module.monitor.engine.AlarmEngine;
import org.dromara.visor.module.monitor.engine.MonitorContext;
import org.dromara.visor.module.monitor.entity.domain.MonitorHostDO;
@@ -146,8 +148,13 @@ public class MonitorAgentEndpointServiceImpl implements MonitorAgentEndpointServ
update.setMonitorConfig(JSON.toJSONString(newConfig));
}
monitorHostDAO.updateById(update);
// 删除元数据缓存
Long policyId = monitorHost.getPolicyId();
if (policyId != null) {
RedisMaps.delete(MonitorHostCacheKeyDefine.MONITOR_HOST_POLICY_HOST_TAGS.format(policyId));
}
}
// 设置配置缓存
// 重新加载监控主机上下文
if (newConfig != null) {
monitorContext.reloadMonitorHost(agentKey);
}

View File

@@ -40,6 +40,7 @@ import org.dromara.visor.framework.biz.operator.log.core.utils.OperatorLogs;
import org.dromara.visor.framework.influxdb.core.query.FluxQueryBuilder;
import org.dromara.visor.framework.influxdb.core.utils.InfluxdbUtils;
import org.dromara.visor.framework.mybatis.core.query.Conditions;
import org.dromara.visor.framework.redis.core.utils.RedisMaps;
import org.dromara.visor.module.asset.api.HostAgentApi;
import org.dromara.visor.module.asset.api.HostApi;
import org.dromara.visor.module.asset.entity.dto.host.HostAgentLogDTO;
@@ -51,6 +52,7 @@ import org.dromara.visor.module.monitor.constant.MetricsConst;
import org.dromara.visor.module.monitor.convert.MonitorHostConvert;
import org.dromara.visor.module.monitor.dao.AlarmPolicyDAO;
import org.dromara.visor.module.monitor.dao.MonitorHostDAO;
import org.dromara.visor.module.monitor.define.cache.MonitorHostCacheKeyDefine;
import org.dromara.visor.module.monitor.engine.MonitorContext;
import org.dromara.visor.module.monitor.entity.domain.AlarmPolicyDO;
import org.dromara.visor.module.monitor.entity.domain.MonitorHostDO;
@@ -74,6 +76,7 @@ import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* 监控主机 服务实现类
@@ -235,6 +238,48 @@ public class MonitorHostServiceImpl implements MonitorHostService {
return seriesList;
}
@Override
public List<String> getMonitorHostPolicyRuleTags(Long policyId, String measurement) {
MeasurementEnum measurementEnum = MeasurementEnum.of(measurement);
if (measurementEnum == null) {
return Collections.emptyList();
}
// 查询缓存
String cacheKey = MonitorHostCacheKeyDefine.MONITOR_HOST_POLICY_HOST_TAGS.format(policyId);
String value = RedisMaps.get(cacheKey, measurement);
if (!Strings.isBlank(value)) {
return JSON.parseArray(value, String.class);
}
// 查询规则下的全部主机
List<MonitorHostMetaDTO> metas = monitorHostDAO.selectByPolicyId(policyId)
.stream()
.map(MonitorHostDO::getMonitorMeta)
.filter(Objects::nonNull)
.map(s -> JSON.parseObject(s, MonitorHostMetaDTO.class))
.filter(Objects::nonNull)
.collect(Collectors.toList());
// 映射数据
Function<MonitorHostMetaDTO, List<String>> tagsGetter;
if (MeasurementEnum.CPU.equals(measurementEnum)) {
tagsGetter = MonitorHostMetaDTO::getCpus;
} else if (MeasurementEnum.DISK.equals(measurementEnum)) {
tagsGetter = MonitorHostMetaDTO::getDisks;
} else if (MeasurementEnum.NETWORK.equals(measurementEnum)) {
tagsGetter = MonitorHostMetaDTO::getNets;
} else {
return Collections.emptyList();
}
List<String> tags = metas.stream()
.map(tagsGetter)
.flatMap(Collection::stream)
.filter(Objects::nonNull)
.distinct()
.collect(Collectors.toList());
// 设置缓存
RedisMaps.putJson(cacheKey, MonitorHostCacheKeyDefine.MONITOR_HOST_POLICY_HOST_TAGS, measurement, tags);
return tags;
}
@Override
@Transactional(rollbackFor = Exception.class)
public Integer updateMonitorHostById(MonitorHostUpdateRequest request) {
@@ -272,7 +317,15 @@ public class MonitorHostServiceImpl implements MonitorHostService {
if (policyId == null) {
monitorHostDAO.setPolicyIdWithNullById(id);
}
// 更新缓存
// 删除元数据缓存
List<String> tagsCacheKeyList = Stream.of(policyId, monitorHost.getPolicyId())
.filter(Objects::nonNull)
.map(MonitorHostCacheKeyDefine.MONITOR_HOST_POLICY_HOST_TAGS::format)
.collect(Collectors.toList());
if (!tagsCacheKeyList.isEmpty()) {
RedisMaps.delete(tagsCacheKeyList);
}
// 重新加载监控主机上下文
monitorContext.reloadMonitorHost(host.getAgentKey());
log.info("MonitorHostService-updateMonitorHostById effect: {}", effect);
return effect;

View File

@@ -72,8 +72,8 @@ export function updateAlarmRuleSwitch(request: AlarmRuleUpdateRequest) {
/**
* 查询全部监控告警规则
*/
export function getAlarmRuleList(policyId: number, metricsMeasurement: string = '') {
return axios.get<Array<AlarmRuleQueryResponse>>('/monitor/alarm-policy-rule/list', { params: { policyId, metricsMeasurement } });
export function getAlarmRuleList(policyId: number, measurement: string = '') {
return axios.get<Array<AlarmRuleQueryResponse>>('/monitor/alarm-policy-rule/list', { params: { policyId, measurement } });
}
/**

View File

@@ -177,6 +177,13 @@ export function getMonitorHostPage(request: MonitorHostQueryRequest) {
return axios.post<DataGrid<MonitorHostQueryResponse>>('/monitor/monitor-host/query', request);
}
/**
* 查询监控告警标签
*/
export function getMonitorHostPolicyRuleTags(policyId: number, measurement: string = '') {
return axios.get<Array<string>>('/monitor/monitor-host/host-tags', { params: { policyId, measurement } });
}
/**
* 更新监控主机
*/

View File

@@ -6,7 +6,7 @@
:unmount-on-close="true"
:ok-button-props="{ disabled: loading }"
:cancel-button-props="{ disabled: loading }"
:on-before-ok="handlerOk"
:on-before-ok="handleOk"
@cancel="handleClose">
<a-spin class="full drawer-form-large" :loading="loading">
<a-form :model="formModel"
@@ -17,11 +17,12 @@
<!-- 监控指标 -->
<a-form-item field="metricsId" label="监控指标">
<monitor-metrics-selector v-model="formModel.metricsId"
class="metrics-selector"
:class="[ hasTags ? 'metrics-selector-has-tag' : 'metrics-selector-no-tag']"
placeholder="请选择监控指标"
allow-clear />
<!-- 添加标签 -->
<a-button title="添加标签"
<a-button v-if="hasTags"
title="添加标签"
:disabled="formModel.allEffect === 1"
@click="addTag">
<template #icon>
@@ -44,6 +45,7 @@
class="tag-values"
style="width: 260px"
:max-tag-count="2"
:options="measurementTags[measurement] || []"
placeholder="标签值"
tag-nowrap
multiple
@@ -72,7 +74,7 @@
</a-form-item>
</a-col>
<!-- 全部生效 -->
<a-col :span="12">
<a-col v-if="hasTags" :span="12">
<a-form-item field="allEffect"
label="全部生效"
tooltip="开启后则忽略标签, 并生效与已配置标签的规则 (通常用于默认策略)"
@@ -190,16 +192,18 @@
import type { MetricsQueryResponse } from '@/api/monitor/metrics';
import type { RuleTag } from '../types/const';
import type { FormHandle } from '@/types/form';
import { ref, computed } from 'vue';
import { ref, computed, watch } from 'vue';
import useLoading from '@/hooks/loading';
import useVisible from '@/hooks/visible';
import formRules from '../types/form.rules';
import { MetricsUnitKey } from '../types/const';
import { MetricsUnitKey, MeasurementKey } from '../types/const';
import { assignOmitRecord } from '@/utils';
import { TriggerConditionKey, LevelKey, DefaultCondition, DefaultLevel, } from '../types/const';
import { createAlarmRule, updateAlarmRule } from '@/api/monitor/alarm-rule';
import { isBoolean } from '@/utils/is';
import { Message } from '@arco-design/web-vue';
import { useDictStore, useCacheStore } from '@/store';
import { getMonitorHostPolicyRuleTags } from '@/api/monitor/monitor-host';
import MonitorMetricsSelector from '@/components/monitor/metrics/selector/index.vue';
const emits = defineEmits(['added', 'updated']);
@@ -214,6 +218,9 @@
const formRef = ref<any>();
const formModel = ref<AlarmRuleUpdateRequest>({});
const tags = ref<Array<RuleTag>>([]);
const hasTags = ref(false);
const measurement = ref('');
const measurementTags = ref<Record<string, string[]>>({});
const defaultForm = (): AlarmRuleUpdateRequest => {
return {
@@ -223,7 +230,7 @@
tags: undefined,
level: DefaultLevel,
ruleSwitch: 1,
allEffect: 0,
allEffect: 1,
triggerCondition: DefaultCondition,
threshold: undefined,
consecutiveCount: 1,
@@ -232,18 +239,28 @@
};
};
// 指标单位
const metricsUnit = computed(() => {
const metricsId = formModel.value.metricsId;
// 检查是否有 tags
watch(() => formModel.value.metricsId, (metricsId) => {
if (!metricsId) {
return '';
hasTags.value = false;
return;
}
// 读取指标单位
const unit = (monitorMetrics as Array<MetricsQueryResponse>).find(m => m.id === metricsId)?.unit;
if (!unit) {
return '';
// 获取数据集
const measurementValue = (monitorMetrics as Array<MetricsQueryResponse> || []).find(m => m.id === metricsId)?.measurement;
if (!measurementValue) {
hasTags.value = false;
return;
}
measurement.value = measurementValue;
// 获取标签
const value = getDictValue(MeasurementKey, measurementValue, 'hasTags');
if (value === true) {
hasTags.value = true;
// 加载全部标签
loadTags();
} else {
hasTags.value = false;
}
return getDictValue(MetricsUnitKey, unit, 'alarmUnit');
});
// 打开新增
@@ -284,7 +301,8 @@
// 添加标签
const addTag = () => {
tags.value.push({ key: '', value: [] });
const hasNameTag = tags.value.some(s => s.key === 'name');
tags.value.push({ key: hasNameTag ? '' : 'name', value: [] });
};
// 移除标签
@@ -292,8 +310,34 @@
tags.value.splice(index, 1);
};
// 指标单位
const metricsUnit = computed(() => {
const metricsId = formModel.value.metricsId;
if (!metricsId) {
return '';
}
// 读取指标单位
const unit = (monitorMetrics as Array<MetricsQueryResponse> || []).find(m => m.id === metricsId)?.unit;
if (!unit) {
return '';
}
return getDictValue(MetricsUnitKey, unit, 'alarmUnit');
});
// 加载全部标签
const loadTags = () => {
const tags = measurementTags.value[measurement.value];
if (tags) {
return;
}
// 加载标签
getMonitorHostPolicyRuleTags(formModel.value.policyId as number, measurement.value).then(({ data }) => {
measurementTags.value[measurement.value as any] = data;
});
};
// 确定
const handlerOk = async () => {
const handleOk = async () => {
setLoading(true);
try {
// 验证参数
@@ -301,21 +345,37 @@
if (error) {
return false;
}
for (let tag of tags.value) {
if (!tag.key) {
Message.error('请输入标签名称');
return false;
}
if (!tag.value) {
Message.error('请输入标签值');
return false;
if (!hasTags.value) {
// 无 tag
formModel.value.allEffect = 1;
} else {
// 有 tag
if (formModel.value.allEffect === 1) {
// 全部生效
tags.value = [];
} else {
// 检查 tag
if (!tags.value.length) {
Message.error('请选择全部生效或添加对应的标签');
return false;
}
for (let tag of tags.value) {
if (!tag.key) {
Message.error('请输入标签名称');
return false;
}
if (!tag.value) {
Message.error('请输入标签值');
return false;
}
}
}
}
if (formHandle.value == 'add') {
// 新增
await createAlarmRule({
...formModel.value,
tags: formModel.value.allEffect === 1 ? '[]' : JSON.stringify(tags.value)
tags: JSON.stringify(tags.value)
});
Message.success('创建成功');
emits('added');
@@ -323,13 +383,13 @@
// 修改
await updateAlarmRule({
...formModel.value,
tags: formModel.value.allEffect === 1 ? '[]' : JSON.stringify(tags.value)
tags: JSON.stringify(tags.value)
});
Message.success('修改成功');
emits('updated');
}
// 清空
handlerClear();
handleClose();
return true;
} catch (e) {
return false;
} finally {
@@ -339,18 +399,23 @@
// 关闭
const handleClose = () => {
handlerClear();
handleClear();
setVisible(false);
};
// 清空
const handlerClear = () => {
const handleClear = () => {
setLoading(false);
};
</script>
<style lang="less" scoped>
:deep(.metrics-selector) {
:deep(.metrics-selector-no-tag) {
width: 100%;
}
:deep(.metrics-selector-has-tag) {
width: calc(100% - 42px);
margin-right: 12px;
}