From d8fd6277689d5f91c826e2374ccc968aa8211af8 Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Wed, 6 Aug 2014 20:19:38 +0000 Subject: [PATCH] checkpoint --- app/models/profile_suggestion.rb | 58 +++++++++++++++++++++++++++++++++++++++++++++------------- app/views/profile/_upload_image.html.erb | 2 +- db/migrate/20140720144212_create_profile_suggestions.rb | 2 ++ db/schema.rb | 4 +++- 4 files changed, 51 insertions(+), 15 deletions(-) diff --git a/app/models/profile_suggestion.rb b/app/models/profile_suggestion.rb index 1d273b6..764e7df 100644 --- a/app/models/profile_suggestion.rb +++ b/app/models/profile_suggestion.rb @@ -66,8 +66,16 @@ class ProfileSuggestion < ActiveRecord::Base # Number of communities in common COMMON_COMMUNITIES = 2 - # Number of friends in common - COMMON_TAGS = 2 + # Number of tags in common + COMMON_TAGS = 5 + + def self.connections(rule) + rule.split(/.*_with_/).last + '_connections' + end + + def self.counter(rule) + rule.split(/.*_with_/).last + '_count' + end def self.register_suggestions(person, suggested_profiles, rule) return if suggested_profiles.nil? @@ -75,10 +83,9 @@ class ProfileSuggestion < ActiveRecord::Base suggested_profiles = suggested_profiles.where("profiles.id NOT IN (#{already_suggested_profiles})") if already_suggested_profiles.present? suggested_profiles = suggested_profiles.limit(SUGGESTIONS_BY_RULE) return if suggested_profiles.blank? - counter = rule.split(/.*_with_/).last suggested_profiles.each do |suggested_profile| suggestion = person.profile_suggestions.find_or_initialize_by_suggestion_id(suggested_profile.id) - suggestion.send(counter+'=', suggested_profile.common_count.to_i) + suggestion.send(counter(rule)+'=', suggested_profile.common_count.to_i) suggestion.save! end end @@ -95,18 +102,43 @@ class ProfileSuggestion < ActiveRecord::Base # need here. def self.people_with_common_friends(person) - person_friends = person.friends.map(&:id) - return if person_friends.blank? - person.environment.people. - select("profiles.*, suggestions.count AS common_count"). - joins(" - INNER JOIN (SELECT person_id, count(person_id) FROM + "SELECT person_id as profiles_id, array_agg(friend_id) as common_friends_connections, count(person_id) as common_friends_count FROM friendships WHERE friend_id IN (#{person_friends.join(',')}) AND person_id NOT IN (#{(person_friends << person.id).join(',')}) GROUP BY person_id - HAVING count(person_id) >= #{COMMON_FRIENDS}) AS suggestions - ON profiles.id = suggestions.person_id") - end + HAVING count(person_id) >= #{COMMON_FRIENDS}" + end + + def self.all_suggestions(person) + select_string = "profiles.*, " + RULES.map { rule| "suggestions.#{counter(rule)} as #{counter(rule)}, suggestions.#{connections(rule)} as #{connections(rule)}" }.join(',') + suggestions_join = RULES.map.with_index do |rule, i| + if i == 0 + "(#{self.send(rule, person)}) AS #{rule}" + else + previous_rule = RULES[i-1] + "LEFT OUTER JOIN (#{self.send(rule, person)}) AS #{rule} ON #{previous_rule}.profiles_id = #{rule}.profiles_id" + end + end.join(' ') + join_string = "INNER JOIN (SELECT * FROM #{suggestions_join}) AS suggestions ON profiles.id = suggestions.profiles_id" + person.environment.profiles. + select(select_string). + joins(join_string) + + end + +# def self.people_with_common_friends(person) +# person_friends = person.friends.map(&:id) +# return if person_friends.blank? +# person.environment.people. +# select("profiles.*, suggestions.count as common_count, suggestions.array_agg as connections"). +# joins(" +# INNER JOIN (SELECT person_id, array_agg(friend_id), count(person_id) FROM +# friendships WHERE friend_id IN (#{person_friends.join(',')}) AND +# person_id NOT IN (#{(person_friends << person.id).join(',')}) +# GROUP BY person_id +# HAVING count(person_id) >= #{COMMON_FRIENDS}) AS suggestions +# ON profiles.id = suggestions.person_id") +# end def self.people_with_common_communities(person) person_communities = person.communities.map(&:id) diff --git a/app/views/profile/_upload_image.html.erb b/app/views/profile/_upload_image.html.erb index 444e4ba..247e310 100644 --- a/app/views/profile/_upload_image.html.erb +++ b/app/views/profile/_upload_image.html.erb @@ -10,5 +10,5 @@ -
+

diff --git a/db/migrate/20140720144212_create_profile_suggestions.rb b/db/migrate/20140720144212_create_profile_suggestions.rb index 8a3b726..b3860e4 100644 --- a/db/migrate/20140720144212_create_profile_suggestions.rb +++ b/db/migrate/20140720144212_create_profile_suggestions.rb @@ -6,10 +6,12 @@ class CreateProfileSuggestions < ActiveRecord::Migration t.string :suggestion_type t.text :categories t.boolean :enabled, :default => true + t.float :score t.timestamps end add_index :profile_suggestions, :person_id add_index :profile_suggestions, :suggestion_id + add_index :profile_suggestions, :score end end diff --git a/db/schema.rb b/db/schema.rb index 0cfcd64..36c79b4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20140720144212) do +ActiveRecord::Schema.define(:version => 20140805205626) do create_table "abuse_reports", :force => true do |t| t.integer "reporter_id" @@ -452,11 +452,13 @@ ActiveRecord::Schema.define(:version => 20140720144212) do t.string "suggestion_type" t.text "categories" t.boolean "enabled", :default => true + t.float "score" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false end add_index "profile_suggestions", ["person_id"], :name => "index_profile_suggestions_on_person_id" + add_index "profile_suggestions", ["score"], :name => "index_profile_suggestions_on_score" add_index "profile_suggestions", ["suggestion_id"], :name => "index_profile_suggestions_on_suggestion_id" create_table "profiles", :force => true do |t| -- libgit2 0.21.2