fix(系统管理): 补充资源池剩余并发数数据获取

This commit is contained in:
guoyuqi 2024-10-15 19:08:35 +08:00 committed by Craftsman
parent 504691496e
commit 49af0211e3
3 changed files with 40 additions and 16 deletions

View File

@ -19,7 +19,7 @@ public class TestResourcePoolDTO extends TestResourcePool {
private int maxConcurrentNumber; private int maxConcurrentNumber;
@Schema(description = "剩余并发数") @Schema(description = "剩余并发数")
private Boolean lastConcurrentNumber;; private int lastConcurrentNumber;;
@Schema(description = "组织名称集合") @Schema(description = "组织名称集合")
private List<String> orgNames; private List<String> orgNames;

View File

@ -3,11 +3,10 @@ package io.metersphere.system.service;
import io.metersphere.sdk.constants.HttpMethodConstants; import io.metersphere.sdk.constants.HttpMethodConstants;
import io.metersphere.sdk.constants.OperationLogConstants; import io.metersphere.sdk.constants.OperationLogConstants;
import io.metersphere.sdk.constants.ResourcePoolTypeEnum; import io.metersphere.sdk.constants.ResourcePoolTypeEnum;
import io.metersphere.sdk.dto.pool.ResourcePoolNodeMetric;
import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.*;
import io.metersphere.sdk.util.CommonBeanFactory; import io.metersphere.system.controller.handler.ResultHolder;
import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.Translator;
import io.metersphere.system.domain.*; import io.metersphere.system.domain.*;
import io.metersphere.system.dto.pool.*; import io.metersphere.system.dto.pool.*;
import io.metersphere.system.dto.sdk.OptionDTO; 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.log.dto.LogDTO;
import io.metersphere.system.mapper.*; import io.metersphere.system.mapper.*;
import io.metersphere.system.uid.IDGenerator; import io.metersphere.system.uid.IDGenerator;
import io.metersphere.system.utils.TaskRunnerClient;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.BooleanUtils;
@ -48,6 +48,8 @@ public class TestResourcePoolService {
@Resource @Resource
private ExtResourcePoolMapper extResourcePoolMapper; private ExtResourcePoolMapper extResourcePoolMapper;
private final static String poolControllerUrl = "http://%s:%s/metric";
public void checkAndSaveOrgRelation(TestResourcePool testResourcePool, String id, TestResourceDTO testResourceDTO) { public void checkAndSaveOrgRelation(TestResourcePool testResourcePool, String id, TestResourceDTO testResourceDTO) {
//防止前端传入的应用组织为空 //防止前端传入的应用组织为空
@ -111,7 +113,7 @@ public class TestResourcePoolService {
if (CollectionUtils.isEmpty(testResourceDTO.getNodesList())) { if (CollectionUtils.isEmpty(testResourceDTO.getNodesList())) {
testResourceDTO.setNodesList(new ArrayList<>()); 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); TestResourcePoolValidateService testResourcePoolValidateService = CommonBeanFactory.getBean(TestResourcePoolValidateService.class);
if (testResourcePoolValidateService != null) { if (testResourcePoolValidateService != null) {
testResourcePoolValidateService.validateNodeList(testResourceDTO.getNodesList()); testResourcePoolValidateService.validateNodeList(testResourceDTO.getNodesList());
@ -155,21 +157,48 @@ public class TestResourcePoolService {
testResourcePoolDTO.setOrgNames(orgNameList); 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 concurrentNumber = 0;
int occupiedConcurrentNumber = 0;
for (TestResourceNodeDTO testResourceNodeDTO : testResourceDTO.getNodesList()) { 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 { } else {
testResourcePoolDTO.setMaxConcurrentNumber(testResourceDTO.getConcurrentNumber()); testResourcePoolDTO.setMaxConcurrentNumber(testResourceDTO.getConcurrentNumber());
} }
//TODO 调接口获取剩余并发
testResourcePoolDTOS.add(testResourcePoolDTO); testResourcePoolDTOS.add(testResourcePoolDTO);
}); });
return testResourcePoolDTOS; 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) { public void checkTestResourcePool(TestResourcePool testResourcePool) {
String resourcePoolName = testResourcePool.getName(); String resourcePoolName = testResourcePool.getName();
TestResourcePoolExample example = new TestResourcePoolExample(); TestResourcePoolExample example = new TestResourcePoolExample();
@ -264,7 +293,7 @@ public class TestResourcePoolService {
* 校验该组织是否有权限使用该资源池 * 校验该组织是否有权限使用该资源池
* *
* @param resourcePool 资源池对象 * @param resourcePool 资源池对象
* @param orgId 组织id * @param orgId 组织id
* @return boolean * @return boolean
*/ */
public boolean validateOrgResourcePool(TestResourcePool resourcePool, String orgId) { public boolean validateOrgResourcePool(TestResourcePool resourcePool, String orgId) {

View File

@ -632,9 +632,4 @@ class TestResourcePoolControllerTests extends BaseTest {
return testResourcePoolDTO; return testResourcePoolDTO;
} }
@Test
void resourceEnumName() throws Exception {
System.out.println(ResourcePoolTypeEnum.K8S.getName());
}
} }