added rake task for generating a CSV of top actives from cohorts

This commit is contained in:
danielgrippi 2011-11-01 13:46:06 -07:00
parent bfb4ac4bb4
commit 9de67aaee8
2 changed files with 26 additions and 1 deletions

View file

@ -162,7 +162,16 @@ SQL
### % of cohort came back last week ### % of cohort came back last week
def retention(n) def retention(n)
week_created(n).where("current_sign_in_at > ?", Time.now - 1.week).count.to_f/week_created(n).count users_by_week(n).count.to_f/week_created(n).count
end
def top_active_users(n)
ten_percent_lim = (users_by_week(n).count.to_f * 0.3).ceil
users_by_week(n).order("sign_in_count DESC").limit(ten_percent_lim).select('email, username, sign_in_count')
end
def users_by_week(n)
week_created(n).where("current_sign_in_at > ?", Time.now - 1.week)
end end
protected protected

View file

@ -21,4 +21,20 @@ namespace :stats do
:attachments => [{:name => "retention_numbers_#{Time.now.to_s}.csv", :file => string}]}) :attachments => [{:name => "retention_numbers_#{Time.now.to_s}.csv", :file => string}]})
emails.each {|e| e.deliver} emails.each {|e| e.deliver}
end end
task :top_actives => :environment do
require 'fastercsv'
string = FasterCSV.generate do |csv|
(0..32).each do |i|
actives = ActiveRecord::Base.connection.select_all(Statistics.new.top_active_users(i).to_sql)
actives.each do |a|
csv << [i.to_s, a['email'], a['username'], a['sign_in_count']]
end
end
end
File.open("#{Rails.root}/tmp/top_actives.csv", 'w') {|f| f.write(string) }
end
end end