Created
September 6, 2014 04:37
-
-
Save supremebeing7/cd25e9a5ee73f2e46958 to your computer and use it in GitHub Desktop.
Rake task for sending scheduled reports by day, week and month
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This task is written so that it can be run daily, but will only send weekly and monthly reports at the correct time intervals (i.e. once a week or once a month) | |
namespace :report do | |
task :all => :environment do | |
report_for_all_users('daily_report', 'day') | |
if Time.now.monday? | |
report_for_all_users('weekly_report', 'week') | |
end | |
if Time.now.day == 1 | |
report_for_all_users('monthly_report', 'month') | |
end | |
end | |
task :daily => :environment do | |
report_for_all_users('daily_report', 'day') | |
end | |
task :weekly => :environment do | |
report_for_all_users('weekly_report', 'week') | |
end | |
task :monthly => :environment do | |
report_for_all_users('monthly_report', 'month') | |
end | |
def report_for_all_users(flag, time_period) | |
puts "Getting users who want #{flag.to_s.humanize}s" | |
users = User.where("admin = true AND settings -> '#{flag}' = '1'") | |
puts "Sending #{flag.to_s.humanize}s for #{users.count} users" | |
users.each do |user| | |
puts "Sending report for #{user.name}" | |
Report.generate_and_send(user, time_period) | |
end | |
puts "#{flag.to_s.humanize}s sent!" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Report | |
attr_reader :title, :item_analytics, :login_analytics, :activities | |
def initialize(attributes) | |
@title = attributes[:title] | |
@item_analytics = attributes[:item_analytics] | |
@login_analytics = attributes[:login_analytics] | |
@activities = attributes[:activities] | |
end | |
def self.generate_all(user) | |
[ | |
Report.generate_data(user, 'day'), | |
Report.generate_data(user, 'week'), | |
Report.generate_data(user, 'month') | |
] | |
end | |
def self.generate_and_send(user, time_period) | |
report = Report.generate_data(user, time_period) | |
UserMailer.report_or_alert_email(user, report, "#{time_period}_report_email").deliver | |
end | |
def self.generate_data(user, time_period) | |
Report.new(title: Report.get_title(time_period), | |
item_analytics: Report.get_report_data(user, time_period, 'timestamp', 'item_analytics'), | |
login_analytics: Report.get_report_data(user, time_period, 'login_at', 'login_analytics'), | |
activities: Report.get_report_data(user, time_period, 'timestamp', 'activities')) | |
end | |
def self.get_title(time_period) | |
case time_period | |
when 'day' | |
"Daily Report for #{1.day.ago.beginning_of_day.strftime('%D')}" | |
when 'week' | |
"Weekly Report for #{1.week.ago.beginning_of_week.strftime('%D')} - #{1.week.ago.end_of_week.strftime('%D')}" | |
when 'month' | |
"Monthly Report for #{1.month.ago.strftime('%B %Y')}" | |
end | |
end | |
def self.get_report_data(user, time_period, timestamp, data_type) | |
user.account.send(data_type.to_sym).where( | |
"#{timestamp} > ? AND #{timestamp} < ?", | |
Report.start_time(time_period), | |
Report.end_time(time_period)) | |
end | |
def self.start_time(time_period) | |
1.send(time_period.to_sym).ago.send("beginning_of_#{time_period}".to_sym) | |
end | |
def self.end_time(time_period) | |
1.send(time_period.to_sym).ago.send("end_of_#{time_period}".to_sym) | |
end | |
def self.resend_reports(user_id) | |
user = User.find(user_id) | |
Report.generate_and_send(user, 'day') if user.daily_report | |
Report.generate_and_send(user, 'week') if user.weekly_report | |
Report.generate_and_send(user, 'month') if user.monthly_report | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment