perf: 限制其他用户流水线数量

pull/229/head
xiaojunnuo 2024-10-26 23:54:49 +08:00
parent 526c48450b
commit 315e43746b
6 changed files with 54 additions and 26 deletions

View File

@ -15,6 +15,7 @@ export class SysPublicSettings extends BaseSettings {
static __title__ = '系统公共设置'; static __title__ = '系统公共设置';
static __access__ = 'public'; static __access__ = 'public';
registerEnabled = false; registerEnabled = false;
limitUserPipelineCount = 0;
managerOtherUserPipeline = false; managerOtherUserPipeline = false;
icpNo?: string; icpNo?: string;
// triggerOnStartup = false; // triggerOnStartup = false;

View File

@ -127,14 +127,12 @@ export class SysSettingsService extends BaseService<SysSettingsEntity> {
async reloadPrivateSettings() { async reloadPrivateSettings() {
const bean = await this.getPrivateSettings(); const bean = await this.getPrivateSettings();
if (bean.httpProxy || bean.httpsProxy) { const opts = {
const opts = { httpProxy: bean.httpProxy,
httpProxy: bean.httpProxy, httpsProxy: bean.httpsProxy,
httpsProxy: bean.httpsProxy, };
}; setGlobalProxy(opts);
setGlobalProxy(opts); agents.setGlobalProxy(opts);
agents.setGlobalProxy(opts);
}
} }
async updateByKey(key: string, setting: any) { async updateByKey(key: string, setting: any) {

View File

@ -5,6 +5,7 @@ export type SysSettings = { public: SysPublicSetting; private: SysPrivateSetting
export type SysPublicSetting = { export type SysPublicSetting = {
registerEnabled?: boolean; registerEnabled?: boolean;
limitUserPipelineCount?: number;
managerOtherUserPipeline?: boolean; managerOtherUserPipeline?: boolean;
icpNo?: string; icpNo?: string;
}; };

View File

@ -16,6 +16,10 @@
<a-form-item label="开启自助注册" :name="['public', 'registerEnabled']"> <a-form-item label="开启自助注册" :name="['public', 'registerEnabled']">
<a-switch v-model:checked="formState.public.registerEnabled" /> <a-switch v-model:checked="formState.public.registerEnabled" />
</a-form-item> </a-form-item>
<a-form-item label="限制用户流水线数量" :name="['public', 'limitUserPipelineCount']">
<a-input-number v-model:value="formState.public.limitUserPipelineCount" />
<div class="helper">0为不限制</div>
</a-form-item>
<a-form-item label="管理其他用户流水线" :name="['public', 'managerOtherUserPipeline']"> <a-form-item label="管理其他用户流水线" :name="['public', 'managerOtherUserPipeline']">
<a-switch v-model:checked="formState.public.managerOtherUserPipeline" /> <a-switch v-model:checked="formState.public.managerOtherUserPipeline" />
</a-form-item> </a-form-item>
@ -30,7 +34,7 @@
<a-form-item label="HTTPS代理" :name="['private', 'httpsProxy']" :rules="urlRules"> <a-form-item label="HTTPS代理" :name="['private', 'httpsProxy']" :rules="urlRules">
<div class="flex"> <div class="flex">
<a-input v-model:value="formState.private.httpsProxy" placeholder="http://192.168.1.2:18010/" /> <a-input v-model:value="formState.private.httpsProxy" placeholder="http://192.168.1.2:18010/" />
<a-button class="ml-5" type="primary" title="保存后,再点击测试" @click="testProxy"></a-button> <a-button class="ml-5" type="primary" :loading="testProxyLoading" title="保存后,再点击测试" @click="testProxy"></a-button>
</div> </div>
<div class="helper">一般这两个代理填一样的</div> <div class="helper">一般这两个代理填一样的</div>
</a-form-item> </a-form-item>
@ -57,7 +61,7 @@ defineOptions({
const formState = reactive<Partial<SysSettings>>({ const formState = reactive<Partial<SysSettings>>({
public: { public: {
registerEnabled: false, registerEnabled: false,
limitUserPipelineCount: 10, limitUserPipelineCount: 0,
managerOtherUserPipeline: false, managerOtherUserPipeline: false,
icpNo: "" icpNo: ""
}, },
@ -101,13 +105,19 @@ async function stopOtherUserTimer() {
}); });
} }
const testProxyLoading = ref(false);
async function testProxy() { async function testProxy() {
const res = await api.TestProxy(); testProxyLoading.value = true;
const content = `测试google:${res.google === true ? "成功" : "失败" + res.google},测试百度:${res.baidu === true ? "成功" : "失败:" + res.baidu}`; try {
notification.success({ const res = await api.TestProxy();
message: "测试完成", const content = `测试google:${res.google === true ? "成功" : "失败" + res.google},测试百度:${res.baidu === true ? "成功" : "失败:" + res.baidu}`;
description: content notification.success({
}); message: "测试完成",
description: content
});
} finally {
testProxyLoading.value = false;
}
} }
</script> </script>

View File

@ -111,7 +111,9 @@ export class SysSettingsController extends CrudController<SysSettingsService> {
await http.request({ await http.request({
url: google, url: google,
method: 'GET', method: 'GET',
timeout: 4000, timeout: 5000,
logRes: false,
logParams: false,
}); });
googleRes = true; googleRes = true;
} catch (e) { } catch (e) {
@ -123,7 +125,9 @@ export class SysSettingsController extends CrudController<SysSettingsService> {
await http.request({ await http.request({
url: baidu, url: baidu,
method: 'GET', method: 'GET',
timeout: 4000, timeout: 5000,
logRes: false,
logParams: false,
}); });
baiduRes = true; baiduRes = true;
} catch (e) { } catch (e) {

View File

@ -1,7 +1,7 @@
import { Config, Inject, Provide, Scope, ScopeEnum, sleep } from '@midwayjs/core'; import { Config, Inject, Provide, Scope, ScopeEnum, sleep } from '@midwayjs/core';
import { InjectEntityModel } from '@midwayjs/typeorm'; import { InjectEntityModel } from '@midwayjs/typeorm';
import { In, Repository } from 'typeorm'; import { In, Repository } from 'typeorm';
import { BaseService, NeedVIPException, PageReq } from '@certd/lib-server'; import { BaseService, NeedVIPException, PageReq, SysPublicSettings, SysSettingsService } from '@certd/lib-server';
import { PipelineEntity } from '../entity/pipeline.js'; import { PipelineEntity } from '../entity/pipeline.js';
import { PipelineDetail } from '../entity/vo/pipeline-detail.js'; import { PipelineDetail } from '../entity/vo/pipeline-detail.js';
import { Executor, isPlus, logger, Pipeline, ResultType, RunHistory, UserInfo } from '@certd/pipeline'; import { Executor, isPlus, logger, Pipeline, ResultType, RunHistory, UserInfo } from '@certd/pipeline';
@ -47,6 +47,9 @@ export class PipelineService extends BaseService<PipelineEntity> {
@Inject() @Inject()
pluginConfigGetter: PluginConfigGetter; pluginConfigGetter: PluginConfigGetter;
@Inject()
sysSettingsService: SysSettingsService;
@Inject() @Inject()
userService: UserService; userService: UserService;
@ -121,16 +124,27 @@ export class PipelineService extends BaseService<PipelineEntity> {
old = await this.info(bean.id); old = await this.info(bean.id);
} }
const isUpdate = bean.id > 0 && old != null; const isUpdate = bean.id > 0 && old != null;
if (!isPlus()) { if (!isUpdate) {
let count = await this.repository.count(); //如果是添加,校验数量
if (!isUpdate) { if (!isPlus()) {
//如果是添加要加1 const count = await this.repository.count();
count += 1; if (count >= freeCount) {
throw new NeedVIPException(`基础版最多只能创建${freeCount}条流水线`);
}
} }
if (count > freeCount) { const userId = bean.userId;
throw new NeedVIPException('基础版最多只能创建10个pipeline'); const userIsAdmin = await this.userService.isAdmin(userId);
if (!userIsAdmin) {
//非管理员用户限制pipeline数量
const count = await this.repository.count({ where: { userId } });
const sysPublic = await this.sysSettingsService.getSetting<SysPublicSettings>(SysPublicSettings);
const limitUserPipelineCount = sysPublic.limitUserPipelineCount;
if (limitUserPipelineCount && limitUserPipelineCount > 0 && count >= limitUserPipelineCount) {
throw new NeedVIPException(`您最多只能创建${limitUserPipelineCount}条流水线`);
}
} }
} }
if (!isUpdate) { if (!isUpdate) {
//如果是添加先保存一下获取到id更新pipeline.id //如果是添加先保存一下获取到id更新pipeline.id
await this.addOrUpdate(bean); await this.addOrUpdate(bean);