diff options
author | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2018-05-22 13:30:45 +0300 |
---|---|---|
committer | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2018-05-22 13:30:45 +0300 |
commit | 6c63f96e0a26fa046fb0cc4664240e37db8b37d8 (patch) | |
tree | 60665118a4d454b2d68c4c15a6f76912b3763c4f /app/models/ci | |
parent | 9520d2ff278f12cf2e01a755b1ea12213fd22edb (diff) |
Preload number of warnings in every stage in a pipeline
This makes it possible to avoid N+1 queries when loading pipelines
table.
Diffstat (limited to 'app/models/ci')
-rw-r--r-- | app/models/ci/pipeline.rb | 2 | ||||
-rw-r--r-- | app/models/ci/stage.rb | 13 |
2 files changed, 13 insertions, 2 deletions
diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb index e7569e0d31e..9462afe9e71 100644 --- a/app/models/ci/pipeline.rb +++ b/app/models/ci/pipeline.rb @@ -411,7 +411,7 @@ module Ci def number_of_warnings BatchLoader.for(id).batch(default_value: 0) do |pipeline_ids, loader| - Build.where(commit_id: pipeline_ids) + ::Ci::Build.where(commit_id: pipeline_ids) .latest .failed_but_allowed .group(:commit_id) diff --git a/app/models/ci/stage.rb b/app/models/ci/stage.rb index 4fc8a00d9c3..faedb9c29fd 100644 --- a/app/models/ci/stage.rb +++ b/app/models/ci/stage.rb @@ -89,7 +89,18 @@ module Ci end def has_warnings? - statuses.latest.failed_but_allowed.any? + number_of_warnings.positive? + end + + def number_of_warnings + BatchLoader.for(id).batch(default_value: 0) do |stage_ids, loader| + ::Ci::Build.where(stage_id: stage_ids) + .latest + .failed_but_allowed + .group(:stage_id) + .count + .each { |id, amount| loader.call(id, amount) } + end end def detailed_status(current_user) |