481 lines
11 KiB
JavaScript
481 lines
11 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 Oee_metricsDBApi {
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const oee_metrics = await db.oee_metrics.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
oee: data.oee || null,
|
|
cpm: data.cpm || null,
|
|
uptime: data.uptime || null,
|
|
downtime: data.downtime || null,
|
|
quality: data.quality || null,
|
|
mtbf: data.mtbf || null,
|
|
mttr: data.mttr || null,
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
await oee_metrics.setMachine(data.machine || null, {
|
|
transaction,
|
|
});
|
|
|
|
return oee_metrics;
|
|
}
|
|
|
|
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 oee_metricsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
oee: item.oee || null,
|
|
cpm: item.cpm || null,
|
|
uptime: item.uptime || null,
|
|
downtime: item.downtime || null,
|
|
quality: item.quality || null,
|
|
mtbf: item.mtbf || null,
|
|
mttr: item.mttr || null,
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const oee_metrics = await db.oee_metrics.bulkCreate(oee_metricsData, {
|
|
transaction,
|
|
});
|
|
|
|
// For each item created, replace relation files
|
|
|
|
return oee_metrics;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const oee_metrics = await db.oee_metrics.findByPk(id, {}, { transaction });
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.oee !== undefined) updatePayload.oee = data.oee;
|
|
|
|
if (data.cpm !== undefined) updatePayload.cpm = data.cpm;
|
|
|
|
if (data.uptime !== undefined) updatePayload.uptime = data.uptime;
|
|
|
|
if (data.downtime !== undefined) updatePayload.downtime = data.downtime;
|
|
|
|
if (data.quality !== undefined) updatePayload.quality = data.quality;
|
|
|
|
if (data.mtbf !== undefined) updatePayload.mtbf = data.mtbf;
|
|
|
|
if (data.mttr !== undefined) updatePayload.mttr = data.mttr;
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await oee_metrics.update(updatePayload, { transaction });
|
|
|
|
if (data.machine !== undefined) {
|
|
await oee_metrics.setMachine(
|
|
data.machine,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
return oee_metrics;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const oee_metrics = await db.oee_metrics.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of oee_metrics) {
|
|
await record.update({ deletedBy: currentUser.id }, { transaction });
|
|
}
|
|
for (const record of oee_metrics) {
|
|
await record.destroy({ transaction });
|
|
}
|
|
});
|
|
|
|
return oee_metrics;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const oee_metrics = await db.oee_metrics.findByPk(id, options);
|
|
|
|
await oee_metrics.update(
|
|
{
|
|
deletedBy: currentUser.id,
|
|
},
|
|
{
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
await oee_metrics.destroy({
|
|
transaction,
|
|
});
|
|
|
|
return oee_metrics;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const oee_metrics = await db.oee_metrics.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!oee_metrics) {
|
|
return oee_metrics;
|
|
}
|
|
|
|
const output = oee_metrics.get({ plain: true });
|
|
|
|
output.machine = await oee_metrics.getMachine({
|
|
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.machines,
|
|
as: 'machine',
|
|
|
|
where: filter.machine
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: filter.machine
|
|
.split('|')
|
|
.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
name: {
|
|
[Op.or]: filter.machine
|
|
.split('|')
|
|
.map((term) => ({ [Op.iLike]: `%${term}%` })),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: {},
|
|
},
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
if (filter.oeeRange) {
|
|
const [start, end] = filter.oeeRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
oee: {
|
|
...where.oee,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
oee: {
|
|
...where.oee,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.cpmRange) {
|
|
const [start, end] = filter.cpmRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
cpm: {
|
|
...where.cpm,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
cpm: {
|
|
...where.cpm,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.uptimeRange) {
|
|
const [start, end] = filter.uptimeRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
uptime: {
|
|
...where.uptime,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
uptime: {
|
|
...where.uptime,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.downtimeRange) {
|
|
const [start, end] = filter.downtimeRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
downtime: {
|
|
...where.downtime,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
downtime: {
|
|
...where.downtime,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.qualityRange) {
|
|
const [start, end] = filter.qualityRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
quality: {
|
|
...where.quality,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
quality: {
|
|
...where.quality,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.mtbfRange) {
|
|
const [start, end] = filter.mtbfRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
mtbf: {
|
|
...where.mtbf,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
mtbf: {
|
|
...where.mtbf,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.mttrRange) {
|
|
const [start, end] = filter.mttrRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
mttr: {
|
|
...where.mttr,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
mttr: {
|
|
...where.mttr,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true',
|
|
};
|
|
}
|
|
|
|
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.oee_metrics.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('oee_metrics', 'oee', query),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.oee_metrics.findAll({
|
|
attributes: ['id', 'oee'],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['oee', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.oee,
|
|
}));
|
|
}
|
|
};
|