38260-vm/backend/src/db/api/simulation_ticks.js
2026-02-07 03:34:22 +00:00

522 lines
13 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 Simulation_ticksDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const simulation_ticks = await db.simulation_ticks.create(
{
id: data.id || undefined,
tick_number: data.tick_number
||
null
,
sim_time: data.sim_time
||
null
,
delta_seconds: data.delta_seconds
||
null
,
state_hash: data.state_hash
||
null
,
notes: data.notes
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await simulation_ticks.setSimulation_run( data.simulation_run || null, {
transaction,
});
return simulation_ticks;
}
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 simulation_ticksData = data.map((item, index) => ({
id: item.id || undefined,
tick_number: item.tick_number
||
null
,
sim_time: item.sim_time
||
null
,
delta_seconds: item.delta_seconds
||
null
,
state_hash: item.state_hash
||
null
,
notes: item.notes
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const simulation_ticks = await db.simulation_ticks.bulkCreate(simulation_ticksData, { transaction });
// For each item created, replace relation files
return simulation_ticks;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const simulation_ticks = await db.simulation_ticks.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.tick_number !== undefined) updatePayload.tick_number = data.tick_number;
if (data.sim_time !== undefined) updatePayload.sim_time = data.sim_time;
if (data.delta_seconds !== undefined) updatePayload.delta_seconds = data.delta_seconds;
if (data.state_hash !== undefined) updatePayload.state_hash = data.state_hash;
if (data.notes !== undefined) updatePayload.notes = data.notes;
updatePayload.updatedById = currentUser.id;
await simulation_ticks.update(updatePayload, {transaction});
if (data.simulation_run !== undefined) {
await simulation_ticks.setSimulation_run(
data.simulation_run,
{ transaction }
);
}
return simulation_ticks;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const simulation_ticks = await db.simulation_ticks.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of simulation_ticks) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of simulation_ticks) {
await record.destroy({transaction});
}
});
return simulation_ticks;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const simulation_ticks = await db.simulation_ticks.findByPk(id, options);
await simulation_ticks.update({
deletedBy: currentUser.id
}, {
transaction,
});
await simulation_ticks.destroy({
transaction
});
return simulation_ticks;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const simulation_ticks = await db.simulation_ticks.findOne(
{ where },
{ transaction },
);
if (!simulation_ticks) {
return simulation_ticks;
}
const output = simulation_ticks.get({plain: true});
output.simulation_run = await simulation_ticks.getSimulation_run({
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.simulation_runs,
as: 'simulation_run',
where: filter.simulation_run ? {
[Op.or]: [
{ id: { [Op.in]: filter.simulation_run.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.simulation_run.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.state_hash) {
where = {
...where,
[Op.and]: Utils.ilike(
'simulation_ticks',
'state_hash',
filter.state_hash,
),
};
}
if (filter.notes) {
where = {
...where,
[Op.and]: Utils.ilike(
'simulation_ticks',
'notes',
filter.notes,
),
};
}
if (filter.tick_numberRange) {
const [start, end] = filter.tick_numberRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
tick_number: {
...where.tick_number,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
tick_number: {
...where.tick_number,
[Op.lte]: end,
},
};
}
}
if (filter.sim_timeRange) {
const [start, end] = filter.sim_timeRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
sim_time: {
...where.sim_time,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
sim_time: {
...where.sim_time,
[Op.lte]: end,
},
};
}
}
if (filter.delta_secondsRange) {
const [start, end] = filter.delta_secondsRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
delta_seconds: {
...where.delta_seconds,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
delta_seconds: {
...where.delta_seconds,
[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.simulation_ticks.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(
'simulation_ticks',
'state_hash',
query,
),
],
};
}
const records = await db.simulation_ticks.findAll({
attributes: [ 'id', 'state_hash' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['state_hash', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.state_hash,
}));
}
};