diff --git a/lib/trifle/stats/formatter/category.rb b/lib/trifle/stats/formatter/category.rb index 473cb4b..f637a38 100644 --- a/lib/trifle/stats/formatter/category.rb +++ b/lib/trifle/stats/formatter/category.rb @@ -6,12 +6,19 @@ class Formatter class Category Trifle::Stats::Series.register_formatter(:category, self) - def format(series:, path:) + def format(series:, path:, slices: 1, &block) keys = path.split('.') - series[:at].each_with_object(Hash.new(0)).with_index do |(_at, map), i| - series[:values][i].dig(*keys).each do |key, value| - k, v = block_given? ? yield(key, value) : [key.to_s, value.to_f] - map[k] += v + result = series[:at].zip(series[:values].map { |v| v.dig(*keys) || {} }) + sliced(result: result, slices: slices, block: block) + end + + def sliced(result:, slices:, block: nil) # rubocop:disable Metrics/AbcSize + result[(result.count - (result.count / slices * slices))..].each_slice(result.count / slices).map do |slice| + slice.each_with_object(Hash.new(0)) do |(_at, data), map| + data.each do |key, value| + k, v = block ? block.call(key, value) : [key.to_s, value.to_f] + map[k] += v + end end end end diff --git a/lib/trifle/stats/formatter/timeline.rb b/lib/trifle/stats/formatter/timeline.rb index 35d76e8..b7647bc 100644 --- a/lib/trifle/stats/formatter/timeline.rb +++ b/lib/trifle/stats/formatter/timeline.rb @@ -6,11 +6,17 @@ class Formatter class Timeline Trifle::Stats::Series.register_formatter(:timeline, self) - def format(series:, path:) + def format(series:, path:, slices: 1, &block) keys = path.split('.') - series[:at].map.with_index do |at, i| - value = series[:values][i].dig(*keys) - block_given? ? yield(at, value) : [at, value.to_f] + result = series[:at].zip(series[:values].map { |v| v.dig(*keys) }) + sliced(result: result, slices: slices, block: block) + end + + def sliced(result:, slices:, block: nil) + result[(result.count - (result.count / slices * slices))..].each_slice(result.count / slices).map do |slice| + slice.map do |at, value| + block ? block.call(at, value) : [at, value.to_f] + end end end end