Rakefile 7.48 KB
Newer Older
Rocky Duan committed
1
require 'rubygems'
2 3 4 5 6
require 'bundler'

Bundler.setup
Bundler.require

Rocky Duan committed
7 8 9

desc "Load the environment"
task :environment do
10 11
  environment = ENV["SINATRA_ENV"] || "development"
  Sinatra::Base.environment = environment
Rocky Duan committed
12 13
  Mongoid.load!("config/mongoid.yml")
  Mongoid.logger.level = Logger::INFO
14 15 16 17 18
  module CommentService
    class << self; attr_accessor :config; end
  end

  CommentService.config = YAML.load_file("config/application.yml")
Rocky Duan committed
19

20 21 22
  Dir[File.dirname(__FILE__) + '/lib/**/*.rb'].each {|file| require file}
  Dir[File.dirname(__FILE__) + '/models/*.rb'].each {|file| require file}
  Dir[File.dirname(__FILE__) + '/models/observers/*.rb'].each {|file| require file}
Rocky Duan committed
23

24 25 26 27 28 29 30
  Mongoid.observers = PostReplyObserver, PostTopicObserver, AtUserObserver
  Mongoid.instantiate_observers

end

def create_test_user(id)
  User.create!(external_id: id, username: "user#{id}", email: "user#{id}@test.com")
Rocky Duan committed
31 32
end

Rocky Duan committed
33 34
Dir.glob('lib/tasks/*.rake').each { |r| import r }

35 36 37 38
task :console => :environment do
  binding.pry
end

Rocky Duan committed
39
namespace :db do
40 41 42 43 44
  task :init => :environment do
    puts "creating indexes..."
    Comment.create_indexes
    CommentThread.create_indexes
    User.create_indexes
45
    Notification.create_indexes
46
    Subscription.create_indexes
47 48 49
    Delayed::Backend::Mongoid::Job.create_indexes
    puts "finished"
  end
Rocky Duan committed
50

Rocky Duan committed
51 52 53
  task :clean => :environment do
    Comment.delete_all
    CommentThread.delete_all
54
    CommentThread.recalculate_all_context_tag_weights!
Rocky Duan committed
55 56 57 58 59
    User.delete_all
    Notification.delete_all
    Subscription.delete_all
  end

Mike Chen committed
60 61 62
  THREADS_PER_COMMENTABLE = 20
  TOP_COMMENTS_PER_THREAD = 4
  ADDITIONAL_COMMENTS_PER_THREAD = 20
63

Rocky Duan committed
64 65
  COURSE_ID = "MITx/6.002x/2012_Fall"

66
  def generate_comments_for(commentable_id)
Rocky Duan committed
67
    level_limit = YAML.load_file("config/application.yml")["level_limit"]
Rocky Duan committed
68

69 70 71 72 73 74 75 76 77
    tag_seeds = [
      "artificial-intelligence",
      "random rant",
      "c++",
      "c#",
      "java-sucks",
      "2012",
    ]

78 79 80 81 82
    users = User.all.to_a

    puts "Generating threads and comments for #{commentable_id}..."

    threads = []
83 84
    top_comments = []
    additional_comments = []
85

86
    THREADS_PER_COMMENTABLE.times do
87 88
      inner_top_comments = []

Rocky Duan committed
89
      comment_thread = CommentThread.new(commentable_id: commentable_id, body: Faker::Lorem.paragraphs.join("\n\n"), title: Faker::Lorem.sentence(6))
90
      comment_thread.author = users.sample
91
      comment_thread.tags = tag_seeds.sort_by{rand}[0..2].join(",")
Rocky Duan committed
92
      comment_thread.course_id = COURSE_ID
93 94
      comment_thread.save!
      threads << comment_thread
Rocky Duan committed
95 96
      users.sample(3).each {|user| user.subscribe(comment_thread)}
      (1 + rand(TOP_COMMENTS_PER_THREAD)).times do
Rocky Duan committed
97
        comment = comment_thread.comments.new(body: Faker::Lorem.paragraph(2))
98 99
        comment.author = users.sample
        comment.endorsed = [true, false].sample
100
        comment.comment_thread = comment_thread
Rocky Duan committed
101
        comment.course_id = COURSE_ID
102
        comment.save!
103
        top_comments << comment
104
        inner_top_comments << comment
105
      end
Rocky Duan committed
106
      (1 + rand(ADDITIONAL_COMMENTS_PER_THREAD)).times do
107
        comment = inner_top_comments.sample
Rocky Duan committed
108
        sub_comment = comment.children.new(body: Faker::Lorem.paragraph(2))
109 110
        sub_comment.author = users.sample
        sub_comment.endorsed = [true, false].sample
111
        sub_comment.comment_thread = comment_thread
Rocky Duan committed
112
        sub_comment.course_id = COURSE_ID
113
        sub_comment.save!
114
        additional_comments << sub_comment
115 116 117
      end
    end

Mike Chen committed
118
    # puts "voting"
Rocky Duan committed
119

Mike Chen committed
120 121 122 123 124
    # (threads + top_comments + additional_comments).each do |c|
    #   users.each do |user|
    #     user.vote(c, [:up, :down].sample)
    #   end
    # end
125 126 127 128 129 130 131 132 133 134
    puts "finished"
  end


  task :generate_comments, [:commentable_id] => :environment do |t, args|

    generate_comments_for(args[:commentable_id])

  end

Rocky Duan committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
  task :bulk_seed, [:num] => :environment do |t, args|
    Mongoid.configure do |config|
      config.connect_to("cs_comments_service_bulk_test")
    end
    connnection = Mongo::Connection.new("127.0.0.1", "27017")
    db = Mongo::Connection.new.db("cs_comments_service_bulk_test")

    CommentThread.create_indexes
    Comment.create_indexes
    Content.delete_all
    coll = db.collection("contents")
    args[:num].to_i.times do
      doc = {"_type" => "CommentThread", "anonymous" => [true, false].sample, "at_position_list" => [], 
          "tags_array" => [], 
          "comment_count" => 0, "title" => Faker::Lorem.sentence(6), "author_id" => rand(1..10).to_s, 
          "body" => Faker::Lorem.paragraphs.join("\n\n"), "course_id" => COURSE_ID, "created_at" => Time.now,
          "commentable_id" => COURSE_ID, "closed" => [true, false].sample, "updated_at" => Time.now, "last_activity_at" => Time.now,
          "votes" => {"count" => 0, "down" => [], "down_count" => 0, "point" => 0, "up" => [], "up_count" => []}}
      coll.insert(doc)
    end
    binding.pry
    Tire.index('comment_threads').delete
    CommentThread.create_elasticsearch_index
    Tire.index('comment_threads') { import CommentThread.all }
  end

Mike Chen committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
  task :seed_fast => :environment do
    ADDITIONAL_COMMENTS_PER_THREAD = 20

    config = YAML.load_file("config/mongoid.yml")[Sinatra::Base.environment]["sessions"]["default"]
    connnection = Mongo::Connection.new(config["hosts"][0].split(":")[0], config["hosts"][0].split(":")[1])
    db = Mongo::Connection.new.db(config["database"])
    coll = db.collection("contents")
    Comment.delete_all
    CommentThread.each do |thread|
      ADDITIONAL_COMMENTS_PER_THREAD.times do
        doc = {"_type" => "Comment", "anonymous" => false, "at_position_list" => [], 
          "author_id" => rand(1..10).to_s, "body" => Faker::Lorem.paragraphs.join("\n\n"), 
          "comment_thread_id" => BSON::ObjectId.from_string(thread.id.to_s), "course_id" => COURSE_ID, 
          "created_at" => Time.now,
          "endorsed" => [true, false].sample, "parent_ids" => [], "updated_at" => Time.now,
          "votes" => {"count" => 0, "down" => [], "down_count" => 0, "point" => 0, "up" => [], "up_count" => []}}
        coll.insert(doc)
      end
    end
  end

182 183 184 185
  task :seed => :environment do

    Comment.delete_all
    CommentThread.delete_all
186
    CommentThread.recalculate_all_context_tag_weights!
187 188 189
    User.delete_all
    Notification.delete_all
    Subscription.delete_all
Rocky Duan committed
190 191
    Tire.index 'comment_threads' do delete end
    CommentThread.create_elasticsearch_index
192 193 194

    beginning_time = Time.now

195
    users = (1..10).map {|id| create_test_user(id)}
Mike Chen committed
196 197 198 199 200 201 202 203 204 205
    # 3.times do
    #   other_user = users[1..9].sample
    #   users.first.subscribe(other_user)
    # end

    # 10.times do
    #   user = users.sample
    #   other_user = users.select{|u| u != user}.sample
    #   user.subscribe(other_user)
    # end
206 207 208 209
    generate_comments_for("video_1")
    generate_comments_for("lab_1")
    generate_comments_for("lab_2")

Rocky Duan committed
210 211 212 213 214 215 216
    end_time = Time.now

    puts "Number of comments generated: #{Comment.count}"
    puts "Number of comment threads generated: #{CommentThread.count}"

    puts "Time elapsed #{(end_time - beginning_time)*1000} milliseconds"

Rocky Duan committed
217
  end
Rocky Duan committed
218

Rocky Duan committed
219 220 221 222
  task :reindex_search => :environment do 
    Tire.index('comment_threads').delete
    CommentThread.create_elasticsearch_index
    Tire.index('comment_threads') { import CommentThread.all }
Rocky Duan committed
223
  end
Rocky Duan committed
224
end
225 226 227 228 229 230 231 232 233 234 235 236

namespace :jobs do
  desc "Clear the delayed_job queue."
  task :clear => :environment do
    Delayed::Job.delete_all
  end

  desc "Start a delayed_job worker."
  task :work => :environment do
    Delayed::Worker.new(:min_priority => ENV['MIN_PRIORITY'], :max_priority => ENV['MAX_PRIORITY'], :queues => (ENV['QUEUES'] || ENV['QUEUE'] || '').split(','), :quiet => false).start
  end
end