feat(系统管理): 新增资源池容量详情接口

This commit is contained in:
guoyuqi 2024-10-16 17:50:24 +08:00 committed by Craftsman
parent d4f7ecf205
commit be339e568e
5 changed files with 161 additions and 29 deletions

View File

@ -3,13 +3,18 @@ package io.metersphere.system.controller;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.sdk.dto.pool.ResourcePoolNodeMetric;
import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.system.dto.pool.TestResourcePoolCapacityRequest;
import io.metersphere.system.dto.pool.TestResourcePoolDTO;
import io.metersphere.system.dto.pool.TestResourcePoolRequest;
import io.metersphere.system.dto.pool.TestResourcePoolReturnDTO;
import io.metersphere.system.dto.sdk.QueryResourcePoolRequest;
import io.metersphere.system.dto.taskhub.TaskHubItemDTO;
import io.metersphere.system.dto.taskhub.request.TaskHubItemRequest;
import io.metersphere.system.log.annotation.Log;
import io.metersphere.system.log.constants.OperationLogType;
import io.metersphere.system.service.BaseTaskHubService;
import io.metersphere.system.service.TestResourcePoolService;
import io.metersphere.system.utils.PageUtils;
import io.metersphere.system.utils.Pager;
@ -17,6 +22,7 @@ import io.metersphere.validation.groups.Updated;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
@ -30,6 +36,8 @@ public class TestResourcePoolController {
@Resource
private TestResourcePoolService testResourcePoolService;
@Resource
private BaseTaskHubService baseTaskHubService;
@PostMapping("/update")
@Operation(summary = "系统设置-系统-资源池-更新资源池")
@ -58,6 +66,27 @@ public class TestResourcePoolController {
return testResourcePoolService.getTestResourcePoolDetail(testResourcePoolId);
}
@PostMapping("/capacity/detail")
@Operation(summary = "系统-资源池-查看资源池详细")
@RequiresPermissions(PermissionConstants.SYSTEM_TEST_RESOURCE_POOL_READ)
public ResourcePoolNodeMetric getTestResourcePoolCapacityDetail(@Validated @RequestBody TestResourcePoolCapacityRequest request) {
return testResourcePoolService.getTestResourcePoolCapacityDetail(request);
}
@PostMapping("/capacity/task/list")
@Operation(summary = "系统-资源池-查看资源池详细")
@RequiresPermissions(PermissionConstants.SYSTEM_TEST_RESOURCE_POOL_READ)
public Pager<List<TaskHubItemDTO>> getCaseTaskItemList(@Validated @RequestBody TestResourcePoolCapacityRequest request) {
TaskHubItemRequest taskHubItemRequest = new TaskHubItemRequest();
BeanUtils.copyBean(taskHubItemRequest, request);
taskHubItemRequest.setResourcePoolIds(List.of(request.getPoolId()));
if (StringUtils.isNotBlank(request.getIp()) && StringUtils.isNotBlank(request.getPort())) {
String node = new StringBuilder().append(request.getIp()).append(":").append(request.getPort()).toString();
taskHubItemRequest.setResourcePoolNodes(List.of(node));
}
return baseTaskHubService.getCaseTaskItemList(taskHubItemRequest, null, null);
}

View File

@ -0,0 +1,26 @@
package io.metersphere.system.dto.pool;
import io.metersphere.system.dto.sdk.BasePageRequest;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
/**
* @author guoyuqi
*/
@Data
public class TestResourcePoolCapacityRequest extends BasePageRequest implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
@Schema(description = "资源池id")
private String poolId;
@Schema(description = "节点IP")
private String ip;
@Schema(description = "节点端口")
private String port;
}

View File

@ -6,32 +6,7 @@
SELECT exec_task_item.*, exec_task.num, exec_task.task_name, exec_task.trigger_mode
FROM exec_task_item
inner join exec_task on exec_task_item.task_id = exec_task.id
<where>
<if test="request.keyword != null and request.keyword != ''">
and (
exec_task.num like concat('%', #{request.keyword},'%')
or exec_task_item.resource_name like concat('%', #{request.keyword},'%')
)
</if>
<if test="orgId != null and orgId != ''">
and exec_task_item.organization_id = #{orgId}
</if>
<if test="projectId != null and projectId != ''">
and exec_task_item.project_id = #{projectId}
</if>
<if test="request.resourcePoolIds != null and request.resourcePoolIds.size() > 0">
and exec_task_item.resource_pool_id in
<foreach collection="request.resourcePoolIds" item="resourcePoolId" separator="," open="(" close=")">
#{request.resourcePoolId}
</foreach>
</if>
<if test="request.resourcePoolNodes != null and request.resourcePoolNodes.size() > 0">
and exec_task_item.resource_pool_node in
<foreach collection="request.resourcePoolNodes" item="resourcePoolNode" separator="," open="(" close=")">
#{request.resourcePoolNode}
</foreach>
</if>
</where>
<include refid="queryWhereCondition"/>
</select>
<select id="selectExecInfoByResourceIds" resultType="io.metersphere.system.domain.ExecTaskItem">
SELECT id, resource_id
@ -64,4 +39,57 @@
</foreach>
</select>
<sql id="queryWhereCondition">
<where>
<if test="orgId != null and orgId != ''">
and exec_task_item.organization_id = #{orgId}
</if>
<if test="request.keyword != null and request.keyword != ''">
and (
exec_task.num like concat('%', #{request.keyword},'%')
or exec_task_item.resource_name like concat('%', #{request.keyword},'%')
)
</if>
<if test="projectId != null and projectId != ''">
and exec_task_item.project_id = #{projectId}
</if>
<if test="request.resourcePoolIds != null and request.resourcePoolIds.size() > 0">
and exec_task_item.resource_pool_id in
<foreach collection="request.resourcePoolIds" item="resourcePoolId" separator="," open="(" close=")">
#{resourcePoolId}
</foreach>
</if>
<if test="request.resourcePoolNodes != null and request.resourcePoolNodes.size() > 0">
and exec_task_item.resource_pool_node in
<foreach collection="request.resourcePoolNodes" item="resourcePoolNode" separator="," open="(" close=")">
#{resourcePoolNode}
</foreach>
</if>
<include refid="filters">
<property name="filter" value="request.filter"/>
</include>
</where>
</sql>
<sql id="filters">
<if test="${filter} != null and ${filter}.size() > 0">
<foreach collection="${filter}.entrySet()" index="key" item="values">
<if test="values != null and values.size() > 0">
<choose>
<!-- 状态 -->
<when test="key=='status'">
and exec_task_item.status in
<include refid="io.metersphere.system.mapper.BaseMapper.filterInWrapper"/>
</when>
<!-- 执行结果 -->
<when test="key=='result'">
and exec_task_item.result in
<include refid="io.metersphere.system.mapper.BaseMapper.filterInWrapper"/>
</when>
</choose>
</if>
</foreach>
</if>
</sql>
</mapper>

View File

@ -164,7 +164,7 @@ public class TestResourcePoolService {
for (TestResourceNodeDTO testResourceNodeDTO : testResourceDTO.getNodesList()) {
maxConcurrentNumber = maxConcurrentNumber + testResourceNodeDTO.getConcurrentNumber();
//TODO 调接口获取剩余并发
ResourcePoolNodeMetric nodeMetric = getNodeMetric();
ResourcePoolNodeMetric nodeMetric = getNodeMetric(testResourceNodeDTO.getIp(), testResourceNodeDTO.getPort());
if (nodeMetric != null) {
concurrentNumber = concurrentNumber + (nodeMetric.getConcurrentNumber() == null ? 0 :nodeMetric.getConcurrentNumber());
occupiedConcurrentNumber = occupiedConcurrentNumber +(nodeMetric.getOccupiedConcurrentNumber() == null ? 0 :nodeMetric.getOccupiedConcurrentNumber());
@ -181,10 +181,10 @@ public class TestResourcePoolService {
return testResourcePoolDTOS;
}
public ResourcePoolNodeMetric getNodeMetric() {
public ResourcePoolNodeMetric getNodeMetric(String ip, String port) {
ResourcePoolNodeMetric resourcePoolNodeMetric = new ResourcePoolNodeMetric();
try {
ResultHolder body = TaskRunnerClient.get(poolControllerUrl);
ResultHolder body = TaskRunnerClient.get(String.format(poolControllerUrl, ip, port));
if (body == null) {
return null;
}
@ -307,4 +307,7 @@ public class TestResourcePoolService {
return testResourcePoolOrganizationMapper.countByExample(example) >= 1;
}
public ResourcePoolNodeMetric getTestResourcePoolCapacityDetail(TestResourcePoolCapacityRequest request) {
return getNodeMetric(request.getIp(), request.getPort());
}
}

View File

@ -73,6 +73,9 @@ class TestResourcePoolControllerTests extends BaseTest {
private static final String TEST_RESOURCE_POOL_ADD = "/test/resource/pool/add";
private static final String TEST_RESOURCE_POOL_UPDATE = "/test/resource/pool/update";
private static final String TEST_RESOURCE_POOL_CAPACITY_LIST = "/test/resource/pool/capacity/task/list";
private static final String TEST_RESOURCE_POOL_CAPACITY_DETAIL = "/test/resource/pool/capacity/detail";
private static final ResultMatcher ERROR_REQUEST_MATCHER = status().is5xxServerError();
@ -632,4 +635,47 @@ class TestResourcePoolControllerTests extends BaseTest {
return testResourcePoolDTO;
}
@Test
@Order(18)
public void getTestResourcePoolCapacityDetail() throws Exception {
TestResourcePoolCapacityRequest request = new TestResourcePoolCapacityRequest();
request.setPoolId("test_pool_1");
request.setIp("172.16.200.8");
request.setPort("8082");
this.requestPost(TEST_RESOURCE_POOL_CAPACITY_DETAIL, request);
request.setCurrent(1);
request.setPageSize(10);
MvcResult mvcResult = this.requestPostWithOkAndReturn(TEST_RESOURCE_POOL_CAPACITY_DETAIL, request);
// 获取返回值
String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
ResultHolder resultHolder = JSON.parseObject(returnData, ResultHolder.class);
// 返回请求正常
Assertions.assertNotNull(resultHolder);
}
@Test
@Order(19)
public void getSystemTaskItemPage() throws Exception {
TestResourcePoolCapacityRequest request = new TestResourcePoolCapacityRequest();
request.setPoolId("test_pool_1");
request.setIp("172.16.200.8");
request.setPort("8082");
request.setCurrent(1);
request.setPageSize(10);
MvcResult mvcResult = this.requestPostWithOkAndReturn(TEST_RESOURCE_POOL_CAPACITY_LIST, request);
// 获取返回值
String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
ResultHolder resultHolder = JSON.parseObject(returnData, ResultHolder.class);
// 返回请求正常
Assertions.assertNotNull(resultHolder);
request.setIp(null);
request.setPort(null);
mvcResult = this.requestPostWithOkAndReturn(TEST_RESOURCE_POOL_CAPACITY_LIST, request);
// 获取返回值
returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
resultHolder = JSON.parseObject(returnData, ResultHolder.class);
// 返回请求正常
Assertions.assertNotNull(resultHolder);
}
}