BigW Consortium Gitlab

has_status.rb 2.94 KB
Newer Older
1
module HasStatus
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
  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 26
      "(CASE
        WHEN (#{builds})=(#{success}) THEN 'success'
27
        WHEN (#{builds})=(#{created}) THEN 'created'
28 29 30
        WHEN (#{builds})=(#{success})+(#{skipped}) THEN 'skipped'
        WHEN (#{builds})=(#{success})+(#{skipped})+(#{canceled}) THEN 'canceled'
        WHEN (#{builds})=(#{created})+(#{skipped})+(#{pending}) THEN 'pending'
31
        WHEN (#{running})+(#{pending})+(#{created})>0 THEN 'running'
32 33 34 35
        ELSE 'failed'
      END)"
    end

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

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

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

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

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

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

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

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

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

  def complete?
87
    COMPLETED_STATUSES.include?(status)
88
  end
89 90 91 92 93 94 95

  private

  def calculate_duration
    if started_at && finished_at
      finished_at - started_at
    elsif started_at
96
      Time.now - started_at
97 98
    end
  end
99
end