diff --git a/lib/database/adapters/GaqSummaryAdapter.js b/lib/database/adapters/GaqSummaryAdapter.js new file mode 100644 index 0000000000..52e9ce0a30 --- /dev/null +++ b/lib/database/adapters/GaqSummaryAdapter.js @@ -0,0 +1,64 @@ +/** + * @license + * Copyright CERN and copyright holders of ALICE O2. This software is + * distributed under the terms of the GNU General Public License v3 (GPL + * Version 3), copied verbatim in the file "COPYING". + * + * See http://alice-o2.web.cern.ch/license for full licensing information. + * + * In applying this license CERN does not waive the privileges and immunities + * granted to it by virtue of its status as an Intergovernmental Organization + * or submit itself to any jurisdiction. + */ + +/** + * GaqSummaryAdapter + */ +class GaqSummaryAdapter { + /** + * Constructor + */ + constructor() { + this.toEntity = this.toEntity.bind(this); + } + + /** + * Converts the given database object to an entity object. + * + * @param {SequelizeGaqSummary} databaseObject Object to convert. + * @returns {GaqSummaryRecord} Converted entity object. + */ + toEntity(databaseObject) { + const { + dataPassId, + runNumber, + badRunCoverage, + explicitlyNotBadRunCoverage, + mcReproducibleCoverage, + missingVerificationsCount, + undefinedQualityPeriodsCount, + notComputable, + invalidatedAt, + computedAt, + createdAt, + updatedAt, + } = databaseObject; + + return { + dataPassId, + runNumber, + badRunCoverage, + explicitlyNotBadRunCoverage, + mcReproducibleCoverage, + missingVerificationsCount, + undefinedQualityPeriodsCount, + notComputable, + invalidatedAt: invalidatedAt ? new Date(invalidatedAt).getTime() : null, + computedAt: computedAt ? new Date(computedAt).getTime() : null, + createdAt: new Date(createdAt).getTime(), + updatedAt: new Date(updatedAt).getTime(), + }; + } +} + +module.exports = { GaqSummaryAdapter }; diff --git a/lib/database/adapters/index.js b/lib/database/adapters/index.js index 433a4febeb..1f90a27ab6 100644 --- a/lib/database/adapters/index.js +++ b/lib/database/adapters/index.js @@ -27,6 +27,7 @@ const EorReasonAdapter = require('./EorReasonAdapter'); const FlpRoleAdapter = require('./FlpRoleAdapter'); const { HostAdapter } = require('./HostAdapter.js'); const { GaqDetectorAdapter } = require('./GaqDetectorAdapter.js'); +const { GaqSummaryAdapter } = require('./GaqSummaryAdapter.js'); const { LhcFillAdapter } = require('./LhcFillAdapter.js'); const { LhcFillStatisticsAdapter } = require('./LhcFillStatisticsAdapter.js'); const LhcPeriodAdapter = require('./LhcPeriodAdapter'); @@ -63,6 +64,7 @@ const environmentHistoryItemAdapter = new EnvironmentHistoryItemAdapter(); const eorReasonAdapter = new EorReasonAdapter(); const flpRoleAdapter = new FlpRoleAdapter(); const gaqDetectorAdapter = new GaqDetectorAdapter(); +const gaqSummaryAdapter = new GaqSummaryAdapter(); const hostAdapter = new HostAdapter(); const lhcFillAdapter = new LhcFillAdapter(); const lhcFillStatisticsAdapter = new LhcFillStatisticsAdapter(); @@ -164,6 +166,7 @@ module.exports = { eorReasonAdapter, flpRoleAdapter, gaqDetectorAdapter, + gaqSummaryAdapter, hostAdapter, lhcFillAdapter, lhcFillStatisticsAdapter, diff --git a/lib/database/migrations/v1/20261202100000-create-gaq-summary-tables.js b/lib/database/migrations/v1/20261202100000-create-gaq-summary-tables.js new file mode 100644 index 0000000000..ced71e5505 --- /dev/null +++ b/lib/database/migrations/v1/20261202100000-create-gaq-summary-tables.js @@ -0,0 +1,76 @@ +'use strict'; + +/** @type {import('sequelize-cli').Migration} */ +module.exports = { + up: async (queryInterface, Sequelize) => queryInterface.sequelize.transaction(async (transaction) => { + await queryInterface.createTable('gaq_summaries', { + data_pass_id: { + type: Sequelize.INTEGER, + primaryKey: true, + allowNull: false, + references: { + model: 'data_passes', + key: 'id', + }, + }, + run_number: { + type: Sequelize.INTEGER, + primaryKey: true, + allowNull: false, + references: { + model: 'runs', + key: 'run_number', + }, + }, + bad_run_coverage: { + type: Sequelize.FLOAT, + }, + explicitly_not_bad_run_coverage: { + type: Sequelize.FLOAT, + }, + mc_reproducible_coverage: { + type: Sequelize.FLOAT, + }, + missing_verifications_count: { + type: Sequelize.INTEGER, + }, + undefined_quality_periods_count: { + type: Sequelize.INTEGER, + }, + not_computable: { + type: Sequelize.BOOLEAN, + allowNull: false, + defaultValue: false, + }, + invalidated_at: { + type: Sequelize.DATE(3), + allowNull: true, + defaultValue: null, + }, + computed_at: { + type: Sequelize.DATE(3), + allowNull: true, + defaultValue: null, + }, + created_at: { + type: Sequelize.DATE(3), + allowNull: false, + defaultValue: Sequelize.literal('CURRENT_TIMESTAMP(3)'), + }, + updated_at: { + type: Sequelize.DATE(3), + allowNull: false, + defaultValue: Sequelize.literal('CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3)'), + }, + }, { transaction }); + + await queryInterface.addIndex('gaq_summaries', { + name: 'gaq_summaries_invalidated_at_idx', + fields: ['invalidated_at'], + }, { transaction }); + }), + + down: async (queryInterface) => queryInterface.sequelize.transaction(async (transaction) => { + await queryInterface.dropTable('gaq_summaries', { transaction }); + }), +}; diff --git a/lib/database/models/gaqSummary.js b/lib/database/models/gaqSummary.js new file mode 100644 index 0000000000..93aab8a632 --- /dev/null +++ b/lib/database/models/gaqSummary.js @@ -0,0 +1,83 @@ +/** + * @license + * Copyright CERN and copyright holders of ALICE O2. This software is + * distributed under the terms of the GNU General Public License v3 (GPL + * Version 3), copied verbatim in the file "COPYING". + * + * See http://alice-o2.web.cern.ch/license for full licensing information. + * + * In applying this license CERN does not waive the privileges and immunities + * granted to it by virtue of its status as an Intergovernmental Organization + * or submit itself to any jurisdiction. + */ + +/* + * Stored GAQ (Global Aggregated Quality) summary, one row per run in a data pass + * + * - The three coverages (`badRunCoverage`, `explicitlyNotBadRunCoverage`, `mcReproducibleCoverage`) are the raw split of the run's + * data and are not combined: `badRunCoverage` does not include the mc reproducible part. The `mcReproducibleAsNotBad` option is + * applied when reading, so it must never be baked into the stored values + * - Null coverages and counts mean that the summary has never been computed, or is not computable (see `notComputable`) + * - `invalidatedAt` not null means that the summary is stale since that moment, and is to be recomputed + * - `computedAt` is the moment of the last successful computation, null if never computed. It is needed because `updatedAt` + * cannot tell it: `updatedAt` changes on any write to the row, including the ones that only set or clear `invalidatedAt`. + * It allows to display when the values were calculated, and to tell how old they are (`computedAt` against `invalidatedAt`) + * when investigating stale summaries + */ +module.exports = (sequelize) => { + const Sequelize = require('sequelize'); + + const GaqSummary = sequelize.define('GaqSummary', { + dataPassId: { + type: Sequelize.INTEGER, + primaryKey: true, + }, + runNumber: { + type: Sequelize.INTEGER, + primaryKey: true, + }, + badRunCoverage: { + type: Sequelize.FLOAT, + }, + explicitlyNotBadRunCoverage: { + type: Sequelize.FLOAT, + }, + mcReproducibleCoverage: { + type: Sequelize.FLOAT, + }, + missingVerificationsCount: { + type: Sequelize.INTEGER, + }, + undefinedQualityPeriodsCount: { + type: Sequelize.INTEGER, + }, + notComputable: { + type: Sequelize.BOOLEAN, + allowNull: false, + defaultValue: false, + }, + invalidatedAt: { + type: Sequelize.DATE(3), + }, + computedAt: { + type: Sequelize.DATE(3), + }, + }, { + tableName: 'gaq_summaries', + indexes: [ + { + name: 'gaq_summaries_invalidated_at_idx', + fields: ['invalidated_at'], + }, + ], + }); + + GaqSummary.removeAttribute('id'); + + GaqSummary.associate = (models) => { + GaqSummary.belongsTo(models.Run, { foreignKey: 'runNumber', as: 'run' }); + GaqSummary.belongsTo(models.DataPass, { foreignKey: 'dataPassId', as: 'dataPass' }); + }; + + return GaqSummary; +}; diff --git a/lib/database/models/index.js b/lib/database/models/index.js index 87d793fac3..2549209c5b 100644 --- a/lib/database/models/index.js +++ b/lib/database/models/index.js @@ -27,6 +27,7 @@ const EorReason = require('./eorreason'); const EpnRoleSession = require('./epnrolesession'); const FlpRole = require('./flprole'); const GaqDetector = require('./gaqDetector.js'); +const GaqSummary = require('./gaqSummary.js'); const Host = require('./host.js'); const LhcFill = require('./lhcFill'); const LhcFillStatistics = require('./lhcFillStatistics.js'); @@ -66,6 +67,7 @@ module.exports = (sequelize) => { EpnRoleSessionkey: EpnRoleSession(sequelize), FlpRole: FlpRole(sequelize), GaqDetector: GaqDetector(sequelize), + GaqSummary: GaqSummary(sequelize), Host: Host(sequelize), LhcFill: LhcFill(sequelize), LhcFillStatistics: LhcFillStatistics(sequelize), diff --git a/lib/database/models/typedefs/SequelizeGaqSummary.js b/lib/database/models/typedefs/SequelizeGaqSummary.js new file mode 100644 index 0000000000..45a2efac36 --- /dev/null +++ b/lib/database/models/typedefs/SequelizeGaqSummary.js @@ -0,0 +1,37 @@ +/** + * @license + * Copyright CERN and copyright holders of ALICE O2. This software is + * distributed under the terms of the GNU General Public License v3 (GPL + * Version 3), copied verbatim in the file "COPYING". + * + * See http://alice-o2.web.cern.ch/license for full licensing information. + * + * In applying this license CERN does not waive the privileges and immunities + * granted to it by virtue of its status as an Intergovernmental Organization + * or submit itself to any jurisdiction. + */ + +/** + * @typedef SequelizeGaqSummary GAQ: Global Aggregated Quality, stored summary of a run in a data pass + * + * The three coverages are the raw split of the run's data by aggregated quality (bad, mc reproducible and explicitly + * not bad), and are NOT combined: `badRunCoverage` does not include `mcReproducibleCoverage`. Combining them according to + * `mcReproducibleAsNotBad` is done when the summary is read, so that toggling it does not require a recomputation. + * + * Coverages and counts are null when the summary has never been computed, or when it is not computable (see `notComputable`). + * + * @property {number} dataPassId + * @property {number} runNumber + * @property {number|null} badRunCoverage - fraction of run's data with aggregated quality bad, excluding mc reproducible + * @property {number|null} explicitlyNotBadRunCoverage - fraction of run's data with aggregated quality explicitly not bad + * @property {number|null} mcReproducibleCoverage - fraction of run's data with aggregated quality mc reproducible + * @property {number|null} missingVerificationsCount - number of QC flags that are unverified and have not been discarded + * @property {number|null} undefinedQualityPeriodsCount - number of GAQ periods with no QC flag, counted once per detector without flag + * @property {boolean} notComputable - true if the last computation could not produce a summary, coverages and counts are then null + * @property {Date|null} invalidatedAt - null if the summary is up to date, otherwise the moment since which it is stale + * @property {Date|null} computedAt - moment of the last successful computation, null if never computed. Unlike `updatedAt`, which + * changes on any write (including setting or clearing `invalidatedAt`), it changes only when the values are computed. It allows + * to display when the values were calculated and to tell how old they are when investigating stale summaries + * @property {Date} createdAt + * @property {Date} updatedAt + */ diff --git a/lib/database/repositories/GaqSummaryRepository.js b/lib/database/repositories/GaqSummaryRepository.js new file mode 100644 index 0000000000..98986de513 --- /dev/null +++ b/lib/database/repositories/GaqSummaryRepository.js @@ -0,0 +1,33 @@ +/** + * @license + * Copyright CERN and copyright holders of ALICE O2. This software is + * distributed under the terms of the GNU General Public License v3 (GPL + * Version 3), copied verbatim in the file "COPYING". + * + * See http://alice-o2.web.cern.ch/license for full licensing information. + * + * In applying this license CERN does not waive the privileges and immunities + * granted to it by virtue of its status as an Intergovernmental Organization + * or submit itself to any jurisdiction. + */ + +const { + models: { + GaqSummary, + }, +} = require('..'); +const Repository = require('./Repository'); + +/** + * GaqSummary repository + */ +class GaqSummaryRepository extends Repository { + /** + * Creates a new `GaqSummaryRepository` instance. + */ + constructor() { + super(GaqSummary); + } +} + +module.exports = new GaqSummaryRepository(); diff --git a/lib/database/repositories/index.js b/lib/database/repositories/index.js index 53673cc9c1..b76c01614b 100644 --- a/lib/database/repositories/index.js +++ b/lib/database/repositories/index.js @@ -27,6 +27,7 @@ const EnvironmentRepository = require('./EnvironmentRepository'); const EorReasonRepository = require('./EorReasonRepository'); const FlpRoleRepository = require('./FlpRoleRepository'); const GaqDetectorRepository = require('./GaqDetectorRepository.js'); +const GaqSummaryRepository = require('./GaqSummaryRepository.js'); const HostRepository = require('./HostRepository.js'); const LhcFillRepository = require('./LhcFillRepository'); const LhcFillStatisticsRepository = require('./LhcFillStatisticsRepository.js'); @@ -72,6 +73,7 @@ module.exports = { EorReasonRepository, FlpRoleRepository, GaqDetectorRepository, + GaqSummaryRepository, HostRepository, LhcFillRepository, LhcFillStatisticsRepository, diff --git a/lib/domain/entities/GaqSummaryRecord.js b/lib/domain/entities/GaqSummaryRecord.js new file mode 100644 index 0000000000..da6b31f6ca --- /dev/null +++ b/lib/domain/entities/GaqSummaryRecord.js @@ -0,0 +1,38 @@ +/** + * @license + * Copyright CERN and copyright holders of ALICE O2. This software is + * distributed under the terms of the GNU General Public License v3 (GPL + * Version 3), copied verbatim in the file "COPYING". + * + * See http://alice-o2.web.cern.ch/license for full licensing information. + * + * In applying this license CERN does not waive the privileges and immunities + * granted to it by virtue of its status as an Intergovernmental Organization + * or submit itself to any jurisdiction. + */ + +/** + * @typedef GaqSummaryRecord GAQ: Global Aggregated Quality, stored summary of a run in a data pass + * + * Not to be confused with `GaqSummary` (see QcSummary.js), which is the mapping from run number to the summary served by the API. + * + * The three coverages are the raw split of the run's data by aggregated quality (bad, mc reproducible and explicitly + * not bad), and are NOT combined: `badRunCoverage` does not include `mcReproducibleCoverage`. + * + * Coverages and counts are null when the summary has never been computed, or when it is not computable (see `notComputable`). + * + * @property {number} dataPassId + * @property {number} runNumber + * @property {number|null} badRunCoverage - fraction of run's data with aggregated quality bad, excluding mc reproducible + * @property {number|null} explicitlyNotBadRunCoverage - fraction of run's data with aggregated quality explicitly not bad + * @property {number|null} mcReproducibleCoverage - fraction of run's data with aggregated quality mc reproducible + * @property {number|null} missingVerificationsCount - number of QC flags that are unverified and have not been discarded + * @property {number|null} undefinedQualityPeriodsCount - number of GAQ periods with no QC flag, counted once per detector without flag + * @property {boolean} notComputable - true if the last computation could not produce a summary, coverages and counts are then null + * @property {number|null} invalidatedAt - null if the summary is up to date, otherwise the timestamp since which it is stale + * @property {number|null} computedAt - timestamp of the last successful computation, null if never computed. Unlike `updatedAt`, + * which changes on any write (including setting or clearing `invalidatedAt`), it changes only when the values are computed. It allows + * to display when the values were calculated and to tell how old they are when investigating stale summaries + * @property {number} createdAt + * @property {number} updatedAt + */