diff --git a/src/main/java/neatlogic/module/inspect/api/job/CreateInspectAppJobApi.java b/src/main/java/neatlogic/module/inspect/api/job/CreateInspectAppJobApi.java index 166ceaee1d380f0a60333088db84891c4592170e..0d92e742e6d7b8ed29f434c65138c455a9762b35 100644 --- a/src/main/java/neatlogic/module/inspect/api/job/CreateInspectAppJobApi.java +++ b/src/main/java/neatlogic/module/inspect/api/job/CreateInspectAppJobApi.java @@ -25,7 +25,6 @@ import neatlogic.framework.autoexec.crossover.IAutoexecJobActionCrossoverService import neatlogic.framework.autoexec.dao.mapper.AutoexecCombopMapper; import neatlogic.framework.autoexec.dto.combop.AutoexecCombopExecuteConfigVo; import neatlogic.framework.autoexec.dto.combop.AutoexecCombopExecuteNodeConfigVo; -import neatlogic.framework.autoexec.dto.combop.AutoexecCombopVo; import neatlogic.framework.autoexec.dto.job.AutoexecJobVo; import neatlogic.framework.autoexec.job.action.core.AutoexecJobActionHandlerFactory; import neatlogic.framework.autoexec.job.action.core.IAutoexecJobActionHandler; @@ -36,10 +35,14 @@ import neatlogic.framework.cmdb.dto.ci.CiVo; import neatlogic.framework.cmdb.dto.resourcecenter.ResourceSearchVo; import neatlogic.framework.cmdb.dto.resourcecenter.ResourceVo; import neatlogic.framework.cmdb.exception.resourcecenter.AppSystemNotFoundException; +import neatlogic.framework.cmdb.resourcecenter.datasource.core.IResourceCenterDataSource; +import neatlogic.framework.cmdb.resourcecenter.datasource.core.ResourceCenterDataSourceFactory; import neatlogic.framework.common.constvalue.ApiParamType; import neatlogic.framework.crossover.CrossoverServiceFactory; +import neatlogic.framework.exception.core.ApiRuntimeException; import neatlogic.framework.inspect.constvalue.JobSource; import neatlogic.framework.inspect.dao.mapper.InspectMapper; +import neatlogic.framework.inspect.dto.InspectCiCombopVo; import neatlogic.framework.restful.annotation.*; import neatlogic.framework.restful.constvalue.OperationTypeEnum; import neatlogic.framework.restful.core.privateapi.PrivateApiComponentBase; @@ -52,6 +55,8 @@ import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.*; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.stream.Collectors; @Transactional @Service @@ -90,9 +95,13 @@ public class CreateInspectAppJobApi extends PrivateApiComponentBase { if (appSystemVo == null) { throw new AppSystemNotFoundException(appSystemId); } + List inspectStatusList = new ArrayList<>(); + inspectStatusList.add("warn"); + inspectStatusList.add("critical"); + inspectStatusList.add("fatal"); + Set allResourceTypeIdSet = new HashSet<>(); + IResourceCenterDataSource resourceCenterDataSource = ResourceCenterDataSourceFactory.getResourceCenterDataSource(); List searchList = new ArrayList<>(); - List allEnvIdList = new ArrayList<>(); - Set allAppModuleIdSet = new HashSet<>(); JSONArray envList = paramObj.getJSONArray("envList"); for (int i = 0; i < envList.size(); i++) { JSONObject envObj = envList.getJSONObject(i); @@ -103,7 +112,6 @@ public class CreateInspectAppJobApi extends PrivateApiComponentBase { if (envId == null) { continue; } - allEnvIdList.add(envId); JSONArray appModuleIdArray = envObj.getJSONArray("appModuleIdList"); if (CollectionUtils.isEmpty(appModuleIdArray)) { continue; @@ -113,11 +121,17 @@ public class CreateInspectAppJobApi extends PrivateApiComponentBase { if (appModuleId == null) { continue; } - allAppModuleIdSet.add(appModuleId); + Set typeIdSet = new HashSet<>(); + Map> viewName2TypeIdListMap = resourceCenterDataSource.getAppResourceTypeIdListByAppSystemIdAndAppModuleIdAndEnvIdAndInspectStatusList(appSystemId, appModuleId, envId, inspectStatusList); + for (Map.Entry> entry : viewName2TypeIdListMap.entrySet()) { + typeIdSet.addAll(entry.getValue()); + allResourceTypeIdSet.addAll(entry.getValue()); + } ResourceSearchVo searchVo = new ResourceSearchVo(); searchVo.setAppSystemId(appSystemId); searchVo.setAppModuleId(appModuleId); searchVo.setEnvId(envId); + searchVo.setTypeIdList(new ArrayList<>(typeIdSet)); searchList.add(searchVo); } } @@ -125,39 +139,17 @@ public class CreateInspectAppJobApi extends PrivateApiComponentBase { return null; } List autoexecJobList = new ArrayList<>(); - List inspectStatusList = new ArrayList<>(); - Set allResourceTypeIdSet = new HashSet<>(); - inspectStatusList.add("warn"); - inspectStatusList.add("critical"); - inspectStatusList.add("fatal"); - for (ResourceSearchVo searchVo : searchList) { - searchVo.setInspectStatusList(inspectStatusList); - Set resourceTypeIdSet = resourceCrossoverMapper.getResourceTypeIdListByAppSystemIdAndModuleIdAndEnvIdAndInspectStatusList(searchVo); - if (CollectionUtils.isEmpty(resourceTypeIdSet)) { - continue; - } - allResourceTypeIdSet.addAll(resourceTypeIdSet); - List resourceTypeIdList = new ArrayList<>(resourceTypeIdSet); - searchVo.setTypeIdList(resourceTypeIdList); - } Map ciMap = new HashMap<>(); Map ciIdToCombopIdMap = new HashMap<>(); + List combopIdList = new ArrayList<>(); if (CollectionUtils.isNotEmpty(allResourceTypeIdSet)) { ICiCrossoverMapper ciCrossoverMapper = CrossoverServiceFactory.getApi(ICiCrossoverMapper.class); List ciVoList = ciCrossoverMapper.getCiByIdList(new ArrayList<>(allResourceTypeIdSet)); - for (CiVo ciVo : ciVoList) { - Long ciId = ciVo.getId(); - Long combopId = inspectMapper.getCombopIdByCiId(ciId); - if (combopId == null) { - continue; - } - AutoexecCombopVo combopVo = autoexecCombopMapper.getAutoexecCombopById(combopId); - if (combopVo == null) { - continue; - } - ciMap.put(ciId, ciVo); - ciIdToCombopIdMap.put(ciId, combopId); - } + ciMap = ciVoList.stream().filter(Objects::nonNull).collect(Collectors.toMap(CiVo::getId, e -> e)); + List ciCombopList = inspectMapper.searchInspectCiCombopListByCiIdList(new ArrayList<>(allResourceTypeIdSet)); + ciIdToCombopIdMap = ciCombopList.stream().filter(Objects::nonNull).collect(Collectors.toMap(InspectCiCombopVo::getId, InspectCiCombopVo::getCombopId)); + combopIdList = ciCombopList.stream().filter(Objects::nonNull).map(InspectCiCombopVo::getCombopId).filter(Objects::nonNull).collect(Collectors.toList()); + combopIdList = autoexecCombopMapper.checkAutoexecCombopIdListIsExists(combopIdList); } for (ResourceSearchVo searchVo : searchList) { if (CollectionUtils.isEmpty(searchVo.getTypeIdList())) { @@ -168,7 +160,13 @@ public class CreateInspectAppJobApi extends PrivateApiComponentBase { if (combopId == null) { continue; } + if (!combopIdList.contains(combopId)) { + continue; + } CiVo ciVo = ciMap.get(ciId); + if (ciVo == null) { + continue; + } AutoexecJobVo jobVo = new AutoexecJobVo(); jobVo.setRoundCount(64); jobVo.setOperationId(combopId); @@ -180,23 +178,13 @@ public class CreateInspectAppJobApi extends PrivateApiComponentBase { jobVo.setRouteId(appSystemId.toString()); AutoexecCombopExecuteConfigVo executeConfig = new AutoexecCombopExecuteConfigVo(); AutoexecCombopExecuteNodeConfigVo executeNodeConfig = new AutoexecCombopExecuteNodeConfigVo(); - JSONObject filter = paramObj.getJSONObject("filter"); - if (filter == null) { - filter = new JSONObject(); - } - List typeIdList = new ArrayList<>(); - typeIdList.add(ciId); - filter.put("typeIdList", typeIdList); - List envIdList = new ArrayList<>(); - envIdList.add(searchVo.getEnvId()); - filter.put("envIdList", envIdList); - List appModuleIdList = new ArrayList<>(); - appModuleIdList.add(searchVo.getAppModuleId()); - filter.put("appModuleIdList", appModuleIdList); - List appSystemIdList = new ArrayList<>(); - appSystemIdList.add(searchVo.getAppSystemId()); - filter.put("appSystemIdList", appSystemIdList); - executeNodeConfig.setFilter(filter); + JSONObject filter = new JSONObject(); + filter.put("typeId", ciId); + filter.put("envId", searchVo.getEnvId()); + filter.put("appModuleId", searchVo.getAppModuleId()); + filter.put("appSystemId", searchVo.getAppSystemId()); + filter.put("inspectStatusList", inspectStatusList); + executeNodeConfig.setOtherFilter(filter); executeConfig.setExecuteNodeConfig(executeNodeConfig); jobVo.setExecuteConfig(executeConfig); autoexecJobList.add(jobVo); @@ -205,9 +193,10 @@ public class CreateInspectAppJobApi extends PrivateApiComponentBase { if (CollectionUtils.isEmpty(autoexecJobList)) { return null; } - + Queue jobIdQueue = new ConcurrentLinkedQueue<>(); + Queue exceptionMessageQueue = new ConcurrentLinkedQueue<>(); BatchRunner runner = new BatchRunner<>(); - runner.execute(autoexecJobList, 3, (threadIndex, dataIndex, jobVo) -> { + runner.execute(autoexecJobList, 1, (threadIndex, dataIndex, jobVo) -> { try { IAutoexecJobActionCrossoverService autoexecJobActionCrossoverService = CrossoverServiceFactory.getApi(IAutoexecJobActionCrossoverService.class); autoexecJobActionCrossoverService.validateAndCreateJobFromCombop(jobVo); @@ -215,11 +204,18 @@ public class CreateInspectAppJobApi extends PrivateApiComponentBase { jobVo.setAction(JobAction.FIRE.getValue()); jobVo.setIsFirstFire(1); fireAction.doService(jobVo); + jobIdQueue.add(jobVo.getId()); } catch (Exception e) { logger.error(e.getMessage(), e); + exceptionMessageQueue.add(e.getMessage()); } }, "INSPECT-APP-JOB-MULTI-CREATE"); - return null; + if (CollectionUtils.isNotEmpty(exceptionMessageQueue)) { + throw new ApiRuntimeException(String.join(", ", exceptionMessageQueue)); + } + JSONObject resultObj = new JSONObject(); + resultObj.put("jobIdList", jobIdQueue); + return resultObj; } @Override diff --git a/src/main/java/neatlogic/module/inspect/job/node/UpdateNodesByOtherHandler.java b/src/main/java/neatlogic/module/inspect/job/node/UpdateNodesByOtherHandler.java new file mode 100644 index 0000000000000000000000000000000000000000..cd53a546f9da469b51f96d080b131cb494230c3b --- /dev/null +++ b/src/main/java/neatlogic/module/inspect/job/node/UpdateNodesByOtherHandler.java @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2025 深圳极向量科技有限公司 All Rights Reserved. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package neatlogic.module.inspect.job.node; + +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import neatlogic.framework.autoexec.crossover.IAutoexecJobCrossoverService; +import neatlogic.framework.autoexec.dto.combop.AutoexecCombopExecuteNodeConfigVo; +import neatlogic.framework.autoexec.dto.job.AutoexecJobVo; +import neatlogic.framework.autoexec.job.node.IUpdateNodes; +import neatlogic.framework.cmdb.dto.resourcecenter.ResourceSearchVo; +import neatlogic.framework.cmdb.dto.resourcecenter.ResourceVo; +import neatlogic.framework.cmdb.dto.resourcecenter.config.ResourceEntityVo; +import neatlogic.framework.cmdb.resourcecenter.datasource.core.IResourceCenterDataSource; +import neatlogic.framework.cmdb.resourcecenter.datasource.core.ResourceCenterDataSourceFactory; +import neatlogic.framework.crossover.CrossoverServiceFactory; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.collections4.MapUtils; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Component +public class UpdateNodesByOtherHandler implements IUpdateNodes { + + @Override + public boolean update(AutoexecCombopExecuteNodeConfigVo executeNodeConfigVo, AutoexecJobVo jobVo, String userName, Long protocolId) { + boolean isHasNode = false; + if (executeNodeConfigVo == null) { + return false; + } + JSONObject otherFilter = executeNodeConfigVo.getOtherFilter(); + if (MapUtils.isNotEmpty(otherFilter)) { + Long appSystemId = otherFilter.getLong("appSystemId"); + Long appModuleId = otherFilter.getLong("appModuleId"); + Long envId = otherFilter.getLong("envId"); + Long typeId = otherFilter.getLong("typeId"); + JSONArray inspectStatusList = otherFilter.getJSONArray("inspectStatusList"); + ResourceSearchVo searchVo = new ResourceSearchVo(); + searchVo.setAppSystemId(appSystemId); + searchVo.setAppModuleId(appModuleId); + searchVo.setEnvId(envId); + searchVo.setTypeId(typeId); + searchVo.setInspectStatusList(inspectStatusList.toJavaList(String.class)); + IAutoexecJobCrossoverService autoexecJobCrossoverService = CrossoverServiceFactory.getApi(IAutoexecJobCrossoverService.class); + IResourceCenterDataSource resourceCenterDataSource = ResourceCenterDataSourceFactory.getResourceCenterDataSource(); + List appViewList = resourceCenterDataSource.getAppViewList(); + if (CollectionUtils.isNotEmpty(appViewList)) { + for (ResourceEntityVo resourceEntityVo : appViewList) { + searchVo.setViewName(resourceEntityVo.getName()); + searchVo.setCurrentPage(1); + searchVo.setPageSize(100); + List resourceList = resourceCenterDataSource.getAppResourceList(searchVo, true); + if (CollectionUtils.isNotEmpty(resourceList)) { + autoexecJobCrossoverService.updateJobPhaseNode(jobVo, resourceList, userName, protocolId); + isHasNode = true; + for (int currentPage = 2; currentPage <= searchVo.getPageCount(); currentPage++) { + searchVo.setCurrentPage(currentPage); + resourceList = resourceCenterDataSource.getAppResourceList(searchVo, true); + if (CollectionUtils.isNotEmpty(resourceList)) { + autoexecJobCrossoverService.updateJobPhaseNode(jobVo, resourceList, userName, protocolId); + } + } + } + } + } + } + return isHasNode; + } + +}