Class: Cloudtasker::Batch::BatchProgress

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudtasker/batch/batch_progress.rb

Overview

Capture the progress of a batch

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(batch_state = {}) ⇒ BatchProgress

Build a new instance of the class.

Parameters:

  • batch_state (Hash) (defaults to: {})

    The batch state



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_stateObject (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.

Parameters:

Returns:



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

#completedInteger

Return the number of completed jobs.

Returns:

  • (Integer)

    The number of completed jobs.



34
35
36
# File 'lib/cloudtasker/batch/batch_progress.rb', line 34

def completed
  @completed ||= count('completed')
end

#deadInteger

Return the number of dead jobs.

Returns:

  • (Integer)

    The number of dead jobs.



70
71
72
# File 'lib/cloudtasker/batch/batch_progress.rb', line 70

def dead
  @dead ||= count('dead')
end

#doneInteger

Return the number of jobs completed or dead.

Returns:

  • (Integer)

    The number of jobs done.



88
89
90
# File 'lib/cloudtasker/batch/batch_progress.rb', line 88

def done
  completed + dead
end

#erroredInteger

Return the number of jobs with errors.

Returns:

  • (Integer)

    The number of errored jobs.



61
62
63
# File 'lib/cloudtasker/batch/batch_progress.rb', line 61

def errored
  @errored ||= count('errored')
end

#pendingInteger

Return the number of jobs not completed yet.

Returns:

  • (Integer)

    The number of jobs pending.



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)`

Parameters:

  • min_total (Integer) (defaults to: 0)

    The minimum for the total number of jobs

  • smoothing (Integer) (defaults to: 0)

    An additive smoothing for the total number of jobs

Returns:

  • (Float)

    The progress percentage.



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

#processingInteger

Return the number of processing jobs.

Returns:

  • (Integer)

    The number of processing jobs.



52
53
54
# File 'lib/cloudtasker/batch/batch_progress.rb', line 52

def processing
  @processing ||= count('processing')
end

#scheduledInteger

Return the number of scheduled jobs.

Returns:

  • (Integer)

    The number of scheduled jobs.



43
44
45
# File 'lib/cloudtasker/batch/batch_progress.rb', line 43

def scheduled
  @scheduled ||= count('scheduled')
end

#totalInteger

Return the total number jobs.

Returns:

  • (Integer)

    The number number of jobs.



25
26
27
# File 'lib/cloudtasker/batch/batch_progress.rb', line 25

def total
  count
end