has_status.rb 3.6 KB
Newer Older
1
module HasStatus
K
Kamil Trzcinski 已提交
2 3
  extend ActiveSupport::Concern

D
Douwe Maan 已提交
4
  DEFAULT_STATUS = 'created'.freeze
5 6
  BLOCKED_STATUS = 'manual'.freeze
  AVAILABLE_STATUSES = %w[created pending running success failed canceled skipped manual].freeze
7 8
  STARTED_STATUSES = %w[running success failed skipped manual].freeze
  ACTIVE_STATUSES = %w[pending running].freeze
D
Douwe Maan 已提交
9
  COMPLETED_STATUSES = %w[success failed canceled skipped].freeze
K
Kamil Trzcinski 已提交
10
  ORDERED_STATUSES = %w[failed pending running manual canceled success skipped created].freeze
11 12

  class_methods do
K
Kamil Trzcinski 已提交
13
    def status_sql
14 15
      scope = respond_to?(:exclude_ignored) ? exclude_ignored : all

16
      builds = scope.select('count(*)').to_sql
L
Lin Jen-Shin 已提交
17
      created = scope.created.select('count(*)').to_sql
18
      success = scope.success.select('count(*)').to_sql
19
      manual = scope.manual.select('count(*)').to_sql
20 21 22
      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
K
Kamil Trzcinski 已提交
24

25
      "(CASE
26
        WHEN (#{builds})=(#{skipped}) THEN 'skipped'
27
        WHEN (#{builds})=(#{success}) THEN 'success'
28
        WHEN (#{builds})=(#{created}) THEN 'created'
K
Kamil Trzcinski 已提交
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})>0 THEN 'running'
33
        WHEN (#{manual})>0 THEN 'manual'
34
        WHEN (#{created})>0 THEN 'running'
K
Kamil Trzcinski 已提交
35 36 37 38
        ELSE 'failed'
      END)"
    end

K
Kamil Trzcinski 已提交
39
    def status
40
      all.pluck(status_sql).first
K
Kamil Trzcinski 已提交
41 42
    end

43 44 45 46 47
    def started_at
      all.minimum(:started_at)
    end

    def finished_at
K
Kamil Trzcinski 已提交
48
      all.maximum(:finished_at)
49
    end
50 51 52 53

    def all_state_names
      state_machines.values.flat_map(&:states).flat_map { |s| s.map(&:name) }
    end
K
Kamil Trzcinski 已提交
54 55 56
  end

  included do
57
    validates :status, inclusion: { in: AVAILABLE_STATUSES }
K
Kamil Trzcinski 已提交
58

59 60
    state_machine :status, initial: :created do
      state :created, value: 'created'
K
Kamil Trzcinski 已提交
61 62 63 64 65
      state :pending, value: 'pending'
      state :running, value: 'running'
      state :failed, value: 'failed'
      state :success, value: 'success'
      state :canceled, value: 'canceled'
K
Kamil Trzcinski 已提交
66
      state :skipped, value: 'skipped'
67
      state :manual, value: 'manual'
K
Kamil Trzcinski 已提交
68 69
    end

70
    scope :created, -> { where(status: 'created') }
K
Kamil Trzcinski 已提交
71
    scope :relevant, -> { where(status: AVAILABLE_STATUSES - ['created']) }
K
Kamil Trzcinski 已提交
72 73 74 75
    scope :running, -> { where(status: 'running') }
    scope :pending, -> { where(status: 'pending') }
    scope :success, -> { where(status: 'success') }
    scope :failed, -> { where(status: 'failed')  }
K
Kamil Trzcinski 已提交
76
    scope :canceled, -> { where(status: 'canceled')  }
K
Kamil Trzcinski 已提交
77
    scope :skipped, -> { where(status: 'skipped')  }
78
    scope :manual, -> { where(status: 'manual')  }
79
    scope :created_or_pending, -> { where(status: [:created, :pending]) }
K
Kamil Trzcinski 已提交
80 81
    scope :running_or_pending, -> { where(status: [:running, :pending]) }
    scope :finished, -> { where(status: [:success, :failed, :canceled]) }
82
    scope :failed_or_canceled, -> { where(status: [:failed, :canceled]) }
83 84

    scope :cancelable, -> do
85
      where(status: [:running, :pending, :created, :manual])
86
    end
K
Kamil Trzcinski 已提交
87 88 89
  end

  def started?
90
    STARTED_STATUSES.include?(status) && started_at
K
Kamil Trzcinski 已提交
91 92 93
  end

  def active?
94
    ACTIVE_STATUSES.include?(status)
K
Kamil Trzcinski 已提交
95 96 97
  end

  def complete?
98
    COMPLETED_STATUSES.include?(status)
K
Kamil Trzcinski 已提交
99
  end
100

101 102 103 104
  def blocked?
    BLOCKED_STATUS == status
  end

105 106 107 108 109 110
  private

  def calculate_duration
    if started_at && finished_at
      finished_at - started_at
    elsif started_at
111
      Time.now - started_at
112 113
    end
  end
114
end