lib/tabs/metrics/task.rb in tabs-0.9.1 vs lib/tabs/metrics/task.rb in tabs-1.0.0

- old
+ new

@@ -35,11 +35,11 @@ started_tokens = tokens_for_period(range, resolution, "started") completed_tokens = tokens_for_period(range, resolution, "completed") matching_tokens = started_tokens.select { |token| completed_tokens.include? token } completion_rate = (matching_tokens.size.to_f / range.size).round(Config.decimal_precision) elapsed_times = matching_tokens.map { |t| t.time_elapsed(resolution) } - average_completion_time = (elapsed_times.sum) / matching_tokens.size + average_completion_time = matching_tokens.blank? ? 0.0 : (elapsed_times.sum) / matching_tokens.size Stats.new( started_tokens.size, completed_tokens.size, matching_tokens.size, completion_rate, @@ -49,21 +49,23 @@ def drop! del_by_prefix("stat:task:#{key}") end + def storage_key(resolution, timestamp, type) + formatted_time = Tabs::Resolution.serialize(resolution, timestamp) + "stat:task:#{key}:#{type}:#{resolution}:#{formatted_time}" + end + private def tokens_for_period(range, resolution, type) keys = keys_for_range(range, resolution, type) - mget(*keys).compact.map(&:to_a).flatten.map { |t| Token.new(t, key) } + smembers_all(*keys).compact.map(&:to_a).flatten.map { |t| Token.new(t, key) } end def keys_for_range(range, resolution, type) - range.map do |date| - formatted_time = Tabs::Resolution.serialize(resolution, date) - "stat:task:#{key}:#{type}:#{formatted_time}" - end + range.map { |timestamp| storage_key(resolution, timestamp, type) } end end end end