refactor(系统设置): 创建项目默认分配资源池

【【接口测试】接口测试禁用本地执行之后,执行接口测试未自动选择可用资源池】https://www.tapd.cn/55049933/bugtrace/bugs/view?bug_id=1155049933001025470

Signed-off-by: fit2-zhao <yong.zhao@fit2cloud.com>
This commit is contained in:
fit2-zhao 2023-04-18 10:20:07 +08:00 committed by fit2-zhao
parent 6fc1763bc3
commit 65596aa859
5 changed files with 55 additions and 14 deletions

View File

@ -53,4 +53,6 @@ public interface BaseProjectMapper {
Project selectOne();
List<String> selectPage(long pageNum, long pageSize);
long count();
}

View File

@ -439,7 +439,25 @@
SELECT * FROM project LIMIT 1
</select>
<select id="selectPage" resultType="java.lang.String">
SELECT id FROM project LIMIT #{pageNum}, #{pageSize}
SELECT
project.id
FROM
project
LEFT JOIN project_application ON project.id = project_application.project_id
AND type = 'POOL_ENABLE'
WHERE
project_application.project_id IS NULL
LIMIT #{pageNum}, #{pageSize}
</select>
<select id="count" resultType="java.lang.Long">
SELECT
COUNT( project.id )
FROM
project
LEFT JOIN project_application ON project.id = project_application.project_id
AND type = 'POOL_ENABLE'
WHERE
project_application.project_id IS NULL
</select>
</mapper>

View File

@ -381,7 +381,7 @@ public class BaseProjectService {
}
public long count() {
return projectMapper.countByExample(new ProjectExample());
return baseProjectMapper.count();
}
public List<String> getPage(long pageNum, long pageSize) {

View File

@ -70,7 +70,8 @@ public class SystemParameterService {
private BaseProjectApplicationService baseProjectApplicationService;
@Resource
private SqlSessionFactory sqlSessionFactory;
@Resource
private ProjectApplicationMapper projectApplicationMapper;
public String searchEmail() {
return baseSystemParameterMapper.email();
@ -277,7 +278,7 @@ public class SystemParameterService {
return null;
}
public void saveProjectApplication(List<String> projectIds, String poolId, ProjectApplicationMapper batchMapper) {
public void batchSaveApp(List<String> projectIds, String poolId, ProjectApplicationMapper batchMapper) {
if (CollectionUtils.isNotEmpty(projectIds)) {
List<String> appProjectIds = baseProjectApplicationService.getProjectIds(projectIds);
projectIds.removeAll(appProjectIds);
@ -298,17 +299,16 @@ public class SystemParameterService {
}
}
public void saveProjectApp(String poolId, long size) {
public void batchSaveProjectApp(String poolId, long size) {
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
ProjectApplicationMapper batchMapper = sqlSession.getMapper(ProjectApplicationMapper.class);
final long pageSize = 500;
long pageTotal = size / pageSize;
pageTotal = pageTotal > 0 ? pageTotal : 1;
for (int i = 0; i < pageTotal; i++) {
int pageTotal = (int) Math.ceil(size / pageSize);
for (int i = 0; i <= pageTotal; i++) {
long pageNum = i * pageSize;
List<String> projectIds = baseProjectService.getPage(pageNum, pageSize);
if (CollectionUtils.isNotEmpty(projectIds)) {
saveProjectApplication(projectIds, poolId, batchMapper);
batchSaveApp(projectIds, poolId, batchMapper);
}
}
sqlSession.flushStatements();
@ -317,6 +317,26 @@ public class SystemParameterService {
}
}
public void saveProjectApplication(String projectId) {
BaseSystemConfigDTO config = getBaseInfo();
if (config != null && StringUtils.equals(config.getRunMode(), "POOL")) {
TestResourcePoolDTO poolDTO = getTestResourcePool();
if (poolDTO != null) {
ProjectApplication applicationValue = new ProjectApplication();
applicationValue.setProjectId(projectId);
applicationValue.setType(ProjectApplicationType.RESOURCE_POOL_ID.name());
applicationValue.setTypeValue(poolDTO.getId());
projectApplicationMapper.insert(applicationValue);
ProjectApplication application = new ProjectApplication();
application.setProjectId(projectId);
application.setType(ProjectApplicationType.POOL_ENABLE.name());
application.setTypeValue(String.valueOf(true));
projectApplicationMapper.insert(application);
}
}
}
public void saveBaseInfo(List<SystemParameter> parameters) {
SystemParameterExample example = new SystemParameterExample();
@ -337,9 +357,9 @@ public class SystemParameterService {
// 启用资源池
if (param.getParamKey().equals("base.run.mode") && param.getParamValue().equals("POOL")) {
TestResourcePoolDTO poolDTO = getTestResourcePool();
if (poolDTO != null) {
long size = baseProjectService.count();
saveProjectApp(poolDTO.getId(), size);
long size = baseProjectService.count();
if (poolDTO != null && size > 0) {
batchSaveProjectApp(poolDTO.getId(), size);
}
}
// 去掉路径最后的 /

View File

@ -4,7 +4,6 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import io.metersphere.base.domain.*;
import io.metersphere.base.mapper.*;
import io.metersphere.base.mapper.ext.BaseProjectMapper;
import io.metersphere.base.mapper.ext.BaseProjectVersionMapper;
import io.metersphere.base.mapper.ext.BaseUserGroupMapper;
import io.metersphere.base.mapper.ext.BaseUserMapper;
import io.metersphere.commons.constants.IssuesManagePlatform;
@ -61,7 +60,7 @@ public class SystemProjectService {
@Value("${tcp.mock.port}")
private String tcpMockPorts;
@Resource
private BaseProjectVersionMapper baseProjectVersionMapper;
private SystemParameterService systemParameterService;
@Resource
private ProjectApplicationMapper projectApplicationMapper;
@Resource
@ -102,6 +101,8 @@ public class SystemProjectService {
// 初始化项目默认节点
kafkaTemplate.send(KafkaTopicConstants.PROJECT_CREATED_TOPIC, project.getId());
LogUtil.info("send create_project message, project id: " + project.getId());
// 初始化项目资源池
systemParameterService.saveProjectApplication(project.getId());
return project;
}