649 lines
16 KiB
JavaScript
649 lines
16 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 Monitoring_jobsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const monitoring_jobs = await db.monitoring_jobs.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
job_name: data.job_name
|
|
||
|
|
null
|
|
,
|
|
|
|
job_type: data.job_type
|
|
||
|
|
null
|
|
,
|
|
|
|
job_status: data.job_status
|
|
||
|
|
null
|
|
,
|
|
|
|
scheduled_for: data.scheduled_for
|
|
||
|
|
null
|
|
,
|
|
|
|
started_at_time: data.started_at_time
|
|
||
|
|
null
|
|
,
|
|
|
|
finished_at_time: data.finished_at_time
|
|
||
|
|
null
|
|
,
|
|
|
|
retry_count: data.retry_count
|
|
||
|
|
null
|
|
,
|
|
|
|
max_retries: data.max_retries
|
|
||
|
|
null
|
|
,
|
|
|
|
last_error_message: data.last_error_message
|
|
||
|
|
null
|
|
,
|
|
|
|
runtime_log: data.runtime_log
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await monitoring_jobs.setChannel( data.channel || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return monitoring_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 monitoring_jobsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
job_name: item.job_name
|
|
||
|
|
null
|
|
,
|
|
|
|
job_type: item.job_type
|
|
||
|
|
null
|
|
,
|
|
|
|
job_status: item.job_status
|
|
||
|
|
null
|
|
,
|
|
|
|
scheduled_for: item.scheduled_for
|
|
||
|
|
null
|
|
,
|
|
|
|
started_at_time: item.started_at_time
|
|
||
|
|
null
|
|
,
|
|
|
|
finished_at_time: item.finished_at_time
|
|
||
|
|
null
|
|
,
|
|
|
|
retry_count: item.retry_count
|
|
||
|
|
null
|
|
,
|
|
|
|
max_retries: item.max_retries
|
|
||
|
|
null
|
|
,
|
|
|
|
last_error_message: item.last_error_message
|
|
||
|
|
null
|
|
,
|
|
|
|
runtime_log: item.runtime_log
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const monitoring_jobs = await db.monitoring_jobs.bulkCreate(monitoring_jobsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return monitoring_jobs;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const monitoring_jobs = await db.monitoring_jobs.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.job_name !== undefined) updatePayload.job_name = data.job_name;
|
|
|
|
|
|
if (data.job_type !== undefined) updatePayload.job_type = data.job_type;
|
|
|
|
|
|
if (data.job_status !== undefined) updatePayload.job_status = data.job_status;
|
|
|
|
|
|
if (data.scheduled_for !== undefined) updatePayload.scheduled_for = data.scheduled_for;
|
|
|
|
|
|
if (data.started_at_time !== undefined) updatePayload.started_at_time = data.started_at_time;
|
|
|
|
|
|
if (data.finished_at_time !== undefined) updatePayload.finished_at_time = data.finished_at_time;
|
|
|
|
|
|
if (data.retry_count !== undefined) updatePayload.retry_count = data.retry_count;
|
|
|
|
|
|
if (data.max_retries !== undefined) updatePayload.max_retries = data.max_retries;
|
|
|
|
|
|
if (data.last_error_message !== undefined) updatePayload.last_error_message = data.last_error_message;
|
|
|
|
|
|
if (data.runtime_log !== undefined) updatePayload.runtime_log = data.runtime_log;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await monitoring_jobs.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.channel !== undefined) {
|
|
await monitoring_jobs.setChannel(
|
|
|
|
data.channel,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return monitoring_jobs;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const monitoring_jobs = await db.monitoring_jobs.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of monitoring_jobs) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of monitoring_jobs) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return monitoring_jobs;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const monitoring_jobs = await db.monitoring_jobs.findByPk(id, options);
|
|
|
|
await monitoring_jobs.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await monitoring_jobs.destroy({
|
|
transaction
|
|
});
|
|
|
|
return monitoring_jobs;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const monitoring_jobs = await db.monitoring_jobs.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!monitoring_jobs) {
|
|
return monitoring_jobs;
|
|
}
|
|
|
|
const output = monitoring_jobs.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.channel = await monitoring_jobs.getChannel({
|
|
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.telegram_channels,
|
|
as: 'channel',
|
|
|
|
where: filter.channel ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.channel.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
channel_title: {
|
|
[Op.or]: filter.channel.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.job_name) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'monitoring_jobs',
|
|
'job_name',
|
|
filter.job_name,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.last_error_message) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'monitoring_jobs',
|
|
'last_error_message',
|
|
filter.last_error_message,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.runtime_log) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'monitoring_jobs',
|
|
'runtime_log',
|
|
filter.runtime_log,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.scheduled_forRange) {
|
|
const [start, end] = filter.scheduled_forRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
scheduled_for: {
|
|
...where.scheduled_for,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
scheduled_for: {
|
|
...where.scheduled_for,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.started_at_timeRange) {
|
|
const [start, end] = filter.started_at_timeRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
started_at_time: {
|
|
...where.started_at_time,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
started_at_time: {
|
|
...where.started_at_time,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.finished_at_timeRange) {
|
|
const [start, end] = filter.finished_at_timeRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
finished_at_time: {
|
|
...where.finished_at_time,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
finished_at_time: {
|
|
...where.finished_at_time,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.retry_countRange) {
|
|
const [start, end] = filter.retry_countRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
retry_count: {
|
|
...where.retry_count,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
retry_count: {
|
|
...where.retry_count,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.max_retriesRange) {
|
|
const [start, end] = filter.max_retriesRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
max_retries: {
|
|
...where.max_retries,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
max_retries: {
|
|
...where.max_retries,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.job_type) {
|
|
where = {
|
|
...where,
|
|
job_type: filter.job_type,
|
|
};
|
|
}
|
|
|
|
if (filter.job_status) {
|
|
where = {
|
|
...where,
|
|
job_status: filter.job_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.monitoring_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(
|
|
'monitoring_jobs',
|
|
'job_name',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.monitoring_jobs.findAll({
|
|
attributes: [ 'id', 'job_name' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['job_name', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.job_name,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|