From 185d1cd9ad1465a07ae6bd935a4ad0e645ea8458 Mon Sep 17 00:00:00 2001 From: bayuzhen Date: Thu, 30 May 2024 15:00:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86=E5=A4=A7=E5=B1=8F=E5=B1=95?= =?UTF-8?q?=E7=A4=BA=E8=B5=84=E6=BA=90=E4=BD=BF=E7=94=A8=E6=83=85=E5=86=B5?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../resource/server/ServerServiceImpl.java | 178 ++++++++++-------- 1 file changed, 98 insertions(+), 80 deletions(-) diff --git a/bocloud.ims.service/src/main/java/com/bocloud/ims/service/resource/server/ServerServiceImpl.java b/bocloud.ims.service/src/main/java/com/bocloud/ims/service/resource/server/ServerServiceImpl.java index 566a313..3acdd42 100644 --- a/bocloud.ims.service/src/main/java/com/bocloud/ims/service/resource/server/ServerServiceImpl.java +++ b/bocloud.ims.service/src/main/java/com/bocloud/ims/service/resource/server/ServerServiceImpl.java @@ -150,88 +150,31 @@ public class ServerServiceImpl implements ServerService { vendorId = Long.valueOf(paramMap.get("vendorId").toString()); } } - CloudVendor vendor = cloudVendorRepository.listById(vendorId); - if (simple) { - int total = this.serverRepository.count(params, labelContent, vendorType); - List beans = this.serverRepository.list(params, sorter, labelContent); - gridBean = new GridBean(1, 1, total, beans); + if (null == vendorId) { + Integer totals = 0; + List vendorList = cloudVendorRepository.queryProxy().col(GenericEntity::getDeleted).eq(0).list(); + List servers = new ArrayList<>(); + for (CloudVendor cloudVendor : vendorList) { + vendorType = cloudVendor.getType(); + int total = this.serverRepository.counts(params, labelContent, vendorType, clusterName, vdcName); + List list = this.serverRepository.list(page, rows, params, sorter, labelContent, vendorType, clusterName, vdcName); + getServerList(rows, params, cloudVendor, list); + servers.addAll(list); + totals += total; + } + gridBean = GridHelper.getBean(page, rows, totals, servers); } else { - int total = this.serverRepository.counts(params, labelContent, vendorType, clusterName, vdcName); - List list = this.serverRepository.list(page, rows, params, sorter, labelContent, vendorType, clusterName, vdcName); - - String azUuid, azName, ratioDesc; - Long cpuUsed, cpuHz; - Double memoryCapacity, memUsed; - ServerConfig config; - Map allAzUuidNameMap = sangforAzService.getAllAzUuidNameMap(); - Map netMap = new HashMap<>(); - if (null != vendor && "CLOUDTOWER".equals(vendor.getType())) { - List collect = list.stream().map(Server::getId).collect(Collectors.toList()); - List networkCardList = networkCardRepository.queryProxy().col(NetworkCard::getHostId).in(collect) - .col(NetworkCard::getCategory).eq("PM") - .col(GenericEntity::getDeleted).eq(false).list(); - netMap = networkCardList.stream().collect(Collectors.toMap(NetworkCard::getHostId, NetworkCard::getInstanceId, (existingValue, newValue) -> newValue)); + CloudVendor vendor = cloudVendorRepository.listById(vendorId); + if (simple) { + int total = this.serverRepository.count(params, labelContent, vendorType); + List beans = this.serverRepository.list(params, sorter, labelContent); + gridBean = new GridBean(1, 1, total, beans); + } else { + int total = this.serverRepository.counts(params, labelContent, vendorType, clusterName, vdcName); + List list = this.serverRepository.list(page, rows, params, sorter, labelContent, vendorType, clusterName, vdcName); + getServerList(rows, params, vendor, list); + gridBean = GridHelper.getBean(page, rows, total, list); } - for (Server server : list) { - if (null != vendor && "CLOUDTOWER".equals(vendor.getType())) { - server.setNicUuid(netMap.get(server.getId())); - } - azUuid = server.getAzUuid(); - if (StringUtils.isNotEmpty(azUuid)) { - azName = allAzUuidNameMap.get(azUuid); - if (StringUtils.isNotEmpty(azName)) { - server.setAzName(azName); - } - } - - List networkCards = networkCardRepository.listByHostIdAndSwitcherAvailable(server.getId(), true); - server.setNetcards(networkCards); - params.clear(); - params.add(new Param(MapTools.simpleMap("hostId", server.getId()), Sign.EQ)); - int vmNum = cloudServerRepository.count(params, 0L); - params.add(new Param(MapTools.simpleMap("isTemplate", 1), Sign.EQ)); - int templateNum = cloudServerRepository.count(params, 0L); - server.setVmNum(vmNum - templateNum); - server.setTemplateNum(templateNum); - if (!StringUtils.isEmpty(server.getResourceGroup())) { - Dictionary dictionary = dictionaryRepository.query(server.getResourceGroup(), "RESOURCE_GROUP"); - if (null != dictionary) { - server.setResourceGroupName(dictionary.getName()); - } - } - // 设置这个物理机的资源池 - if (ServerAssetCategory.X86_SERVER.name().equals(server.getAssetCategory())) { - // 这是一个x86物理机 - server.setGroupName(poolGroupRepository.getGroupNameByResourceTarget(server.getId(), - ResourceCategory.Computer.name(), ResourceCatalog.PHYSICAL.name())); - } - Optional.ofNullable(this.serverConfigRepository.getBySid(server.getId())) - .ifPresent(server::setConfig); - config = server.getConfig(); - - Optional.ofNullable(server.getRole()).ifPresent(role -> { - server.setRoles(new ArrayList<>(Arrays.asList(role.split(",")))); - server.getRoles().remove("nfs_gateway"); - }); - - // 深信服6.8宿主机下拉框增加CPU和内存利用率(非实时数据,以上次同步时候的数据计算) - if ("6.8".equalsIgnoreCase(vendor.getVersion()) && CloudProvider.SANGFOR.name().equals(vendor.getType()) && rows == 9999) { - ratioDesc = "(CPU" + new BigDecimal(server.getCpuRatio()).multiply(new BigDecimal(100)).setScale(0, RoundingMode.HALF_UP).toString() + "%" + "-内存" + new BigDecimal(server.getMemRatio()).multiply(new BigDecimal(100)).setScale(0, RoundingMode.HALF_UP).toString() + "%)"; - server.setName(server.getName() + ratioDesc); - } - // vmware宿主机下拉框增加CPU和内存利用率(非实时数据,以上次同步时候的数据计算) - if (CloudProvider.VMWARE.name().equals(vendor.getType()) && rows >= 9999 && config != null) { - cpuUsed = (config.getCpuUsed() != null ? config.getCpuUsed() : 0L); - cpuHz = (config.getCpuHz() != null ? config.getCpuHz() : 0L); - memUsed = (config.getMemUsed() != null ? config.getMemUsed() : 0D); - memoryCapacity = (config.getMemoryCapacity() != null ? config.getMemoryCapacity() : 0D); - if (cpuHz > 0L && memoryCapacity > 0D) { - ratioDesc = "(CPU" + new BigDecimal(cpuUsed * 1.0D / cpuHz).multiply(new BigDecimal(100)).setScale(0, RoundingMode.HALF_UP).toString() + "%" + "-内存" + new BigDecimal(memUsed / memoryCapacity).multiply(new BigDecimal(100)).setScale(0, RoundingMode.HALF_UP).toString() + "%)"; - server.setName(server.getName() + ratioDesc); - } - } - } - gridBean = GridHelper.getBean(page, rows, total, list); } return new BocloudResult(true, gridBean, "查询成功"); } catch (Exception e) { @@ -240,6 +183,81 @@ public class ServerServiceImpl implements ServerService { } } + private void getServerList(int rows, List params, CloudVendor vendor, List list) throws Exception { + String azUuid, azName, ratioDesc; + Long cpuUsed, cpuHz; + Double memoryCapacity, memUsed; + ServerConfig config; + Map allAzUuidNameMap = sangforAzService.getAllAzUuidNameMap(); + Map netMap = new HashMap<>(); + if (null != vendor && "CLOUDTOWER".equals(vendor.getType())) { + List collect = list.stream().map(Server::getId).collect(Collectors.toList()); + List networkCardList = networkCardRepository.queryProxy().col(NetworkCard::getHostId).in(collect) + .col(NetworkCard::getCategory).eq("PM") + .col(GenericEntity::getDeleted).eq(false).list(); + netMap = networkCardList.stream().collect(Collectors.toMap(NetworkCard::getHostId, NetworkCard::getInstanceId, (existingValue, newValue) -> newValue)); + } + for (Server server : list) { + if (null != vendor && "CLOUDTOWER".equals(vendor.getType())) { + server.setNicUuid(netMap.get(server.getId())); + } + azUuid = server.getAzUuid(); + if (StringUtils.isNotEmpty(azUuid)) { + azName = allAzUuidNameMap.get(azUuid); + if (StringUtils.isNotEmpty(azName)) { + server.setAzName(azName); + } + } + + List networkCards = networkCardRepository.listByHostIdAndSwitcherAvailable(server.getId(), true); + server.setNetcards(networkCards); + params.clear(); + params.add(new Param(MapTools.simpleMap("hostId", server.getId()), Sign.EQ)); + int vmNum = cloudServerRepository.count(params, 0L); + params.add(new Param(MapTools.simpleMap("isTemplate", 1), Sign.EQ)); + int templateNum = cloudServerRepository.count(params, 0L); + server.setVmNum(vmNum - templateNum); + server.setTemplateNum(templateNum); + if (!StringUtils.isEmpty(server.getResourceGroup())) { + Dictionary dictionary = dictionaryRepository.query(server.getResourceGroup(), "RESOURCE_GROUP"); + if (null != dictionary) { + server.setResourceGroupName(dictionary.getName()); + } + } + // 设置这个物理机的资源池 + if (ServerAssetCategory.X86_SERVER.name().equals(server.getAssetCategory())) { + // 这是一个x86物理机 + server.setGroupName(poolGroupRepository.getGroupNameByResourceTarget(server.getId(), + ResourceCategory.Computer.name(), ResourceCatalog.PHYSICAL.name())); + } + Optional.ofNullable(this.serverConfigRepository.getBySid(server.getId())) + .ifPresent(server::setConfig); + config = server.getConfig(); + + Optional.ofNullable(server.getRole()).ifPresent(role -> { + server.setRoles(new ArrayList<>(Arrays.asList(role.split(",")))); + server.getRoles().remove("nfs_gateway"); + }); + + // 深信服6.8宿主机下拉框增加CPU和内存利用率(非实时数据,以上次同步时候的数据计算) + if ("6.8".equalsIgnoreCase(vendor.getVersion()) && CloudProvider.SANGFOR.name().equals(vendor.getType()) && rows == 9999) { + ratioDesc = "(CPU" + new BigDecimal(server.getCpuRatio()).multiply(new BigDecimal(100)).setScale(0, RoundingMode.HALF_UP).toString() + "%" + "-内存" + new BigDecimal(server.getMemRatio()).multiply(new BigDecimal(100)).setScale(0, RoundingMode.HALF_UP).toString() + "%)"; + server.setName(server.getName() + ratioDesc); + } + // vmware宿主机下拉框增加CPU和内存利用率(非实时数据,以上次同步时候的数据计算) + if (CloudProvider.VMWARE.name().equals(vendor.getType()) && rows >= 9999 && config != null) { + cpuUsed = (config.getCpuUsed() != null ? config.getCpuUsed() : 0L); + cpuHz = (config.getCpuHz() != null ? config.getCpuHz() : 0L); + memUsed = (config.getMemUsed() != null ? config.getMemUsed() : 0D); + memoryCapacity = (config.getMemoryCapacity() != null ? config.getMemoryCapacity() : 0D); + if (cpuHz > 0L && memoryCapacity > 0D) { + ratioDesc = "(CPU" + new BigDecimal(cpuUsed * 1.0D / cpuHz).multiply(new BigDecimal(100)).setScale(0, RoundingMode.HALF_UP).toString() + "%" + "-内存" + new BigDecimal(memUsed / memoryCapacity).multiply(new BigDecimal(100)).setScale(0, RoundingMode.HALF_UP).toString() + "%)"; + server.setName(server.getName() + ratioDesc); + } + } + } + } + @Override public BocloudResult modify(String params, RequestContext context) { return new BocloudResult(false, ServerService.class.getSimpleName() + "的modify方法未实现");