perf: 流水线支持名称模糊查询

This commit is contained in:
xiaojunnuo
2024-08-04 02:35:45 +08:00
parent a9717b9a0d
commit 59897c4cea
14 changed files with 157 additions and 28 deletions

View File

@@ -1 +1,5 @@
alter table pi_pipeline alter column title type varchar(100) using title::varchar(100);
alter table pi_pipeline alter column content type text using content::text;
alter table pi_storage alter column value type text using value::text;
alter table pi_pipeline add "order" integer default 0;

View File

@@ -0,0 +1,3 @@
alter table pi_pipeline add COLUMN "order" integer default 0;

View File

@@ -118,10 +118,9 @@ export abstract class BaseService<T> {
}
const qb = this.getRepository().createQueryBuilder('main');
if (order && order.prop) {
qb.orderBy('main.' + order.prop, order.asc ? 'ASC' : 'DESC');
} else {
qb.orderBy('id', 'DESC');
qb.addOrderBy('main.' + order.prop, order.asc ? 'ASC' : 'DESC');
}
qb.addOrderBy('id', 'DESC');
qb.offset(page.offset).limit(page.limit);
//根据bean query
if (query) {

View File

@@ -9,12 +9,7 @@ export abstract class CrudController<T> extends BaseController {
@Body(ALL)
body
) {
const pageRet = await this.getService().page(
body?.query,
body?.page,
body?.sort,
null
);
const pageRet = await this.getService().page(body?.query, body?.page, body?.sort, null);
return this.ok(pageRet);
}
@@ -54,6 +49,7 @@ export abstract class CrudController<T> extends BaseController {
await this.getService().update(bean);
return this.ok(null);
}
@Post('/delete')
async delete(
@Query('id')

View File

@@ -23,9 +23,19 @@ export class PipelineController extends CrudController<PipelineService> {
@Post('/page', { summary: Constants.per.authOnly })
async page(@Body(ALL) body) {
body.query.userId = this.ctx.user.id;
const title = body.query.title;
delete body.query.title;
const buildQuery = qb => {
qb.where({});
if (title) {
qb.where('title like :title', { title: `%${title}%` });
}
};
if (!body.sort || !body.sort?.prop) {
body.sort = { prop: 'order', asc: false };
}
return super.page({ ...body, buildQuery });
}
@@ -48,7 +58,6 @@ export class PipelineController extends CrudController<PipelineService> {
await this.service.checkUserId(bean.id, this.ctx.user.id);
}
await this.service.save(bean);
await this.service.registerTriggerById(bean.id);
return this.ok(bean.id);
}

View File

@@ -37,6 +37,16 @@ export class PipelineEntity {
})
lastHistoryTime: number;
// 变量
lastVars: any;
@Column({
name: 'order',
comment: '排序',
nullable: true,
})
order: number;
@Column({
name: 'create_time',
comment: '创建时间',

View File

@@ -12,7 +12,7 @@ export class DbStorage implements IStorage {
this.storageService = storageService;
}
remove(scope: string, namespace: string, version: string, key: string): Promise<void> {
async remove(scope: string, namespace: string, version: string, key: string): Promise<void> {
throw new Error('Method not implemented.');
}

View File

@@ -47,8 +47,23 @@ export class PipelineService extends BaseService<PipelineEntity> {
return this.repository;
}
async update(entity) {
await super.update(entity);
async page(query: any, page: { offset: number; limit: number }, order: any, buildQuery: any) {
const result = await super.page(query, page, order, buildQuery);
const pipelineIds: number[] = [];
const recordMap = {};
for (const record of result.records) {
pipelineIds.push(record.id);
recordMap[record.id] = record;
}
const vars = await this.storageService.findPipelineVars(pipelineIds);
for (const varEntity of vars) {
const record = recordMap[varEntity.namespace];
if (record) {
const value = JSON.parse(varEntity.value);
record.lastVars = value.value;
}
}
return result;
}
public async registerTriggerById(pipelineId) {
@@ -71,10 +86,18 @@ export class PipelineService extends BaseService<PipelineEntity> {
return new PipelineDetail(pipeline);
}
async update(bean: PipelineEntity) {
await this.clearTriggers(bean.id);
await super.update(bean);
await this.registerTriggerById(bean.id);
}
async save(bean: PipelineEntity) {
await this.clearTriggers(bean.id);
const pipeline = JSON.parse(bean.content);
bean.title = pipeline.title;
await this.addOrUpdate(bean);
await this.registerTriggerById(bean.id);
}
/**
@@ -153,6 +176,14 @@ export class PipelineService extends BaseService<PipelineEntity> {
}
async delete(id: number) {
await this.clearTriggers(id);
//TODO 删除storage
// const storage = new DbStorage(pipeline.userId, this.storageService);
// await storage.remove(pipeline.id);
await super.delete([id]);
}
async clearTriggers(id: number) {
const pipeline = await this.info(id);
if (!pipeline) {
return;
@@ -163,7 +194,6 @@ export class PipelineService extends BaseService<PipelineEntity> {
this.removeCron(id, trigger);
}
}
await super.delete([id]);
}
removeCron(pipelineId, trigger) {
@@ -176,6 +206,7 @@ export class PipelineService extends BaseService<PipelineEntity> {
if (cron == null) {
return;
}
cron = cron.trim();
if (cron.startsWith('*')) {
cron = '0' + cron.substring(1, cron.length);
}
@@ -183,7 +214,7 @@ export class PipelineService extends BaseService<PipelineEntity> {
this.cron.remove(name);
this.cron.register({
name,
cron: cron,
cron,
job: async () => {
logger.info('定时任务触发:', pipelineId, trigger.id);
try {
@@ -198,6 +229,9 @@ export class PipelineService extends BaseService<PipelineEntity> {
async run(id: number, triggerId: string) {
const entity: PipelineEntity = await this.info(id);
if (entity.disabled) {
return;
}
const pipeline = JSON.parse(entity.content);
if (!pipeline.stages || pipeline.stages.length === 0) {

View File

@@ -1,6 +1,6 @@
import { Provide, Scope, ScopeEnum } from '@midwayjs/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { In, Repository } from 'typeorm';
import { BaseService } from '../../../basic/base-service.js';
import { StorageEntity } from '../entity/storage.js';
@@ -41,4 +41,14 @@ export class StorageService extends BaseService<StorageEntity> {
}
return;
}
async findPipelineVars(pipelineIds: number[]) {
return await this.repository.find({
where: {
scope: 'pipeline',
namespace: In(pipelineIds),
key: 'vars',
},
});
}
}