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(batches = []) ⇒ BatchProgress

Build a new instance of the class.

Parameters:



16
17
18
# File 'lib/cloudtasker/batch/batch_progress.rb', line 16

def initialize(batches = [])
  @batches = batches
end

Instance Attribute Details

#batchesObject (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.

Parameters:

Returns:



137
138
139
# File 'lib/cloudtasker/batch/batch_progress.rb', line 137

def +(other)
  self.class.new(batches + other.batches)
end

#completedInteger

Return the number of completed jobs.

Returns:

  • (Integer)

    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

Parameters:

  • status (String) (defaults to: 'all')

    The status to count

Returns:

  • (Integer)

    The number of jobs in the 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

#deadInteger

Return the number of dead jobs.

Returns:

  • (Integer)

    The number of dead jobs.



83
84
85
# File 'lib/cloudtasker/batch/batch_progress.rb', line 83

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

#doneInteger

Return the number of jobs completed or dead.

Returns:

  • (Integer)

    The number of jobs done.



101
102
103
# File 'lib/cloudtasker/batch/batch_progress.rb', line 101

def done
  completed + dead
end

#erroredInteger

Return the number of jobs with errors.

Returns:

  • (Integer)

    The number of errored jobs.



74
75
76
# File 'lib/cloudtasker/batch/batch_progress.rb', line 74

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

#pendingInteger

Return the number of jobs not completed yet.

Returns:

  • (Integer)

    The number of jobs pending.



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

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.



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

#processingInteger

Return the number of processing jobs.

Returns:

  • (Integer)

    The number of processing jobs.



65
66
67
# File 'lib/cloudtasker/batch/batch_progress.rb', line 65

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

#scheduledInteger

Return the number of scheduled jobs.

Returns:

  • (Integer)

    The number of scheduled jobs.



56
57
58
# File 'lib/cloudtasker/batch/batch_progress.rb', line 56

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

#totalInteger

Return the total number jobs.

Returns:

  • (Integer)

    The number number of jobs.



38
39
40
# File 'lib/cloudtasker/batch/batch_progress.rb', line 38

def total
  count
end