refactor(系统设置): 默认项目分配配额限制的资源池
Signed-off-by: fit2-zhao <yong.zhao@fit2cloud.com>
This commit is contained in:
parent
4bfb7574a6
commit
186aad2204
|
@ -10,11 +10,13 @@ import io.metersphere.commons.constants.ReportKeys;
|
||||||
import io.metersphere.commons.exception.MSException;
|
import io.metersphere.commons.exception.MSException;
|
||||||
import io.metersphere.commons.utils.JSON;
|
import io.metersphere.commons.utils.JSON;
|
||||||
import io.metersphere.commons.utils.LogUtil;
|
import io.metersphere.commons.utils.LogUtil;
|
||||||
import io.metersphere.commons.utils.SessionUtils;
|
|
||||||
|
|
||||||
import io.metersphere.i18n.Translator;
|
import io.metersphere.i18n.Translator;
|
||||||
import io.metersphere.quota.dto.*;
|
import io.metersphere.quota.dto.CountDto;
|
||||||
|
import io.metersphere.quota.dto.QuotaConstants;
|
||||||
|
import io.metersphere.quota.dto.ReportTimeInfo;
|
||||||
|
import io.metersphere.quota.dto.TestOverview;
|
||||||
import io.metersphere.request.TestPlanRequest;
|
import io.metersphere.request.TestPlanRequest;
|
||||||
|
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;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
@ -22,7 +24,6 @@ import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Propagation;
|
import org.springframework.transaction.annotation.Propagation;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import jakarta.annotation.Resource;
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.text.DecimalFormat;
|
import java.text.DecimalFormat;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
|
@ -220,9 +221,8 @@ public class BaseQuotaService {
|
||||||
* 获取可用资源池集合
|
* 获取可用资源池集合
|
||||||
* @return 资源池名称Set集合
|
* @return 资源池名称Set集合
|
||||||
*/
|
*/
|
||||||
public Set<String> getQuotaResourcePools() {
|
public Set<String> getQuotaResourcePools(String projectId) {
|
||||||
Set<String> pools = new HashSet<>();
|
Set<String> pools = new HashSet<>();
|
||||||
String projectId = SessionUtils.getCurrentProjectId();
|
|
||||||
Quota pjQuota = quotaManagementService.getProjectQuota(projectId);
|
Quota pjQuota = quotaManagementService.getProjectQuota(projectId);
|
||||||
if (pjQuota != null) {
|
if (pjQuota != null) {
|
||||||
if (isValid(pjQuota, pjQuota.getResourcePool())) {
|
if (isValid(pjQuota, pjQuota.getResourcePool())) {
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
package io.metersphere.service;
|
package io.metersphere.service;
|
||||||
|
|
||||||
import io.metersphere.base.domain.*;
|
import io.metersphere.base.domain.*;
|
||||||
import io.metersphere.base.mapper.*;
|
import io.metersphere.base.mapper.ProjectApplicationMapper;
|
||||||
|
import io.metersphere.base.mapper.SystemHeaderMapper;
|
||||||
|
import io.metersphere.base.mapper.SystemParameterMapper;
|
||||||
|
import io.metersphere.base.mapper.UserHeaderMapper;
|
||||||
import io.metersphere.base.mapper.ext.BaseSystemParameterMapper;
|
import io.metersphere.base.mapper.ext.BaseSystemParameterMapper;
|
||||||
import io.metersphere.commons.constants.MicroServiceName;
|
import io.metersphere.commons.constants.MicroServiceName;
|
||||||
import io.metersphere.commons.constants.ParamConstants;
|
import io.metersphere.commons.constants.ParamConstants;
|
||||||
|
@ -24,6 +27,7 @@ import io.metersphere.notice.domain.MailInfo;
|
||||||
import io.metersphere.notice.domain.Receiver;
|
import io.metersphere.notice.domain.Receiver;
|
||||||
import io.metersphere.notice.sender.NoticeModel;
|
import io.metersphere.notice.sender.NoticeModel;
|
||||||
import io.metersphere.notice.sender.impl.MailNoticeSender;
|
import io.metersphere.notice.sender.impl.MailNoticeSender;
|
||||||
|
import io.metersphere.quota.service.BaseQuotaService;
|
||||||
import io.metersphere.request.HeaderRequest;
|
import io.metersphere.request.HeaderRequest;
|
||||||
import io.metersphere.request.resourcepool.QueryResourcePoolRequest;
|
import io.metersphere.request.resourcepool.QueryResourcePoolRequest;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
|
@ -38,10 +42,7 @@ import org.springframework.mail.javamail.JavaMailSenderImpl;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.*;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.UUID;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
|
||||||
|
@ -63,7 +64,10 @@ public class SystemParameterService {
|
||||||
@Resource
|
@Resource
|
||||||
private MicroService microService;
|
private MicroService microService;
|
||||||
@Resource
|
@Resource
|
||||||
|
private BaseQuotaService baseQuotaService;
|
||||||
|
@Resource
|
||||||
private BaseTestResourcePoolService baseTestResourcePoolService;
|
private BaseTestResourcePoolService baseTestResourcePoolService;
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private BaseProjectService baseProjectService;
|
private BaseProjectService baseProjectService;
|
||||||
@Resource
|
@Resource
|
||||||
|
@ -262,28 +266,34 @@ public class SystemParameterService {
|
||||||
return baseSystemConfigDTO;
|
return baseSystemConfigDTO;
|
||||||
}
|
}
|
||||||
|
|
||||||
public TestResourcePoolDTO getTestResourcePool() {
|
public List<TestResourcePoolDTO> getTestResourcePool() {
|
||||||
QueryResourcePoolRequest resourcePoolRequest = new QueryResourcePoolRequest();
|
QueryResourcePoolRequest resourcePoolRequest = new QueryResourcePoolRequest();
|
||||||
resourcePoolRequest.setStatus(ResourceStatusEnum.VALID.name());
|
resourcePoolRequest.setStatus(ResourceStatusEnum.VALID.name());
|
||||||
List<TestResourcePoolDTO> poolDTOS = baseTestResourcePoolService.listResourcePools(resourcePoolRequest);
|
return baseTestResourcePoolService.listResourcePools(resourcePoolRequest);
|
||||||
if (CollectionUtils.isNotEmpty(poolDTOS)) {
|
|
||||||
List<TestResourcePoolDTO> localPools = poolDTOS.stream().filter(item ->
|
|
||||||
StringUtils.equals(item.getName(), "LOCAL")).collect(Collectors.toList());
|
|
||||||
if (CollectionUtils.isNotEmpty(localPools)) {
|
|
||||||
return localPools.get(0);
|
|
||||||
} else {
|
|
||||||
return poolDTOS.get(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String filterQuota(List<TestResourcePoolDTO> list, String projectId) {
|
||||||
|
Set<String> pools = baseQuotaService.getQuotaResourcePools(projectId);
|
||||||
|
List<TestResourcePoolDTO> poolList = new ArrayList<>();
|
||||||
|
if (!pools.isEmpty()) {
|
||||||
|
poolList = list.stream().filter(pool -> pools.contains(pool.getId())).collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
if (CollectionUtils.isNotEmpty(poolList)) {
|
||||||
|
return poolList.get(0).getId();
|
||||||
|
} else if (CollectionUtils.isNotEmpty(list)) {
|
||||||
|
return list.get(0).getId();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void batchSaveApp(List<String> projectIds, String poolId, ProjectApplicationMapper batchMapper) {
|
public void batchSaveApp(List<String> projectIds, List<TestResourcePoolDTO> poolList, ProjectApplicationMapper batchMapper) {
|
||||||
if (CollectionUtils.isNotEmpty(projectIds)) {
|
if (CollectionUtils.isNotEmpty(projectIds)) {
|
||||||
List<String> appProjectIds = baseProjectApplicationService.getProjectIds(projectIds);
|
List<String> appProjectIds = baseProjectApplicationService.getProjectIds(projectIds);
|
||||||
projectIds.removeAll(appProjectIds);
|
projectIds.removeAll(appProjectIds);
|
||||||
// 添加
|
// 添加
|
||||||
projectIds.forEach(item -> {
|
projectIds.forEach(item -> {
|
||||||
|
String id = filterQuota(poolList, item);
|
||||||
|
if (StringUtils.isNotBlank(id)) {
|
||||||
ProjectApplication application = new ProjectApplication();
|
ProjectApplication application = new ProjectApplication();
|
||||||
application.setProjectId(item);
|
application.setProjectId(item);
|
||||||
application.setType(ProjectApplicationType.POOL_ENABLE.name());
|
application.setType(ProjectApplicationType.POOL_ENABLE.name());
|
||||||
|
@ -293,13 +303,14 @@ public class SystemParameterService {
|
||||||
ProjectApplication applicationValue = new ProjectApplication();
|
ProjectApplication applicationValue = new ProjectApplication();
|
||||||
applicationValue.setProjectId(item);
|
applicationValue.setProjectId(item);
|
||||||
applicationValue.setType(ProjectApplicationType.RESOURCE_POOL_ID.name());
|
applicationValue.setType(ProjectApplicationType.RESOURCE_POOL_ID.name());
|
||||||
applicationValue.setTypeValue(poolId);
|
applicationValue.setTypeValue(id);
|
||||||
batchMapper.insert(applicationValue);
|
batchMapper.insert(applicationValue);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void batchSaveProjectApp(String poolId, long size) {
|
public void batchSaveProjectApp(List<TestResourcePoolDTO> poolList, long size) {
|
||||||
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
|
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
|
||||||
ProjectApplicationMapper batchMapper = sqlSession.getMapper(ProjectApplicationMapper.class);
|
ProjectApplicationMapper batchMapper = sqlSession.getMapper(ProjectApplicationMapper.class);
|
||||||
final long pageSize = 500;
|
final long pageSize = 500;
|
||||||
|
@ -308,7 +319,7 @@ public class SystemParameterService {
|
||||||
long pageNum = i * pageSize;
|
long pageNum = i * pageSize;
|
||||||
List<String> projectIds = baseProjectService.getPage(pageNum, pageSize);
|
List<String> projectIds = baseProjectService.getPage(pageNum, pageSize);
|
||||||
if (CollectionUtils.isNotEmpty(projectIds)) {
|
if (CollectionUtils.isNotEmpty(projectIds)) {
|
||||||
batchSaveApp(projectIds, poolId, batchMapper);
|
batchSaveApp(projectIds, poolList, batchMapper);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sqlSession.flushStatements();
|
sqlSession.flushStatements();
|
||||||
|
@ -320,12 +331,13 @@ public class SystemParameterService {
|
||||||
public void saveProjectApplication(String projectId) {
|
public void saveProjectApplication(String projectId) {
|
||||||
BaseSystemConfigDTO config = getBaseInfo();
|
BaseSystemConfigDTO config = getBaseInfo();
|
||||||
if (config != null && StringUtils.equals(config.getRunMode(), "POOL")) {
|
if (config != null && StringUtils.equals(config.getRunMode(), "POOL")) {
|
||||||
TestResourcePoolDTO poolDTO = getTestResourcePool();
|
List<TestResourcePoolDTO> poolList = getTestResourcePool();
|
||||||
if (poolDTO != null) {
|
String id = filterQuota(poolList, projectId);
|
||||||
|
if (StringUtils.isNotBlank(id)) {
|
||||||
ProjectApplication applicationValue = new ProjectApplication();
|
ProjectApplication applicationValue = new ProjectApplication();
|
||||||
applicationValue.setProjectId(projectId);
|
applicationValue.setProjectId(projectId);
|
||||||
applicationValue.setType(ProjectApplicationType.RESOURCE_POOL_ID.name());
|
applicationValue.setType(ProjectApplicationType.RESOURCE_POOL_ID.name());
|
||||||
applicationValue.setTypeValue(poolDTO.getId());
|
applicationValue.setTypeValue(id);
|
||||||
projectApplicationMapper.insert(applicationValue);
|
projectApplicationMapper.insert(applicationValue);
|
||||||
|
|
||||||
ProjectApplication application = new ProjectApplication();
|
ProjectApplication application = new ProjectApplication();
|
||||||
|
@ -356,10 +368,10 @@ public class SystemParameterService {
|
||||||
}
|
}
|
||||||
// 启用资源池
|
// 启用资源池
|
||||||
if (param.getParamKey().equals("base.run.mode") && param.getParamValue().equals("POOL")) {
|
if (param.getParamKey().equals("base.run.mode") && param.getParamValue().equals("POOL")) {
|
||||||
TestResourcePoolDTO poolDTO = getTestResourcePool();
|
|
||||||
long size = baseProjectService.count();
|
long size = baseProjectService.count();
|
||||||
if (poolDTO != null && size > 0) {
|
List<TestResourcePoolDTO> poolList = getTestResourcePool();
|
||||||
batchSaveProjectApp(poolDTO.getId(), size);
|
if (CollectionUtils.isNotEmpty(poolList) && size > 0) {
|
||||||
|
batchSaveProjectApp(poolList, size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 去掉路径最后的 /
|
// 去掉路径最后的 /
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package io.metersphere.xpack.resourcepool.service;
|
package io.metersphere.xpack.resourcepool.service;
|
||||||
|
|
||||||
|
import io.metersphere.commons.utils.SessionUtils;
|
||||||
import io.metersphere.dto.TestResourcePoolDTO;
|
import io.metersphere.dto.TestResourcePoolDTO;
|
||||||
import io.metersphere.quota.service.BaseQuotaService;
|
import io.metersphere.quota.service.BaseQuotaService;
|
||||||
import io.metersphere.request.resourcepool.QueryResourcePoolRequest;
|
import io.metersphere.request.resourcepool.QueryResourcePoolRequest;
|
||||||
|
@ -23,9 +24,9 @@ public class ValidQuotaResourcePoolService {
|
||||||
public List<TestResourcePoolDTO> listValidQuotaResourcePools() {
|
public List<TestResourcePoolDTO> listValidQuotaResourcePools() {
|
||||||
return filterQuota(listValidResourcePools());
|
return filterQuota(listValidResourcePools());
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<TestResourcePoolDTO> filterQuota(List<TestResourcePoolDTO> list) {
|
private List<TestResourcePoolDTO> filterQuota(List<TestResourcePoolDTO> list) {
|
||||||
Set<String> pools = baseQuotaService.getQuotaResourcePools();
|
String projectId = SessionUtils.getCurrentProjectId();
|
||||||
|
Set<String> pools = baseQuotaService.getQuotaResourcePools(projectId);
|
||||||
if (!pools.isEmpty()) {
|
if (!pools.isEmpty()) {
|
||||||
return list.stream().filter(pool -> pools.contains(pool.getId())).collect(Collectors.toList());
|
return list.stream().filter(pool -> pools.contains(pool.getId())).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue