Add API support for querying admin reports by date range

This commit is contained in:
Robin Ward 2014-11-05 13:11:23 -05:00
parent fde5e739c9
commit 068d22e9b3
5 changed files with 11 additions and 12 deletions

View File

@ -13,8 +13,8 @@ class EmailLog < ActiveRecord::Base
User.where(id: user_id).update_all("last_emailed_at = CURRENT_TIMESTAMP") if user_id.present? and !skipped
end
def self.count_per_day(sinceDaysAgo = 30)
where('created_at > ? and skipped = false', sinceDaysAgo.days.ago).group('date(created_at)').order('date(created_at)').count
def self.count_per_day(start_date, end_date)
where('created_at >= ? and created_at < ? AND skipped = false', start_date, end_date).group('date(created_at)').order('date(created_at)').count
end
def self.for(reply_key)
@ -22,8 +22,7 @@ class EmailLog < ActiveRecord::Base
end
def self.last_sent_email_address
where(email_type: 'signup').order('created_at DESC')
.first.try(:to_address)
where(email_type: 'signup').order('created_at DESC').first.try(:to_address)
end
end

View File

@ -39,8 +39,8 @@ class Report
# Load the report
report = Report.new(type)
report.start_date = opts[:start_date]
report.end_date = opts[:end_date]
report.start_date = opts[:start_date] if opts[:start_date]
report.end_date = opts[:end_date] if opts[:end_date]
send(report_method, report)
report
end
@ -70,7 +70,7 @@ class Report
end
def self.report_about(report, subject_class, report_method = :count_per_day)
basic_report_about report, subject_class, report_method, default_days
basic_report_about report, subject_class, report_method, report.start_date, report.end_date
add_counts(report, subject_class)
end

View File

@ -531,8 +531,8 @@ class User < ActiveRecord::Base
.limit(3)
end
def self.count_by_signup_date(sinceDaysAgo=30)
where('created_at > ?', sinceDaysAgo.days.ago).group('date(created_at)').order('date(created_at)').count
def self.count_by_signup_date(start_date, end_date)
where('created_at >= ? and created_at < ?', start_date, end_date).group('date(created_at)').order('date(created_at)').count
end

View File

@ -31,7 +31,7 @@ describe Admin::ReportsController do
context 'missing report' do
before do
Report.expects(:find).with('active').returns(nil)
Report.expects(:find).with('active', instance_of(Hash)).returns(nil)
xhr :get, :show, type: 'active'
end
@ -42,7 +42,7 @@ describe Admin::ReportsController do
context 'a report is found' do
before do
Report.expects(:find).with('active').returns(Report.new('active'))
Report.expects(:find).with('active', instance_of(Hash)).returns(Report.new('active'))
xhr :get, :show, type: 'active'
end

View File

@ -30,7 +30,7 @@ describe EmailLog do
it "counts sent emails" do
user.email_logs.create(email_type: 'blah', to_address: user.email)
user.email_logs.create(email_type: 'blah', to_address: user.email, skipped: true)
described_class.count_per_day.first[1].should == 1
described_class.count_per_day(1.day.ago, Time.now).first[1].should == 1
end
end