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
-
#batch_state ⇒ Object
readonly
Returns the value of attribute batch_state.
Instance Method Summary collapse
-
#+(other) ⇒ Cloudtasker::Batch::BatchProgress
Add a batch progress to another one.
-
#completed ⇒ Integer
Return the number of completed jobs.
-
#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(batch_state = {}) ⇒ 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(batch_state = {}) ⇒ BatchProgress
Build a new instance of the class.
16 17 18 |
# File 'lib/cloudtasker/batch/batch_progress.rb', line 16 def initialize(batch_state = {}) @batch_state = batch_state end |
Instance Attribute Details
#batch_state ⇒ Object (readonly)
Returns the value of attribute batch_state.
9 10 11 |
# File 'lib/cloudtasker/batch/batch_progress.rb', line 9 def batch_state @batch_state end |
Instance Method Details
#+(other) ⇒ Cloudtasker::Batch::BatchProgress
Add a batch progress to another one.
124 125 126 |
# File 'lib/cloudtasker/batch/batch_progress.rb', line 124 def +(other) self.class.new(batch_state.to_h.merge(other.batch_state.to_h)) end |
#completed ⇒ Integer
Return the number of completed jobs.
34 35 36 |
# File 'lib/cloudtasker/batch/batch_progress.rb', line 34 def completed @completed ||= count('completed') end |
#dead ⇒ Integer
Return the number of dead jobs.
70 71 72 |
# File 'lib/cloudtasker/batch/batch_progress.rb', line 70 def dead @dead ||= count('dead') end |
#done ⇒ Integer
Return the number of jobs completed or dead.
88 89 90 |
# File 'lib/cloudtasker/batch/batch_progress.rb', line 88 def done completed + dead end |
#errored ⇒ Integer
Return the number of jobs with errors.
61 62 63 |
# File 'lib/cloudtasker/batch/batch_progress.rb', line 61 def errored @errored ||= count('errored') end |
#pending ⇒ Integer
Return the number of jobs not completed yet.
79 80 81 |
# File 'lib/cloudtasker/batch/batch_progress.rb', line 79 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)`
106 107 108 109 110 111 112 113 114 115 |
# File 'lib/cloudtasker/batch/batch_progress.rb', line 106 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.
52 53 54 |
# File 'lib/cloudtasker/batch/batch_progress.rb', line 52 def processing @processing ||= count('processing') end |
#scheduled ⇒ Integer
Return the number of scheduled jobs.
43 44 45 |
# File 'lib/cloudtasker/batch/batch_progress.rb', line 43 def scheduled @scheduled ||= count('scheduled') end |
#total ⇒ Integer
Return the total number jobs.
25 26 27 |
# File 'lib/cloudtasker/batch/batch_progress.rb', line 25 def total count end |