lib/rocket_job/batch/statistics.rb in rocketjob-5.1.1 vs lib/rocket_job/batch/statistics.rb in rocketjob-5.2.0.beta1
- old
+ new
@@ -1,6 +1,6 @@
-require 'active_support/concern'
+require "active_support/concern"
module RocketJob
module Batch
# Allow statistics to be gathered while a batch job is running
module Statistics
@@ -20,15 +20,16 @@
hash.each_pair { |key, increment| inc_key(key, increment) }
self
end
def inc_key(key, increment = 1)
- return if increment == 0
+ return if increment.zero?
+
if in_memory
# For tests and in-process execution
inc_in_memory(key, increment)
- elsif key && key != ''
+ elsif key && key != ""
stats["statistics.#{key}"] += increment
end
self
end
@@ -38,15 +39,15 @@
private
# Navigates path and creates child hashes as needed at the end is reached
def inc_in_memory(key, increment)
- paths = key.to_s.split('.')
+ paths = key.to_s.split(".")
last = paths.pop
return unless last
- target = paths.inject(in_memory) { |target, key| target.key?(key) ? target[key] : target[key] = Hash.new(0) }
+ target = paths.inject(in_memory) { |target, key| target.key?(key) ? target[key] : target[key] = Hash.new(0) }
target[last] += increment
end
end
included do
@@ -55,11 +56,12 @@
around_slice :statistics_capture
end
# Increment a statistic
def statistics_inc(key, increment = 1)
- return if key.nil? || key == ''
+ return if key.nil? || key == ""
+
# Being called within tests outside of a perform
@slice_statistics ||= Stats.new(new_record? ? statistics : nil)
key.is_a?(Hash) ? @slice_statistics.inc(key) : @slice_statistics.inc_key(key, increment)
end
@@ -68,15 +70,15 @@
# Capture the number of successful and failed tradelines
# as well as those with notices and alerts.
def statistics_capture
@slice_statistics = Stats.new(new_record? ? statistics : nil)
yield
- collection.update_one({_id: id}, {'$inc' => @slice_statistics.stats}) unless @slice_statistics.empty?
+ collection.update_one({_id: id}, {"$inc" => @slice_statistics.stats}) unless @slice_statistics.empty?
end
# Overrides RocketJob::Batch::Logger#rocket_job_batch_log_payload
def rocket_job_batch_log_payload
- h = {
+ h = {
from: aasm.from_state,
to: aasm.to_state,
event: aasm.current_event
}
h[:statistics] = statistics.dup if statistics.present? && (completed? || failed?)