Class: Cloudtasker::Batch::BatchProgress
- Inherits:
-
Object
- Object
- Cloudtasker::Batch::BatchProgress
- Defined in:
- lib/cloudtasker/batch/batch_progress.rb
Overview
Capture the progress of a batch
Instance Attribute Summary collapse
-
#batches ⇒ Object
readonly
Returns the value of attribute batches.
Instance Method Summary collapse
-
#+(other) ⇒ Cloudtasker::Batch::BatchProgress
Add a batch progress to another one.
-
#completed ⇒ Integer
Return the number of completed jobs.
-
#count(status = 'all') ⇒ Integer
Count the number of items in a given status.
-
#dead ⇒ Integer
Return the number of dead jobs.
-
#done ⇒ Integer
Return the number of jobs completed or dead.
-
#errored ⇒ Integer
Return the number of jobs with errors.
-
#initialize(batches = []) ⇒ BatchProgress
constructor
Build a new instance of the class.
-
#pending ⇒ Integer
Return the number of jobs not completed yet.
-
#percent(min_total: 0, smoothing: 0) ⇒ Float
Return the batch progress percentage.
-
#processing ⇒ Integer
Return the number of processing jobs.
-
#scheduled ⇒ Integer
Return the number of scheduled jobs.
-
#total ⇒ Integer
Return the total number jobs.
Constructor Details
#initialize(batches = []) ⇒ BatchProgress
Build a new instance of the class.
16 17 18 |
# File 'lib/cloudtasker/batch/batch_progress.rb', line 16 def initialize(batches = []) @batches = batches end |
Instance Attribute Details
#batches ⇒ Object (readonly)
Returns the value of attribute batches.
9 10 11 |
# File 'lib/cloudtasker/batch/batch_progress.rb', line 9 def batches @batches end |
Instance Method Details
#+(other) ⇒ Cloudtasker::Batch::BatchProgress
Add a batch progress to another one.
137 138 139 |
# File 'lib/cloudtasker/batch/batch_progress.rb', line 137 def +(other) self.class.new(batches + other.batches) end |
#completed ⇒ Integer
Return the number of completed jobs.
47 48 49 |
# File 'lib/cloudtasker/batch/batch_progress.rb', line 47 def completed @completed ||= count('completed') end |
#count(status = 'all') ⇒ Integer
Count the number of items in a given status
29 30 31 |
# File 'lib/cloudtasker/batch/batch_progress.rb', line 29 def count(status = 'all') batches.sum { |e| e.batch_state_count(status) } end |
#dead ⇒ Integer
Return the number of dead jobs.
83 84 85 |
# File 'lib/cloudtasker/batch/batch_progress.rb', line 83 def dead @dead ||= count('dead') end |
#done ⇒ Integer
Return the number of jobs completed or dead.
101 102 103 |
# File 'lib/cloudtasker/batch/batch_progress.rb', line 101 def done completed + dead end |
#errored ⇒ Integer
Return the number of jobs with errors.
74 75 76 |
# File 'lib/cloudtasker/batch/batch_progress.rb', line 74 def errored @errored ||= count('errored') end |
#pending ⇒ Integer
Return the number of jobs not completed yet.
92 93 94 |
# File 'lib/cloudtasker/batch/batch_progress.rb', line 92 def pending total - done end |
#percent(min_total: 0, smoothing: 0) ⇒ Float
Return the batch progress percentage.
A ‘min_total` can be specified to linearize the calculation, while jobs get added at the start of the batch.
Similarly a ‘smoothing` parameter can be specified to add a constant to the total and linearize the calculation, which becomes: `done / (total + smoothing)`
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/cloudtasker/batch/batch_progress.rb', line 119 def percent(min_total: 0, smoothing: 0) # Get the total value to use actual_total = [min_total, total + smoothing].max # Abort if we cannot divide return 0 if actual_total.zero? # Calculate progress (done.to_f / actual_total) * 100 end |
#processing ⇒ Integer
Return the number of processing jobs.
65 66 67 |
# File 'lib/cloudtasker/batch/batch_progress.rb', line 65 def processing @processing ||= count('processing') end |
#scheduled ⇒ Integer
Return the number of scheduled jobs.
56 57 58 |
# File 'lib/cloudtasker/batch/batch_progress.rb', line 56 def scheduled @scheduled ||= count('scheduled') end |
#total ⇒ Integer
Return the total number jobs.
38 39 40 |
# File 'lib/cloudtasker/batch/batch_progress.rb', line 38 def total count end |