2016-08-24 22:55:32 -04:00
|
|
|
module HasStatus
|
2016-03-31 13:51:28 -04:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2016-08-11 09:22:35 -04:00
|
|
|
AVAILABLE_STATUSES = %w[created pending running success failed canceled skipped]
|
|
|
|
STARTED_STATUSES = %w[running success failed skipped]
|
|
|
|
ACTIVE_STATUSES = %w[pending running]
|
|
|
|
COMPLETED_STATUSES = %w[success failed canceled]
|
2016-04-13 09:40:12 -04:00
|
|
|
|
|
|
|
class_methods do
|
2016-04-12 13:59:44 -04:00
|
|
|
def status_sql
|
2016-08-11 09:22:35 -04:00
|
|
|
scope = all.relevant
|
|
|
|
builds = scope.select('count(*)').to_sql
|
|
|
|
success = scope.success.select('count(*)').to_sql
|
|
|
|
ignored = scope.ignored.select('count(*)').to_sql if scope.respond_to?(:ignored)
|
2016-04-12 13:59:44 -04:00
|
|
|
ignored ||= '0'
|
2016-08-11 09:22:35 -04:00
|
|
|
pending = scope.pending.select('count(*)').to_sql
|
|
|
|
running = scope.running.select('count(*)').to_sql
|
|
|
|
canceled = scope.canceled.select('count(*)').to_sql
|
|
|
|
skipped = scope.skipped.select('count(*)').to_sql
|
2016-04-12 13:59:44 -04:00
|
|
|
|
|
|
|
deduce_status = "(CASE
|
2016-04-13 14:51:03 -04:00
|
|
|
WHEN (#{builds})=0 THEN NULL
|
2016-04-13 11:26:22 -04:00
|
|
|
WHEN (#{builds})=(#{skipped}) THEN 'skipped'
|
2016-07-18 08:48:36 -04:00
|
|
|
WHEN (#{builds})=(#{success})+(#{ignored})+(#{skipped}) THEN 'success'
|
|
|
|
WHEN (#{builds})=(#{pending})+(#{skipped}) THEN 'pending'
|
|
|
|
WHEN (#{builds})=(#{canceled})+(#{success})+(#{ignored})+(#{skipped}) THEN 'canceled'
|
2016-04-12 13:59:44 -04:00
|
|
|
WHEN (#{running})+(#{pending})>0 THEN 'running'
|
|
|
|
ELSE 'failed'
|
|
|
|
END)"
|
|
|
|
|
|
|
|
deduce_status
|
|
|
|
end
|
|
|
|
|
2016-03-31 13:51:28 -04:00
|
|
|
def status
|
2016-04-13 09:40:12 -04:00
|
|
|
all.pluck(self.status_sql).first
|
2016-03-31 13:51:28 -04:00
|
|
|
end
|
|
|
|
|
2016-04-13 14:51:03 -04:00
|
|
|
def started_at
|
|
|
|
all.minimum(:started_at)
|
|
|
|
end
|
|
|
|
|
|
|
|
def finished_at
|
2016-04-16 15:46:26 -04:00
|
|
|
all.maximum(:finished_at)
|
2016-04-13 14:51:03 -04:00
|
|
|
end
|
2016-03-31 13:51:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
included do
|
2016-04-13 09:40:12 -04:00
|
|
|
validates :status, inclusion: { in: AVAILABLE_STATUSES }
|
2016-03-31 13:51:28 -04:00
|
|
|
|
2016-08-11 09:22:35 -04:00
|
|
|
state_machine :status, initial: :created do
|
|
|
|
state :created, value: 'created'
|
2016-03-31 13:51:28 -04:00
|
|
|
state :pending, value: 'pending'
|
|
|
|
state :running, value: 'running'
|
|
|
|
state :failed, value: 'failed'
|
|
|
|
state :success, value: 'success'
|
|
|
|
state :canceled, value: 'canceled'
|
2016-04-11 10:55:40 -04:00
|
|
|
state :skipped, value: 'skipped'
|
2016-03-31 13:51:28 -04:00
|
|
|
end
|
|
|
|
|
2016-08-11 09:22:35 -04:00
|
|
|
scope :created, -> { where(status: 'created') }
|
|
|
|
scope :relevant, -> { where.not(status: 'created') }
|
2016-03-31 13:51:28 -04:00
|
|
|
scope :running, -> { where(status: 'running') }
|
|
|
|
scope :pending, -> { where(status: 'pending') }
|
|
|
|
scope :success, -> { where(status: 'success') }
|
|
|
|
scope :failed, -> { where(status: 'failed') }
|
2016-04-12 13:57:54 -04:00
|
|
|
scope :canceled, -> { where(status: 'canceled') }
|
2016-04-13 11:26:22 -04:00
|
|
|
scope :skipped, -> { where(status: 'skipped') }
|
2016-03-31 13:51:28 -04:00
|
|
|
scope :running_or_pending, -> { where(status: [:running, :pending]) }
|
|
|
|
scope :finished, -> { where(status: [:success, :failed, :canceled]) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def started?
|
2016-08-11 09:22:35 -04:00
|
|
|
STARTED_STATUSES.include?(status) && started_at
|
2016-03-31 13:51:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def active?
|
2016-08-11 09:22:35 -04:00
|
|
|
ACTIVE_STATUSES.include?(status)
|
2016-03-31 13:51:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def complete?
|
2016-08-11 09:22:35 -04:00
|
|
|
COMPLETED_STATUSES.include?(status)
|
2016-03-31 13:51:28 -04:00
|
|
|
end
|
2016-08-10 10:45:30 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def calculate_duration
|
|
|
|
if started_at && finished_at
|
|
|
|
finished_at - started_at
|
|
|
|
elsif started_at
|
2016-08-19 04:57:25 -04:00
|
|
|
Time.now - started_at
|
2016-08-10 10:45:30 -04:00
|
|
|
end
|
|
|
|
end
|
2016-04-12 04:23:31 -04:00
|
|
|
end
|