diff --git a/config/locales/en.yml b/config/locales/en.yml index 98cef7e45..ef8a1c6bb 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -17,4 +17,7 @@ en: files_by_extension: Files by extension lines_by_extension: Lines by extension hour_of_day: Hour of day - hour: Hour \ No newline at end of file + hour: Hour + percentage: Percentage + day: Day + day_of_week: Day of week \ No newline at end of file diff --git a/lib/git_stats/core_extensions/hash.rb b/lib/git_stats/core_extensions/hash.rb index 4fd5a947a..6eb313194 100644 --- a/lib/git_stats/core_extensions/hash.rb +++ b/lib/git_stats/core_extensions/hash.rb @@ -1,6 +1,8 @@ class Hash - def to_key_indexed_array + def to_key_indexed_array(params = {}) raise ArgumentError.new('all the keys must be numbers to convert to key indexed array') unless all? { |k, v| k.is_a? Numeric } - inject([]) { |acc, (k, v)| acc[k] = v; acc } + min_size = params[:min_size] || 0 + default = params[:default] + inject(Array.new(min_size, default)) { |acc, (k, v)| acc[k] = v; acc }.map { |e| e || default } end end \ No newline at end of file diff --git a/lib/git_stats/git_data/activity.rb b/lib/git_stats/git_data/activity.rb index 00e2b781a..9396163be 100644 --- a/lib/git_stats/git_data/activity.rb +++ b/lib/git_stats/git_data/activity.rb @@ -10,10 +10,18 @@ module GitStats @by_hour ||= default_hash end + def by_hour_array + by_hour.to_key_indexed_array(min_size: 24, default: 0) + end + def by_wday @by_wday ||= default_hash end + def by_wday_array + by_wday.to_key_indexed_array(min_size: 7, default: 0) + end + def by_wday_hour @by_wday_hour ||= default_double_hash end diff --git a/lib/git_stats/stats_view/charts/activity_charts.rb b/lib/git_stats/stats_view/charts/activity_charts.rb index d98d1762e..ffe75312b 100644 --- a/lib/git_stats/stats_view/charts/activity_charts.rb +++ b/lib/git_stats/stats_view/charts/activity_charts.rb @@ -13,7 +13,19 @@ module GitStats y_text: :commits.t, x_text: :hour.t, data_x: (0..23), - data_y: @activity.by_hour.to_key_indexed_array + data_y: @activity.by_hour_array + ) + end + end + + def activity_by_wday + Chart.new do |f| + f.simple_column_chart( + title: :commits_by_wday.t, + y_text: :commits.t, + x_text: :day.t, + data_x: Date::ABBR_DAYNAMES, + data_y: @activity.by_wday_array ) end end diff --git a/lib/git_stats/stats_view/charts/charts.rb b/lib/git_stats/stats_view/charts/charts.rb index 01f46de1c..280cacc2e 100644 --- a/lib/git_stats/stats_view/charts/charts.rb +++ b/lib/git_stats/stats_view/charts/charts.rb @@ -5,7 +5,7 @@ module GitStats delegate :files_by_extension, :lines_by_extension, :files_by_date, :lines_by_date, :lines_added_by_author, :lines_deleted_by_author, to: :repo_charts delegate :by_authors_wday, to: :authors_charts - delegate :activity_by_hour, to: :activity_charts + delegate :activity_by_hour, :activity_by_wday, to: :activity_charts attr_reader :repo diff --git a/spec/hash_extension_spec.rb b/spec/hash_extension_spec.rb index 4afd44e17..478a22db1 100644 --- a/spec/hash_extension_spec.rb +++ b/spec/hash_extension_spec.rb @@ -11,5 +11,15 @@ describe Hash do hash = {1 => 'x', 'b' => 2} expect { hash.to_key_indexed_array }.to raise_error(ArgumentError) end + + context 'should take optional min_size and default parameters' do + let(:hash) { {1 => 'x', 2 => 1, 5 => 'a'} } + it 'should fill array with defaults up to min_size' do + hash.to_key_indexed_array(min_size: 8, default: 0).should == [0, 'x', 1, 0, 0, 'a', 0, 0] + end + it 'should use default value where key is not in hash' do + hash.to_key_indexed_array(min_size: 2, default: 0).should == [0, 'x', 1, 0, 0, 'a'] + end + end end end \ No newline at end of file diff --git a/templates/activity.haml b/templates/activity.haml index a30742195..a29045df9 100644 --- a/templates/activity.haml +++ b/templates/activity.haml @@ -1,3 +1,34 @@ .page-header %h1.pagination-centered= :hour_of_day.t -= high_chart("activity_by_hour", charts.activity_by_hour) \ No newline at end of file +%table{:class => "table table-bordered table-condensed"} + %tr + %th= :hour.t + - (0..23).each do |h| + %th= h + %tr + %td= :commits.t + - repo.activity.by_hour_array.each do |commits| + %td= commits + %tr + %td= :percentage.t + - repo.activity.by_hour_array.each do |commits| + %td= (commits * 100.0 / repo.activity.by_hour_array.sum).round(2) += high_chart("activity_by_hour", charts.activity_by_hour) + + +.page-header + %h1.pagination-centered= :day_of_week.t +%table{:class => "table table-bordered table-condensed"} + %tr + %th= :day.t + - Date::ABBR_DAYNAMES.each do |d| + %th= d + %tr + %td= :commits.t + - repo.activity.by_wday_array.each do |commits| + %td= commits + %tr + %td= :percentage.t + - repo.activity.by_wday_array.each do |commits| + %td= (commits * 100.0 / repo.activity.by_wday_array.sum).round(2) += high_chart("activity_by_wday", charts.activity_by_wday) \ No newline at end of file