day of week chart

This commit is contained in:
Tomasz Gieniusz 2012-10-21 13:44:42 +02:00
parent 4a590ddd40
commit 628b650e2f
7 changed files with 72 additions and 6 deletions

View File

@ -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
hour: Hour
percentage: Percentage
day: Day
day_of_week: Day of week

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -1,3 +1,34 @@
.page-header
%h1.pagination-centered= :hour_of_day.t
= high_chart("activity_by_hour", charts.activity_by_hour)
%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)