🔨 修改标签查询逻辑.

This commit is contained in:
lijiahangmax
2025-10-07 16:06:00 +08:00
parent 1d5c579e64
commit 963cd0b227
15 changed files with 97 additions and 112 deletions

View File

@@ -48,10 +48,10 @@ public interface HostAgentApi {
/**
* 获取缓存名称
*
* @param agentKeyList agentKeyList
* @param agentKeys agentKeys
* @return nameMap
*/
Map<String, String> getNameCacheByAgentKey(List<String> agentKeyList);
Map<String, String> getNameCacheByAgentKey(List<String> agentKeys);
/**
* 获取缓存名称

View File

@@ -103,11 +103,11 @@ public class HostAgentApiImpl implements HostAgentApi {
}
@Override
public Map<String, String> getNameCacheByAgentKey(List<String> agentKeyList) {
public Map<String, String> getNameCacheByAgentKey(List<String> agentKeys) {
Map<String, String> result = new HashMap<>();
List<String> queryList = new ArrayList<>();
// 查询缓存
for (String agentKey : agentKeyList) {
for (String agentKey : agentKeys) {
HostBaseDTO host = AGENT_HOST_CACHE.get(agentKey);
if (host != null) {
result.put(agentKey, host.getName());

View File

@@ -92,14 +92,14 @@ public interface HostDAO extends IMapper<HostDO> {
/**
* 更新探针信息
*
* @param keys agentKeyList
* @param update update
* @param agentKeys agentKeys
* @param update update
* @return effect
*/
default int updateByAgentKeys(List<String> keys, HostDO update) {
default int updateByAgentKeys(List<String> agentKeys, HostDO update) {
update.setUpdateTime(new Date());
// 更新
return this.update(update, Conditions.in(HostDO::getAgentKey, keys));
return this.update(update, Conditions.in(HostDO::getAgentKey, agentKeys));
}
}

View File

@@ -212,25 +212,25 @@ public class HostAgentEndpointServiceImpl implements HostAgentEndpointService {
/**
* 标记在线状态
*
* @param agentKeyList agentKeyList
* @param status status
* @param agentKeys agentKeys
* @param status status
*/
private void markOnlineStatus(List<String> agentKeyList, AgentOnlineStatusEnum status) {
if (Lists.isEmpty(agentKeyList)) {
private void markOnlineStatus(List<String> agentKeys, AgentOnlineStatusEnum status) {
if (Lists.isEmpty(agentKeys)) {
return;
}
log.info("HostAgentEndpointService mark {}. count: {}, keys: {}", status, agentKeyList.size(), agentKeyList);
log.info("HostAgentEndpointService mark {}. count: {}, keys: {}", status, agentKeys.size(), agentKeys);
// 更新数据
HostDO update = HostDO.builder()
.agentOnlineStatus(status.getValue())
.agentOnlineChangeTime(new Date())
.build();
int effect = hostDAO.updateByAgentKeys(agentKeyList, update);
int effect = hostDAO.updateByAgentKeys(agentKeys, update);
// 更新缓存
agentKeyList.forEach(s -> ONLINE_STATUS_CACHE.put(s, status.getValue()));
agentKeys.forEach(s -> ONLINE_STATUS_CACHE.put(s, status.getValue()));
log.info("HostAgentEndpointService mark {}. effect: {}", status, effect);
// 插入日志
List<HostAgentLogDO> logList = hostDAO.selectIdByAgentKeys(agentKeyList)
List<HostAgentLogDO> logList = hostDAO.selectIdByAgentKeys(agentKeys)
.stream()
.map(s -> {
HostAgentLogDO agentLog = HostAgentLogDO.builder()
@@ -250,7 +250,7 @@ public class HostAgentEndpointServiceImpl implements HostAgentEndpointService {
}
// 发送已下线事件
if (AgentOnlineStatusEnum.OFFLINE.equals(status)) {
SpringHolder.publishEvent(new AgentOfflineEvent(agentKeyList));
SpringHolder.publishEvent(new AgentOfflineEvent(agentKeys));
}
}

View File

@@ -38,10 +38,7 @@ import org.dromara.visor.framework.web.core.annotation.RestWrapper;
import org.dromara.visor.module.monitor.define.operator.MonitorHostOperatorType;
import org.dromara.visor.module.monitor.engine.MonitorContext;
import org.dromara.visor.module.monitor.entity.dto.AgentMetricsDataDTO;
import org.dromara.visor.module.monitor.entity.request.host.MonitorHostChartRequest;
import org.dromara.visor.module.monitor.entity.request.host.MonitorHostQueryRequest;
import org.dromara.visor.module.monitor.entity.request.host.MonitorHostSwitchUpdateRequest;
import org.dromara.visor.module.monitor.entity.request.host.MonitorHostUpdateRequest;
import org.dromara.visor.module.monitor.entity.request.host.*;
import org.dromara.visor.module.monitor.entity.vo.MonitorHostMetricsDataVO;
import org.dromara.visor.module.monitor.entity.vo.MonitorHostVO;
import org.dromara.visor.module.monitor.service.MonitorHostService;
@@ -95,7 +92,7 @@ public class MonitorHostController {
@Operation(summary = "查询监控指标")
@PreAuthorize("@ss.hasPermission('monitor:monitor-host:query')")
public List<MonitorHostMetricsDataVO> getMonitorHostMetrics(@Validated(Key.class) @RequestBody MonitorHostQueryRequest request) {
return monitorHostService.getMonitorHostMetrics(request.getAgentKeyList());
return monitorHostService.getMonitorHostMetrics(request.getAgentKeys());
}
@IgnoreLog(IgnoreLogMode.RET)
@@ -107,14 +104,11 @@ public class MonitorHostController {
}
@IgnoreLog(IgnoreLogMode.RET)
@GetMapping("/host-tags")
@PostMapping("/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);
public List<String> getMonitorHostTags(@RequestBody MonitorHostQueryTagRequest request) {
return monitorHostService.getMonitorHostTags(request);
}
@DemoDisableApi

View File

@@ -46,7 +46,7 @@ public class MonitorHostQueryRequest extends BaseQueryRequest {
@NotEmpty(groups = Key.class)
@Schema(description = "agentKey")
private List<String> agentKeyList;
private List<String> agentKeys;
@Schema(description = "搜索")
private String searchValue;

View File

@@ -20,29 +20,34 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.visor.module.monitor.define.cache;
package org.dromara.visor.module.monitor.entity.request.host;
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 io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.dromara.visor.common.entity.BaseQueryRequest;
import java.util.concurrent.TimeUnit;
import java.util.List;
/**
* 监控主机缓存 key
* 监控主机标签 查询请求对象
*
* @author Jiahang Li
* @version 1.0.0
* @since 2025-9-14 00:12
* @since 2025-8-14 16:27
*/
public interface MonitorHostCacheKeyDefine {
@Data
@EqualsAndHashCode(callSuper = true)
@Schema(name = "MonitorHostQueryTagRequest", description = "监控主机标签 查询请求对象")
public class MonitorHostQueryTagRequest extends BaseQueryRequest {
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();
@Schema(description = "数据集")
private String measurement;
@Schema(description = "策略id")
private Long policyId;
@Schema(description = "agentKey")
private List<String> agentKeys;
}

View File

@@ -24,10 +24,7 @@ package org.dromara.visor.module.monitor.service;
import cn.orionsec.kit.lang.define.wrapper.DataGrid;
import org.dromara.visor.common.entity.chart.TimeChartSeries;
import org.dromara.visor.module.monitor.entity.request.host.MonitorHostChartRequest;
import org.dromara.visor.module.monitor.entity.request.host.MonitorHostQueryRequest;
import org.dromara.visor.module.monitor.entity.request.host.MonitorHostSwitchUpdateRequest;
import org.dromara.visor.module.monitor.entity.request.host.MonitorHostUpdateRequest;
import org.dromara.visor.module.monitor.entity.request.host.*;
import org.dromara.visor.module.monitor.entity.vo.MonitorHostMetricsDataVO;
import org.dromara.visor.module.monitor.entity.vo.MonitorHostVO;
@@ -53,10 +50,10 @@ public interface MonitorHostService {
/**
* 获取监控主机指标数据
*
* @param agentKeyList agentKeyList
* @param agentKeys agentKeys
* @return metrics
*/
List<MonitorHostMetricsDataVO> getMonitorHostMetrics(List<String> agentKeyList);
List<MonitorHostMetricsDataVO> getMonitorHostMetrics(List<String> agentKeys);
/**
* 获取监控主机图表数据
@@ -67,13 +64,12 @@ public interface MonitorHostService {
List<TimeChartSeries> getMonitorHostChart(MonitorHostChartRequest request);
/**
* 查询监控告警规则主机标签
* 查询监控告警标签
*
* @param policyId policyId
* @param measurement measurement
* @param request request
* @return tags
*/
List<String> getMonitorHostPolicyRuleTags(Long policyId, String measurement);
List<String> getMonitorHostTags(MonitorHostQueryTagRequest request);
/**
* 更新监控主机

View File

@@ -35,12 +35,10 @@ 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;
@@ -148,11 +146,6 @@ 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) {

View File

@@ -40,7 +40,6 @@ 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;
@@ -52,15 +51,11 @@ 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;
import org.dromara.visor.module.monitor.entity.dto.*;
import org.dromara.visor.module.monitor.entity.request.host.MonitorHostChartRequest;
import org.dromara.visor.module.monitor.entity.request.host.MonitorHostQueryRequest;
import org.dromara.visor.module.monitor.entity.request.host.MonitorHostSwitchUpdateRequest;
import org.dromara.visor.module.monitor.entity.request.host.MonitorHostUpdateRequest;
import org.dromara.visor.module.monitor.entity.request.host.*;
import org.dromara.visor.module.monitor.entity.vo.MonitorHostMetricsDataVO;
import org.dromara.visor.module.monitor.entity.vo.MonitorHostVO;
import org.dromara.visor.module.monitor.enums.AlarmSwitchEnum;
@@ -76,7 +71,6 @@ 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;
/**
* 监控主机 服务实现类
@@ -191,8 +185,8 @@ public class MonitorHostServiceImpl implements MonitorHostService {
}
@Override
public List<MonitorHostMetricsDataVO> getMonitorHostMetrics(List<String> agentKeyList) {
return agentKeyList.stream()
public List<MonitorHostMetricsDataVO> getMonitorHostMetrics(List<String> agentKeys) {
return agentKeys.stream()
.map(s -> this.getHostMetricsData(s, null))
.collect(Collectors.toList());
}
@@ -239,25 +233,11 @@ public class MonitorHostServiceImpl implements MonitorHostService {
}
@Override
public List<String> getMonitorHostPolicyRuleTags(Long policyId, String measurement) {
MeasurementEnum measurementEnum = MeasurementEnum.of(measurement);
public List<String> getMonitorHostTags(MonitorHostQueryTagRequest request) {
MeasurementEnum measurementEnum = MeasurementEnum.of(request.getMeasurement());
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)) {
@@ -269,15 +249,25 @@ public class MonitorHostServiceImpl implements MonitorHostService {
} else {
return Collections.emptyList();
}
List<String> tags = metas.stream()
// 查询监控主机元数据
List<MonitorHostMetaDTO> metas = monitorHostDAO.of()
.createValidateWrapper()
.eq(MonitorHostDO::getPolicyId, request.getPolicyId())
.in(MonitorHostDO::getAgentKey, request.getAgentKeys())
.then()
.stream()
.map(MonitorHostDO::getMonitorMeta)
.filter(Objects::nonNull)
.map(s -> JSON.parseObject(s, MonitorHostMetaDTO.class))
.filter(Objects::nonNull)
.collect(Collectors.toList());
// 获取 tag
return 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
@@ -317,14 +307,6 @@ 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);