debian-mirror-gitlab/app/models/concerns/has_status.rb

167 lines
6.2 KiB
Ruby
Raw Normal View History

2018-11-20 20:47:30 +05:30
# frozen_string_literal: true
2016-09-29 09:46:39 +05:30
module HasStatus
2016-06-02 11:05:42 +05:30
extend ActiveSupport::Concern
2019-12-04 20:38:33 +05:30
DEFAULT_STATUS = 'created'
2018-12-05 23:21:45 +05:30
BLOCKED_STATUS = %w[manual scheduled].freeze
2020-03-13 15:44:24 +05:30
AVAILABLE_STATUSES = %w[created waiting_for_resource preparing pending running success failed canceled skipped manual scheduled].freeze
2018-12-05 23:21:45 +05:30
STARTED_STATUSES = %w[running success failed skipped manual scheduled].freeze
2020-03-13 15:44:24 +05:30
ACTIVE_STATUSES = %w[waiting_for_resource preparing pending running].freeze
2017-08-17 22:00:37 +05:30
COMPLETED_STATUSES = %w[success failed canceled skipped].freeze
2020-03-13 15:44:24 +05:30
ORDERED_STATUSES = %w[failed preparing pending running waiting_for_resource manual scheduled canceled success skipped created].freeze
2019-12-21 20:55:43 +05:30
PASSED_WITH_WARNINGS_STATUSES = %w[failed canceled].to_set.freeze
EXCLUDE_IGNORED_STATUSES = %w[manual failed canceled].to_set.freeze
2018-03-17 18:26:18 +05:30
STATUSES_ENUM = { created: 0, pending: 1, running: 2, success: 3,
2018-12-05 23:21:45 +05:30
failed: 4, canceled: 5, skipped: 6, manual: 7,
2020-03-13 15:44:24 +05:30
scheduled: 8, preparing: 9, waiting_for_resource: 10 }.freeze
2016-06-02 11:05:42 +05:30
2018-11-08 19:23:39 +05:30
UnknownStatusError = Class.new(StandardError)
2016-06-02 11:05:42 +05:30
class_methods do
2019-12-21 20:55:43 +05:30
def legacy_status_sql
2017-09-10 17:25:29 +05:30
scope_relevant = respond_to?(:exclude_ignored) ? exclude_ignored : all
scope_warnings = respond_to?(:failed_but_allowed) ? failed_but_allowed : none
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
2018-12-05 23:21:45 +05:30
scheduled = scope_relevant.scheduled.select('count(*)').to_sql
2019-07-07 11:18:12 +05:30
preparing = scope_relevant.preparing.select('count(*)').to_sql
2020-03-13 15:44:24 +05:30
waiting_for_resource = scope_relevant.waiting_for_resource.select('count(*)').to_sql
2017-09-10 17:25:29 +05:30
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-06-02 11:05:42 +05:30
2019-09-30 21:07:59 +05:30
Arel.sql(
"(CASE
WHEN (#{builds})=(#{skipped}) AND (#{warnings}) THEN 'success'
WHEN (#{builds})=(#{skipped}) THEN 'skipped'
WHEN (#{builds})=(#{success}) THEN 'success'
WHEN (#{builds})=(#{created}) THEN 'created'
WHEN (#{builds})=(#{preparing}) THEN 'preparing'
WHEN (#{builds})=(#{success})+(#{skipped}) THEN 'success'
WHEN (#{builds})=(#{success})+(#{skipped})+(#{canceled}) THEN 'canceled'
WHEN (#{builds})=(#{created})+(#{skipped})+(#{pending}) THEN 'pending'
WHEN (#{running})+(#{pending})>0 THEN 'running'
2020-03-13 15:44:24 +05:30
WHEN (#{waiting_for_resource})>0 THEN 'waiting_for_resource'
2019-09-30 21:07:59 +05:30
WHEN (#{manual})>0 THEN 'manual'
WHEN (#{scheduled})>0 THEN 'scheduled'
WHEN (#{preparing})>0 THEN 'preparing'
WHEN (#{created})>0 THEN 'running'
ELSE 'failed'
END)"
)
2016-06-02 11:05:42 +05:30
end
2019-12-21 20:55:43 +05:30
def legacy_status
all.pluck(legacy_status_sql).first
end
# This method should not be used.
# This method performs expensive calculation of status:
# 1. By plucking all related objects,
# 2. Or executes expensive SQL query
2020-04-22 19:07:51 +05:30
def slow_composite_status(project:)
2020-06-23 00:09:42 +05:30
if ::Gitlab::Ci::Features.composite_status?(project)
2019-12-21 20:55:43 +05:30
Gitlab::Ci::Status::Composite
.new(all, with_allow_failure: columns_hash.key?('allow_failure'))
.status
else
legacy_status
end
2016-06-02 11:05:42 +05:30
end
def started_at
all.minimum(:started_at)
end
def finished_at
all.maximum(:finished_at)
end
2016-11-03 12:29:30 +05:30
def all_state_names
state_machines.values.flat_map(&:states).flat_map { |s| s.map(&:name) }
end
2019-07-31 22:56:46 +05:30
def completed_statuses
COMPLETED_STATUSES.map(&:to_sym)
end
2016-06-02 11:05:42 +05:30
end
included do
validates :status, inclusion: { in: AVAILABLE_STATUSES }
2016-09-13 17:45:13 +05:30
state_machine :status, initial: :created do
state :created, value: 'created'
2020-03-13 15:44:24 +05:30
state :waiting_for_resource, value: 'waiting_for_resource'
2019-07-07 11:18:12 +05:30
state :preparing, value: 'preparing'
2016-06-02 11:05:42 +05:30
state :pending, value: 'pending'
state :running, value: 'running'
state :failed, value: 'failed'
state :success, value: 'success'
state :canceled, value: 'canceled'
state :skipped, value: 'skipped'
2017-08-17 22:00:37 +05:30
state :manual, value: 'manual'
2018-12-05 23:21:45 +05:30
state :scheduled, value: 'scheduled'
2016-06-02 11:05:42 +05:30
end
2019-09-30 21:07:59 +05:30
scope :created, -> { with_status(:created) }
2020-03-13 15:44:24 +05:30
scope :waiting_for_resource, -> { with_status(:waiting_for_resource) }
2019-09-30 21:07:59 +05:30
scope :preparing, -> { with_status(:preparing) }
scope :relevant, -> { without_status(:created) }
scope :running, -> { with_status(:running) }
scope :pending, -> { with_status(:pending) }
scope :success, -> { with_status(:success) }
scope :failed, -> { with_status(:failed) }
scope :canceled, -> { with_status(:canceled) }
scope :skipped, -> { with_status(:skipped) }
scope :manual, -> { with_status(:manual) }
scope :scheduled, -> { with_status(:scheduled) }
2020-03-13 15:44:24 +05:30
scope :alive, -> { with_status(:created, :waiting_for_resource, :preparing, :pending, :running) }
scope :alive_or_scheduled, -> { with_status(:created, :waiting_for_resource, :preparing, :pending, :running, :scheduled) }
2019-09-30 21:07:59 +05:30
scope :created_or_pending, -> { with_status(:created, :pending) }
scope :running_or_pending, -> { with_status(:running, :pending) }
scope :finished, -> { with_status(:success, :failed, :canceled) }
scope :failed_or_canceled, -> { with_status(:failed, :canceled) }
2019-10-12 21:52:04 +05:30
scope :incomplete, -> { without_statuses(completed_statuses) }
2017-08-17 22:00:37 +05:30
scope :cancelable, -> do
2020-03-13 15:44:24 +05:30
where(status: [:running, :waiting_for_resource, :preparing, :pending, :created, :scheduled])
2017-08-17 22:00:37 +05:30
end
2019-10-12 21:52:04 +05:30
scope :without_statuses, -> (names) do
with_status(all_state_names - names.to_a)
end
2016-06-02 11:05:42 +05:30
end
def started?
2016-09-13 17:45:13 +05:30
STARTED_STATUSES.include?(status) && started_at
2016-06-02 11:05:42 +05:30
end
def active?
2016-09-13 17:45:13 +05:30
ACTIVE_STATUSES.include?(status)
2016-06-02 11:05:42 +05:30
end
def complete?
2016-09-13 17:45:13 +05:30
COMPLETED_STATUSES.include?(status)
end
2017-08-17 22:00:37 +05:30
def blocked?
2018-12-05 23:21:45 +05:30
BLOCKED_STATUS.include?(status)
2017-08-17 22:00:37 +05:30
end
2016-09-13 17:45:13 +05:30
private
def calculate_duration
if started_at && finished_at
finished_at - started_at
elsif started_at
2020-06-23 00:09:42 +05:30
Time.current - started_at
2016-09-13 17:45:13 +05:30
end
2016-06-02 11:05:42 +05:30
end
end