Skip to content

Commit

Permalink
Add basic trend to individual month results ("+50% from last month") …
Browse files Browse the repository at this point in the history
…lots of todos here
  • Loading branch information
courtenay committed Jan 13, 2010
1 parent 57128a9 commit 8c18d00
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
16 changes: 14 additions & 2 deletions lib/weed/stats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,15 @@ def self.find_by_day(date, conditions)
end
end

def self.by_month(year, month, conditions)
def self.by_month(year, month, conditions, flags = [])
# nil bucket_id means ANY bucket_id, but that's only relevant on creating cached records
cached_conditions = { :bucket_id => nil }.merge(conditions)
Weed::CachedStats.with_scope(:find => {:conditions => cached_conditions }) do
if flags && flags == :trend || flags.include?(:trend)
this_month = Date.new year, month, 1
previous_month = this_month - 2 # go back a day
previous = by_month(previous_month.year, previous_month.month, conditions)
end
result = Weed::CachedStats.with_scope(:find => {:conditions => cached_conditions }) do
unless cached = Weed::CachedStats.first(:conditions => ['period = ? AND year = ? AND month = ?', 'month', year, month])
days = Weed::CachedStats.count(:conditions => ['period = ? AND year = ? AND month = ?', 'day', year, month])
today = Date.today
Expand All @@ -68,6 +73,13 @@ def self.by_month(year, month, conditions)
cached.counter
end
end
if flags && flags == :trend || flags.include?(:trend)
delta = result - previous
[result, previous, "#{(delta * 100 / previous)}%"]
# todo: store this in the CachedStats model so we don't have to calc it each time
else
result
end
end

def self.by_year(year, conditions)
Expand Down
30 changes: 30 additions & 0 deletions test/stats_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,36 @@ class StatsTest < ActiveSupport::TestCase
assert_equal 0, Weed::CachedStats.sum('counter', :conditions => { :year => date.year, :period => "year", :bucket_id => 13 })
end

context "with trends" do
it "gives a trend" do
date = Date.new(2010, 1, 4)
2.times { Weed::Stats.hit!({ :bucket_id => 15, :cdate => date - 40}) }
2.times { Weed::Stats.hit!({ :bucket_id => 15, :cdate => date - 20}) }
2.times { Weed::Stats.hit!({ :bucket_id => 15, :cdate => date }) }

stats = Weed::Stats.by_month date.year, date.month, { :bucket_id => 15 }, :trend
assert_equal [2, 2, '0%'], stats
end

it "shows a rising trend" do
date = Date.new(2010, 1, 4)
2.times { Weed::Stats.hit!({ :bucket_id => 125, :cdate => date - 20}) }
3.times { Weed::Stats.hit!({ :bucket_id => 125, :cdate => date }) }

stats = Weed::Stats.by_month date.year, date.month, { :bucket_id => 125 }, :trend
assert_equal [3, 2, '50%'], stats
end

it "shows a falling trend" do
date = Date.new(2010, 1, 4)
4.times { Weed::Stats.hit!({ :bucket_id => 135, :cdate => date - 20}) }
2.times { Weed::Stats.hit!({ :bucket_id => 135, :cdate => date }) }

stats = Weed::Stats.by_month date.year, date.month, { :bucket_id => 135 }, :trend
assert_equal [2, 4, '-50%'], stats
end
end

# this is slow as fuck
# it "calculates total results" do
# # provide a range of year data
Expand Down
1 change: 0 additions & 1 deletion weed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ class Application < Sinatra::Base
end
{ "state" => "success", "imported" => counter }.to_json
end


# todo: auth

Expand Down

0 comments on commit 8c18d00

Please sign in to comment.