|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright CERN and copyright holders of ALICE O2. This software is |
| 4 | + * distributed under the terms of the GNU General Public License v3 (GPL |
| 5 | + * Version 3), copied verbatim in the file "COPYING". |
| 6 | + * |
| 7 | + * See http://alice-o2.web.cern.ch/license for full licensing information. |
| 8 | + * |
| 9 | + * In applying this license CERN does not waive the privileges and immunities |
| 10 | + * granted to it by virtue of its status as an Intergovernmental Organization |
| 11 | + * or submit itself to any jurisdiction. |
| 12 | + */ |
| 13 | +import { getOneDataPassOrFail } from '../dataPasses/getOneDataPassOrFail.js'; |
| 14 | +import { Op } from 'sequelize'; |
| 15 | +import { qcFlagAdapter } from '../../../database/adapters/index.js'; |
| 16 | +import { QcFlagRepository } from '../../../database/repositories/index.js'; |
| 17 | +import { QcFlagSummaryService } from './QcFlagSummaryService.js'; |
| 18 | + |
| 19 | +const QC_SUMMARY_PROPERTIES = { |
| 20 | + badEffectiveRunCoverage: 'badEffectiveRunCoverage', |
| 21 | + explicitlyNotBadEffectiveRunCoverage: 'explicitlyNotBadEffectiveRunCoverage', |
| 22 | + missingVerificationsCount: 'missingVerificationsCount', |
| 23 | + mcReproducible: 'mcReproducible', |
| 24 | +}; |
| 25 | + |
| 26 | +/** |
| 27 | + * Globally aggregated quality (QC flags aggregated for a predefined list of detectors per runs) service |
| 28 | + */ |
| 29 | +export class GaqService { |
| 30 | + /** |
| 31 | + * Get GAQ summary |
| 32 | + * |
| 33 | + * @param {number} dataPassId id of data pass id |
| 34 | + * @param {object} [options] additional options |
| 35 | + * @param {boolean} [options.mcReproducibleAsNotBad = false] if set to true, |
| 36 | + * `Limited Acceptance MC Reproducible` flag type is treated as good one |
| 37 | + * @return {Promise<GaqSummary[]>} Resolves with the GAQ Summary |
| 38 | + */ |
| 39 | + async getSummary(dataPassId, { mcReproducibleAsNotBad = false } = {}) { |
| 40 | + await getOneDataPassOrFail({ id: dataPassId }); |
| 41 | + const runGaqSubSummaries = await QcFlagRepository.getRunGaqSubSummaries(dataPassId, { mcReproducibleAsNotBad }); |
| 42 | + |
| 43 | + const summary = {}; |
| 44 | + const flagsAndVerifications = {}; |
| 45 | + |
| 46 | + // Fold list of subSummaries into one summary |
| 47 | + for (const subSummary of runGaqSubSummaries) { |
| 48 | + const { |
| 49 | + runNumber, |
| 50 | + flagsIds, |
| 51 | + verifiedFlagsIds, |
| 52 | + } = subSummary; |
| 53 | + |
| 54 | + if (!summary[runNumber]) { |
| 55 | + summary[runNumber] = { [QC_SUMMARY_PROPERTIES.mcReproducible]: false }; |
| 56 | + } |
| 57 | + if (!flagsAndVerifications[runNumber]) { |
| 58 | + flagsAndVerifications[runNumber] = {}; |
| 59 | + } |
| 60 | + |
| 61 | + const runSummary = summary[runNumber]; |
| 62 | + |
| 63 | + const distinctRunFlagsIds = flagsAndVerifications[runNumber]?.distinctFlagsIds ?? []; |
| 64 | + const distinctRunVerifiedFlagsIds = flagsAndVerifications[runNumber]?.distinctVerifiedFlagsIds ?? []; |
| 65 | + |
| 66 | + flagsAndVerifications[runNumber] = { |
| 67 | + distinctFlagsIds: new Set([...distinctRunFlagsIds, ...flagsIds]), |
| 68 | + distinctVerifiedFlagsIds: new Set([...distinctRunVerifiedFlagsIds, ...verifiedFlagsIds]), |
| 69 | + }; |
| 70 | + |
| 71 | + QcFlagSummaryService.mergeIntoSummaryUnit(runSummary, subSummary); |
| 72 | + } |
| 73 | + |
| 74 | + for (const [runNumber, { distinctFlagsIds, distinctVerifiedFlagsIds }] of Object.entries(flagsAndVerifications)) { |
| 75 | + summary[runNumber][QC_SUMMARY_PROPERTIES.missingVerificationsCount] = distinctFlagsIds.size - distinctVerifiedFlagsIds.size; |
| 76 | + } |
| 77 | + |
| 78 | + return summary; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Find QC flags in GAQ effective periods for given data pass and run |
| 83 | + * |
| 84 | + * @param {number} dataPassId id od data pass |
| 85 | + * @param {number} runNumber run number |
| 86 | + * @return {Promise<GaqFlags[]>} promise of aggregated QC flags |
| 87 | + */ |
| 88 | + async getFlagsForDataPassAndRun(dataPassId, runNumber) { |
| 89 | + const gaqPeriods = await QcFlagRepository.findGaqPeriods(dataPassId, runNumber); |
| 90 | + const qcFlags = (await QcFlagRepository.findAll({ |
| 91 | + where: { id: { [Op.in]: gaqPeriods.flatMap(({ contributingFlagIds }) => contributingFlagIds) } }, |
| 92 | + include: [ |
| 93 | + { association: 'flagType' }, |
| 94 | + { association: 'createdBy' }, |
| 95 | + { association: 'verifications', include: [{ association: 'createdBy' }] }, |
| 96 | + ], |
| 97 | + })).map(qcFlagAdapter.toEntity); |
| 98 | + |
| 99 | + const idToFlag = Object.fromEntries(qcFlags.map((flag) => [flag.id, flag])); |
| 100 | + |
| 101 | + return gaqPeriods.map(({ |
| 102 | + contributingFlagIds, |
| 103 | + from, |
| 104 | + to, |
| 105 | + }) => ({ |
| 106 | + from, |
| 107 | + to, |
| 108 | + contributingFlags: contributingFlagIds.map((id) => idToFlag[id]), |
| 109 | + })); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +export const gaqService = new GaqService(); |
0 commit comments