Class: Request::Statistics::Counter

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

Instance Method Summary collapse

Constructor Details

#initialize(statistics = {}) ⇒ Counter


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

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

Instance Method Details

#completedObject


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

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

#pendingObject


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

def pending
  %w[pending blocked].map(&method(:[])).sum
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.


42
43
44
45
46
# File 'app/models/request/statistics.rb', line 42

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


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

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

#totalObject

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


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

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