Skip to content

Instantly share code, notes, and snippets.

@samanthamjohn
Created February 4, 2015 22:11
Show Gist options
  • Save samanthamjohn/4c9199b0b765560f5e28 to your computer and use it in GitHub Desktop.
Save samanthamjohn/4c9199b0b765560f5e28 to your computer and use it in GitHub Desktop.
User Appreciation Stats
def find_projects(date_str)
start_date = Date.parse(date_str)
end_date = start_date + 1.month
Project.published.filtered.where("created_at > ? AND created_at <= ?", start_date, end_date)
end
def has_thing(project, method)
project.send(method).present? && project.send(method) > 0
end
projects = find_projects("june 1, 2014")
all_users = Set.new
users_with_played_project = Set.new
users_with_remixed_project = Set.new
users_with_starred_project = Set.new
projects.each do |project|
all_users.add(project.user.try(:id))
users_with_played_project.add(project.user.try(:id)) if has_thing(project, :play_count)
users_with_remixed_project.add(project.user.try(:id)) if has_thing(project, :project_remixes_count)
users_with_starred_project.add(project.user.try(:id)) if has_thing(project, :stars_count)
end
all_users_count = all_users.count.to_f
played_users_count = users_with_played_project.count.to_f
remixed_users_count = users_with_remixed_project.count.to_f
starred_users_count = users_with_starred_project.count.to_f
percent_with_plays = played_users_count/all_users_count * 100
percent_with_remixes = remixed_users_count/all_users_count * 100
percent_with_stars = starred_users_count/all_users_count * 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment