refactor(系统设置): 配额管理

This commit is contained in:
shiziyuan9527 2023-02-07 16:50:25 +08:00 committed by lyh
parent 983d6be64f
commit cc0f4c3dfa
2 changed files with 60 additions and 67 deletions

View File

@ -165,26 +165,13 @@ public class BaseQuotaService {
private Object getQuotaCount(Quota quota, String type) {
Object count = null;
switch (type) {
case API:
count = quota.getApi();
break;
case LOAD:
count = quota.getPerformance();
break;
case DURATION:
count = quota.getDuration();
break;
case MAX_THREAD:
count = quota.getMaxThreads();
break;
case MEMBER:
count = quota.getMember();
break;
case PROJECT:
count = quota.getProject();
break;
default:
MSException.throwException("get quota count fail, don't have type: " + type);
case API -> count = quota.getApi();
case LOAD -> count = quota.getPerformance();
case DURATION -> count = quota.getDuration();
case MAX_THREAD -> count = quota.getMaxThreads();
case MEMBER -> count = quota.getMember();
case PROJECT -> count = quota.getProject();
default -> MSException.throwException("get quota count fail, don't have type: " + type);
}
return count;
}
@ -192,23 +179,19 @@ public class BaseQuotaService {
/**
* 性能测试配额检查
* @param request 压力配置
* @param checkPerformance 检查创建数量配额 / 检查并发数和时间
* @param checkPerformanceCount 检查创建数量配额 / 检查并发数和时间
*/
@Transactional(noRollbackFor = MSException.class, rollbackFor = Exception.class)
public void checkLoadTestQuota(TestPlanRequest request, boolean checkPerformance) {
String loadConfig = request.getLoadConfiguration();
int threadNum = 0;
long duration = 0;
if (loadConfig != null) {
threadNum = getIntegerValue(loadConfig, "TargetLevel");
duration = getIntegerValue(loadConfig, "duration");
}
public void checkLoadTestQuota(TestPlanRequest request, boolean checkPerformanceCount) {
String projectId = request.getProjectId();
if (checkPerformance) {
if (checkPerformanceCount) {
this.checkPerformance(projectId);
} else {
checkMaxThread(projectId, threadNum);
checkDuration(projectId, duration);
String loadConfig = request.getLoadConfiguration();
if (loadConfig != null) {
checkMaxThread(projectId, getIntegerValue(loadConfig, "TargetLevel"));
checkDuration(projectId, getIntegerValue(loadConfig, "duration"));
}
}
}
@ -606,7 +589,6 @@ public class BaseQuotaService {
}
}
// todo
private List<LoadTestReportResult> queryReportResult(String id, String key) {
return extLoadTestReportResultMapper.selectByIdAndKey(id, key);
}
@ -641,8 +623,7 @@ public class BaseQuotaService {
try {
overview = JSON.parseObject(content, TestOverview.class);
} catch (Exception e) {
LogUtil.error("parse test overview error.");
LogUtil.error(e.getMessage(), e);
LogUtil.error("parse test overview error.", e);
}
return overview;
}

View File

@ -8,7 +8,6 @@ import io.metersphere.base.mapper.QuotaMapper;
import io.metersphere.base.mapper.ext.ExtQuotaMapper;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.JSON;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.i18n.Translator;
import io.metersphere.log.utils.ReflexObjectUtil;
import io.metersphere.log.vo.DetailColumn;
@ -16,12 +15,12 @@ import io.metersphere.log.vo.OperatingLogDetails;
import io.metersphere.log.vo.system.SystemReference;
import io.metersphere.quota.dto.QuotaConstants;
import io.metersphere.quota.dto.QuotaResult;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import jakarta.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;
@ -29,8 +28,12 @@ import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
/**
* Quota project_id workspace_id 不同时存在
* project_id不为空: 表示数据是项目配额
* workspace_id不为空: 表示数据是工作空间配额
* project_idworkspace_id都为空: 表示数据是默认配额
* 工作空间默认配额(全局唯一)QuotaConstants.DefaultType.workspace
* 工作空间下项目的默认配额(工作空间下唯一)QuotaConstants.prefix + workspace_id
* 项目默认配额(工作空间下唯一)QuotaConstants.prefix + workspace_id
*/
@Service
@ -57,14 +60,22 @@ public class QuotaManagementService {
if (StringUtils.isBlank(workspaceId)) {
return new Quota();
}
Quota quota = quotaMapper.selectByPrimaryKey(QuotaConstants.prefix + workspaceId);
String workspaceGlobalQuotaId = getWorkspaceGlobalQuotaId(workspaceId);
Quota quota = quotaMapper.selectByPrimaryKey(workspaceGlobalQuotaId);
if (quota == null) {
quota = new Quota();
quota.setId(QuotaConstants.prefix + workspaceId);
quota.setId(workspaceGlobalQuotaId);
}
return quota;
}
private String getWorkspaceGlobalQuotaId(String workspaceId) {
if (StringUtils.isBlank(workspaceId)) {
MSException.throwException("get workspace global quota id error, workspace id is null.");
}
return QuotaConstants.prefix + workspaceId;
}
public void saveQuota(Quota quota) {
if (!isDefaultQuota(quota)) {
// 保存项目配额时检查工作空间下所有项目配额总和是否大于工作空间配额
@ -107,33 +118,8 @@ public class QuotaManagementService {
quota.setId(UUID.randomUUID().toString());
}
if (StringUtils.isNotBlank(quota.getWorkspaceId()) && StringUtils.isNotBlank(quota.getProjectId())) {
LogUtil.error("save quota error, illegal parameter, workspace id and project id cannot exist at the same time");
return;
} else if (StringUtils.isNotBlank(quota.getWorkspaceId()) && StringUtils.isBlank(quota.getProjectId())) {
QuotaExample quotaExample = new QuotaExample();
quotaExample.createCriteria().andWorkspaceIdEqualTo(quota.getWorkspaceId())
.andProjectIdIsNull();
if (quotaMapper.countByExample(quotaExample) > 0) {
LogUtil.error("save quota error, repeat insert workspace quota, id is: " + quota.getWorkspaceId());
return;
}
} else if (StringUtils.isNotBlank(quota.getProjectId()) && StringUtils.isBlank(quota.getWorkspaceId())) {
QuotaExample quotaExample = new QuotaExample();
quotaExample.createCriteria().andProjectIdEqualTo(quota.getProjectId())
.andWorkspaceIdIsNull();
if (quotaMapper.countByExample(quotaExample) > 0) {
LogUtil.error("save quota error, repeat insert project quota, id is: " + quota.getProjectId());
return;
}
} else if (this.isDefaultQuota(quota)) {
QuotaExample quotaExample = new QuotaExample();
quotaExample.createCriteria().andIdEqualTo(quota.getId());
if (quotaMapper.countByExample(quotaExample) > 0) {
LogUtil.error("save quota error, repeat insert default quota, id is: " + quota.getId());
return;
}
}
this.checkQuotaSaveParam(quota);
BigDecimal vumTotal = quota.getVumTotal();
BigDecimal max = BigDecimal.valueOf(99999999.00);
if (vumTotal.compareTo(max) > 0) {
@ -142,6 +128,32 @@ public class QuotaManagementService {
quotaMapper.insert(quota);
}
private void checkQuotaSaveParam(Quota quota) {
if (StringUtils.isNotBlank(quota.getWorkspaceId()) && StringUtils.isNotBlank(quota.getProjectId())) {
MSException.throwException("illegal parameter, workspace id and project id cannot exist at the same time");
} else if (StringUtils.isNotBlank(quota.getWorkspaceId()) && StringUtils.isBlank(quota.getProjectId())) {
QuotaExample quotaExample = new QuotaExample();
quotaExample.createCriteria().andWorkspaceIdEqualTo(quota.getWorkspaceId())
.andProjectIdIsNull();
if (quotaMapper.countByExample(quotaExample) > 0) {
MSException.throwException("repeat insert workspace quota, id is: " + quota.getWorkspaceId());
}
} else if (StringUtils.isNotBlank(quota.getProjectId()) && StringUtils.isBlank(quota.getWorkspaceId())) {
QuotaExample quotaExample = new QuotaExample();
quotaExample.createCriteria().andProjectIdEqualTo(quota.getProjectId())
.andWorkspaceIdIsNull();
if (quotaMapper.countByExample(quotaExample) > 0) {
MSException.throwException("repeat insert project quota, id is: " + quota.getProjectId());
}
} else if (this.isDefaultQuota(quota)) {
QuotaExample quotaExample = new QuotaExample();
quotaExample.createCriteria().andIdEqualTo(quota.getId());
if (quotaMapper.countByExample(quotaExample) > 0) {
MSException.throwException("repeat insert default quota, id is: " + quota.getId());
}
}
}
public void wsVumCompare(Quota quota) {
if (quota == null) {
return;