diff --git a/app/lib/database/adapters/DataPassAdapter.js b/app/lib/database/adapters/DataPassAdapter.js index 2f96e9c7a..f8461237d 100644 --- a/app/lib/database/adapters/DataPassAdapter.js +++ b/app/lib/database/adapters/DataPassAdapter.js @@ -26,7 +26,9 @@ class DataPassAdapter { description, reconstructedEvents, outputSize, - } = databaseObject; + runsCount, + simulationPassesCount, + } = databaseObject.dataValues; return { id, @@ -34,6 +36,8 @@ class DataPassAdapter { description, reconstructedEvents, outputSize, + runsCount: Number(runsCount), + simulationPassesCount: Number(simulationPassesCount), }; } diff --git a/app/lib/services/dataPasses/DataPassService.js b/app/lib/services/dataPasses/DataPassService.js index 6d9abd65c..75be0c827 100644 --- a/app/lib/services/dataPasses/DataPassService.js +++ b/app/lib/services/dataPasses/DataPassService.js @@ -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 { /** @@ -31,8 +67,14 @@ class DataPassService { * @returns {Promise} 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)), + }; } /** @@ -42,14 +84,17 @@ class DataPassService { * @returns {Promise} 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)), + }; } /** @@ -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)), + }; } }