Commit 234be12e authored by Kamil Trzcinski's avatar Kamil Trzcinski

Optimise CI status accessor

parent 8e84acbf
...@@ -90,31 +90,6 @@ class CommitStatus < ActiveRecord::Base ...@@ -90,31 +90,6 @@ class CommitStatus < ActiveRecord::Base
group('stage').order(order_by).pluck(:stage, order_by).map(&:first).compact group('stage').order(order_by).pluck(:stage, order_by).map(&:first).compact
end end
def self.status_sql
builds = all.select('count(id)').to_sql
success = all.success.select('count(id)').to_sql
ignored = all.failed.where(allow_failure: true).select('count(id)').to_sql if all.try(:ignored)
ignored ||= '0'
pending = all.pending.select('count(id)').to_sql
running = all.running.select('count(id)').to_sql
canceled = all.canceled.select('count(id)').to_sql
deduce_status = "(CASE
WHEN (#{builds})=0 THEN 'skipped'
WHEN (#{builds})=(#{success})+(#{ignored}) THEN 'success'
WHEN (#{builds})=(#{pending}) THEN 'pending'
WHEN (#{builds})=(#{canceled}) THEN 'canceled'
WHEN (#{running})+(#{pending})>0 THEN 'running'
ELSE 'failed'
END)"
deduce_status
end
def self.status
pluck(self.status_sql).first
end
def self.stages_status def self.stages_status
Hash[group(:stage).pluck(:stage, self.status_sql)] Hash[group(:stage).pluck(:stage, self.status_sql)]
end end
......
...@@ -2,23 +2,29 @@ module CiStatus ...@@ -2,23 +2,29 @@ module CiStatus
extend ActiveSupport::Concern extend ActiveSupport::Concern
module ClassMethods module ClassMethods
def status def status_sql
objs = all.to_a builds = all.select('count(id)').to_sql
if objs.none? success = all.success.select('count(id)').to_sql
nil ignored = all.failed.where(allow_failure: true).select('count(id)').to_sql if all.try(:ignored)
elsif objs.all? { |status| status.success? || status.try(:ignored?) } ignored ||= '0'
'success' pending = all.pending.select('count(id)').to_sql
elsif objs.all?(&:pending?) running = all.running.select('count(id)').to_sql
'pending' canceled = all.canceled.select('count(id)').to_sql
elsif objs.any?(&:running?) || all.any?(&:pending?)
'running' deduce_status = "(CASE
elsif objs.all?(&:canceled?) WHEN (#{builds})=0 THEN 'skipped'
'canceled' WHEN (#{builds})=(#{success})+(#{ignored}) THEN 'success'
elsif objs.all?(&:skipped?) WHEN (#{builds})=(#{pending}) THEN 'pending'
'skipped' WHEN (#{builds})=(#{canceled}) THEN 'canceled'
else WHEN (#{running})+(#{pending})>0 THEN 'running'
'failed' ELSE 'failed'
END)"
deduce_status
end end
def status
pluck(self.status_sql).first
end end
def duration def duration
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment