Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 65 additions & 40 deletions app/lib/alimonitor-services/BookkeepingService.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ const AbstractServiceSynchronizer = require('./AbstractServiceSynchronizer.js');
const Utils = require('../utils');
const ServicesDataCommons = require('./ServicesDataCommons.js');
const EndpintFormatter = require('./ServicesEndpointsFormatter.js');
const { databaseManager: {
repositories: {
RunRepository,
DetectorSubsystemRepository,
PeriodRepository,
BeamTypeRepository,
RunDetectorsRepository,
},
} } = require('../database/DatabaseManager.js');

/**
* BookkeepingService used to synchronize runs
Expand All @@ -27,25 +36,28 @@ class BookkeepingService extends AbstractServiceSynchronizer {
this.batchSize = 100;

this.ketpFields = {
id: 'ali-bk-id',
runNumber: 'run_number',
lhcPeriod: 'period',
timeO2Start: 'time_start',
timeO2End: 'time_end',
timeTrgStart: 'time_trg_start',
timeTrgEnd: 'time_trg_end',
definition: 'run_type',
lhcBeamEnergy: 'energy',
runNumber: true,
lhcPeriod: 'periodName',
timeO2Start: true,
timeO2End: true,
timeTrgStart: true,
timeTrgEnd: true,
definition: 'runType',
lhcBeamEnergy: true,
detectorsQualities: 'detectors',
aliceL3Current: 'l3_current_val',
aliceL3Polarity: 'l3_current_polarity',
aliceDipoleCurrent: 'dipole_current_val',
aliceDipolePolarity: 'dipole_current_polarity',
fillNumber: 'fill_number',
pdpBeamType: 'beam_type',
fillNumber: true,
pdpBeamType: 'beamType',
};

this.RUN_TYPE_PHYSICS = 'PHYSICS';
DetectorSubsystemRepository.findAll({ raw: true }).then((r) => {
this.detectorsNameToId = r?.length > 0 ? r :
Utils.throwWrapper(new Error('Incorrect setup of database, no detector subsystems data in it'));
this.detectorsNameToId = Object.fromEntries(this.detectorsNameToId.map(({ id, name }) => [name, id]));
}).catch(this.logger.error);
}

async sync() {
Expand Down Expand Up @@ -80,10 +92,10 @@ class BookkeepingService extends AbstractServiceSynchronizer {
run.detectorNames = detectors.map(({ name }) => name.trim());
run.detectorQualities = detectors.map(({ quality }) => quality);

this.coilsCurrentsFieldsParsing(run, 'l3_current_val', 'l3_current_polarity', 'l3_current');
this.coilsCurrentsFieldsParsing(run, 'dipole_current_val', 'dipole_current_polarity', 'dipole_current');
this.coilsCurrentsFieldsParsing(run, 'l3_current_val', 'l3_current_polarity', 'l3CurrentVal');
this.coilsCurrentsFieldsParsing(run, 'dipole_current_val', 'dipole_current_polarity', 'dipoleCurrentVal');
ServicesDataCommons.mapBeamTypeToCommonFormat(run);
run.fill_number = Number(run.fill_number);
run.fillNumber = Number(run.fillNumber);
return run;
} catch (e) {
this.logger.error(e);
Expand All @@ -98,37 +110,50 @@ class BookkeepingService extends AbstractServiceSynchronizer {
} else if (run[polFN] == 'POSITIVE') {
run[tFN] = run[valFN];
} else {
throw `incorrect polarity type: '${run[polFN]}' for run: ${run.run_number}`;
throw `incorrect polarity type: '${run[polFN]}' for run: ${run.runNumber}`;
}
} else {
run[tFN] = null;
}
delete run[valFN];
delete run[polFN];
}

async dbAction(dbClient, d) {
const { period } = d;
const year = ServicesDataCommons.extractPeriodYear(period);
d = Utils.adjusetObjValuesToSql(d);
const period_insert = d.period ? `call insert_period(${d.period}, ${year}, ${d.beam_type});` : '';

const detectorInSql = `${d.detectorNames}::varchar[]`;
const detectorQualitiesInSql = `${d.detectorQualities}::varchar[]`;
const pgCommand = `${period_insert}; call insert_run (
${d.run_number},
${d.period},
${d.time_trg_start},
${d.time_trg_end},
${d.time_start},
${d.time_end},
${d.run_type},
${d.fill_number},
${d.energy},
${detectorInSql},
${detectorQualitiesInSql},
${d.l3_current},
${d.dipole_current}
);`;
return await dbClient.query(pgCommand);
async dbAction(_, run) {
const { periodName, detectorNames, detectorQualities, beamType } = run;
delete run.periodName;
delete run.detectorNames;
delete run.detectorQualities;
delete run.beamType;

const year = ServicesDataCommons.extractPeriodYear(periodName);
const { detectorsNameToId } = this;

return await BeamTypeRepository.T.findOrCreate({
where: {
name: beamType,
},
}).then(async ([beamType, _]) => await PeriodRepository.T.findOrCreate({
where: {
name: periodName,
year,
BeamTypeId: beamType.id,
},
})).then(async ([period, _]) => await RunRepository.T.findOrCreate({
where: {
runNumber: run.runNumber,
},
defaults: { PeriodId: period.id, ...run },
})).then(async ([run, _]) => {
const d = detectorNames?.map((detectorName, i) => ({
run_number: run.runNumber,
detector_id: detectorsNameToId[detectorName],
quality: detectorQualities[i] }));

await RunDetectorsRepository.T.bulkCreate(
d, { updateOnDublicate: ['quality'] },
);
});
}

metaDataHandler(requestJsonResult) {
Expand Down
4 changes: 2 additions & 2 deletions app/lib/alimonitor-services/MonalisaService.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class MonalisaService extends AbstractServiceSynchronizer {
d.rawDes = description;
const { period } = d;
const period_insert =
d?.period?.name ? `call insert_period(${period.name}, ${period.year}, ${period.beam_type});` : '';
d?.period?.name ? `call insert_period(${period.name}, ${period.year}, ${period.beamType});` : '';
const pgCommand = `${period_insert}; call insert_prod(
${d.name},
${d.period.name},
Expand All @@ -100,7 +100,7 @@ class MonalisaService extends AbstractServiceSynchronizer {
year += 2000;
}
period.year = year;
period.beam_type = rowData.beam_type;
period.beamType = rowData.beam_type;

return period;
} catch (e) {
Expand Down
6 changes: 3 additions & 3 deletions app/lib/alimonitor-services/ServicesDataCommons.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ const { rctData } = require('../config/configProvider.js');
* @returns {Object} dataObject
*/
function mapBeamTypeToCommonFormat(dataObject) {
dataObject.beam_type = Utils.switchCase(
dataObject.beam_type,
dataObject.beamType = Utils.switchCase(
dataObject.beamType,
rctData.mapping.beamType.values,
{ default: dataObject.beam_type },
{ default: dataObject.beamType },
);
return dataObject;
}
Expand Down
49 changes: 35 additions & 14 deletions app/lib/database/repositories/Repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Repository {
/**
* Creates a new `Repository` instance.
*
* @param {Object} model The database model to use.
* @param {Model} model The database model to use.
*/
constructor(model) {
this.model = model;
Expand All @@ -50,8 +50,8 @@ class Repository {
/**
* Returns all entities.
*
* @param {Object} queryBuilder the find query (see sequelize findAll options)
* @returns {Promise<array>} Promise object representing the full mock data
* @param {QueryBuilder|Object} queryBuilder the find query (see sequelize findAll options)
* @returns {Promise<Model>} Promise object representing the full mock data
*/
async findAll(queryBuilder = new QueryBuilder()) {
queryBuilder = queryBuilder instanceof QueryBuilder ? queryBuilder : new QueryBuilder(queryBuilder);
Expand All @@ -61,8 +61,8 @@ class Repository {
/**
* Returns one entity.
*
* @param {Object} queryBuilder the find query (see sequelize findOne options)
* @returns {Promise<Object>} Promise object representing the full mock data
* @param {QueryBuilder|Object} queryBuilder the find query (see sequelize findOne options)
* @returns {Promise<Model>} Promise object representing the full mock data
*/
async findOne(queryBuilder = {}) {
queryBuilder = queryBuilder instanceof QueryBuilder ? queryBuilder : new QueryBuilder(queryBuilder);
Expand All @@ -73,9 +73,9 @@ class Repository {
/**
* Apply a patch on a given dbObject and save the dbObject to the database
*
* @param {Object} dbOject the database object on which to apply the patch
* @param {Model} dbOject the database object on which to apply the patch
* @param {Object} patch the patch to apply
* @return {Promise<void>} promise that resolves when the patch has been applied
* @return {Promise<boolean>} promise that resolves when the patch has been applied
*/
async updateOne(dbOject, patch) {
return dbOject.update(patch);
Expand All @@ -84,10 +84,10 @@ class Repository {
/**
* Find a dbObject using query clause, apply given patch to it and save the dbObject to the database
*
* @param {Object} dbOject the database object on which to apply the patch
* @param {QueryBuilder|Object} dbOject the database object on which to apply the patch
* @param {Object} patch the patch to apply
* @throws {NotFoundEntityError} if cannot find dbObject with given query clause
* @return {Promise<void>} promise that resolves when the patch has been applied
* @return {Promise<Model>} promise that resolves when the patch has been applied
*/
async findOneAndUpdate(queryBuilder, patch) {
const entity = await this.model.findOne(queryBuilder) ??
Expand All @@ -98,19 +98,40 @@ class Repository {
/**
* Create new object in db
* @param {Object} dbObjectParams
* @return {Promise<Model>}
*/
async create(dbObjectParams) {
await this.model.create(dbObjectParams);
async create(dbObjectParams, opts) {
await this.model.create(dbObjectParams, opts);
}

/**
* Create new object in db
* @param {Object} queryBuilder
* Remove one object from database according search
* @param {QueryBuilder|Object} queryBuilder
* @return {Promise<boolean>} indicate if model object was deleted from db
*/
async removeOne(queryBuilder) {
queryBuilder = queryBuilder instanceof QueryBuilder ? queryBuilder : new QueryBuilder(queryBuilder);
queryBuilder.add({limit: 1})
return await this.model.destroy(queryBuilder.toImplementation());
return await this.model.destroy(queryBuilder.toImplementation()) == 1;
}

/**
* Find or create model instance in db
* @param {QueryBuilder|Object} queryBuilder
* @return {Promise<[Model, boolean]>}
*/
async findOrCreate(queryBuilder) {
queryBuilder = queryBuilder instanceof QueryBuilder ? queryBuilder : new QueryBuilder(queryBuilder);
return await this.model.findOrCreate(queryBuilder.toImplementation());
}

/**
* Create new objects in db
* @param {Array<Object>} dbObjectParams
* @return {Promise<Model>}
*/
async bulkCreate(dbObjectParams, opts) {
return await this.model.bulkCreate(dbObjectParams, opts)
}

_asT(customOptions) {
Expand Down
10 changes: 8 additions & 2 deletions app/lib/utils/obj-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,14 @@ function filterObject(obj, keptFields, suppressUndefined = false) {
}
const res = {};
for (const [nr, nl] of Object.entries(keptFields)) {
if (!suppressUndefined || res[nl]) {
res[nl] = obj[nr];
if (!suppressUndefined || obj[nr]) {
if (typeof nl === 'boolean') {
if (nl) {
res[nr] = obj[nr];
}
} else {
res[nl] = obj[nr];
}
}
}
return res;
Expand Down

This file was deleted.

Loading