@@ -16,21 +16,19 @@ const { models: { QcFlag } } = require('..');
1616const Repository = require ( './Repository' ) ;
1717
1818const GAQ_PERIODS_VIEW = `
19+ SELECT * FROM (
1920 SELECT
2021 data_pass_id,
2122 run_number,
22- timestamp AS \`from\`,
23- NTH_VALUE(timestamp, 2) OVER (
24- PARTITION BY data_pass_id,
25- run_number
26- ORDER BY ap.timestamp
27- ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING
28- ) AS \`to\`
23+ LAG(timestamp) OVER w AS \`from\`,
24+ timestamp AS \`to\`,
25+ LAG(ordering_timestamp) OVER w AS from_ordering_timestamp
2926 FROM (
3027 (
3128 SELECT gaqd.data_pass_id,
3229 gaqd.run_number,
33- COALESCE(UNIX_TIMESTAMP(qcfep.\`from\`), 0) AS timestamp
30+ qcfep.\`from\` AS timestamp,
31+ COALESCE(qcfep.\`from\`, '0001-01-01 00:00:00.000') AS ordering_timestamp
3432 FROM quality_control_flag_effective_periods AS qcfep
3533 INNER JOIN quality_control_flags AS qcf ON qcf.id = qcfep.flag_id
3634 INNER JOIN data_pass_quality_control_flag AS dpqcf ON dpqcf.quality_control_flag_id = qcf.id
@@ -45,7 +43,8 @@ const GAQ_PERIODS_VIEW = `
4543 (
4644 SELECT gaqd.data_pass_id,
4745 gaqd.run_number,
48- UNIX_TIMESTAMP(COALESCE(qcfep.\`to\`, NOW())) AS timestamp
46+ qcfep.\`to\` AS timestamp,
47+ COALESCE(qcfep.\`from\`, NOW()) AS ordering_timestamp
4948 FROM quality_control_flag_effective_periods AS qcfep
5049 INNER JOIN quality_control_flags AS qcf ON qcf.id = qcfep.flag_id
5150 INNER JOIN data_pass_quality_control_flag AS dpqcf ON dpqcf.quality_control_flag_id = qcf.id
@@ -56,8 +55,15 @@ const GAQ_PERIODS_VIEW = `
5655 AND gaqd.run_number = qcf.run_number
5756 AND gaqd.detector_id = qcf.detector_id
5857 )
59- ORDER BY timestamp
60- ) AS ap
58+ ORDER BY ordering_timestamp
59+ ) AS ap
60+ WINDOW w AS (
61+ PARTITION BY data_pass_id,
62+ run_number
63+ ORDER BY ap.timestamp
64+ )
65+ ) as gaq_periods_with_last_nullish_row
66+ WHERE gaq_periods_with_last_nullish_row.from_ordering_timestamp IS NOT NULL
6167 ` ;
6268
6369/**
@@ -104,8 +110,8 @@ class QcFlagRepository extends Repository {
104110 SELECT
105111 gaq_periods.data_pass_id AS dataPassId,
106112 gaq_periods.run_number AS runNumber,
107- IF( gaq_periods.\`from\` = 0, null, gaq_periods.\`from\` * 1000) AS \`from\`,
108- IF( gaq_periods.\`to\` = UNIX_TIMESTAMP(NOW()), null, gaq_periods.\`to\` * 1000) AS \`to\`,
113+ gaq_periods.\`from\` AS \`from\`,
114+ gaq_periods.\`to\` AS \`to\`,
109115 group_concat(qcf.id) AS contributingFlagIds
110116
111117 FROM quality_control_flags AS qcf
@@ -118,8 +124,8 @@ class QcFlagRepository extends Repository {
118124 AND gaqd.run_number = gaq_periods.run_number
119125 AND gaqd.detector_id = qcf.detector_id
120126 AND gaq_periods.run_number = qcf.run_number
121- AND (qcfep.\`from\` IS NULL OR UNIX_TIMESTAMP( qcfep.\`from\`) <= gaq_periods.\`from\`)
122- AND (qcfep.\`to\` IS NULL OR gaq_periods.\`to\` <= UNIX_TIMESTAMP( qcfep.\`to\`) )
127+ AND (qcfep.\`from\` IS NULL OR qcfep.\`from\` <= gaq_periods.\`from\`)
128+ AND (qcfep.\`to\` IS NULL OR gaq_periods.\`to\` <= qcfep.\`to\`)
123129
124130 WHERE gaq_periods.data_pass_id = ${ dataPassId }
125131 ${ runNumber ? `AND gaq_periods.run_number = ${ runNumber } ` : '' }
@@ -160,8 +166,8 @@ class QcFlagRepository extends Repository {
160166 SELECT
161167 gaq_periods.data_pass_id AS dataPassId,
162168 gaq_periods.run_number AS runNumber,
163- IF( gaq_periods.\`from\` = 0, null, gaq_periods.\`from\`) AS \`from\`,
164- IF( gaq_periods.\`to\` = UNIX_TIMESTAMP(NOW()), null, gaq_periods.\`to\`) AS \`to\`,
169+ gaq_periods.\`from\` AS \`from\`,
170+ gaq_periods.\`to\` AS \`to\`,
165171 SUM(IF(qcft.monte_carlo_reproducible AND :mcReproducibleAsNotBad, false, qcft.bad)) >= 1 AS bad,
166172 SUM(qcft.bad) = SUM(qcft.monte_carlo_reproducible) AND SUM(qcft.monte_carlo_reproducible) AS mcReproducible,
167173 GROUP_CONCAT( DISTINCT qcfv.flag_id ) AS verifiedFlagsList,
@@ -183,8 +189,8 @@ class QcFlagRepository extends Repository {
183189 AND gaqd.run_number = gaq_periods.run_number
184190 AND gaqd.detector_id = qcf.detector_id
185191 AND gaq_periods.run_number = qcf.run_number
186- AND (qcfep.\`from\` IS NULL OR UNIX_TIMESTAMP( qcfep.\`from\`) <= gaq_periods.\`from\`)
187- AND (qcfep.\`to\` IS NULL OR gaq_periods.\`to\` <= UNIX_TIMESTAMP( qcfep.\`to\`) )
192+ AND (qcfep.\`from\` IS NULL OR qcfep.\`from\` <= gaq_periods.\`from\`)
193+ AND (qcfep.\`to\` IS NULL OR gaq_periods.\`to\` <= qcfep.\`to\`)
188194
189195 GROUP BY
190196 gaq_periods.data_pass_id,
@@ -210,11 +216,12 @@ class QcFlagRepository extends Repository {
210216 null
211217 ),
212218 SUM(
213- COALESCE(effectivePeriods.\`to\`, UNIX_TIMESTAMP(run.qc_time_end))
214- - COALESCE(effectivePeriods.\`from\`, UNIX_TIMESTAMP(run.qc_time_start))
215- ) / (
216- UNIX_TIMESTAMP(run.qc_time_end) - UNIX_TIMESTAMP(run.qc_time_start)
217- )
219+ TIMESTAMPDIFF(
220+ MICROSECOND,
221+ COALESCE(effectivePeriods.\`to\`,run.qc_time_end),
222+ COALESCE(effectivePeriods.\`from\`, run.qc_time_start)
223+ )
224+ ) / (TIMESTAMPDIFF(MICROSECOND, run.qc_time_end, run.qc_time_start))
218225 ) AS effectiveRunCoverage
219226
220227 FROM (${ effectivePeriodsWithTypeSubQuery } ) AS effectivePeriods
0 commit comments