Generate a "TOTAL" row for build-profile table (#7614)

Merge pull request 7614
This commit is contained in:
Ashwin Maroli 2019-04-12 23:18:33 +05:30 committed by jekyllbot
parent 3cce93d9b8
commit ec0971ab17
1 changed files with 22 additions and 2 deletions

View File

@ -3,6 +3,8 @@
module Jekyll
class LiquidRenderer
class Table
GAUGES = [:count, :bytes, :time].freeze
def initialize(stats)
@stats = stats
end
@ -19,6 +21,8 @@ module Jekyll
str = +"\n"
table_head = data.shift
table_foot = data.pop
str << generate_row(table_head, widths)
str << generate_table_head_border(table_head, widths)
@ -26,6 +30,9 @@ module Jekyll
str << generate_row(row_data, widths)
end
str << generate_table_head_border(table_foot, widths)
str << generate_row(table_foot, widths).rstrip
str << "\n"
str
end
@ -71,13 +78,16 @@ module Jekyll
widths
end
# rubocop:disable Metrics/AbcSize
def data_for_table(num_of_rows)
sorted = @stats.sort_by { |_, file_stats| -file_stats[:time] }
sorted = sorted.slice(0, num_of_rows)
table = [%w(Filename Count Bytes Time)]
table = [header_labels]
totals = Hash.new { |hash, key| hash[key] = 0 }
sorted.each do |filename, file_stats|
GAUGES.each { |gauge| totals[gauge] += file_stats[gauge] }
row = []
row << filename
row << file_stats[:count].to_s
@ -86,7 +96,17 @@ module Jekyll
table << row
end
table
footer = []
footer << "TOTAL (for #{sorted.size} files)"
footer << totals[:count].to_s
footer << format_bytes(totals[:bytes])
footer << format("%.3f", totals[:time])
table << footer
end
# rubocop:enable Metrics/AbcSize
def header_labels
GAUGES.map { |gauge| gauge.to_s.capitalize }.unshift("Filename")
end
def format_bytes(bytes)