Commit 4944363e by Rocky Duan

seed for mongodb

parent 323c2feb
source :rubygems source :rubygems
gem 'rake'
gem 'sinatra' gem 'sinatra'
gem 'activerecord' gem 'activerecord'
...@@ -15,14 +17,14 @@ gem 'ampex' ...@@ -15,14 +17,14 @@ gem 'ampex'
gem 'thumbs_up', :git => "git@github.com:dementrock/thumbs_up.git" gem 'thumbs_up', :git => "git@github.com:dementrock/thumbs_up.git"
# ruby-mongo-driver # ruby-mongo-driver
gem 'mongo' gem 'mongo', "1.6.2"
gem 'bson' gem 'bson'
gem 'bson_ext' gem 'bson_ext'
gem 'mongoid' gem 'mongoid', "~> 2.4.0"
gem 'voteable_mongo'
gem 'mongoid-tree' gem 'mongoid-tree'
gem 'voteable_mongo'
group :test do group :test do
gem 'rspec' gem 'rspec'
......
require 'rubygems' require 'rubygems'
require 'active_record' require 'mongo'
require 'mongoid'
require 'yaml' require 'yaml'
require 'logger' require 'logger'
require 'sinatra'
require 'mongoid/tree'
require 'voteable_mongo'
desc "Load the environment" desc "Load the environment"
task :environment do task :environment do
env = ENV["SINATRA_ENV"] || "development" env = ENV["SINATRA_ENV"] || "development"
databases = YAML.load_file("config/database.yml") Sinatra::Base.environment = env
ActiveRecord::Base.establish_connection(databases[env]) Mongoid.load!("config/mongoid.yml")
Mongoid.logger.level = Logger::INFO
end end
namespace :db do namespace :db do
desc "Migrate the database"
task :migrate => :environment do
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Migration.verbose = true
ActiveRecord::Migrator.migrate("db/migrate")
end
task :seed => :environment do task :seed => :environment do
require_relative 'models/comment.rb' require_relative 'models/comment.rb'
require_relative 'models/comment_thread.rb' require_relative 'models/comment_thread.rb'
require_relative 'models/vote.rb'
require_relative 'models/user.rb' require_relative 'models/user.rb'
Comment.delete_all Comment.delete_all
CommentThread.delete_all CommentThread.delete_all
Vote.delete_all
User.delete_all User.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"]
def generate_comments(commentable_type, commentable_id, level_limit) user = User.create!(external_id: "1")
comment_thread = CommentThread.create! :commentable_type => commentable_type, :commentable_id => commentable_id
def generate_comments(commentable_type, commentable_id, level_limit, user)
5.times do 5.times do
comment_thread.root_comments.create :body => "top comment", :title => "top #{rand(10)}", :user_id => 1, :course_id => 1, :comment_thread_id => comment_thread.id, :endorsed => [true, false].sample comment_thread = CommentThread.new(
end commentable_type: commentable_type, commentable_id: commentable_id,
10.times do body: "This is a post", title: "Post No.#{rand(10)}",
comment = Comment.all.reject{|c| c.is_root? or c.depth >= level_limit or c.comment_thread_id != comment_thread.id}.sample course_id: "1")
comment.children.create :body => "comment body", :title => "comment title #{rand(50)}", :user_id => 1, :course_id => 1, :comment_thread_id => comment_thread.id, :endorsed => [true, false].sample comment_thread.author = user
comment_thread.save!
3.times do
comment = Comment.new(body: "top comment", course_id: "1")
comment.comment_thread = comment_thread
comment.author = user
comment.endorsed = [true, false].sample
comment.save!
end
100.times do
comment = Comment.where(comment_thread_id: comment_thread.id).reject{|c| c.depth >= level_limit}.sample
sub_comment = Comment.new(body: "comment body", course_id: "1")
sub_comment.author = user
sub_comment.endorsed = [true, false].sample
sub_comment.save!
comment.children << sub_comment
end
puts "Generating a comment thread for #{commentable_type} No.#{commentable_id}"
end end
end end
generate_comments("questions" , 1, level_limit) generate_comments("questions" , 1, level_limit, user)
generate_comments("questions" , 2, level_limit) generate_comments("questions" , 2, level_limit, user)
generate_comments("questions" , 3, level_limit) generate_comments("questions" , 3, level_limit, user)
generate_comments("courses" , 1, level_limit) generate_comments("courses" , 1, level_limit, user)
generate_comments("lectures" , 1, level_limit) generate_comments("lectures" , 1, level_limit, user)
generate_comments("lectures" , 2, level_limit) generate_comments("lectures" , 2, level_limit, user)
generate_comments("lectures" , 3, level_limit) generate_comments("lectures" , 3, level_limit, user)
=begin
Comment.all.reject{|c| c.is_root?}.each do |c| puts "voting"
(1..20).each do |id| users = []
user = User.find_or_create_by_id(id) (1..20).each do |id|
user.vote(c, {:direction => [:up, :down].sample}) users << User.find_or_create_by(external_id: id.to_s)
end
current = 0
total = Comment.count
Comment.all.each do |c|
(0...20).each do |i|
users[i].vote(c, [:up, :down].sample)
end end
current += 1
puts "voted #{current}/#{total}"
end end
=end
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"
end end
end end
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 100
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 100
development:
host: localhost
database: cs_comments_service_development
test:
host: localhost
database: cs_comments_service_test
require 'mongoid'
class Comment class Comment
include Mongoid::Document include Mongoid::Document
include Mongoid::Tree include Mongoid::Tree
include Mongo::Voteable include Mongo::Voteable
field :ancestry, type:String include Mongoid::Timestamps
voteable self, :up => +1, :down => -1
field :body, type: String field :body, type: String
field :user_id, type: String
field :course_id, type: String field :course_id, type: String
belongs_to :comment_thread field :endorsed, type: Boolean, default: false
belongs_to :author, class_name: "User", index: true
belongs_to :comment_thread, index: true
attr_accessible :body, :course_id
validates_presence_of :body
validates_presence_of :course_id
validates_presence_of :author
before_destroy :delete_descendants
#after_create :create_feeds
end end
class CommentThread class CommentThread
include Mongoid::Document include Mongoid::Document
include Mongo::Voteable
include Mongoid::Timestamps
voteable self, :up => +1, :down => -1
field :title, type: String field :title, type: String
field :body, type: String field :body, type: String
field :course_id, type: String field :course_id, type: String, index: true
field :commentable_id, type: String field :commentable_id, type: String
field :commentable_type, type: String field :commentable_type, type: String
belongs_to :author, class_name: "User"
has_many :comments belongs_to :author, class_name: "User", index: true
has_many :comments, dependent: :destroy # Use destroy to envoke callback on the top-level comments
attr_accessible :title, :body, :course_id, :commentable_id, :commentable_type
validates_presence_of :title
validates_presence_of :body
validates_presence_of :course_id
validates_presence_of :commentable_id
validates_presence_of :commentable_type
index [:commentable_type, :commentable_id]
#after_create :create_feeds
end end
class User class User
include Mongoid::Document include Mongoid::Document
include Mongo::Voter include Mongo::Voter
field :external_id, type: String field :external_id, type: String
has_many :comments has_many :comments
attr_accessible :external_id
validates_uniqueness_of :external_id
validates_presence_of :external_id
index :external_id, unique: true
end end
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