From 49af0211e3998c79339abf4cf79a1c0dcfc38cab Mon Sep 17 00:00:00 2001 From: guoyuqi Date: Tue, 15 Oct 2024 19:08:35 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E7=B3=BB=E7=BB=9F=E7=AE=A1=E7=90=86):=20?= =?UTF-8?q?=E8=A1=A5=E5=85=85=E8=B5=84=E6=BA=90=E6=B1=A0=E5=89=A9=E4=BD=99?= =?UTF-8?q?=E5=B9=B6=E5=8F=91=E6=95=B0=E6=95=B0=E6=8D=AE=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/dto/pool/TestResourcePoolDTO.java | 2 +- .../service/TestResourcePoolService.java | 49 +++++++++++++++---- .../TestResourcePoolControllerTests.java | 5 -- 3 files changed, 40 insertions(+), 16 deletions(-) diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/dto/pool/TestResourcePoolDTO.java b/backend/services/system-setting/src/main/java/io/metersphere/system/dto/pool/TestResourcePoolDTO.java index 51f29bf840..fe081424a2 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/dto/pool/TestResourcePoolDTO.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/dto/pool/TestResourcePoolDTO.java @@ -19,7 +19,7 @@ public class TestResourcePoolDTO extends TestResourcePool { private int maxConcurrentNumber; @Schema(description = "剩余并发数") - private Boolean lastConcurrentNumber;; + private int lastConcurrentNumber;; @Schema(description = "组织名称集合") private List orgNames; diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/TestResourcePoolService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/TestResourcePoolService.java index cd9c4248bc..3a824e751b 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/TestResourcePoolService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/TestResourcePoolService.java @@ -3,11 +3,10 @@ package io.metersphere.system.service; import io.metersphere.sdk.constants.HttpMethodConstants; import io.metersphere.sdk.constants.OperationLogConstants; import io.metersphere.sdk.constants.ResourcePoolTypeEnum; +import io.metersphere.sdk.dto.pool.ResourcePoolNodeMetric; import io.metersphere.sdk.exception.MSException; -import io.metersphere.sdk.util.BeanUtils; -import io.metersphere.sdk.util.CommonBeanFactory; -import io.metersphere.sdk.util.JSON; -import io.metersphere.sdk.util.Translator; +import io.metersphere.sdk.util.*; +import io.metersphere.system.controller.handler.ResultHolder; import io.metersphere.system.domain.*; import io.metersphere.system.dto.pool.*; import io.metersphere.system.dto.sdk.OptionDTO; @@ -17,6 +16,7 @@ import io.metersphere.system.log.constants.OperationLogType; import io.metersphere.system.log.dto.LogDTO; import io.metersphere.system.mapper.*; import io.metersphere.system.uid.IDGenerator; +import io.metersphere.system.utils.TaskRunnerClient; import jakarta.annotation.Resource; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.BooleanUtils; @@ -48,6 +48,8 @@ public class TestResourcePoolService { @Resource private ExtResourcePoolMapper extResourcePoolMapper; + private final static String poolControllerUrl = "http://%s:%s/metric"; + public void checkAndSaveOrgRelation(TestResourcePool testResourcePool, String id, TestResourceDTO testResourceDTO) { //防止前端传入的应用组织为空 @@ -111,7 +113,7 @@ public class TestResourcePoolService { if (CollectionUtils.isEmpty(testResourceDTO.getNodesList())) { testResourceDTO.setNodesList(new ArrayList<>()); } - if (StringUtils.equalsIgnoreCase(testResourcePool.getType(), ResourcePoolTypeEnum.NODE.getName())){ + if (StringUtils.equalsIgnoreCase(testResourcePool.getType(), ResourcePoolTypeEnum.NODE.getName())) { TestResourcePoolValidateService testResourcePoolValidateService = CommonBeanFactory.getBean(TestResourcePoolValidateService.class); if (testResourcePoolValidateService != null) { testResourcePoolValidateService.validateNodeList(testResourceDTO.getNodesList()); @@ -155,21 +157,48 @@ public class TestResourcePoolService { testResourcePoolDTO.setOrgNames(orgNameList); } //获取最大并发 - if (StringUtils.equalsIgnoreCase(pool.getType(),ResourcePoolTypeEnum.NODE.getName())) { + if (StringUtils.equalsIgnoreCase(pool.getType(), ResourcePoolTypeEnum.NODE.getName())) { + int maxConcurrentNumber = 0; int concurrentNumber = 0; + int occupiedConcurrentNumber = 0; for (TestResourceNodeDTO testResourceNodeDTO : testResourceDTO.getNodesList()) { - concurrentNumber = concurrentNumber+testResourceNodeDTO.getConcurrentNumber(); + maxConcurrentNumber = maxConcurrentNumber + testResourceNodeDTO.getConcurrentNumber(); + //TODO: 调接口获取剩余并发 + ResourcePoolNodeMetric nodeMetric = getNodeMetric(); + if (nodeMetric != null) { + concurrentNumber = concurrentNumber + (nodeMetric.getConcurrentNumber() == null ? 0 :nodeMetric.getConcurrentNumber()); + occupiedConcurrentNumber = occupiedConcurrentNumber +(nodeMetric.getOccupiedConcurrentNumber() == null ? 0 :nodeMetric.getOccupiedConcurrentNumber()); + } } - testResourcePoolDTO.setMaxConcurrentNumber(concurrentNumber); + testResourcePoolDTO.setLastConcurrentNumber(concurrentNumber-occupiedConcurrentNumber); + testResourcePoolDTO.setMaxConcurrentNumber(maxConcurrentNumber); } else { testResourcePoolDTO.setMaxConcurrentNumber(testResourceDTO.getConcurrentNumber()); } - //TODO: 调接口获取剩余并发 + testResourcePoolDTOS.add(testResourcePoolDTO); }); return testResourcePoolDTOS; } + public ResourcePoolNodeMetric getNodeMetric() { + ResourcePoolNodeMetric resourcePoolNodeMetric = new ResourcePoolNodeMetric(); + try { + ResultHolder body = TaskRunnerClient.get(poolControllerUrl); + if (body == null) { + return null; + } + if (body.getData() != null && StringUtils.equalsIgnoreCase("OK", body.getData().toString())) { + return null; + } + resourcePoolNodeMetric = JSON.parseObject(JSON.toJSONString(body.getData()), ResourcePoolNodeMetric.class); + } catch (Exception e) { + LogUtils.error(e.getMessage(), e); + } + + return resourcePoolNodeMetric; + } + public void checkTestResourcePool(TestResourcePool testResourcePool) { String resourcePoolName = testResourcePool.getName(); TestResourcePoolExample example = new TestResourcePoolExample(); @@ -264,7 +293,7 @@ public class TestResourcePoolService { * 校验该组织是否有权限使用该资源池 * * @param resourcePool 资源池对象 - * @param orgId 组织id + * @param orgId 组织id * @return boolean */ public boolean validateOrgResourcePool(TestResourcePool resourcePool, String orgId) { diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/TestResourcePoolControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/TestResourcePoolControllerTests.java index 0ddad5dc18..c96292b63b 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/TestResourcePoolControllerTests.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/TestResourcePoolControllerTests.java @@ -632,9 +632,4 @@ class TestResourcePoolControllerTests extends BaseTest { return testResourcePoolDTO; } - @Test - void resourceEnumName() throws Exception { - System.out.println(ResourcePoolTypeEnum.K8S.getName()); - } - } \ No newline at end of file