statuseable.rb 2.68 KB
Newer Older
1
module Statuseable
2 3
  extend ActiveSupport::Concern

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

  class_methods do
10
    def status_sql
11 12 13 14
      scope = all.relevant
      builds = scope.select('count(*)').to_sql
      success = scope.success.select('count(*)').to_sql
      ignored = scope.ignored.select('count(*)').to_sql if scope.respond_to?(:ignored)
15
      ignored ||= '0'
16 17 18 19
      pending = scope.pending.select('count(*)').to_sql
      running = scope.running.select('count(*)').to_sql
      canceled = scope.canceled.select('count(*)').to_sql
      skipped = scope.skipped.select('count(*)').to_sql
20 21

      deduce_status = "(CASE
22
        WHEN (#{builds})=0 THEN NULL
Kamil Trzcinski's avatar
Kamil Trzcinski committed
23
        WHEN (#{builds})=(#{skipped}) THEN 'skipped'
24 25 26
        WHEN (#{builds})=(#{success})+(#{ignored})+(#{skipped}) THEN 'success'
        WHEN (#{builds})=(#{pending})+(#{skipped}) THEN 'pending'
        WHEN (#{builds})=(#{canceled})+(#{success})+(#{ignored})+(#{skipped}) THEN 'canceled'
27 28 29 30 31 32 33
        WHEN (#{running})+(#{pending})>0 THEN 'running'
        ELSE 'failed'
      END)"

      deduce_status
    end

34
    def status
35
      all.pluck(self.status_sql).first
36 37 38 39
    end

    def duration
      duration_array = all.map(&:duration).compact
40
      duration_array.reduce(:+)
41
    end
42 43 44 45 46 47

    def started_at
      all.minimum(:started_at)
    end

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

  included do
53
    validates :status, inclusion: { in: AVAILABLE_STATUSES }
54

55 56
    state_machine :status, initial: :created do
      state :created, value: 'created'
57 58 59 60 61
      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
62
      state :skipped, value: 'skipped'
63 64
    end

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

  def started?
78
    STARTED_STATUSES.include?(status) && started_at
79 80 81
  end

  def active?
82
    ACTIVE_STATUSES.include?(status)
83 84 85
  end

  def complete?
86
    COMPLETED_STATUSES.include?(status)
87
  end
88
end