GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Description: A simple application written in merb to help you manage ad serving across multiple sites
Clone URL: git://github.com/kneath/greed.git
kneath (author)
Sun May 11 22:18:10 -0700 2008
commit  5f97998a36ef6d4af5a6b18ed73e976b29a17a99
tree    43b24e6fb938e876d6c3a4c7128fce8398df6f44
parent  45bab61ace1174c37cbfeea8c44c83cb0f45fcde
greed / app / models / report.rb
100644 25 lines (21 sloc) 1.069 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Report
  
  def self.days_from_range(start_date, end_date)
    raise TypeError("Expected Date, got #{start_date.class.to_s}") unless start_date.is_a?(Date)
    raise TypeError("Expected Date, got #{end_date.class.to_s}") unless end_date.is_a?(Date)
        
    stats = {}
    raw_stats = Impression.find_by_sql("SELECT DATE_FORMAT(created_at, \"%Y%j\") AS day_index, created_at, COUNT(*) AS impressions, SUM(clicked) AS clicks FROM impressions WHERE created_at > '#{start_date.to_formatted_s(:db)}' AND created_at < '#{end_date.to_formatted_s(:db)}' GROUP BY day_index")
    raw_stats.each do |stat|
      stats[stat.day_index.to_s] = stat
    end
    
    days = []
    current_date = start_date
    while (current_date < end_date) do
      day = DateRange.new(current_date.to_time, (current_date + 1.day).to_time)
      stat = stats[current_date.year.to_s + current_date.yday.to_s]
      day.impressions, day.clicks = stat["impressions"].to_i, stat["clicks"].to_i unless stat.nil?
      days << day
      current_date += 1
    end
    days.reverse
  end
  
end