Commit d50568f4 by Rocky Duan

fixed index bug

parent aaa72d0f
...@@ -4,7 +4,6 @@ require 'bundler' ...@@ -4,7 +4,6 @@ require 'bundler'
Bundler.setup Bundler.setup
Bundler.require Bundler.require
desc "Load the environment" desc "Load the environment"
task :environment do task :environment do
environment = ENV["SINATRA_ENV"] || "development" environment = ENV["SINATRA_ENV"] || "development"
...@@ -30,7 +29,7 @@ def create_test_user(id) ...@@ -30,7 +29,7 @@ def create_test_user(id)
User.create!(external_id: id, username: "user#{id}", email: "user#{id}@test.com") User.create!(external_id: id, username: "user#{id}", email: "user#{id}@test.com")
end end
#Dir.glob('lib/tasks/*.rake').each { |r| import r } Dir.glob('lib/tasks/*.rake').each { |r| import r }
task :console => :environment do task :console => :environment do
binding.pry binding.pry
...@@ -38,28 +37,23 @@ end ...@@ -38,28 +37,23 @@ end
namespace :db do namespace :db do
task :init => :environment do task :init => :environment do
puts "creating indexes..." puts "recreating indexes..."
Comment.create_indexes [Comment, CommentThread, User, Notification, Subscription, Activity, Delayed::Backend::Mongoid::Job].each(&:remove_indexes).each(&:create_indexes)
CommentThread.create_indexes
User.create_indexes
Notification.create_indexes
Subscription.create_indexes
Delayed::Backend::Mongoid::Job.create_indexes
puts "finished" puts "finished"
end end
task :clean => :environment do task :clean => :environment do
Comment.delete_all Comment.delete_all
CommentThread.delete_all CommentThread.delete_all
Content.recalculate_all_context_tag_weights! CommentThread.recalculate_all_context_tag_weights!
User.delete_all User.delete_all
Notification.delete_all Notification.delete_all
Subscription.delete_all Subscription.delete_all
end end
THREADS_PER_COMMENTABLE = 20 THREADS_PER_COMMENTABLE = 20
TOP_COMMENTS_PER_THREAD = 4 TOP_COMMENTS_PER_THREAD = 3
ADDITIONAL_COMMENTS_PER_THREAD = 20 ADDITIONAL_COMMENTS_PER_THREAD = 4
COURSE_ID = "MITx/6.002x/2012_Fall" COURSE_ID = "MITx/6.002x/2012_Fall"
...@@ -115,13 +109,13 @@ namespace :db do ...@@ -115,13 +109,13 @@ namespace :db do
end end
end end
# puts "voting" puts "voting"
# (threads + top_comments + additional_comments).each do |c| (threads + top_comments + additional_comments).each do |c|
# users.each do |user| users.each do |user|
# user.vote(c, [:up, :down].sample) user.vote(c, [:up, :down].sample)
# end end
# end end
puts "finished" puts "finished"
end end
...@@ -183,7 +177,7 @@ namespace :db do ...@@ -183,7 +177,7 @@ namespace :db do
Comment.delete_all Comment.delete_all
CommentThread.delete_all CommentThread.delete_all
Content.recalculate_all_context_tag_weights! CommentThread.recalculate_all_context_tag_weights!
User.delete_all User.delete_all
Notification.delete_all Notification.delete_all
Subscription.delete_all Subscription.delete_all
...@@ -204,8 +198,8 @@ namespace :db do ...@@ -204,8 +198,8 @@ namespace :db do
# user.subscribe(other_user) # user.subscribe(other_user)
# end # end
generate_comments_for("video_1") generate_comments_for("video_1")
#generate_comments_for("lab_1") generate_comments_for("lab_1")
#generate_comments_for("lab_2") generate_comments_for("lab_2")
end_time = Time.now end_time = Time.now
......
...@@ -10,7 +10,6 @@ get "#{APIPREFIX}/:commentable_id/threads" do |commentable_id| ...@@ -10,7 +10,6 @@ get "#{APIPREFIX}/:commentable_id/threads" do |commentable_id|
end end
post "#{APIPREFIX}/:commentable_id/threads" do |commentable_id| post "#{APIPREFIX}/:commentable_id/threads" do |commentable_id|
puts "creating thread"
thread = CommentThread.new(params.slice(*%w[title body course_id]).merge(commentable_id: commentable_id)) thread = CommentThread.new(params.slice(*%w[title body course_id]).merge(commentable_id: commentable_id))
thread.anonymous = bool_anonymous || false thread.anonymous = bool_anonymous || false
thread.tags = params["tags"] || "" thread.tags = params["tags"] || ""
...@@ -22,5 +21,4 @@ post "#{APIPREFIX}/:commentable_id/threads" do |commentable_id| ...@@ -22,5 +21,4 @@ post "#{APIPREFIX}/:commentable_id/threads" do |commentable_id|
user.subscribe(thread) if bool_auto_subscribe user.subscribe(thread) if bool_auto_subscribe
thread.to_hash.to_json thread.to_hash.to_json
end end
puts CommentThread.count
end end
seed_size: seed_size:
commentables: 20 commentables: 1
users: 20 users: 1
threads: 20 threads: 5
top_comments: 100 top_comments: 0
sub_comments: 100 sub_comments: 0
votes: 100 votes: 0
tags: 1000 tags: 0
require 'rest_client' require 'rest_client'
namespace :benchmark do namespace :benchmark do
task :bulk_generate do task :bulk_generate => :environment do
#[Comment, CommentThread, User, Notification, Subscription].each(&:delete_all).each(&:create_indexes) [Comment, CommentThread, User, Notification, Subscription].each(&:delete_all).each(&:create_indexes)
#Delayed::Backend::Mongoid::Job.create_indexes Delayed::Backend::Mongoid::Job.create_indexes
seed_config = YAML.load_file("config/benchmark.yml").with_indifferent_access seed_config = YAML.load_file("config/benchmark.yml").with_indifferent_access
...@@ -18,18 +18,18 @@ namespace :benchmark do ...@@ -18,18 +18,18 @@ namespace :benchmark do
PREFIX = "http://localhost:4567/api/v1" PREFIX = "http://localhost:4567/api/v1"
#Benchmark.bm(31) do |x| Benchmark.bm(31) do |x|
RestClient.get "#{PREFIX}/clean" RestClient.get "#{PREFIX}/clean"
#x.report "create users via api" do x.report "create users via api" do
(1..USERS).each do |user_id| (1..USERS).each do |user_id|
data = { id: user_id, username: "user#{user_id}", email: "user#{user_id}@test.com" } data = { id: user_id, username: "user#{user_id}", email: "user#{user_id}@test.com" }
RestClient.post "#{PREFIX}/users", data RestClient.post "#{PREFIX}/users", data
end end
#end end
#x.report "create new threads via api" do x.report "create new threads via api" do
(1..THREADS).each do |t| (1..THREADS).each do |t|
data = {title: "Interesting question", body: "cool", anonymous: false, \ data = {title: "Interesting question", body: "cool", anonymous: false, \
course_id: "1", user_id: (rand(USERS) + 1).to_s, \ course_id: "1", user_id: (rand(USERS) + 1).to_s, \
...@@ -38,39 +38,37 @@ namespace :benchmark do ...@@ -38,39 +38,37 @@ namespace :benchmark do
RestClient.post "#{PREFIX}/question_#{rand(COMMENTABLES).to_s}/threads", data RestClient.post "#{PREFIX}/question_#{rand(COMMENTABLES).to_s}/threads", data
end end
#end end
#comment_thread_ids = CommentThread.all.to_a.map(&:id) comment_thread_ids = CommentThread.all.to_a.map(&:id)
#binding.pry x.report("create top comments via api") do
#x.report("create top comments via api") do
TOP_COMMENTS.times do TOP_COMMENTS.times do
data = {body: "lalala", anonymous: false, data = {body: "lalala", anonymous: false,
course_id: "1", user_id: (rand(USERS) + 1).to_s} course_id: "1", user_id: (rand(USERS) + 1).to_s}
RestClient.post "#{PREFIX}/threads/#{comment_thread_ids.sample}/comments", data RestClient.post "#{PREFIX}/threads/#{comment_thread_ids.sample}/comments", data
end end
#end end
#top_comment_ids = Comment.all.to_a.map(&:id) top_comment_ids = Comment.all.to_a.map(&:id)
#x.report("create sub comments") do x.report("create sub comments") do
SUB_COMMENTS.times do SUB_COMMENTS.times do
data = {body: "lalala", anonymous: false, data = {body: "lalala", anonymous: false,
course_id: "1", user_id: (rand(USERS) + 1).to_s} course_id: "1", user_id: (rand(USERS) + 1).to_s}
RestClient.post "#{PREFIX}/comments/#{top_comment_ids.sample}", data RestClient.post "#{PREFIX}/comments/#{top_comment_ids.sample}", data
end end
#end end
#x.report("create votes") do x.report("create votes") do
VOTES.times do VOTES.times do
data = {user_id: (rand(USERS) + 1).to_s, value: [:up, :down].sample} data = {user_id: (rand(USERS) + 1).to_s, value: [:up, :down].sample}
RestClient.put "#{PREFIX}/threads/#{comment_thread_ids.sample}/votes", data RestClient.put "#{PREFIX}/threads/#{comment_thread_ids.sample}/votes", data
RestClient.put "#{PREFIX}/comments/#{top_comment_ids.sample}/votes", data RestClient.put "#{PREFIX}/comments/#{top_comment_ids.sample}/votes", data
end end
#end end
end end
#end end
end end
require_relative 'content' require_relative 'content'
class Comment < Content class Comment < Content
include Mongoid::Tree include Mongoid::Tree
include Mongo::Voteable include Mongo::Voteable
include Mongoid::Timestamps include Mongoid::Timestamps
......
require_relative 'content' require_relative 'content'
class CommentThread < Content class CommentThread < Content
include Mongo::Voteable include Mongo::Voteable
include Mongoid::Timestamps include Mongoid::Timestamps
include Mongoid::TaggableWithContext
include Mongoid::TaggableWithContext::AggregationStrategy::RealTime
taggable separator: ',', default: [], sparse_index: true
voteable self, :up => +1, :down => -1 voteable self, :up => +1, :down => -1
......
class Content class Content
include Mongoid::Document include Mongoid::Document
include Mongoid::TaggableWithContext
include Mongoid::TaggableWithContext::AggregationStrategy::RealTime
taggable separator: ',', default: []
def author_with_anonymity(attr=nil, attr_when_anonymous=nil) def author_with_anonymity(attr=nil, attr_when_anonymous=nil)
if not attr if not attr
anonymous ? nil : author anonymous ? nil : author
......
...@@ -137,7 +137,7 @@ describe "app" do ...@@ -137,7 +137,7 @@ describe "app" do
end end
describe "GET /api/v1/threads/tags" do describe "GET /api/v1/threads/tags" do
it "get all tags used in threads" do it "get all tags used in threads" do
Content.recalculate_all_context_tag_weights! CommentThread.recalculate_all_context_tag_weights!
thread1 = CommentThread.all.to_a.first thread1 = CommentThread.all.to_a.first
thread2 = CommentThread.all.to_a.last thread2 = CommentThread.all.to_a.last
thread1.tags = "a, b, c" thread1.tags = "a, b, c"
...@@ -162,7 +162,7 @@ describe "app" do ...@@ -162,7 +162,7 @@ describe "app" do
end end
it "returns autocomplete results" do it "returns autocomplete results" do
CommentThread.delete_all CommentThread.delete_all
Content.recalculate_all_context_tag_weights! CommentThread.recalculate_all_context_tag_weights!
create_comment_thread "c++, clojure, common-lisp, c#, c, coffeescript" create_comment_thread "c++, clojure, common-lisp, c#, c, coffeescript"
create_comment_thread "c++, clojure, common-lisp, c#, c" create_comment_thread "c++, clojure, common-lisp, c#, c"
create_comment_thread "c++, clojure, common-lisp, c#" create_comment_thread "c++, clojure, common-lisp, c#"
......
...@@ -35,12 +35,8 @@ def create_test_user(id) ...@@ -35,12 +35,8 @@ def create_test_user(id)
end end
def init_without_subscriptions def init_without_subscriptions
Comment.delete_all
CommentThread.delete_all [Comment, CommentThread, User, Notification, Subscription, Activity, Delayed::Backend::Mongoid::Job].each(&:delete_all).each(&:remove_indexes).each(&:create_indexes)
Content.recalculate_all_context_tag_weights!
User.delete_all
Notification.delete_all
Subscription.delete_all
Tire.index 'comment_threads' do delete end Tire.index 'comment_threads' do delete end
CommentThread.create_elasticsearch_index CommentThread.create_elasticsearch_index
...@@ -108,12 +104,7 @@ def init_without_subscriptions ...@@ -108,12 +104,7 @@ def init_without_subscriptions
end end
def init_with_subscriptions def init_with_subscriptions
Comment.delete_all [Comment, CommentThread, User, Notification, Subscription, Activity, Delayed::Backend::Mongoid::Job].each(&:delete_all).each(&:remove_indexes).each(&:create_indexes)
CommentThread.delete_all
Content.recalculate_all_context_tag_weights!
User.delete_all
Notification.delete_all
Subscription.delete_all
Tire.index 'comment_threads' do delete end Tire.index 'comment_threads' do delete end
CommentThread.create_elasticsearch_index CommentThread.create_elasticsearch_index
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment