2016-04-16 19:46:26 +00:00
|
|
|
module Statuseable
|
2016-03-31 17:51:28 +00:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2016-04-13 13:40:12 +00:00
|
|
|
AVAILABLE_STATUSES = %w(pending running success failed canceled skipped)
|
|
|
|
|
|
|
|
class_methods do
|
2016-04-12 17:59:44 +00:00
|
|
|
def status_sql
|
2016-04-13 13:40:12 +00:00
|
|
|
builds = all.select('count(*)').to_sql
|
|
|
|
success = all.success.select('count(*)').to_sql
|
2016-04-17 13:32:49 +00:00
|
|
|
ignored = all.ignored.select('count(*)').to_sql if all.respond_to?(:ignored)
|
2016-04-12 17:59:44 +00:00
|
|
|
ignored ||= '0'
|
2016-04-13 13:40:12 +00:00
|
|
|
pending = all.pending.select('count(*)').to_sql
|
|
|
|
running = all.running.select('count(*)').to_sql
|
|
|
|
canceled = all.canceled.select('count(*)').to_sql
|
2016-04-13 15:26:22 +00:00
|
|
|
skipped = all.skipped.select('count(*)').to_sql
|
2016-04-12 17:59:44 +00:00
|
|
|
|
|
|
|
deduce_status = "(CASE
|
2016-04-13 18:51:03 +00:00
|
|
|
WHEN (#{builds})=0 THEN NULL
|
2016-04-12 17:59:44 +00:00
|
|
|
WHEN (#{builds})=(#{success})+(#{ignored}) THEN 'success'
|
|
|
|
WHEN (#{builds})=(#{pending}) THEN 'pending'
|
2016-04-18 07:44:22 +00:00
|
|
|
WHEN (#{builds})=(#{canceled})+(#{success})+(#{ignored}) THEN 'canceled'
|
2016-04-13 15:26:22 +00:00
|
|
|
WHEN (#{builds})=(#{skipped}) THEN 'skipped'
|
2016-04-12 17:59:44 +00:00
|
|
|
WHEN (#{running})+(#{pending})>0 THEN 'running'
|
|
|
|
ELSE 'failed'
|
|
|
|
END)"
|
|
|
|
|
|
|
|
deduce_status
|
|
|
|
end
|
|
|
|
|
2016-03-31 17:51:28 +00:00
|
|
|
def status
|
2016-04-13 13:40:12 +00:00
|
|
|
all.pluck(self.status_sql).first
|
2016-03-31 17:51:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def duration
|
|
|
|
duration_array = all.map(&:duration).compact
|
2016-04-16 20:43:40 +00:00
|
|
|
duration_array.reduce(:+)
|
2016-03-31 17:51:28 +00:00
|
|
|
end
|
2016-04-13 18:51:03 +00:00
|
|
|
|
|
|
|
def started_at
|
|
|
|
all.minimum(:started_at)
|
|
|
|
end
|
|
|
|
|
|
|
|
def finished_at
|
2016-04-16 19:46:26 +00:00
|
|
|
all.maximum(:finished_at)
|
2016-04-13 18:51:03 +00:00
|
|
|
end
|
2016-03-31 17:51:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
included do
|
2016-04-13 13:40:12 +00:00
|
|
|
validates :status, inclusion: { in: AVAILABLE_STATUSES }
|
2016-03-31 17:51:28 +00:00
|
|
|
|
|
|
|
state_machine :status, initial: :pending do
|
|
|
|
state :pending, value: 'pending'
|
|
|
|
state :running, value: 'running'
|
|
|
|
state :failed, value: 'failed'
|
|
|
|
state :success, value: 'success'
|
|
|
|
state :canceled, value: 'canceled'
|
2016-04-11 14:55:40 +00:00
|
|
|
state :skipped, value: 'skipped'
|
2016-03-31 17:51:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
scope :running, -> { where(status: 'running') }
|
|
|
|
scope :pending, -> { where(status: 'pending') }
|
|
|
|
scope :success, -> { where(status: 'success') }
|
|
|
|
scope :failed, -> { where(status: 'failed') }
|
2016-04-12 17:57:54 +00:00
|
|
|
scope :canceled, -> { where(status: 'canceled') }
|
2016-04-13 15:26:22 +00:00
|
|
|
scope :skipped, -> { where(status: 'skipped') }
|
2016-03-31 17:51:28 +00:00
|
|
|
scope :running_or_pending, -> { where(status: [:running, :pending]) }
|
|
|
|
scope :finished, -> { where(status: [:success, :failed, :canceled]) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def started?
|
|
|
|
!pending? && !canceled? && started_at
|
|
|
|
end
|
|
|
|
|
|
|
|
def active?
|
|
|
|
running? || pending?
|
|
|
|
end
|
|
|
|
|
|
|
|
def complete?
|
|
|
|
canceled? || success? || failed?
|
|
|
|
end
|
2016-04-12 08:23:31 +00:00
|
|
|
end
|