添加标签功能 (#1026)

* 添加标签功能
This commit is contained in:
kilo5hz
2022-01-07 22:01:13 +08:00
committed by GitHub
parent 5a5f4b8065
commit 89ed8527d6
7 changed files with 262 additions and 44 deletions
+45 -1
View File
@@ -28,6 +28,7 @@ export default (app: Router) => {
command: Joi.string().required(),
schedule: Joi.string().required(),
name: Joi.string().optional(),
labels: Joi.array().optional(),
}),
}),
async (req: Request, res: Response, next: NextFunction) => {
@@ -83,6 +84,48 @@ export default (app: Router) => {
},
);
route.put(
'/removelabels',
celebrate({
body: Joi.object({
ids:Joi.array().items(Joi.number().required()),
labels:Joi.array().items(Joi.string().required()),
})
}),
async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try {
const cronService = Container.get(CronService);
const data = await cronService.removeLabels(req.body.ids,req.body.labels);
return res.send({ code: 200, data });
} catch (e) {
logger.error('🔥 error: %o', e);
return next(e);
}
},
);
route.put(
'/addlabels',
celebrate({
body: Joi.object({
ids:Joi.array().items(Joi.number().required()),
labels:Joi.array().items(Joi.string().required()),
})
}),
async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try {
const cronService = Container.get(CronService);
const data = await cronService.addLabels(req.body.ids,req.body.labels);
return res.send({ code: 200, data });
} catch (e) {
logger.error('🔥 error: %o', e);
return next(e);
}
},
);
route.put(
'/disable',
celebrate({
@@ -143,10 +186,11 @@ export default (app: Router) => {
'/',
celebrate({
body: Joi.object({
labels: Joi.array().optional(),
command: Joi.string().optional(),
schedule: Joi.string().optional(),
name: Joi.string().optional(),
id: Joi.string().required(),
id: Joi.number().required(),
}),
}),
async (req: Request, res: Response, next: NextFunction) => {
+14
View File
@@ -14,6 +14,7 @@ export class Crontab {
isDisabled?: 1 | 0;
log_path?: string;
isPinned?: 1 | 0;
labels: Array<string>;
last_running_time?: number;
last_execution_time?: number;
@@ -33,6 +34,7 @@ export class Crontab {
this.isDisabled = options.isDisabled || 0;
this.log_path = options.log_path || '';
this.isPinned = options.isPinned || 0;
this.labels = options.labels || [''];
this.last_running_time = options.last_running_time || 0;
this.last_execution_time = options.last_execution_time || 0;
}
@@ -58,6 +60,18 @@ export const CrontabModel = sequelize.define<CronInstance>('Crontab', {
isDisabled: DataTypes.NUMBER,
isPinned: DataTypes.NUMBER,
log_path: DataTypes.STRING,
labels: {
type: DataTypes.STRING,
allowNull: false,
get() {
if (this.getDataValue('labels')) {
return this.getDataValue('labels').split(',')
}
},
set(value) {
this.setDataValue('labels', value.join(','));
},
},
last_running_time: DataTypes.NUMBER,
last_execution_time: DataTypes.NUMBER,
});
+60 -21
View File
@@ -93,30 +93,61 @@ export default class CronService {
await CrontabModel.update({ isPinned: 0 }, { where: { id: ids } });
}
public async addLabels(ids: string[],labels: string[]){
const docs = await CrontabModel.findAll({ where: { id:ids }});
for (const doc of docs) {
await CrontabModel.update({
labels: Array.from(new Set(doc.labels.concat(labels)))
},{ where: {id:doc.id}});
}
}
public async removeLabels(ids: string[],labels: string[]){
const docs = await CrontabModel.findAll({ where: { id:ids }});
for (const doc of docs) {
await CrontabModel.update({
labels: doc.labels.filter( label => !labels.includes(label) )
},{ where: {id:doc.id}});
}
}
public async crontabs(searchText?: string): Promise<Crontab[]> {
let query = {};
if (searchText) {
const encodeText = encodeURIComponent(searchText);
const reg = {
[Op.or]: [
{ [Op.like]: `%${searchText}&` },
{ [Op.like]: `%${encodeText}%` },
],
};
query = {
[Op.or]: [
{
name: reg,
},
{
command: reg,
},
{
schedule: reg,
},
],
};
const textArray = searchText.split(":");
switch (textArray[0]) {
case "name":
query = {name:{[Op.or]:createRegexp(textArray[1])}};
break;
case "command":
query = {command:{[Op.or]:createRegexp(textArray[1])}};
break;
case "schedule":
query = {schedule:{[Op.or]:createRegexp(textArray[1])}};
break;
case "label":
query = {labels:{[Op.or]:createRegexp(textArray[1])}};
break;
default:
const reg = createRegexp(searchText);
query = {
[Op.or]: [
{
name: reg,
},
{
command: reg,
},
{
schedule: reg,
},
{
labels: reg,
},
],
};
break;
}
}
try {
const result = await CrontabModel.findAll({ where: query });
@@ -124,6 +155,14 @@ export default class CronService {
} catch (error) {
throw error;
}
function createRegexp(text:string) {
return {
[Op.or]: [
{ [Op.like]: `%${text}%` },
{ [Op.like]: `%${encodeURIComponent(text)}%` },
],
};
}
}
public async get(id: number): Promise<Crontab> {