diff --git a/lib/statistics.rb b/lib/statistics.rb index b4f8f9fb9..c4b2b656d 100644 --- a/lib/statistics.rb +++ b/lib/statistics.rb @@ -162,7 +162,16 @@ SQL ### % of cohort came back last week 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 protected diff --git a/lib/tasks/stats.rake b/lib/tasks/stats.rake index ccd85b33a..25038579d 100644 --- a/lib/tasks/stats.rake +++ b/lib/tasks/stats.rake @@ -21,4 +21,20 @@ namespace :stats do :attachments => [{:name => "retention_numbers_#{Time.now.to_s}.csv", :file => string}]}) emails.each {|e| e.deliver} 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