774 lines
19 KiB
JavaScript
774 lines
19 KiB
JavaScript
|
|
const db = require('../models');
|
|
const FileDBApi = require('./file');
|
|
const crypto = require('crypto');
|
|
const Utils = require('../utils');
|
|
|
|
|
|
|
|
const Sequelize = db.Sequelize;
|
|
const Op = Sequelize.Op;
|
|
|
|
module.exports = class Render_jobsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const render_jobs = await db.render_jobs.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
stage: data.stage
|
|
||
|
|
null
|
|
,
|
|
|
|
status: data.status
|
|
||
|
|
null
|
|
,
|
|
|
|
attempt: data.attempt
|
|
||
|
|
null
|
|
,
|
|
|
|
max_attempts: data.max_attempts
|
|
||
|
|
null
|
|
,
|
|
|
|
priority: data.priority
|
|
||
|
|
null
|
|
,
|
|
|
|
queued_at: data.queued_at
|
|
||
|
|
null
|
|
,
|
|
|
|
started_at: data.started_at
|
|
||
|
|
null
|
|
,
|
|
|
|
finished_at: data.finished_at
|
|
||
|
|
null
|
|
,
|
|
|
|
duration_ms: data.duration_ms
|
|
||
|
|
null
|
|
,
|
|
|
|
logs_text: data.logs_text
|
|
||
|
|
null
|
|
,
|
|
|
|
error_message: data.error_message
|
|
||
|
|
null
|
|
,
|
|
|
|
worker_ref: data.worker_ref
|
|
||
|
|
null
|
|
,
|
|
|
|
idempotency_key: data.idempotency_key
|
|
||
|
|
null
|
|
,
|
|
|
|
progress_json: data.progress_json
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await render_jobs.setConcept( data.concept || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return render_jobs;
|
|
}
|
|
|
|
|
|
static async bulkImport(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
// Prepare data - wrapping individual data transformations in a map() method
|
|
const render_jobsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
stage: item.stage
|
|
||
|
|
null
|
|
,
|
|
|
|
status: item.status
|
|
||
|
|
null
|
|
,
|
|
|
|
attempt: item.attempt
|
|
||
|
|
null
|
|
,
|
|
|
|
max_attempts: item.max_attempts
|
|
||
|
|
null
|
|
,
|
|
|
|
priority: item.priority
|
|
||
|
|
null
|
|
,
|
|
|
|
queued_at: item.queued_at
|
|
||
|
|
null
|
|
,
|
|
|
|
started_at: item.started_at
|
|
||
|
|
null
|
|
,
|
|
|
|
finished_at: item.finished_at
|
|
||
|
|
null
|
|
,
|
|
|
|
duration_ms: item.duration_ms
|
|
||
|
|
null
|
|
,
|
|
|
|
logs_text: item.logs_text
|
|
||
|
|
null
|
|
,
|
|
|
|
error_message: item.error_message
|
|
||
|
|
null
|
|
,
|
|
|
|
worker_ref: item.worker_ref
|
|
||
|
|
null
|
|
,
|
|
|
|
idempotency_key: item.idempotency_key
|
|
||
|
|
null
|
|
,
|
|
|
|
progress_json: item.progress_json
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const render_jobs = await db.render_jobs.bulkCreate(render_jobsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return render_jobs;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const render_jobs = await db.render_jobs.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.stage !== undefined) updatePayload.stage = data.stage;
|
|
|
|
|
|
if (data.status !== undefined) updatePayload.status = data.status;
|
|
|
|
|
|
if (data.attempt !== undefined) updatePayload.attempt = data.attempt;
|
|
|
|
|
|
if (data.max_attempts !== undefined) updatePayload.max_attempts = data.max_attempts;
|
|
|
|
|
|
if (data.priority !== undefined) updatePayload.priority = data.priority;
|
|
|
|
|
|
if (data.queued_at !== undefined) updatePayload.queued_at = data.queued_at;
|
|
|
|
|
|
if (data.started_at !== undefined) updatePayload.started_at = data.started_at;
|
|
|
|
|
|
if (data.finished_at !== undefined) updatePayload.finished_at = data.finished_at;
|
|
|
|
|
|
if (data.duration_ms !== undefined) updatePayload.duration_ms = data.duration_ms;
|
|
|
|
|
|
if (data.logs_text !== undefined) updatePayload.logs_text = data.logs_text;
|
|
|
|
|
|
if (data.error_message !== undefined) updatePayload.error_message = data.error_message;
|
|
|
|
|
|
if (data.worker_ref !== undefined) updatePayload.worker_ref = data.worker_ref;
|
|
|
|
|
|
if (data.idempotency_key !== undefined) updatePayload.idempotency_key = data.idempotency_key;
|
|
|
|
|
|
if (data.progress_json !== undefined) updatePayload.progress_json = data.progress_json;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await render_jobs.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.concept !== undefined) {
|
|
await render_jobs.setConcept(
|
|
|
|
data.concept,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return render_jobs;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const render_jobs = await db.render_jobs.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of render_jobs) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of render_jobs) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return render_jobs;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const render_jobs = await db.render_jobs.findByPk(id, options);
|
|
|
|
await render_jobs.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await render_jobs.destroy({
|
|
transaction
|
|
});
|
|
|
|
return render_jobs;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const render_jobs = await db.render_jobs.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!render_jobs) {
|
|
return render_jobs;
|
|
}
|
|
|
|
const output = render_jobs.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.concept = await render_jobs.getConcept({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
return output;
|
|
}
|
|
|
|
static async findAll(
|
|
filter,
|
|
options
|
|
) {
|
|
const limit = filter.limit || 0;
|
|
let offset = 0;
|
|
let where = {};
|
|
const currentPage = +filter.page;
|
|
|
|
|
|
|
|
|
|
|
|
offset = currentPage * limit;
|
|
|
|
const orderBy = null;
|
|
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
let include = [
|
|
|
|
{
|
|
model: db.concepts,
|
|
as: 'concept',
|
|
|
|
where: filter.concept ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.concept.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
name: {
|
|
[Op.or]: filter.concept.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.logs_text) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'render_jobs',
|
|
'logs_text',
|
|
filter.logs_text,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.error_message) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'render_jobs',
|
|
'error_message',
|
|
filter.error_message,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.worker_ref) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'render_jobs',
|
|
'worker_ref',
|
|
filter.worker_ref,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.idempotency_key) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'render_jobs',
|
|
'idempotency_key',
|
|
filter.idempotency_key,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.progress_json) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'render_jobs',
|
|
'progress_json',
|
|
filter.progress_json,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.attemptRange) {
|
|
const [start, end] = filter.attemptRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
attempt: {
|
|
...where.attempt,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
attempt: {
|
|
...where.attempt,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.max_attemptsRange) {
|
|
const [start, end] = filter.max_attemptsRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
max_attempts: {
|
|
...where.max_attempts,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
max_attempts: {
|
|
...where.max_attempts,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.priorityRange) {
|
|
const [start, end] = filter.priorityRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
priority: {
|
|
...where.priority,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
priority: {
|
|
...where.priority,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.queued_atRange) {
|
|
const [start, end] = filter.queued_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
queued_at: {
|
|
...where.queued_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
queued_at: {
|
|
...where.queued_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.started_atRange) {
|
|
const [start, end] = filter.started_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
started_at: {
|
|
...where.started_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
started_at: {
|
|
...where.started_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.finished_atRange) {
|
|
const [start, end] = filter.finished_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
finished_at: {
|
|
...where.finished_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
finished_at: {
|
|
...where.finished_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.duration_msRange) {
|
|
const [start, end] = filter.duration_msRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
duration_ms: {
|
|
...where.duration_ms,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
duration_ms: {
|
|
...where.duration_ms,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.stage) {
|
|
where = {
|
|
...where,
|
|
stage: filter.stage,
|
|
};
|
|
}
|
|
|
|
if (filter.status) {
|
|
where = {
|
|
...where,
|
|
status: filter.status,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.createdAtRange) {
|
|
const [start, end] = filter.createdAtRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
['createdAt']: {
|
|
...where.createdAt,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
['createdAt']: {
|
|
...where.createdAt,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
const queryOptions = {
|
|
where,
|
|
include,
|
|
distinct: true,
|
|
order: filter.field && filter.sort
|
|
? [[filter.field, filter.sort]]
|
|
: [['createdAt', 'desc']],
|
|
transaction: options?.transaction,
|
|
logging: console.log
|
|
};
|
|
|
|
if (!options?.countOnly) {
|
|
queryOptions.limit = limit ? Number(limit) : undefined;
|
|
queryOptions.offset = offset ? Number(offset) : undefined;
|
|
}
|
|
|
|
try {
|
|
const { rows, count } = await db.render_jobs.findAndCountAll(queryOptions);
|
|
|
|
return {
|
|
rows: options?.countOnly ? [] : rows,
|
|
count: count
|
|
};
|
|
} catch (error) {
|
|
console.error('Error executing query:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
static async findAllAutocomplete(query, limit, offset, ) {
|
|
let where = {};
|
|
|
|
|
|
|
|
if (query) {
|
|
where = {
|
|
[Op.or]: [
|
|
{ ['id']: Utils.uuid(query) },
|
|
Utils.ilike(
|
|
'render_jobs',
|
|
'idempotency_key',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.render_jobs.findAll({
|
|
attributes: [ 'id', 'idempotency_key' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['idempotency_key', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.idempotency_key,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|