Skip to content

Commit

Permalink
[ORCT-162] provide number of existing related data for data passes vi…
Browse files Browse the repository at this point in the history
…ew (#236)
  • Loading branch information
xsalonx authored Oct 5, 2023
1 parent 164bb91 commit 444c317
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 16 deletions.
6 changes: 5 additions & 1 deletion app/lib/database/adapters/DataPassAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,18 @@ class DataPassAdapter {
description,
reconstructedEvents,
outputSize,
} = databaseObject;
runsCount,
simulationPassesCount,
} = databaseObject.dataValues;

return {
id,
name,
description,
reconstructedEvents,
outputSize,
runsCount: Number(runsCount),
simulationPassesCount: Number(simulationPassesCount),
};
}

Expand Down
90 changes: 75 additions & 15 deletions app/lib/services/dataPasses/DataPassService.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,54 @@
* or submit itself to any jurisdiction.
*/

const { Sequelize } = require('sequelize');
const {
databaseManager: {
repositories: {
DataPassRepository,
},
models: {
Run,
SimulationPass,
},
},
} = require('../../database/DatabaseManager');
const { dataPassAdapter } = require('../../database/adapters');
const { QueryBuilder } = require('../../database/utilities');
const { deepmerge } = require('../../utils');

const additionalFields = [
[Sequelize.fn('count', Sequelize.fn('DISTINCT', Sequelize.col('Runs.run_number'))), 'runsCount'],
[Sequelize.fn('count', Sequelize.fn('DISTINCT', Sequelize.col('SimulationPasses.id'))), 'simulationPassesCount'],
];
const commonGroupClause = ['DataPass.id'];

const commonClause = {
include: [
{
model: Run,
require: false,
attributes: [],
through: {
attributes: [],
},
},
{
model: SimulationPass,
require: false,
attributes: [],
through: {
attributes: [],
},
},
],
attributes: {
include: additionalFields,
},

group: commonGroupClause,
subQuery: false,
};

class DataPassService {
/**
Expand All @@ -31,8 +67,14 @@ class DataPassService {
* @returns {Promise<DataPass[]>} Promise object represents the result of this use case.
*/
async getAll(query) {
const { count, rows } = await DataPassRepository.findAndCountAll(new QueryBuilder().addFromHttpRequestQuery(query));
return { count, rows: rows.map((dataPass) => dataPassAdapter.toEntity(dataPass)) };
const baseClause = commonClause;

const { count, rows } = await DataPassRepository.findAndCountAll(new QueryBuilder(baseClause).addFromHttpRequestQuery(query));

return {
count: count.length,
rows: rows.map((dataPass) => dataPassAdapter.toEntity(dataPass)),
};
}

/**
Expand All @@ -42,14 +84,17 @@ class DataPassService {
* @returns {Promise<DataPass[]>} Promise object represents the result of this use case.
*/
async getDataPassesPerPeriod(periodId, query) {
const baseClause = {
const baseClause = deepmerge(commonClause, {
where: {
period_id: periodId,
},
};
});

const { count, rows } = await DataPassRepository.findAndCountAll(new QueryBuilder(baseClause).addFromHttpRequestQuery(query));
return { count, rows: rows.map((dataPass) => dataPassAdapter.toEntity(dataPass)) };
return {
count: count.length,
rows: rows.map((dataPass) => dataPassAdapter.toEntity(dataPass)),
};
}

/**
Expand All @@ -60,22 +105,37 @@ class DataPassService {
*/
async getAnchoredToSimulationPass(simulationPassId, query) {
const baseClause = {
include: [
{
model: SimulationPass,
require: true,
attributes: [],
through: {
...commonClause,
...{
include: [
{
model: Run,
require: false,
attributes: [],
through: {
attributes: [],
},
},
{
model: SimulationPass,
require: false,
attributes: [],
where: {
sim_pass_id: simulationPassId,
id: simulationPassId,
},
through: {
attributes: [],
},
},
},
],
],
},
};

const { count, rows } = await DataPassRepository.findAndCountAll(new QueryBuilder(baseClause).addFromHttpRequestQuery(query));
return { count, rows: rows.map((dataPass) => dataPassAdapter.toEntity(dataPass)) };
return {
count: count.length,
rows: rows.map((dataPass) => dataPassAdapter.toEntity(dataPass)),
};
}
}

Expand Down

0 comments on commit 444c317

Please sign in to comment.