Class: Request::Statistics::Counter

Inherits:
Object
  • Object
show all
Defined in:
app/models/request/statistics.rb

Overview

rubocop:todo Style/Documentation

Instance Method Summary collapse

Constructor Details

#initialize(statistics = {}) ⇒ Counter

Returns a new instance of Counter.


4
5
6
# File 'app/models/request/statistics.rb', line 4

def initialize(statistics = {})
  @statistics = Hash.new(0).merge(statistics)
end

Instance Method Details

#completedObject


15
16
17
# File 'app/models/request/statistics.rb', line 15

def completed
  %w[passed failed].sum(&method(:[]))
end

#pendingObject


19
20
21
# File 'app/models/request/statistics.rb', line 19

def pending
  %w[pending blocked].sum(&method(:[]))
end

#progressObject

Percentage of passed requests out of those which haven't been failed. I believe the reason failed requests are subtracted from the total, rather than added to pending, are because failed sequencing requests get duplicated, and in this case it wouldn't make sense to increment progress.


39
40
41
42
43
# File 'app/models/request/statistics.rb', line 39

def progress
  return 0 if passed.zero? # If there are no passed then the progress is 0% by definition

  (passed * 100) / (total - failed)
end

#states(exclude: []) ⇒ Object

Returns each state, with is absolute and percentage contribution to the total. Excluded states don't get returned, ideal for excluding pending from progress bars. Note: excluded states still form part of the calculations


30
31
32
33
# File 'app/models/request/statistics.rb', line 30

def states(exclude: [])
  filtered_states = sorted_states.reject { |state, _statistics| exclude.include?(state) || state == 'cancelled' }
  filtered_states.map { |state, absolute| [state, absolute, (absolute * 100) / total] }
end

#totalObject

Cancelled requests get filtered out, as generally they are administrative decisions


11
12
13
# File 'app/models/request/statistics.rb', line 11

def total
  @statistics.values.sum - @statistics['cancelled']
end