Commit cc221a11 by Rocky Duan

add task for generating seed for a single commentable

parent 58015e80
...@@ -84,25 +84,10 @@ namespace :db do ...@@ -84,25 +84,10 @@ namespace :db do
Subscription.delete_all Subscription.delete_all
end end
task :seed => :environment do def generate_comments_for(commentable_id)
Comment.delete_all
CommentThread.delete_all
User.delete_all
Notification.delete_all
Subscription.delete_all
beginning_time = Time.now
level_limit = YAML.load_file("config/application.yml")["level_limit"] level_limit = YAML.load_file("config/application.yml")["level_limit"]
users = (1..10).map {|id| User.find_or_create_by(external_id: id.to_s)} thread_seeds = [
10.times do
users.sample.subscribe(users.sample)
end
THREAD_SEEDS = [
{title: "This is really interesting", body: "best I've ever seen!"}, {title: "This is really interesting", body: "best I've ever seen!"},
{title: "We can probably make this better", body: "Let's do it"}, {title: "We can probably make this better", body: "Let's do it"},
{title: "I don't know where to start", body: "Can anyone help me?"}, {title: "I don't know where to start", body: "Can anyone help me?"},
...@@ -110,7 +95,7 @@ namespace :db do ...@@ -110,7 +95,7 @@ namespace :db do
{title: "I need five threads but I don't know what to put here", body: "So I'll just leave it this way"}, {title: "I need five threads but I don't know what to put here", body: "So I'll just leave it this way"},
] ]
COMMENT_BODY_SEEDS = [ comment_body_seeds = [
"dude I don't know what you're talking about", "dude I don't know what you're talking about",
"hi I'm Jack", "hi I'm Jack",
"hi just sent you a message", "hi just sent you a message",
...@@ -119,56 +104,85 @@ namespace :db do ...@@ -119,56 +104,85 @@ namespace :db do
"haha", "haha",
"lol", "lol",
] ]
def generate_comments(commentable_id, level_limit, users) users = User.all.to_a
THREAD_SEEDS.each do |thread_seed|
comment_thread = CommentThread.new(commentable_id: commentable_id, body: thread_seed[:body], title: thread_seed[:title], course_id: "1") puts "Generating threads and comments for #{commentable_id}..."
comment_thread.author = users.sample
comment_thread.save! threads = []
3.times do comments = []
comment = comment_thread.comments.new(body: COMMENT_BODY_SEEDS.sample, course_id: "1")
comment.author = users.sample thread_seeds.each do |thread_seed|
comment.endorsed = [true, false].sample comment_thread = CommentThread.new(commentable_id: commentable_id, body: thread_seed[:body], title: thread_seed[:title], course_id: "1")
comment.save! comment_thread.author = users.sample
end comment_thread.save!
10.times do threads << comment_thread
comment = Comment.where(comment_thread_id: comment_thread.id).reject{|c| c.depth >= level_limit}.sample 3.times do
sub_comment = comment.children.new(body: COMMENT_BODY_SEEDS.sample, course_id: "1") comment = comment_thread.comments.new(body: comment_body_seeds.sample, course_id: "1")
sub_comment.author = users.sample comment.author = users.sample
sub_comment.endorsed = [true, false].sample comment.endorsed = [true, false].sample
sub_comment.save! comment.save!
end comments << comment
puts "Generating a comment thread for #{commentable_id}" end
10.times do
comment = Comment.where(comment_thread_id: comment_thread.id).reject{|c| c.depth >= level_limit}.sample
sub_comment = comment.children.new(body: comment_body_seeds.sample, course_id: "1")
sub_comment.author = users.sample
sub_comment.endorsed = [true, false].sample
sub_comment.save!
comments << sub_comment
end end
end end
generate_comments("question_1", level_limit, users) puts "voting for these threads & comments.."
generate_comments("question_2", level_limit, users)
generate_comments("course_1", level_limit, users)
generate_comments("lecture_1", level_limit, users)
generate_comments("video_1", level_limit, users)
generate_comments("video_2", level_limit, users)
generate_comments("lab_1", level_limit, users)
generate_comments("lab_2", level_limit, users)
puts "voting"
users = []
(1..10).each do |id|
users << User.find_or_create_by(external_id: id.to_s)
end
CommentThread.all.each do |c| threads.each do |c|
(0...10).each do |i| users.each do |user|
users[i].vote(c, [:up, :down].sample) user.vote(c, [:up, :down].sample)
end end
end end
Comment.all.each do |c| comments.each do |c|
(0...10).each do |i| users.each do |user|
users[i].vote(c, [:up, :down].sample) user.vote(c, [:up, :down].sample)
end end
end end
puts "finished"
end
task :generate_comments, [:commentable_id] => :environment do |t, args|
generate_comments_for(args[:commentable_id])
end
task :seed => :environment do
Comment.delete_all
CommentThread.delete_all
User.delete_all
Notification.delete_all
Subscription.delete_all
beginning_time = Time.now
users = (1..10).map {|id| User.find_or_create_by(external_id: id.to_s)}
10.times do
users.sample.subscribe(users.sample)
end
generate_comments_for("question_1")
generate_comments_for("question_2")
generate_comments_for("course_1")
generate_comments_for("lecture_1")
generate_comments_for("video_1")
generate_comments_for("video_2")
generate_comments_for("lab_1")
generate_comments_for("lab_2")
end_time = Time.now end_time = Time.now
puts "Number of comments generated: #{Comment.count}" puts "Number of comments generated: #{Comment.count}"
......
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