Skip to content

Instantly share code, notes, and snippets.

@milgroma
Created February 3, 2014 15:35
Show Gist options
  • Save milgroma/8786046 to your computer and use it in GitHub Desktop.
Save milgroma/8786046 to your computer and use it in GitHub Desktop.
Are people using our tool?
def self.are_you_using_this
## get all of the users
u = User.all
## for each user, do the following thing
u.each do |u|
## get the date of user’s latest session
last_session_date = u.sessions.last ? u.sessions.last.created_at : nil
## have they logged in once in the last 30 days?
if last_session_date.nil? or last_session_date < 1.month.ago
## if not, set their using to blank
u.using = nil
u.save!
else
## if they have, how many weeks during that month did they log in?
## get the sessions from each week of the month
five_weeks_ago_sessions = u.sessions.where(:created_at => 6.weeks.ago..5.weeks.ago)
four_weeks_ago_sessions = u.sessions.where(:created_at => 5.weeks.ago..4.weeks.ago)
three_weeks_ago_sessions = u.sessions.where(:created_at => 4.weeks.ago..3.weeks.ago)
two_weeks_ago_sessions = u.sessions.where(:created_at => 3.weeks.ago..2.weeks.ago)
last_weeks_sessions = u.sessions.where(:created_at => 2.weeks.ago..1.week.ago)
this_weeks_sessions = u.sessions.where("created_at > ?", 1.week.ago)
## create an array of the weeks in the month
this_past_months_sessions = [this_weeks_sessions, last_weeks_sessions, two_weeks_ago_sessions, three_weeks_ago_sessions, four_weeks_ago_sessions, five_weeks_ago_sessions]
## set a counter to 0
n = 0
## go thru each week in the array of weeks of the month, and increase the counter if the week has sessions
this_past_months_sessions.each do |a|
unless a.blank?
n = n+1
end
end
if n == 1 and u.sessions.last.created_at.beginning_of_day == u.created_at.beginning_of_day
u.using = nil
u.save!
## if the counter is one, they only logged in during one week of the month and it wasn't this past week, so their using should be set to monthly
elsif n == 1 and this_weeks_sessions.blank?
u.using = "monthly"
u.save!
else
## if the counter is more than one, they used it multiple weeks this month. before setting them as a weekly user, let’s see if they are using it multiple days in the last week
## get the sessions from each day in the last week
seven_days_ago_sessions = u.sessions.where(:created_at => 8.days.ago.end_of_day..7.days.ago.end_of_day)
six_days_ago_sessions = u.sessions.where(:created_at => 7.days.ago.end_of_day..6.days.ago.end_of_day)
five_days_ago_sessions = u.sessions.where(:created_at => 6.days.ago.end_of_day..5.days.ago.end_of_day)
four_days_ago_sessions = u.sessions.where(:created_at => 5.days.ago.end_of_day..4.days.ago.end_of_day)
three_days_ago_sessions = u.sessions.where(:created_at => 4.days.ago.end_of_day..3.days.ago.end_of_day)
two_days_ago_sessions = u.sessions.where(:created_at => 3.days.ago.end_of_day..2.days.ago.end_of_day)
yesterdays_sessions = u.sessions.where(:created_at => 2.days.ago.end_of_day..1.days.ago.end_of_day)
## create an array with the days in the last week
this_past_weeks_sessions = [seven_days_ago_sessions, six_days_ago_sessions, five_days_ago_sessions, four_days_ago_sessions, three_days_ago_sessions, two_days_ago_sessions, yesterdays_sessions]
## set a counter to 0
m = 0
## go thru the array of each day of the last week and increase the counter for each day that has a session
this_past_weeks_sessions.each do |b|
unless b.blank?
m = m+1
end
end
## if this is their first week and they only logged in one day this week, then set their using to nil,
## because logging in one day in your first week applies to everyone and isn't enough to determine a usage level
if n == 1 and m == 1
u.using = nil
u.save!
## if they didn’t log in at all in the past week, or they only logged in once, set their using to weekly
elsif m == 0 or m == 1
u.using = "weekly"
u.save!
## if they logged in everyday this past week, set their using to everyDay
elsif m == 7
u.using = "everyDay"
u.save!
## otherwise, set their using to daily
else
u.using = "daily"
u.save!
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment