has_status.rb 3.23 KB
Newer Older
1
module HasStatus
Kamil Trzcinski's avatar
Kamil Trzcinski committed
2
3
  extend ActiveSupport::Concern

Douwe Maan's avatar
Douwe Maan committed
4
5
6
7
8
9
  DEFAULT_STATUS = 'created'.freeze
  AVAILABLE_STATUSES = %w[created pending running success failed canceled skipped].freeze
  STARTED_STATUSES = %w[running success failed skipped].freeze
  ACTIVE_STATUSES = %w[pending running].freeze
  COMPLETED_STATUSES = %w[success failed canceled skipped].freeze
  ORDERED_STATUSES = %w[failed pending running canceled success skipped].freeze
10
11

  class_methods do
Kamil Trzcinski's avatar
Kamil Trzcinski committed
12
    def status_sql
13
14
15
16
17
      scope = if respond_to?(:exclude_ignored)
                exclude_ignored
              else
                all
              end
18
      builds = scope.select('count(*)').to_sql
Lin Jen-Shin's avatar
Lin Jen-Shin committed
19
      created = scope.created.select('count(*)').to_sql
20
21
22
23
      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
24
      canceled = scope.canceled.select('count(*)').to_sql
Kamil Trzcinski's avatar
Kamil Trzcinski committed
25

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

Kamil Trzcinski's avatar
Kamil Trzcinski committed
38
    def status
39
      all.pluck(status_sql).first
Kamil Trzcinski's avatar
Kamil Trzcinski committed
40
41
    end

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

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

    def all_state_names
      state_machines.values.flat_map(&:states).flat_map { |s| s.map(&:name) }
    end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
53
54
55
  end

  included do
56
    validates :status, inclusion: { in: AVAILABLE_STATUSES }
Kamil Trzcinski's avatar
Kamil Trzcinski committed
57

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

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

    scope :cancelable, -> do
      where(status: [:running, :pending, :created])
    end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
83
84
85
  end

  def started?
86
    STARTED_STATUSES.include?(status) && started_at
Kamil Trzcinski's avatar
Kamil Trzcinski committed
87
88
89
  end

  def active?
90
    ACTIVE_STATUSES.include?(status)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
91
92
93
  end

  def complete?
94
    COMPLETED_STATUSES.include?(status)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
95
  end
96
97
98
99
100
101
102

  private

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