Skip to content

Commit

Permalink
added reset task to create recommendations
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego Salazar committed Jan 25, 2017
1 parent 63faa8c commit d92695b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 26 deletions.
2 changes: 1 addition & 1 deletion app/controllers/recommendations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def index
end

@sub_topic = SubTopic.find_by_subtopic_id params[:subtopic]
@recommender = Recommender.new @sub_topic, params.fetch(:per, 4), params[:page]
@recommender = Recommender.new @sub_topic.subtopic_id, params.fetch(:per, 4), params[:page]

respond_to do |format|
format.html
Expand Down
12 changes: 6 additions & 6 deletions app/services/recommender.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Recommender
def initialize(sub_topic, per = 4, page = 0)
@sub_topic, @per, @page = sub_topic, per, page
def initialize(subtopic_id, per = 4, page = 0)
@subtopic_id, @per, @page = sub_topic, per, page
end

def ask
Expand All @@ -9,7 +9,7 @@ def ask
else
get_sub_topics.tap do |sub_topics|
Recommendation.create({
subtopic_id: @sub_topic.subtopic_id,
subtopic_id: @subtopic_id,
recommended_subtopic_ids: sub_topics.map { |s, l| [s, l.size] }
})
end
Expand All @@ -19,15 +19,15 @@ def ask
end

def get_recommendation
@recommendation ||= Recommendation.find_by_subtopic_id @sub_topic.subtopic_id
@recommendation ||= Recommendation.find_by_subtopic_id @subtopic_id
end

def get_sub_topics
sub_topics = {}

users_also_listened.each do |user|
user.listens.each do |listen|
next if listen.subtopic_id == @sub_topic.subtopic_id
next if listen.subtopic_id == @subtopic_id
sub_topics[listen.subtopic_id] ||= []
sub_topics[listen.subtopic_id] << listen
end
Expand All @@ -36,6 +36,6 @@ def get_sub_topics
end

def users_also_listened
User.who_listened_to(@sub_topic.subtopic_id)
User.who_listened_to(@subtopic_id)
end
end
48 changes: 29 additions & 19 deletions lib/tasks/load.rake
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,6 @@ namespace :load do
DATA_FILE = ENV.fetch "DATA_FILE", "./lib/data/listens.json"
SUBTOPIC_FILE = ENV.fetch "SUBTOPIC_FILE", "./lib/data/subtopics.json"

desc "Load the listens.json file into the db"
task listens: :environment do
listens = JSON.parse File.read DATA_FILE
puts "Loading #{listens.size} listen records..."

Listen.bulk_insert do |worker|
listens.each do |listen|
worker.add({
subtopic_id: listen["subtopic"],
listen_date: listen["listenDate"],
user_id: listen["user"]
})
end
end

puts "\aDone."
end

desc "Load the subtopics.json file into the db"
task sub_topics: :environment do
sub_topics = JSON.parse File.read SUBTOPIC_FILE
Expand All @@ -38,6 +20,24 @@ namespace :load do
puts "\aDone."
end

desc "Load the listens.json file into the db"
task listens: :environment do
listens = JSON.parse File.read DATA_FILE
puts "Loading #{listens.size} listen records..."

Listen.bulk_insert do |worker|
listens.each do |listen|
worker.add({
subtopic_id: listen["subtopic"],
listen_date: listen["listenDate"],
user_id: listen["user"]
})
end
end

puts "\aDone."
end

desc "Create users from listen models. Do this after running \`load:listens\`"
task users: :environment do
puts "Creating #{Listen.uniq_user_count} users..."
Expand All @@ -50,6 +50,16 @@ namespace :load do

desc "Clear listens, sub_topics, and users tables"
task reset: :environment do
[Listen, SubTopic, User].map &:delete_all
puts "Deleting all data..."
[Recommendation, Listen, SubTopic, User].map &:delete_all

puts "Loading all data..."
%w[sub_topics listens users].each { |task| Rake::Task["load:#{task}"].invoke }

subtopic_ids = SubTopic.pluck :subtopic_id
puts "Creating #{subtopic_ids.size} recommendations..."

subtopic_ids.each { |subtopic_id| Recommender.new(subtopic_id).ask }
puts "\aDone."
end
end

0 comments on commit d92695b

Please sign in to comment.