2016-08-25 02:55:32 +00:00
|
|
|
module HasStatus
|
2016-03-31 17:51:28 +00:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2017-02-21 23:32:18 +00:00
|
|
|
DEFAULT_STATUS = 'created'.freeze
|
2017-03-03 13:35:19 +00:00
|
|
|
BLOCKED_STATUS = 'manual'.freeze
|
|
|
|
AVAILABLE_STATUSES = %w[created pending running success failed canceled skipped manual].freeze
|
2017-03-04 09:18:25 +00:00
|
|
|
STARTED_STATUSES = %w[running success failed skipped manual].freeze
|
|
|
|
ACTIVE_STATUSES = %w[pending running].freeze
|
2017-02-21 23:32:18 +00:00
|
|
|
COMPLETED_STATUSES = %w[success failed canceled skipped].freeze
|
2017-03-07 10:48:01 +00:00
|
|
|
ORDERED_STATUSES = %w[failed pending running manual canceled success skipped created].freeze
|
2017-07-04 13:31:15 +00:00
|
|
|
STATUSES_ENUM = { created: 0, pending: 1, running: 2, success: 3,
|
2017-07-11 10:01:35 +00:00
|
|
|
failed: 4, canceled: 5, skipped: 6, manual: 7 }.freeze
|
2016-04-13 13:40:12 +00:00
|
|
|
|
|
|
|
class_methods do
|
2016-04-12 17:59:44 +00:00
|
|
|
def status_sql
|
2017-06-15 12:43:47 +00:00
|
|
|
scope_relevant = respond_to?(:exclude_ignored) ? exclude_ignored : all
|
|
|
|
scope_warnings = respond_to?(:failed_but_allowed) ? failed_but_allowed : none
|
2017-03-02 12:45:01 +00:00
|
|
|
|
2017-06-15 12:43:47 +00:00
|
|
|
builds = scope_relevant.select('count(*)').to_sql
|
|
|
|
created = scope_relevant.created.select('count(*)').to_sql
|
|
|
|
success = scope_relevant.success.select('count(*)').to_sql
|
|
|
|
manual = scope_relevant.manual.select('count(*)').to_sql
|
|
|
|
pending = scope_relevant.pending.select('count(*)').to_sql
|
|
|
|
running = scope_relevant.running.select('count(*)').to_sql
|
|
|
|
skipped = scope_relevant.skipped.select('count(*)').to_sql
|
|
|
|
canceled = scope_relevant.canceled.select('count(*)').to_sql
|
|
|
|
warnings = scope_warnings.select('count(*) > 0').to_sql.presence || 'false'
|
2016-04-12 17:59:44 +00:00
|
|
|
|
2016-10-03 10:22:51 +00:00
|
|
|
"(CASE
|
2017-06-15 12:43:47 +00:00
|
|
|
WHEN (#{builds})=(#{skipped}) AND (#{warnings}) THEN 'success'
|
2016-12-05 13:48:37 +00:00
|
|
|
WHEN (#{builds})=(#{skipped}) THEN 'skipped'
|
2016-12-05 16:52:50 +00:00
|
|
|
WHEN (#{builds})=(#{success}) THEN 'success'
|
2016-10-03 11:35:53 +00:00
|
|
|
WHEN (#{builds})=(#{created}) THEN 'created'
|
2016-12-07 11:47:06 +00:00
|
|
|
WHEN (#{builds})=(#{success})+(#{skipped}) THEN 'success'
|
2016-10-03 10:22:51 +00:00
|
|
|
WHEN (#{builds})=(#{success})+(#{skipped})+(#{canceled}) THEN 'canceled'
|
|
|
|
WHEN (#{builds})=(#{created})+(#{skipped})+(#{pending}) THEN 'pending'
|
2017-03-03 11:51:23 +00:00
|
|
|
WHEN (#{running})+(#{pending})>0 THEN 'running'
|
2017-03-03 13:35:19 +00:00
|
|
|
WHEN (#{manual})>0 THEN 'manual'
|
2017-03-20 18:16:48 +00:00
|
|
|
WHEN (#{created})>0 THEN 'running'
|
2016-04-12 17:59:44 +00:00
|
|
|
ELSE 'failed'
|
|
|
|
END)"
|
|
|
|
end
|
|
|
|
|
2016-03-31 17:51:28 +00:00
|
|
|
def status
|
2017-05-08 12:26:08 +00:00
|
|
|
all.pluck(status_sql).first
|
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-10-04 16:10:23 +00:00
|
|
|
|
|
|
|
def all_state_names
|
|
|
|
state_machines.values.flat_map(&:states).flat_map { |s| s.map(&:name) }
|
|
|
|
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
|
|
|
|
2016-08-11 13:22:35 +00:00
|
|
|
state_machine :status, initial: :created do
|
|
|
|
state :created, value: 'created'
|
2016-03-31 17:51:28 +00:00
|
|
|
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'
|
2017-03-03 13:35:19 +00:00
|
|
|
state :manual, value: 'manual'
|
2016-03-31 17:51:28 +00:00
|
|
|
end
|
|
|
|
|
2016-08-11 13:22:35 +00:00
|
|
|
scope :created, -> { where(status: 'created') }
|
2017-04-07 15:06:41 +00:00
|
|
|
scope :relevant, -> { where(status: AVAILABLE_STATUSES - ['created']) }
|
2016-03-31 17:51:28 +00:00
|
|
|
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') }
|
2017-03-03 13:35:19 +00:00
|
|
|
scope :manual, -> { where(status: 'manual') }
|
2017-02-19 22:17:24 +00:00
|
|
|
scope :created_or_pending, -> { where(status: [:created, :pending]) }
|
2016-03-31 17:51:28 +00:00
|
|
|
scope :running_or_pending, -> { where(status: [:running, :pending]) }
|
|
|
|
scope :finished, -> { where(status: [:success, :failed, :canceled]) }
|
2016-11-19 13:04:11 +00:00
|
|
|
scope :failed_or_canceled, -> { where(status: [:failed, :canceled]) }
|
2016-11-16 16:57:50 +00:00
|
|
|
|
|
|
|
scope :cancelable, -> do
|
2017-05-19 18:42:05 +00:00
|
|
|
where(status: [:running, :pending, :created])
|
2016-11-16 16:57:50 +00:00
|
|
|
end
|
2016-03-31 17:51:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def started?
|
2016-08-11 13:22:35 +00:00
|
|
|
STARTED_STATUSES.include?(status) && started_at
|
2016-03-31 17:51:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def active?
|
2016-08-11 13:22:35 +00:00
|
|
|
ACTIVE_STATUSES.include?(status)
|
2016-03-31 17:51:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def complete?
|
2016-08-11 13:22:35 +00:00
|
|
|
COMPLETED_STATUSES.include?(status)
|
2016-03-31 17:51:28 +00:00
|
|
|
end
|
2016-08-10 14:45:30 +00:00
|
|
|
|
2017-03-05 08:24:57 +00:00
|
|
|
def blocked?
|
|
|
|
BLOCKED_STATUS == status
|
|
|
|
end
|
|
|
|
|
2016-08-10 14:45:30 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def calculate_duration
|
|
|
|
if started_at && finished_at
|
|
|
|
finished_at - started_at
|
|
|
|
elsif started_at
|
2016-08-19 08:57:25 +00:00
|
|
|
Time.now - started_at
|
2016-08-10 14:45:30 +00:00
|
|
|
end
|
|
|
|
end
|
2016-04-12 08:23:31 +00:00
|
|
|
end
|