Commit b89e833b by Rocky Duan

sorting - mostly untested

parent a1af1ad7
......@@ -26,6 +26,7 @@ gem 'delayed_job_mongoid', :git => 'git@github.com:dementrock/delayed_job_mongoi
gem 'mongoid-tree', :git => 'git@github.com:dementrock/mongoid-tree.git'
gem 'voteable_mongo', :git => 'git@github.com:dementrock/voteable_mongo.git'
gem 'mongoid_taggable', :git => 'git@github.com:dementrock/mongoid_taggable.git'
gem 'mongoid_magic_counter_cache', :git => 'git@github.com:dementrock/mongoid-magic-counter-cache.git'
gem 'sunspot'
gem 'sunspot_solr'
......
......@@ -86,9 +86,9 @@ namespace :db do
Subscription.delete_all
end
THREADS_PER_COMMENTABLE = 200
TOP_COMMENTS_PER_THREAD = 0
ADDITIONAL_COMMENTS_PER_THREAD = 0
THREADS_PER_COMMENTABLE = 50
TOP_COMMENTS_PER_THREAD = 3
ADDITIONAL_COMMENTS_PER_THREAD = 10
def generate_comments_for(commentable_id)
level_limit = YAML.load_file("config/application.yml")["level_limit"]
......@@ -156,13 +156,13 @@ namespace :db do
end
end
=begin
puts "voting"
(threads + top_comments + additional_comments).each do |c|
users.each do |user|
user.vote(c, [:up, :down].sample)
end
end
=end
puts "finished"
end
......
......@@ -15,19 +15,49 @@ end
CommentService.config = YAML.load_file("config/application.yml")
Mongoid.load!("config/mongoid.yml")
Mongoid.load!("config/mongoid.yml", environment)
Mongoid.logger.level = Logger::INFO
Dir[File.dirname(__FILE__) + '/lib/**/*.rb'].each {|file| require file}
Dir[File.dirname(__FILE__) + '/models/*.rb'].each {|file| require file}
get '/api/v1/search/threads' do
CommentThread.solr_search do
fulltext(params["text"]) if params["text"]
with(:commentable_id, params["commentable_id"]) if params["commentable_id"]
with(:course_id, params["course_id"]) if params["course_id"]
with(:tags).all_of(params["tags"].split /,/) if params["tags"]
end.results.map(&:to_hash).to_json
sort_key_mapper = {
"date" => :created_at,
"votes" => :votes_point,
"comments" => :comment_count,
}
sort_order_mapper = {
"desc" => :desc,
"asc" => :asc,
}
sort_key = sort_key_mapper[params["sort_key"]]
sort_order = sort_order_mapper[params["sort_order"]]
sort_keyword_valid = (!params["sort_key"] && !params["sort_order"] || sort_key && sort_order)
if (!params["text"] && !params["tags"]) || !sort_keyword_valid
{}.to_json
else
page = (params["page"] || 1).to_i
per_page = (params["per_page"] || 20).to_i
search = CommentThread.solr_search do
fulltext(params["text"]) if params["text"]
with(:commentable_id, params["commentable_id"]) if params["commentable_id"]
with(:course_id, params["course_id"]) if params["course_id"]
with(:tags).all_of(params["tags"].split /,/) if params["tags"]
paginate :page => page, :per_page => per_page
order_by(sort_key, sort_order) if sort_key && sort_order
end
num_pages = [1, (search.total / per_page.to_f).ceil].max
{
collection: search.results.map{|t| t.to_hash(recursive: value_to_boolean(params["recursive"]))},
num_pages: num_pages,
page: page,
}.to_json
end
end
delete '/api/v1/:commentable_id/threads' do |commentable_id|
......@@ -36,17 +66,37 @@ delete '/api/v1/:commentable_id/threads' do |commentable_id|
end
get '/api/v1/:commentable_id/threads' do |commentable_id|
page = (params["page"] || 1).to_i
per_page = (params["per_page"] || 20).to_i
comment_threads = commentable.comment_threads
num_pages = [1, (comment_threads.count / per_page.to_f).ceil].max
page = [num_pages, [1, page].max].min
{
collection: comment_threads.page(page).per(per_page).map{|t| t.to_hash(recursive: value_to_boolean(params["recursive"]))},
num_pages: num_pages,
per_page: per_page,
page: page,
}.to_json
sort_key_mapper = {
"date" => :created_at,
"votes" => :"votes.point",
"comments" => :comment_count,
}
sort_order_mapper = {
"desc" => :desc,
"asc" => :asc,
}
sort_key = sort_key_mapper[params["sort_key"]]
sort_order = sort_order_mapper[params["sort_order"]]
sort_keyword_valid = (!params["sort_key"] && !params["sort_order"] || sort_key && sort_order)
if not sort_keyword_valid
{}.to_json
else
page = (params["page"] || 1).to_i
per_page = (params["per_page"] || 20).to_i
comment_threads = commentable.comment_threads
comment_threads = comment_threads.order_by("#{sort_key} #{sort_order}") if sort_key && sort_order
num_pages = [1, (comment_threads.count / per_page.to_f).ceil].max
page = [num_pages, [1, page].max].min
paged_comment_threads = comment_threads.page(page).per(per_page)
{
collection: paged_comment_threads.map{|t| t.to_hash(recursive: value_to_boolean(params["recursive"]))},
num_pages: num_pages,
page: page,
}.to_json
end
end
post '/api/v1/:commentable_id/threads' do |commentable_id|
......
......@@ -46,4 +46,5 @@ helpers do
def value_to_boolean(value)
!!(value.to_s =~ /^true$/i)
end
end
......@@ -4,6 +4,7 @@ class Comment < Content
include Mongoid::Tree
include Mongo::Voteable
include Mongoid::Timestamps
include Mongoid::MagicCounterCache
voteable self, :up => +1, :down => -1
......@@ -22,6 +23,8 @@ class Comment < Content
validates_presence_of :comment_thread, autosave: false
validates_presence_of :author, autosave: false
counter_cache :comment_thread
before_destroy :delete_descendants # TODO async
after_create :generate_notifications
......
......@@ -7,6 +7,7 @@ class CommentThread < Content
voteable self, :up => +1, :down => -1
field :comment_count, type: Integer, default: 0
field :title, type: String
field :body, type: String
field :course_id, type: String
......@@ -18,6 +19,10 @@ class CommentThread < Content
text :title, boost: 5.0, stored: true, more_like_this: true
text :body, stored: true, more_like_this: true
integer :comment_count
integer :votes_point do
votes_point
end
string :course_id
string :commentable_id
string :author_id
......
......@@ -21,33 +21,33 @@ describe "app" do
get "/api/v1/search/threads", tags: [ai, ml].join(",")
last_response.should be_ok
threads = parse last_response.body
threads = parse(last_response.body)['collection']
threads.length.should == 2
threads.select{|t| t["id"] == thread1.id.to_s}.first.should_not be_nil
threads.select{|t| t["id"] == thread2.id.to_s}.first.should_not be_nil
get "/api/v1/search/threads", tags: [ai].join(",")
last_response.should be_ok
threads = parse last_response.body
threads = parse(last_response.body)['collection']
threads.length.should == 2
threads.select{|t| t["id"] == thread1.id.to_s}.first.should_not be_nil
threads.select{|t| t["id"] == thread2.id.to_s}.first.should_not be_nil
get "/api/v1/search/threads", tags: [ai, random1].join(",")
last_response.should be_ok
threads = parse last_response.body
threads = parse(last_response.body)['collection']
threads.length.should == 1
threads.select{|t| t["id"] == thread1.id.to_s}.first.should_not be_nil
get "/api/v1/search/threads", tags: [random1].join(",")
last_response.should be_ok
threads = parse last_response.body
threads = parse(last_response.body)['collection']
threads.length.should == 1
threads.select{|t| t["id"] == thread1.id.to_s}.first.should_not be_nil
get "/api/v1/search/threads", tags: [random1, random2].join(",")
last_response.should be_ok
threads = parse last_response.body
threads = parse(last_response.body)['collection']
threads.length.should == 0
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