BigW Consortium Gitlab

has_status.rb 3.16 KB
Newer Older
1
module HasStatus
2 3
  extend ActiveSupport::Concern

4 5 6
  AVAILABLE_STATUSES = %w[created pending running success failed canceled skipped]
  STARTED_STATUSES = %w[running success failed skipped]
  ACTIVE_STATUSES = %w[pending running]
7
  COMPLETED_STATUSES = %w[success failed canceled skipped]
8
  ORDERED_STATUSES = %w[failed pending running canceled success skipped]
9 10

  class_methods do
11
    def status_sql
12 13 14 15 16
      scope = if respond_to?(:exclude_ignored)
                exclude_ignored
              else
                all
              end
17
      builds = scope.select('count(*)').to_sql
18
      created = scope.created.select('count(*)').to_sql
19 20 21 22
      success = scope.success.select('count(*)').to_sql
      pending = scope.pending.select('count(*)').to_sql
      running = scope.running.select('count(*)').to_sql
      skipped = scope.skipped.select('count(*)').to_sql
23
      canceled = scope.canceled.select('count(*)').to_sql
24

25
      "(CASE
26
        WHEN (#{builds})=(#{skipped}) THEN 'skipped'
27
        WHEN (#{builds})=(#{success}) THEN 'success'
28
        WHEN (#{builds})=(#{created}) THEN 'created'
Kamil Trzcinski committed
29
        WHEN (#{builds})=(#{success})+(#{skipped}) THEN 'success'
30 31
        WHEN (#{builds})=(#{success})+(#{skipped})+(#{canceled}) THEN 'canceled'
        WHEN (#{builds})=(#{created})+(#{skipped})+(#{pending}) THEN 'pending'
32
        WHEN (#{running})+(#{pending})+(#{created})>0 THEN 'running'
33 34 35 36
        ELSE 'failed'
      END)"
    end

37
    def status
38
      all.pluck(status_sql).first
39 40
    end

41 42 43 44 45
    def started_at
      all.minimum(:started_at)
    end

    def finished_at
46
      all.maximum(:finished_at)
47
    end
48 49 50 51

    def all_state_names
      state_machines.values.flat_map(&:states).flat_map { |s| s.map(&:name) }
    end
52 53 54
  end

  included do
55
    validates :status, inclusion: { in: AVAILABLE_STATUSES }
56

57 58
    state_machine :status, initial: :created do
      state :created, value: 'created'
59 60 61 62 63
      state :pending, value: 'pending'
      state :running, value: 'running'
      state :failed, value: 'failed'
      state :success, value: 'success'
      state :canceled, value: 'canceled'
Kamil Trzcinski committed
64
      state :skipped, value: 'skipped'
65 66
    end

67 68
    scope :created, -> { where(status: 'created') }
    scope :relevant, -> { where.not(status: 'created') }
69 70 71 72
    scope :running, -> { where(status: 'running') }
    scope :pending, -> { where(status: 'pending') }
    scope :success, -> { where(status: 'success') }
    scope :failed, -> { where(status: 'failed')  }
73
    scope :canceled, -> { where(status: 'canceled')  }
74
    scope :skipped, -> { where(status: 'skipped')  }
75 76
    scope :running_or_pending, -> { where(status: [:running, :pending]) }
    scope :finished, -> { where(status: [:success, :failed, :canceled]) }
77
    scope :failed_or_canceled, -> { where(status: [:failed, :canceled]) }
78 79 80 81

    scope :cancelable, -> do
      where(status: [:running, :pending, :created])
    end
82 83 84
  end

  def started?
85
    STARTED_STATUSES.include?(status) && started_at
86 87 88
  end

  def active?
89
    ACTIVE_STATUSES.include?(status)
90 91 92
  end

  def complete?
93
    COMPLETED_STATUSES.include?(status)
94
  end
95 96 97 98 99 100 101

  private

  def calculate_duration
    if started_at && finished_at
      finished_at - started_at
    elsif started_at
102
      Time.now - started_at
103 104
    end
  end
105
end