Skip to content

Commit

Permalink
Two months of statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
aiwilliams committed Mar 27, 2009
1 parent a86b6ea commit 8b7159b
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 16 deletions.
6 changes: 5 additions & 1 deletion app/controllers/admin/statistics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ def show

private
def assign_activity_statistics
@statistics = DailyStatistics.new(@account, @teaser)
if params.has_key?('monthly')
@statistics = MonthlyStatistics.new(@account, @teaser)
else
@statistics = DailyStatistics.new(@account, @teaser)
end
end
end
end
12 changes: 6 additions & 6 deletions app/models/activity_statistics.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
class ActivityStatistics
extend ActiveSupport::Memoizable

attr_reader :start_date
attr_reader :start_date, :title

def initialize(account, teaser, start_date)
@account, @teaser, @start_date = account, teaser, start_date
def initialize(title, account, teaser, start_date)
@title, @account, @teaser, @start_date = title, account, teaser, start_date
@dates = (start_date..Date.today)
end

Expand All @@ -24,7 +24,7 @@ def ysteps
def subscribe_counts
counts = @teaser.subscribes.count(:all,
:group => 'date(subscribed_on)',
:conditions => ['subscribed_on in (?)', @dates])
:conditions => ['subscribed_on >= ? and subscribed_on <= ?', @dates.first, @dates.last])
@dates.collect {|d| counts[d.to_s(:db)] || 0 }
end
memoize :subscribe_counts
Expand All @@ -46,7 +46,7 @@ def subscribes?
def visit_counts
counts = @teaser.visits.count(:all,
:group => 'date(visited_at)',
:conditions => ['date(visited_at) in (?)', @dates])
:conditions => ['visited_at >= ? and visited_at < ?', @dates.first.midnight, (@dates.last + 1).midnight])
@dates.collect {|d| counts[d.to_s(:db)] || 0 }
end
memoize :visit_counts
Expand All @@ -70,7 +70,7 @@ def visitor_counts
'date(visited_at), visitor_id',
:distinct => true,
:group => 'date(visited_at)',
:conditions => ['date(visited_at) in (?)', @dates]
:conditions => ['visited_at >= ? and visited_at < ?', @dates.first.midnight, (@dates.last + 1).midnight]
)
@dates.collect {|d| counts[d.to_s(:db)] || 0 }
end
Expand Down
4 changes: 3 additions & 1 deletion app/models/daily_statistics.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
class DailyStatistics < ActivityStatistics
def initialize(account, teaser)
super(account, teaser, Date.today-6.days)
super('Visitors and Subscribers in the Past 7 Days', account, teaser, Date.today-6.days)
end

def kind; 'daily'; end

def xlabels
@dates.collect {|d| d.strftime('%a') }
end
Expand Down
6 changes: 4 additions & 2 deletions app/models/monthly_statistics.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
class MonthlyStatistics < ActivityStatistics
def initialize(account, teaser)
super(account, teaser, Date.today - (8*7).days)
super('Visitors and Subscribers in the Past 2 Months', account, teaser, Date.today - (8*7).days)
end

def kind; 'monthly'; end

def xlabels
intervals = [@start_date]
8.times { intervals.push(intervals.last + 7.days) }
Expand All @@ -15,6 +17,6 @@ def xlabels
end

def xsteps
9
7
end
end
3 changes: 2 additions & 1 deletion app/views/admin/statistics/graph.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"tooltip" : { "shadow": false, "background": "#ffffff" },

"title": {
"text": "Visitors and Subscribers in the Past 7 Days",
"text": "<%= @statistics.title %>",
"style": "{ font-size: 16; margin-top: 5px; margin-bottom: 10px }"
},

Expand Down Expand Up @@ -46,6 +46,7 @@
"colour": "#cccccc",
"grid-colour": "#dddddd",
"steps": <%= @statistics.xsteps %>,
"offset": <%= @statistics.kind == 'daily' ? 'true' : 'false' %>,
"labels": {
"labels": <%= @statistics.xlabels.inspect %>
}
Expand Down
7 changes: 6 additions & 1 deletion app/views/admin/statistics/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@

.box#statistics
.graph
= remote_graph("visitor_statistics", :url => {:action => 'graph'})
= remote_graph("visitor_statistics", :url => url_for(:action => 'graph') + "?#{@statistics.kind}")
%p.views
View:
%a{:class=>(@statistics.kind == 'daily' ? 'selected' : nil), :href=>"?daily"} Past 7 days
%span.separator= "|"
%a{:class=>(@statistics.kind == 'monthly' ? 'selected' : nil), :href=>"?monthly"} Last 2 months
%ul.large_statistics
%li
%big= @statistics.visitors_total
Expand Down
7 changes: 6 additions & 1 deletion spec/integration/announce_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,16 @@

describe 'dashboard' do
it 'should show the statistics of the last seven days' do
pending 'implementation of multiple choices'
navigate_to '/dashboard'
response.should render_template('admin/statistics/show')
response.should have_tag('a.selected', 'Past 7 days')
end

it 'should show the statistics of the last two months' do
navigate_to '/dashboard?monthly'
response.should render_template('admin/statistics/show')
response.should have_tag('a.selected', 'Last 2 months')
end
end

it 'should list the subscribers' do
Expand Down
6 changes: 3 additions & 3 deletions spec/models/activity_statistics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
@stats.visitor_counts.should == [1,2,0,0,0,0,0]
end

it 'should answer 1 step for x' do
it 'should answer daily steps for x' do
@stats.xsteps.should == 1
end
end
Expand Down Expand Up @@ -79,8 +79,8 @@
@stats.visit_counts.should == [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
end

it 'should answer 9 steps for x' do
@stats.xsteps.should == 9
it 'should answer weekly steps for x' do
@stats.xsteps.should == 7
end
end

Expand Down

0 comments on commit 8b7159b

Please sign in to comment.