diff options
| author | tanghao <[email protected]> | 2021-12-14 18:16:28 +0800 |
|---|---|---|
| committer | tanghao <[email protected]> | 2021-12-14 18:16:28 +0800 |
| commit | c5fbaf09736ae37ed374717da5e5622b9a2f02c2 (patch) | |
| tree | 2a5e3fa61a2bde766defaaa4a507a13ac95db358 | |
| parent | e5b2312ca861d7c04b2e37deb2a0adb0fea5b1f6 (diff) | |
feat: NEZ-1389 endpoint 状态 优化nz-v22.02-2021-12-14nz-v22.02-2021-12-14
12 files changed, 238 insertions, 15 deletions
diff --git a/nz-admin/src/main/java/com/nis/common/utils/Constant.java b/nz-admin/src/main/java/com/nis/common/utils/Constant.java index 2c11a53d..6f905996 100644 --- a/nz-admin/src/main/java/com/nis/common/utils/Constant.java +++ b/nz-admin/src/main/java/com/nis/common/utils/Constant.java @@ -1148,4 +1148,10 @@ public class Constant { public static final String[] IMPORTFILE_TYPE_INTVALUES = {"1", "2", "3"}; public static final String[] IMPORTFILE_TYPE_STRVALUES = {"xlsx", "xls", "csv", "json"}; + + public static final String ENDPOINT_METRICS_STATE_CACHE_KEY = "endpoint_metrics_state"; + + public static final String ENDPOINT_LOGS_STATE_CACHE_KEY = "endpoint_logs_state"; + + public static final String ASSET_TALON_STATUS_CACHE_KEY = "asset_talon_status"; } diff --git a/nz-admin/src/main/java/com/nis/modules/asset/entity/AssetTalonStatus.java b/nz-admin/src/main/java/com/nis/modules/asset/entity/AssetTalonStatus.java index 7489b33d..02862f06 100644 --- a/nz-admin/src/main/java/com/nis/modules/asset/entity/AssetTalonStatus.java +++ b/nz-admin/src/main/java/com/nis/modules/asset/entity/AssetTalonStatus.java @@ -25,6 +25,7 @@ public class AssetTalonStatus implements Serializable { private String msg; + @TableField(exist = false) private Date lastUpdate; @TableField(exist = false) diff --git a/nz-admin/src/main/java/com/nis/modules/asset/job/AssetTalonStatusJob.java b/nz-admin/src/main/java/com/nis/modules/asset/job/AssetTalonStatusJob.java index 482040e5..e64d31f2 100644 --- a/nz-admin/src/main/java/com/nis/modules/asset/job/AssetTalonStatusJob.java +++ b/nz-admin/src/main/java/com/nis/modules/asset/job/AssetTalonStatusJob.java @@ -9,6 +9,7 @@ import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; import java.util.function.Function; import java.util.stream.Collectors; @@ -22,9 +23,12 @@ import org.springframework.data.redis.core.RedisTemplate; import org.springframework.scheduling.quartz.QuartzJobBean; import org.springframework.transaction.annotation.Transactional; +import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.nis.common.utils.Constant; import com.nis.common.utils.R; +import com.nis.common.utils.ToolUtil; import com.nis.modules.agent.util.AgentUtil; import com.nis.modules.asset.entity.AssetAsset; import com.nis.modules.asset.entity.AssetTalonStatus; @@ -190,9 +194,34 @@ public class AssetTalonStatusJob extends QuartzJobBean { } talonUpdateStatusList.addAll(abnormalTalonList); - talonStatusService.remove(null); - talonStatusService.saveBatch(talonUpdateStatusList); - + + //判断查询数据与缓存中变化的信息 + Map assetTalonStatuss = redisTemplate.opsForHash().entries(Constant.ASSET_TALON_STATUS_CACHE_KEY); + if(ToolUtil.isNotEmpty(assetTalonStatuss)) { + for(int i=talonUpdateStatusList.size()-1;i>=0;i--) { + AssetTalonStatus assetTalonStatus = talonUpdateStatusList.get(i); + if(assetTalonStatuss.containsKey(assetTalonStatus.getAssetId().toString())) { + AssetTalonStatus cacheEndpointState = JSON.parseObject((String)assetTalonStatuss.get(assetTalonStatus.getAssetId().toString()),AssetTalonStatus.class); + redisTemplate.opsForHash().put(Constant.ASSET_TALON_STATUS_CACHE_KEY, assetTalonStatus.getAssetId().toString(), JSON.toJSONString(assetTalonStatus)); + if(cacheEndpointState.getStatus().equals(assetTalonStatus.getStatus())) { + // 如果状态相同则只更新缓存中的数据信息 + talonUpdateStatusList.remove(i); + } + }else{ + // 数据库存在 缓存中不存在 + redisTemplate.opsForHash().put(Constant.ASSET_TALON_STATUS_CACHE_KEY, assetTalonStatus.getAssetId().toString(), JSON.toJSONString(assetTalonStatus)); + } + } + }else { + for(AssetTalonStatus assetTalonStatus:talonUpdateStatusList) { + redisTemplate.opsForHash().put(Constant.ASSET_TALON_STATUS_CACHE_KEY, assetTalonStatus.getAssetId().toString(), JSON.toJSONString(assetTalonStatus)); + } + } + // 超时周期设置为两个endpointState周期 + redisTemplate.expire(Constant.ASSET_TALON_STATUS_CACHE_KEY, 2, TimeUnit.MINUTES); + if(ToolUtil.isNotEmpty(talonUpdateStatusList)) { + talonStatusService.saveOrUpdateBatch(talonUpdateStatusList); + } Map<Integer, AssetTalonStatus> map = talonUpdateStatusList.stream().collect(Collectors.toMap(AssetTalonStatus::getAssetId, Function.identity())); // 更新 endpoint state 表数据 @@ -224,8 +253,47 @@ public class AssetTalonStatusJob extends QuartzJobBean { } logStateList.add(new MonitorEndpointState(conf.getEndpointId(), conf.getId(), conf.getType(), state, talonStatus.getVersion(), new Date())); } - // 更新 endpoint state - endpointStateDao.deleteConfStateByType(Constant.MonitorConfType.LOGS.getValue()); - endpointStateService.saveBatch(logStateList); + //判断查询数据与缓存中变化的信息 + Map endpointStates = redisTemplate.opsForHash().entries(Constant.ENDPOINT_LOGS_STATE_CACHE_KEY); + if(ToolUtil.isNotEmpty(endpointStates)) { + for(int i=logStateList.size()-1;i>=0;i--) { + MonitorEndpointState endpointState = logStateList.get(i); + if(endpointStates.containsKey(endpointState.getEndpointId().toString())) { + MonitorEndpointState cacheEndpointState = JSON.parseObject((String)endpointStates.get(endpointState.getEndpointId().toString()),MonitorEndpointState.class); + redisTemplate.opsForHash().put(Constant.ENDPOINT_LOGS_STATE_CACHE_KEY, endpointState.getEndpointId().toString(), JSON.toJSONString(endpointState)); + if(cacheEndpointState.getState().equals(endpointState.getState())) { + // 如果状态相同则只更新缓存中的数据信息 + logStateList.remove(i); + } + }else{ + // 数据库存在 缓存中不存在 + redisTemplate.opsForHash().put(Constant.ENDPOINT_LOGS_STATE_CACHE_KEY, endpointState.getEndpointId().toString(), JSON.toJSONString(endpointState)); + } + } + }else { + for(MonitorEndpointState endpointState:logStateList) { + redisTemplate.opsForHash().put(Constant.ENDPOINT_LOGS_STATE_CACHE_KEY, endpointState.getEndpointId().toString(), JSON.toJSONString(endpointState)); + } + } + // 超时周期设置为两个endpointState周期 + redisTemplate.expire(Constant.ENDPOINT_LOGS_STATE_CACHE_KEY, 2, TimeUnit.MINUTES); + // 保存 根据类型以及endpointId进行新增或者修改 + if(ToolUtil.isNotEmpty(logStateList)) { + // 将数据分为新增或者修改 + List<Integer> endpointIds = logStateList.stream().map(MonitorEndpointState::getEndpointId).collect(Collectors.toList()); + List<MonitorEndpointState> existStates = endpointStateService.list(new LambdaQueryWrapper<MonitorEndpointState>() + .in(MonitorEndpointState::getEndpointId, endpointIds).eq(MonitorEndpointState::getType, Constant.MonitorConfType.LOGS.getValue())); + List<Integer> existIds = existStates.stream().map(MonitorEndpointState::getEndpointId).collect(Collectors.toList()); + List<MonitorEndpointState> batchUpdateStates = logStateList.stream().filter(s->existIds.contains(s.getEndpointId())).collect(Collectors.toList()); + List<MonitorEndpointState> batchAddStates = logStateList.stream().filter(s->!existIds.contains(s.getEndpointId())).collect(Collectors.toList()); + if(ToolUtil.isNotEmpty(batchAddStates)) { + log.debug("Endpoint state batch add datas info : %s ",JSON.toJSON(batchAddStates)); + endpointStateService.saveBatch(batchAddStates); + } + if(ToolUtil.isNotEmpty(batchUpdateStates)) { + log.debug("Endpoint state batch update datas info : %s ",JSON.toJSON(batchAddStates)); + endpointStateService.modifyBatchEndpointState(batchUpdateStates, Constant.MonitorConfType.LOGS.getValue()); + } + } } } diff --git a/nz-admin/src/main/java/com/nis/modules/asset/service/impl/AssetAssetServiceImpl.java b/nz-admin/src/main/java/com/nis/modules/asset/service/impl/AssetAssetServiceImpl.java index 6ac0a3b5..f6123d8e 100644 --- a/nz-admin/src/main/java/com/nis/modules/asset/service/impl/AssetAssetServiceImpl.java +++ b/nz-admin/src/main/java/com/nis/modules/asset/service/impl/AssetAssetServiceImpl.java @@ -154,6 +154,9 @@ public class AssetAssetServiceImpl extends ServiceImpl<AssetAssetDao, AssetAsset @Autowired private BasicImportAndExportServices basicImportAndExportServices; + + @Autowired + private AssetTalonStatusService assetTalonStatusService; @SuppressWarnings("unchecked") @Override @@ -948,6 +951,9 @@ public class AssetAssetServiceImpl extends ServiceImpl<AssetAssetDao, AssetAsset assetPingService.remove(new QueryWrapper<AssetPing>().lambda().eq(AssetPing::getAssetId, asset.getId())); redisTemplate.opsForHash().delete(Constant.SYSCONFIG_KEY_ASSET_PING, asset.getId().toString()); + // 修改后处理asset talon status数据 删除缓存和数据库中的数据 + assetTalonStatusService.removeById(asset.getId()); + redisTemplate.opsForHash().delete(Constant.ASSET_TALON_STATUS_CACHE_KEY, asset.getId().toString()); return asset.getId(); } @@ -1024,10 +1030,10 @@ public class AssetAssetServiceImpl extends ServiceImpl<AssetAssetDao, AssetAsset alertMessageService.removeByAssetIds(idList); // 删除 assetPing + List<String> deleteList = idList.stream().map(id-> id.toString()).collect(Collectors.toList()); if (CollectionUtils.isNotEmpty(delAssetHosts)) { assetPingService.remove(new LambdaQueryWrapper<AssetPing>().in(AssetPing::getHost, delAssetHosts)); // 删除缓存中的数据 - List<String> deleteList = idList.stream().map(id-> id.toString()).collect(Collectors.toList()); redisTemplate.opsForHash().delete(Constant.SYSCONFIG_KEY_ASSET_PING, deleteList.toArray()); } @@ -1042,6 +1048,10 @@ public class AssetAssetServiceImpl extends ServiceImpl<AssetAssetDao, AssetAsset } // 删除 关联通过 chart temp 创建的 charts chartService.remove(new LambdaQueryWrapper<VisualChart>().eq(VisualChart::getVarType, 1).in(VisualChart::getVarId, idList)); + + // 删除assetTalonStatus中的数据以及缓存中的数据信息 + assetTalonStatusService.removeByIds(idList); + redisTemplate.opsForHash().delete(Constant.ASSET_TALON_STATUS_CACHE_KEY, deleteList.toArray()); } /*@Override diff --git a/nz-admin/src/main/java/com/nis/modules/endpoint/dao/MonitorEndpointStateDao.java b/nz-admin/src/main/java/com/nis/modules/endpoint/dao/MonitorEndpointStateDao.java index 3853b0fe..6081256a 100644 --- a/nz-admin/src/main/java/com/nis/modules/endpoint/dao/MonitorEndpointStateDao.java +++ b/nz-admin/src/main/java/com/nis/modules/endpoint/dao/MonitorEndpointStateDao.java @@ -2,6 +2,9 @@ package com.nis.modules.endpoint.dao; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.nis.modules.endpoint.entity.MonitorEndpointState; + +import java.util.List; + import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; @@ -10,4 +13,5 @@ public interface MonitorEndpointStateDao extends BaseMapper<MonitorEndpointState void deleteConfStateByType(@Param("type") String type); + void updateBatchEndpointState(@Param("datas")List<MonitorEndpointState> datas,@Param("type")String type); } diff --git a/nz-admin/src/main/java/com/nis/modules/endpoint/entity/MonitorEndpointState.java b/nz-admin/src/main/java/com/nis/modules/endpoint/entity/MonitorEndpointState.java index 800faff6..db0d0e2f 100644 --- a/nz-admin/src/main/java/com/nis/modules/endpoint/entity/MonitorEndpointState.java +++ b/nz-admin/src/main/java/com/nis/modules/endpoint/entity/MonitorEndpointState.java @@ -1,6 +1,7 @@ package com.nis.modules.endpoint.entity; import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.AllArgsConstructor; @@ -32,6 +33,7 @@ public class MonitorEndpointState implements Serializable { private Integer ver; + @TableField(exist = false) private Date time; } diff --git a/nz-admin/src/main/java/com/nis/modules/endpoint/job/EndpointStateJob.java b/nz-admin/src/main/java/com/nis/modules/endpoint/job/EndpointStateJob.java index 5b8020ad..d497b568 100644 --- a/nz-admin/src/main/java/com/nis/modules/endpoint/job/EndpointStateJob.java +++ b/nz-admin/src/main/java/com/nis/modules/endpoint/job/EndpointStateJob.java @@ -2,9 +2,12 @@ package com.nis.modules.endpoint.job; import cn.hutool.core.util.StrUtil; import cn.hutool.log.Log; + +import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.nis.common.utils.Constant; +import com.nis.common.utils.ToolUtil; import com.nis.modules.endpoint.dao.MonitorEndpointStateDao; import com.nis.modules.endpoint.entity.MonitorEndpointConfig; import com.nis.modules.endpoint.entity.MonitorEndpointState; @@ -23,6 +26,7 @@ import org.springframework.scheduling.quartz.QuartzJobBean; import org.springframework.transaction.annotation.Transactional; import java.util.*; +import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; @DisallowConcurrentExecution @@ -70,7 +74,8 @@ public class EndpointStateJob extends QuartzJobBean { /** * 更新 endpoint 状态 */ - @Transactional(rollbackFor = Exception.class) + @SuppressWarnings("unlikely-arg-type") + @Transactional(rollbackFor = Exception.class) public void updateEndpointStatus() { // 随机获取一个 Global Prometheus Promserver promserver = promserverService.getOneAvaPromServer(null, null, Constant.PromserverType.GLOBAL.getValue()); @@ -117,12 +122,52 @@ public class EndpointStateJob extends QuartzJobBean { downEndpointStateList.add(new MonitorEndpointState(conf.getEndpointId(), conf.getId(), conf.getType(), state, conf.getVersion(), new Date())); } - List allState = new ArrayList(); + List<MonitorEndpointState> allState = new ArrayList<MonitorEndpointState>(); allState.addAll(upMetricsEndpointStateList); allState.addAll(downEndpointStateList); - // 保存 - endpointStateDao.deleteConfStateByType(Constant.MonitorConfType.METRICS.getValue()); - endpointStateService.saveBatch(allState); + + //判断查询数据与缓存中变化的信息 + Map endpointStates = redisTemplate.opsForHash().entries(Constant.ENDPOINT_METRICS_STATE_CACHE_KEY); + if(ToolUtil.isNotEmpty(endpointStates)) { + for(int i=allState.size()-1;i>=0;i--) { + MonitorEndpointState endpointState = allState.get(i); + if(endpointStates.containsKey(endpointState.getEndpointId().toString())) { + MonitorEndpointState cacheEndpointState = JSON.parseObject((String)endpointStates.get(endpointState.getEndpointId().toString()),MonitorEndpointState.class); + redisTemplate.opsForHash().put(Constant.ENDPOINT_METRICS_STATE_CACHE_KEY, endpointState.getEndpointId().toString(), JSON.toJSONString(endpointState)); + if(cacheEndpointState.getState().equals(endpointState.getState())) { + // 如果状态相同则只更新缓存中的数据信息 + allState.remove(i); + } + }else{ + // 数据库存在 缓存中不存在 + redisTemplate.opsForHash().put(Constant.ENDPOINT_METRICS_STATE_CACHE_KEY, endpointState.getEndpointId().toString(), JSON.toJSONString(endpointState)); + } + } + }else { + for(MonitorEndpointState endpointState:allState) { + redisTemplate.opsForHash().put(Constant.ENDPOINT_METRICS_STATE_CACHE_KEY, endpointState.getEndpointId().toString(), JSON.toJSONString(endpointState)); + } + } + // 超时周期设置为两个endpointState周期 + redisTemplate.expire(Constant.ENDPOINT_METRICS_STATE_CACHE_KEY, 2, TimeUnit.MINUTES); + // 保存 根据类型以及endpointId进行新增或者修改 + if(ToolUtil.isNotEmpty(allState)) { + // 将数据分为新增或者修改 + List<Integer> endpointIds = allState.stream().map(MonitorEndpointState::getEndpointId).collect(Collectors.toList()); + List<MonitorEndpointState> existStates = endpointStateService.list(new LambdaQueryWrapper<MonitorEndpointState>() + .in(MonitorEndpointState::getEndpointId, endpointIds).eq(MonitorEndpointState::getType, Constant.MonitorConfType.METRICS.getValue())); + List<Integer> existIds = existStates.stream().map(MonitorEndpointState::getEndpointId).collect(Collectors.toList()); + List<MonitorEndpointState> batchUpdateStates = allState.stream().filter(s->existIds.contains(s.getEndpointId())).collect(Collectors.toList()); + List<MonitorEndpointState> batchAddStates = allState.stream().filter(s->!existIds.contains(s.getEndpointId())).collect(Collectors.toList()); + if(ToolUtil.isNotEmpty(batchAddStates)) { + log.debug("Endpoint state batch add datas info : %s ",JSON.toJSON(batchAddStates)); + endpointStateService.saveBatch(batchAddStates); + } + if(ToolUtil.isNotEmpty(batchUpdateStates)) { + log.debug("Endpoint state batch update datas info : %s ",JSON.toJSON(batchAddStates)); + endpointStateService.modifyBatchEndpointState(batchUpdateStates, Constant.MonitorConfType.METRICS.getValue()); + } + } } /** diff --git a/nz-admin/src/main/java/com/nis/modules/endpoint/service/MonitorEndpointStateService.java b/nz-admin/src/main/java/com/nis/modules/endpoint/service/MonitorEndpointStateService.java index 90f2de9b..3340f8e8 100644 --- a/nz-admin/src/main/java/com/nis/modules/endpoint/service/MonitorEndpointStateService.java +++ b/nz-admin/src/main/java/com/nis/modules/endpoint/service/MonitorEndpointStateService.java @@ -1,8 +1,11 @@ package com.nis.modules.endpoint.service; +import java.util.List; + import com.baomidou.mybatisplus.extension.service.IService; import com.nis.modules.endpoint.entity.MonitorEndpointState; public interface MonitorEndpointStateService extends IService<MonitorEndpointState> { - + + void modifyBatchEndpointState(List<MonitorEndpointState> datas,String type); } diff --git a/nz-admin/src/main/java/com/nis/modules/endpoint/service/impl/MonitorEndpointServiceImpl.java b/nz-admin/src/main/java/com/nis/modules/endpoint/service/impl/MonitorEndpointServiceImpl.java index 7a4c76ef..d2d629c3 100644 --- a/nz-admin/src/main/java/com/nis/modules/endpoint/service/impl/MonitorEndpointServiceImpl.java +++ b/nz-admin/src/main/java/com/nis/modules/endpoint/service/impl/MonitorEndpointServiceImpl.java @@ -6,6 +6,8 @@ import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ReUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.log.Log; + +import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONException; import com.alibaba.fastjson.JSONObject; @@ -21,6 +23,7 @@ import com.nis.modules.alert.dao.AlertMessageDao; import com.nis.modules.alert.entity.AlertMessageEntity; import com.nis.modules.alert.service.AlertMessageService; import com.nis.modules.asset.entity.AssetAsset; +import com.nis.modules.asset.entity.AssetPing; import com.nis.modules.asset.service.AssetAssetService; import com.nis.modules.endpoint.dao.MonitorEndpointDao; import com.nis.modules.endpoint.entity.MonitorEndpoint; @@ -46,6 +49,7 @@ import com.nis.modules.sys.service.SysConfigService; import com.nis.modules.sys.shiro.ShiroUtils; import org.apache.commons.collections.CollectionUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.stereotype.Service; import org.springframework.transaction.TransactionStatus; @@ -109,6 +113,9 @@ public class MonitorEndpointServiceImpl extends ServiceImpl<MonitorEndpointDao, @Autowired private DataSourceTransactionManager dataSourceTransactionManager; + @Autowired + private RedisTemplate redisTemplate; + @Override public MonitorEndpoint queryEndpointEntity(Integer id) { MonitorEndpoint endpoint = this.getById(id); @@ -133,12 +140,34 @@ public class MonitorEndpointServiceImpl extends ServiceImpl<MonitorEndpointDao, endpoint.setAlertNum(tempAlert.size()); } + Map endpointMetricsStates = redisTemplate.opsForHash().entries(Constant.ENDPOINT_METRICS_STATE_CACHE_KEY); + Map endpointLogsStates = redisTemplate.opsForHash().entries(Constant.ENDPOINT_LOGS_STATE_CACHE_KEY); + // configs List<MonitorEndpointConfig> endpointConfigs = endpointConfigService.list(new LambdaQueryWrapper<MonitorEndpointConfig>().eq(MonitorEndpointConfig::getEndpointId, endpoint.getId())); for (MonitorEndpointConfig endpointConfig : endpointConfigs) { endpointConfig.setVer(endpointConfig.getVersion()); - MonitorEndpointState endpointStateByConfigId = endpointStateService.getOne(new LambdaQueryWrapper<MonitorEndpointState>().eq(MonitorEndpointState::getConfigId, endpointConfig.getId())); + MonitorEndpointState endpointStateByConfigId = null; + if(endpointConfig.getType().equals(Constant.MonitorConfType.METRICS.getValue())) { + if(ToolUtil.isNotEmpty(endpointMetricsStates)) { + Object object = endpointMetricsStates.get(endpointConfig.getEndpointId().toString()); + if(ToolUtil.isNotEmpty(object)) { + endpointStateByConfigId = JSON.parseObject((String)object,MonitorEndpointState.class); + } + } + }else { + if(ToolUtil.isNotEmpty(endpointLogsStates)) { + Object object = endpointLogsStates.get(endpointConfig.getEndpointId().toString()); + if(ToolUtil.isNotEmpty(object)) { + endpointStateByConfigId = JSON.parseObject((String)object,MonitorEndpointState.class); + } + } + } + // 缓存中未取到值时 从数据库中获取 + if(endpointStateByConfigId == null) { + endpointStateByConfigId = endpointStateService.getOne(new LambdaQueryWrapper<MonitorEndpointState>().eq(MonitorEndpointState::getConfigId, endpointConfig.getId())); + } if (endpointStateByConfigId == null) continue; endpointConfig.setState(endpointStateByConfigId.getState()); @@ -271,12 +300,34 @@ public class MonitorEndpointServiceImpl extends ServiceImpl<MonitorEndpointDao, private void handleResuleList(List<MonitorEndpoint> endpointList) { List<MonitorEndpointConfig> endpointConfigs = endpointConfigService.list(); + Map endpointMetricsStates = redisTemplate.opsForHash().entries(Constant.ENDPOINT_METRICS_STATE_CACHE_KEY); + Map endpointLogsStates = redisTemplate.opsForHash().entries(Constant.ENDPOINT_LOGS_STATE_CACHE_KEY); + for (MonitorEndpoint endpoint : endpointList) { List<MonitorEndpointConfig> configsByEndpointId = endpointConfigs.stream().filter(endpointConfig -> endpoint.getId().equals(endpointConfig.getEndpointId())).collect(Collectors.toList()); for (MonitorEndpointConfig endpointConfig : configsByEndpointId) { endpointConfig.setVer(endpointConfig.getVersion()); - MonitorEndpointState endpointStateByConfigId = endpointStateService.getOne(new LambdaQueryWrapper<MonitorEndpointState>().eq(MonitorEndpointState::getConfigId, endpointConfig.getId())); + MonitorEndpointState endpointStateByConfigId = null; + if(endpointConfig.getType().equals(Constant.MonitorConfType.METRICS.getValue())) { + if(ToolUtil.isNotEmpty(endpointMetricsStates)) { + Object object = endpointMetricsStates.get(endpointConfig.getEndpointId().toString()); + if(ToolUtil.isNotEmpty(object)) { + endpointStateByConfigId = JSON.parseObject((String)object,MonitorEndpointState.class); + } + } + }else { + if(ToolUtil.isNotEmpty(endpointLogsStates)) { + Object object = endpointLogsStates.get(endpointConfig.getEndpointId().toString()); + if(ToolUtil.isNotEmpty(object)) { + endpointStateByConfigId = JSON.parseObject((String)object,MonitorEndpointState.class); + } + } + } + // 缓存中未取到值时 从数据库中获取 + if(endpointStateByConfigId == null) { + endpointStateByConfigId = endpointStateService.getOne(new LambdaQueryWrapper<MonitorEndpointState>().eq(MonitorEndpointState::getConfigId, endpointConfig.getId())); + } if (endpointStateByConfigId == null) continue; endpointConfig.setState(endpointStateByConfigId.getState()); @@ -450,6 +501,9 @@ public class MonitorEndpointServiceImpl extends ServiceImpl<MonitorEndpointDao, // 删除 endpoint state endpointStateService.removeByIds(ids); + List<String> deleteList = ids.stream().map(id-> id.toString()).collect(Collectors.toList()); + redisTemplate.opsForHash().delete(Constant.ENDPOINT_METRICS_STATE_CACHE_KEY, deleteList.toArray()); + redisTemplate.opsForHash().delete(Constant.ENDPOINT_LOGS_STATE_CACHE_KEY, deleteList.toArray()); // 删除 alert alertMessageService.removeByEndpointIds(ids); @@ -1131,6 +1185,11 @@ public class MonitorEndpointServiceImpl extends ServiceImpl<MonitorEndpointDao, // 保存配置信息 this.saveOrUpdateEndpointConfigs(endpoint, module); + + // 修改完成删除 endpoint state数据 删除缓存中的数据 + endpointStateService.removeById(endpoint.getId()); + redisTemplate.opsForHash().delete(Constant.ENDPOINT_METRICS_STATE_CACHE_KEY, endpoint.getId().toString()); + redisTemplate.opsForHash().delete(Constant.ENDPOINT_LOGS_STATE_CACHE_KEY, endpoint.getId().toString()); return endpoint.getId(); } diff --git a/nz-admin/src/main/java/com/nis/modules/endpoint/service/impl/MonitorEndpointServiceStateImpl.java b/nz-admin/src/main/java/com/nis/modules/endpoint/service/impl/MonitorEndpointServiceStateImpl.java index 5747e2c7..76b2a706 100644 --- a/nz-admin/src/main/java/com/nis/modules/endpoint/service/impl/MonitorEndpointServiceStateImpl.java +++ b/nz-admin/src/main/java/com/nis/modules/endpoint/service/impl/MonitorEndpointServiceStateImpl.java @@ -4,9 +4,17 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.nis.modules.endpoint.dao.MonitorEndpointStateDao; import com.nis.modules.endpoint.entity.MonitorEndpointState; import com.nis.modules.endpoint.service.MonitorEndpointStateService; + +import java.util.List; + import org.springframework.stereotype.Service; @Service public class MonitorEndpointServiceStateImpl extends ServiceImpl<MonitorEndpointStateDao, MonitorEndpointState> implements MonitorEndpointStateService { + @Override + public void modifyBatchEndpointState(List<MonitorEndpointState> datas, String type) { + this.baseMapper.updateBatchEndpointState(datas,type); + } + } diff --git a/nz-admin/src/main/resources/db/V2021.12.13__delete column alert talon status monitor endpoint state.sql b/nz-admin/src/main/resources/db/V2021.12.13__delete column alert talon status monitor endpoint state.sql new file mode 100644 index 00000000..c2cb0aac --- /dev/null +++ b/nz-admin/src/main/resources/db/V2021.12.13__delete column alert talon status monitor endpoint state.sql @@ -0,0 +1,9 @@ + +/** + * 2021年12月13日 + * 删除 alert talon status 和 monitor endpoint state 表中的时间字段 + */ + +ALTER TABLE `asset_talon_status` DROP COLUMN `last_update`; + +ALTER TABLE `monitor_endpoint_state` DROP COLUMN `time`;
\ No newline at end of file diff --git a/nz-admin/src/main/resources/mapper/endpoint/MonitorEndpointStateDao.xml b/nz-admin/src/main/resources/mapper/endpoint/MonitorEndpointStateDao.xml index a3d751e2..925e6ea9 100644 --- a/nz-admin/src/main/resources/mapper/endpoint/MonitorEndpointStateDao.xml +++ b/nz-admin/src/main/resources/mapper/endpoint/MonitorEndpointStateDao.xml @@ -7,4 +7,12 @@ DELETE FROM monitor_endpoint_state WHERE type = #{type}; </delete> + <update id="updateBatchEndpointState"> + <foreach collection="datas" separator=";" item="data"> + update monitor_endpoint_state set state=#{data.state}, + config_id = #{data.configId}, + ver = #{data.ver} + where endpoint_id = #{data.endpointId} and type = #{type} + </foreach> + </update> </mapper> |
