Commit d50568f4 by Rocky Duan

fixed index bug

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