diff --git a/bocloud.ams.booter/src/main/java/com/bocloud/ams/booter/controller/module/ModuleController.java b/bocloud.ams.booter/src/main/java/com/bocloud/ams/booter/controller/module/ModuleController.java index 169650f..d468149 100644 --- a/bocloud.ams.booter/src/main/java/com/bocloud/ams/booter/controller/module/ModuleController.java +++ b/bocloud.ams.booter/src/main/java/com/bocloud/ams/booter/controller/module/ModuleController.java @@ -189,4 +189,18 @@ public class ModuleController { return moduleService.modifyProperty(model, id, context); } + /** + * 首页统计模型总数-服务器、虚拟机、网络设备、安全设别 + * + * @param model + * @param id + * @param context + * @return + */ + @PutMapping(value = "/statistics") + @Operation(summary = "首页统计模型总数") + public GeneralResult statistics(@Value(Common.REQ_CONTEXT) RequestContext context) { + return moduleService.statistics(context); + } + } diff --git a/bocloud.ams.repository/src/main/java/com/bocloud/ams/repository/module/ModuleGroupRepository.java b/bocloud.ams.repository/src/main/java/com/bocloud/ams/repository/module/ModuleGroupRepository.java index 4d5f191..561ab4d 100644 --- a/bocloud.ams.repository/src/main/java/com/bocloud/ams/repository/module/ModuleGroupRepository.java +++ b/bocloud.ams.repository/src/main/java/com/bocloud/ams/repository/module/ModuleGroupRepository.java @@ -36,6 +36,11 @@ public class ModuleGroupRepository extends BasicGenericDao { String sql = "select * from ci_module_group where is_deleted =0 and parent_id =0 ORDER BY gmt_create DESC "; return this.list(ModuleGroup.class, sql, null); } + public List queryRootInName(List nameList) throws Exception { + String sql = "select * from ci_module_group where is_deleted =0 and parent_id =0 and name in (:name) ORDER BY gmt_create DESC "; + Map params = MapTools.simpleMap("name", nameList); + return this.list(ModuleGroup.class, sql, params); + } public List queryChildren(Long id) throws Exception { String sql = "select * from ci_module_group where is_deleted =0 and parent_id = :id ORDER BY gmt_create DESC "; diff --git a/bocloud.ams.service/src/main/java/com/bocloud/ams/service/internal/CmpInternalService.java b/bocloud.ams.service/src/main/java/com/bocloud/ams/service/internal/CmpInternalService.java new file mode 100644 index 0000000..4333921 --- /dev/null +++ b/bocloud.ams.service/src/main/java/com/bocloud/ams/service/internal/CmpInternalService.java @@ -0,0 +1,57 @@ +package com.bocloud.ams.service.internal; + + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.bocloud.cmp.boot.model.BocloudToken; +import com.megatron.common.model.GeneralResult; +import com.megatron.framework.core.CurrentService; +import com.megatron.framework.http.core.ServiceFactory; +import com.megatron.framework.http.model.RemoteService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.annotation.Autowired; +import com.megatron.framework.http.core.HttpMethod; +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.Map; + +@Service +@Slf4j +public class CmpInternalService implements InitializingBean { + + + private Map headers = new HashMap<>(4); + private static final com.megatron.framework.core.Service SERVICE = com.megatron.framework.core.Service.create("cmp"); + + + @Autowired + private CurrentService currentService; + @Autowired + private ServiceFactory serviceFactory; + + + + public GeneralResult list(Map params) { + String url = "/v1/vms"; + log.info("params:{}", JSON.toJSONString(params)); + RemoteService remoteService = serviceFactory.build(SERVICE, url, HttpMethod.GET, headers, params); + GeneralResult result = remoteService.invoke(); + log.info("result:{}",JSON.toJSONString(result)); + if (result.isFailed()) { + return new GeneralResult<>(false, result.getMessage()); + } + String data = JSONObject.toJSONString(result.getData()); + return new GeneralResult<>(true, JSONObject.parseObject(data), "success"); + } + + + + @Override + public void afterPropertiesSet() throws Exception { + + this.headers.put(BocloudToken.INTERNAL_TOKEN, currentService.getToken()); + this.headers.put("source", currentService.getService().getName()); + } +} diff --git a/bocloud.ams.service/src/main/java/com/bocloud/ams/service/module/ModuleService.java b/bocloud.ams.service/src/main/java/com/bocloud/ams/service/module/ModuleService.java index bbf6306..abdc232 100644 --- a/bocloud.ams.service/src/main/java/com/bocloud/ams/service/module/ModuleService.java +++ b/bocloud.ams.service/src/main/java/com/bocloud/ams/service/module/ModuleService.java @@ -35,4 +35,6 @@ public interface ModuleService { GeneralResult> propertyList(Long moduleId, Pager pager); GeneralResult modifyProperty(PropertyModel model, Long id, RequestContext context); + + GeneralResult statistics(RequestContext context); } diff --git a/bocloud.ams.service/src/main/java/com/bocloud/ams/service/module/impl/ModuleServiceImpl.java b/bocloud.ams.service/src/main/java/com/bocloud/ams/service/module/impl/ModuleServiceImpl.java index 6b92ff2..a343ce5 100644 --- a/bocloud.ams.service/src/main/java/com/bocloud/ams/service/module/impl/ModuleServiceImpl.java +++ b/bocloud.ams.service/src/main/java/com/bocloud/ams/service/module/impl/ModuleServiceImpl.java @@ -14,6 +14,9 @@ import java.util.concurrent.TimeUnit; import java.util.regex.Pattern; import java.util.stream.Collectors; +import com.alibaba.fastjson.JSONObject; +import com.bocloud.ams.service.internal.CmpInternalService; +import com.megatron.common.utils.MapTools; import org.apache.commons.beanutils.BeanUtilsBean; import org.apache.commons.beanutils.locale.LocaleBeanUtils; import org.apache.commons.collections.CollectionUtils; @@ -107,6 +110,8 @@ public class ModuleServiceImpl implements ModuleService { private CollectionComponentRepository collectionComponentRepository; @Autowired private ModuleGroupRepository moduleGroupRepository; + @Autowired + private CmpInternalService cmpInternalService; @Override public GeneralResult> getModelList(Pager pager) { @@ -118,23 +123,23 @@ public class ModuleServiceImpl implements ModuleService { List params = pager.getParams(); List> collectMap = params.stream().map(Param::getParam).collect(Collectors.toList()); Map map = - collectMap.stream().filter(item -> item.containsKey("flag")).findFirst().orElse(null); + collectMap.stream().filter(item -> item.containsKey("flag")).findFirst().orElse(null); if (null != map) { Param newParam = null; Iterator iterator = params.iterator(); while (iterator.hasNext()) { Map parMap = iterator.next().getParam(); if (parMap.containsKey("moduleGroupId") && parMap.containsKey("flag")) { - Long groupId = Long.valueOf((Integer)parMap.get("moduleGroupId")); + Long groupId = Long.valueOf((Integer) parMap.get("moduleGroupId")); if (parMap.get("flag").equals(true)) { // 递归获取所有子groupId List groups = moduleGroupRepository.all(); Map> groupMap = - groups.stream().collect(Collectors.groupingBy(ModuleGroup::getParentId)); + groups.stream().collect(Collectors.groupingBy(ModuleGroup::getParentId)); List groupIds = new ArrayList<>(); this.getGroups(groupIds, groupId, groupMap); List groupIdList = - groupIds.stream().map(String::valueOf).collect(Collectors.toList()); + groupIds.stream().map(String::valueOf).collect(Collectors.toList()); String ids = String.join(",", groupIdList); Map groupIdMap = new HashMap<>(); groupIdMap.put("moduleGroupId", ids); @@ -160,7 +165,7 @@ public class ModuleServiceImpl implements ModuleService { if (CollectionUtils.isNotEmpty(groups)) { for (CiModule module : modules) { ModuleGroup group = groups.stream().filter(item -> item.getId().equals(module.getGroupId())) - .findFirst().orElse(null); + .findFirst().orElse(null); if (null != group) { module.setParentGroupId(group.getParentId()); } @@ -180,7 +185,7 @@ public class ModuleServiceImpl implements ModuleService { try { List modules = moduleRepository.all(); List list = modules.stream().filter(item -> ModuleConstant.STATUS_ENABLE.equals(item.getStatus())) - .collect(Collectors.toList()); + .collect(Collectors.toList()); return new GeneralResult<>(true, list, "查询成功!"); } catch (Exception e) { log.error("query module is error:", e); @@ -252,12 +257,12 @@ public class ModuleServiceImpl implements ModuleService { return new GeneralResult<>(false, "新增模型属性分组失败"); } Property id = new Property("uuid", "唯一标识", PropertyType.TEXTFIELD.name(), group.getName(), 100, true, true, - 1, module.getId(), true, false, false, target, PropertyConstant.DefaultValueType.INNERFUN, - PropertyConstant.GLOBALLY_UNIQUE_IDENTIFIER, true, false); + 1, module.getId(), true, false, false, target, PropertyConstant.DefaultValueType.INNERFUN, + PropertyConstant.GLOBALLY_UNIQUE_IDENTIFIER, true, false); // name设置为标题列 Property name = new Property("name", "名称", PropertyType.TEXTFIELD.name(), group.getName(), 100, true, true, - 2, module.getId(), true, false, true, target, PropertyConstant.DefaultValueType.FIXED, null, false, - true); + 2, module.getId(), true, false, true, target, PropertyConstant.DefaultValueType.FIXED, null, false, + true); properties.add(id); properties.add(name); propertyRepository.batchSave(properties, 10); @@ -323,11 +328,11 @@ public class ModuleServiceImpl implements ModuleService { List configs = topologyConfigRepository.listAll(); // 获取拓扑路径涉及到的模型数据 List modules = configs.stream().filter(item -> StringUtils.isNotEmpty(item.getTopologyPath())) - .flatMap(config -> Arrays.stream(config.getTopologyPath().split(","))).distinct() - .map(item -> Long.valueOf(item)).collect(Collectors.toList()); + .flatMap(config -> Arrays.stream(config.getTopologyPath().split(","))).distinct() + .map(item -> Long.valueOf(item)).collect(Collectors.toList()); List ignores = configs.stream().filter(item -> StringUtils.isNotEmpty(item.getIgnoreModuleIds())) - .flatMap(config -> Arrays.stream(config.getIgnoreModuleIds().split(","))).distinct() - .map(item -> Long.valueOf(item)).collect(Collectors.toList()); + .flatMap(config -> Arrays.stream(config.getIgnoreModuleIds().split(","))).distinct() + .map(item -> Long.valueOf(item)).collect(Collectors.toList()); modules.addAll(ignores); modules.removeAll(Collections.singleton(null)); // 移除所有的null元素 if (modules.contains(id)) { @@ -419,8 +424,47 @@ public class ModuleServiceImpl implements ModuleService { } } + @Override + public GeneralResult statistics(RequestContext context) { + try { + List nameList = Arrays.asList("网络设备", "物理服务器", "安全设备"); + //查询根节点 + List rootGroups = moduleGroupRepository.queryRootInName(nameList); + log.info("moduleGroups:{}", JSON.toJSONString("moduleGroups")); + + //查询出所有根节点下的子节点 + + List rootGroupIds = rootGroups.stream().map(ModuleGroup::getId).collect(Collectors.toList()); + // 获取模型分组所有数据 + List moduleGroups = moduleGroupRepository.all(); + // 获取根节点的子节点集合 + List children = moduleGroups.stream().filter(item -> rootGroupIds.contains(item.getParentId())) + .collect(Collectors.toList()); + log.info("children:{}", JSON.toJSONString(children)); + + //查询云主机总数 + Pager pager = new Pager(); + pager.setPage(1); + pager.setRows(Integer.MAX_VALUE); + List paramList = new ArrayList<>(); + Param param = new Param(); + param.setParam(MapTools.simpleMap("isTemplate",false)); + param.setSign(Sign.EQ); + paramList.add(param); + pager.setParams(paramList); + GeneralResult list = cmpInternalService.list(pager.toMap()); + log.info("list:{}", JSON.toJSONString(list)); + + + return new GeneralResult<>(true, "查询成功"); + } catch (Exception e) { + log.info("查询失败", e); + return new GeneralResult<>(false, "查询失败"); + } + } + private GeneralResult baseModifyProperty(Long moduleId, List groups, List properties, - Long userId) throws Exception { + Long userId) throws Exception { CiModule module = moduleRepository.query(moduleId); if (module == null) { return GeneralResult.FAILED("修改属性对应的配置模型不存在"); @@ -435,15 +479,15 @@ public class ModuleServiceImpl implements ModuleService { List oldGroups = propertyGroupRepository.listByModuleId(moduleId); // 1-add:新增property数据 以及对应的propertyItem数据 List addProperties = - properties.stream().filter(item -> null == item.getId()).collect(Collectors.toList()); + properties.stream().filter(item -> null == item.getId()).collect(Collectors.toList()); // 3-update:更新property数据(不允许从池表中引入的数据被更新) 以及对应的propertyItem数据 List updateProperties = properties.stream() - .filter(item -> null != item.getId()).collect(Collectors.toList()); + .filter(item -> null != item.getId()).collect(Collectors.toList()); // 2-delete:删除去掉的property数据 以及对应的propertyItem数据 List inputPropertyIds = - properties.stream().filter(item -> null != item.getId()).map(Property::getId).collect(Collectors.toList()); + properties.stream().filter(item -> null != item.getId()).map(Property::getId).collect(Collectors.toList()); List deleteProperties = oldProperties.stream() - .filter(item -> !inputPropertyIds.contains(item.getId())).collect(Collectors.toList()); + .filter(item -> !inputPropertyIds.contains(item.getId())).collect(Collectors.toList()); // 1-delete:删除去掉的property数据 以及 对应的propertyItem数据 if (CollectionUtils.isNotEmpty(deleteProperties)) { // 先校验是否存在被其他模型引用的属性,如果存在就不允许删除 @@ -451,7 +495,7 @@ public class ModuleServiceImpl implements ModuleService { List linkModuleIds = propertyRepository.queryByLinkModuleId(moduleId); if (CollectionUtils.isNotEmpty(linkModuleIds)) { long count = linkModuleIds.stream().map(Property::getPropertyIds).map(item -> item.split(",")) - .flatMap(item -> Arrays.stream(item)).filter(item -> deletePropertyIds.contains(item)).count(); + .flatMap(item -> Arrays.stream(item)).filter(item -> deletePropertyIds.contains(item)).count(); if (count > 0) { return GeneralResult.FAILED("删除属性失败,存在其他模型引用此属性"); } @@ -460,7 +504,7 @@ public class ModuleServiceImpl implements ModuleService { List collectionComponents = collectionComponentRepository.queryByModuleId(moduleId); if (CollectionUtils.isNotEmpty(collectionComponents)) { long count = collectionComponents.stream().map(CollectionComponent::getPropertyId) - .filter(item -> deletePropertyIds.contains(item)).count(); + .filter(item -> deletePropertyIds.contains(item)).count(); if (count > 0) { return GeneralResult.FAILED("删除属性失败,存在属性被采集插件使用"); } @@ -480,7 +524,7 @@ public class ModuleServiceImpl implements ModuleService { // 根据需要删除的property数据删除对应的propertyItem数据 List propertyIds = deleteProperties.stream().map(Property::getId).collect(Collectors.toList()); List deleteItems = oldItems.stream() - .filter(item -> propertyIds.contains(item.getPropertyId())).collect(Collectors.toList()); + .filter(item -> propertyIds.contains(item.getPropertyId())).collect(Collectors.toList()); if (CollectionUtils.isNotEmpty(deleteItems)) { deleteItems.stream().forEach(item -> { item.setGmtModify(new Date()); @@ -522,7 +566,7 @@ public class ModuleServiceImpl implements ModuleService { } // 3-update:更新数据 for (Property property : updateProperties) { - if(null != property.getPropertyPoolId()){ + if (null != property.getPropertyPoolId()) { property.setGmtModify(new Date()); property.setMenderId(userId); propertyRepository.update(property); @@ -530,7 +574,7 @@ public class ModuleServiceImpl implements ModuleService { } // 获取新增item数据 List addItems = new ArrayList<>(); - if (!Common.ONE.equals(property.getTableType())){ + if (!Common.ONE.equals(property.getTableType())) { addItems = property.getItemList().stream().filter(item -> null == item.getId()).collect(Collectors.toList()); addItems.stream().forEach(item -> { item.setModuleId(property.getModuleId()); @@ -546,22 +590,23 @@ public class ModuleServiceImpl implements ModuleService { // 获取更新的item数据 List updateItems = new ArrayList<>(); List updateItemIds = new ArrayList<>(); - if (!Common.ONE.equals(property.getTableType())){ + if (!Common.ONE.equals(property.getTableType())) { updateItems = property.getItemList().stream().filter(item -> null != item.getId()).collect(Collectors.toList()); updateItems.stream().forEach(item -> { item.setMenderId(userId); if (CollectionUtils.isNotEmpty(item.getItemList())) { // 表格属性中结构项数据的下拉选项数据 转成 JSON 存储 item.setItemValue(JSON.toJSONString(item.getItemList())); - } updateItemIds.add(item.getId()); + } + updateItemIds.add(item.getId()); }); } // 根据当前属性id查询出之前的item List updateOldItems = oldItems.stream() - .filter(item -> item.getPropertyId().equals(property.getId())).collect(Collectors.toList()); + .filter(item -> item.getPropertyId().equals(property.getId())).collect(Collectors.toList()); // 获取需要删除的item数据(就是原始的减去现在的) List deleteItems = updateOldItems.stream() - .filter(item -> !updateItemIds.contains(item.getId())).collect(Collectors.toList()); + .filter(item -> !updateItemIds.contains(item.getId())).collect(Collectors.toList()); deleteItems.stream().forEach(item -> { item.setGmtModify(new Date()); item.setMenderId(userId); @@ -588,13 +633,13 @@ public class ModuleServiceImpl implements ModuleService { // 获取需要新增的group数据 if (CollectionUtils.isNotEmpty(groups)) { List addGroups = - groups.stream().filter(item -> null == item.getId()).collect(Collectors.toList()); + groups.stream().filter(item -> null == item.getId()).collect(Collectors.toList()); if (CollectionUtils.isNotEmpty(addGroups)) { propertyGroupRepository.batchSave(addGroups, addGroups.size()); } // 获取需要更新的group数据 List updateGroups = - groups.stream().filter(item -> null != item.getId()).collect(Collectors.toList()); + groups.stream().filter(item -> null != item.getId()).collect(Collectors.toList()); if (CollectionUtils.isNotEmpty(updateGroups)) { updateGroups.stream().forEach(item -> { item.setMenderId(userId); @@ -604,7 +649,7 @@ public class ModuleServiceImpl implements ModuleService { List groupIds = updateGroups.stream().map(PropertyGroup::getId).collect(Collectors.toList()); // 获取需要删除的数据 List deleteGroups = - oldGroups.stream().filter(item -> !groupIds.contains(item.getId())).collect(Collectors.toList()); + oldGroups.stream().filter(item -> !groupIds.contains(item.getId())).collect(Collectors.toList()); if (CollectionUtils.isNotEmpty(deleteGroups)) { deleteGroups.stream().forEach(item -> { item.setModuleId(userId); @@ -636,7 +681,7 @@ public class ModuleServiceImpl implements ModuleService { } code = code.trim(); Pattern compile = Pattern.compile( - "^(create|alter|select|update|union|and|or|delete|insert|trancate|char|substr|commit|ascii|declare|exec|count|show|into|drop|execute)$"); + "^(create|alter|select|update|union|and|or|delete|insert|trancate|char|substr|commit|ascii|declare|exec|count|show|into|drop|execute)$"); if (compile.matcher(code).find()) { throw new Exception("属性编码不能是关键字:" + code); } @@ -658,7 +703,7 @@ public class ModuleServiceImpl implements ModuleService { List items = property.getItemList(); if (CollectionUtils.isNotEmpty(items)) { if (property.getType().equals(PropertyConstant.PropertyType.MULTILINETABLE) - || property.getType().equals(PropertyConstant.PropertyType.SINGLELINETABLE)) { + || property.getType().equals(PropertyConstant.PropertyType.SINGLELINETABLE)) { Map itemNameMap = new HashMap<>(); Map itemCodeMap = new HashMap<>(); List tableItems = property.getItemList(); @@ -681,9 +726,9 @@ public class ModuleServiceImpl implements ModuleService { } itemCodeMap.put(itemCode, itemCode); if (tableItem.getType().equals(PropertyConstant.PropertyType.SINGLESELECT) - || tableItem.getType().equals(PropertyConstant.PropertyType.MULTISELECT) - || tableItem.getType().equals(PropertyConstant.PropertyType.RADIO) - || tableItem.getType().equals(PropertyConstant.PropertyType.CHECKBOX)) { + || tableItem.getType().equals(PropertyConstant.PropertyType.MULTISELECT) + || tableItem.getType().equals(PropertyConstant.PropertyType.RADIO) + || tableItem.getType().equals(PropertyConstant.PropertyType.CHECKBOX)) { validateItemValues(tableItem); } } @@ -754,7 +799,7 @@ public class ModuleServiceImpl implements ModuleService { } private void copyProperties(Property property, PropertyPool pool) - throws IllegalAccessException, InvocationTargetException { + throws IllegalAccessException, InvocationTargetException { Long id = property.getId(); BeanUtilsBean.getInstance().copyProperties(property, pool); property.setId(id);