Commit dd0de522 by Your Name

refactored to make rake task more generic and moved logic into the Content model

parent e2015bfa
1355346191 1355422201
#get_marked_text(text) /home/kevin/mitx_all/cs_comments_service/spec/models/at_user_observer_spec.rb describe "#get_marked_text(text)" do #get_marked_text(text) /home/kevin/mitx_all/cs_comments_service/spec/models/at_user_observer_spec.rb describe "#get_marked_text(text)" do
#get_valid_at_position_list(text) /home/kevin/mitx_all/cs_comments_service/spec/models/at_user_observer_spec.rb describe "#get_valid_at_position_list(text)" do #get_valid_at_position_list(text) /home/kevin/mitx_all/cs_comments_service/spec/models/at_user_observer_spec.rb describe "#get_valid_at_position_list(text)" do
A /home/kevin/mitx_all/cs_comments_service/spec/models/at_user_observer_spec.rb class A A /home/kevin/mitx_all/cs_comments_service/spec/models/at_user_observer_spec.rb class A
...@@ -148,6 +148,7 @@ new_dumb_thread /home/kevin/mitx_all/cs_comments_service/models/comment_thread.r ...@@ -148,6 +148,7 @@ new_dumb_thread /home/kevin/mitx_all/cs_comments_service/models/comment_thread.r
parse /home/kevin/mitx_all/cs_comments_service/spec/spec_helper.rb def parse(text) parse /home/kevin/mitx_all/cs_comments_service/spec/spec_helper.rb def parse(text)
perform_search /home/kevin/mitx_all/cs_comments_service/models/comment_thread.rb def self.perform_search(params, options={}) perform_search /home/kevin/mitx_all/cs_comments_service/models/comment_thread.rb def self.perform_search(params, options={})
process_at_notifications /home/kevin/mitx_all/cs_comments_service/models/observers/at_user_observer.rb def self.process_at_notifications(content) process_at_notifications /home/kevin/mitx_all/cs_comments_service/models/observers/at_user_observer.rb def self.process_at_notifications(content)
prolific_metric /home/kevin/mitx_all/cs_comments_service/models/content.rb def self.prolific_metric
respond ok when unfollowing user twice /home/kevin/mitx_all/cs_comments_service/spec/api/subscription_and_notification_spec.rb it "respond ok when unfollowing user twice" do respond ok when unfollowing user twice /home/kevin/mitx_all/cs_comments_service/spec/api/subscription_and_notification_spec.rb it "respond ok when unfollowing user twice" do
retrieve information of a single comment /home/kevin/mitx_all/cs_comments_service/spec/api/comment_spec.rb it "retrieve information of a single comment" do retrieve information of a single comment /home/kevin/mitx_all/cs_comments_service/spec/api/comment_spec.rb it "retrieve information of a single comment" do
retrieve information of a single comment with its sub comments /home/kevin/mitx_all/cs_comments_service/spec/api/comment_spec.rb it "retrieve information of a single comment with its sub comments" do retrieve information of a single comment with its sub comments /home/kevin/mitx_all/cs_comments_service/spec/api/comment_spec.rb it "retrieve information of a single comment with its sub comments" do
......
...@@ -8,43 +8,58 @@ ROOT = roots[ENV['SINATRA_ENV']] ...@@ -8,43 +8,58 @@ ROOT = roots[ENV['SINATRA_ENV']]
namespace :kpis do namespace :kpis do
#USAGE
#SINATRA_ENV=development rake kpis:prolific
task :prolific => :environment do task :prolific => :environment do
#USAGE
count = 10 #SINATRA_ENV=development rake kpis:prolific
#or
contributors = {} #SINATRA_ENV=development bundle exed rake kpis:prolific
map = "function(){emit(this.author_id,1)}"
reduce = "function(k, vals) { var sum = 0; for(var i in vals) sum += vals[i]; return sum; }"
courses = Content.all.distinct("course_id") courses = Content.all.distinct("course_id")
puts "\n\n*********************************************************************"
puts " Users who have created the most forum content on edX (#{Date.today}) "
puts "*********************************************************************\n\n"
courses.each do |c| courses.each do |c|
contributors[c] = [] contributors = Content.prolific_metric({"course_id" => c})
Content.where(course_id: c).map_reduce(map,reduce).out(replace: "results").each do |d| #now output
contributors[c] << d puts c
end puts "*********************"
contributors.each do |p|
url = ROOT + "/courses/#{c}/discussion/forum/users/#{p['_id']}"
count_string = "#{p['value'].to_i} contributions:".rjust(25)
puts "#{count_string} #{url} "
end
puts "\n"
end end
end
#now sort and limit them
task :starters => :environment do
#USAGE
#SINATRA_ENV=development rake kpis:starters
#or
#SINATRA_ENV=development bundle exed rake kpis:startersgimp
courses = Content.all.distinct("course_id")
puts "\n\n*********************************************************************"
puts " Users who have started the most threads on edX (#{Date.today}) "
puts "*********************************************************************\n\n"
courses.each do |c| courses.each do |c|
#first sort destructively contributors = Content.prolific_metric({"course_id" => c, "_type" => "CommentThread"})
contributors[c].sort! {|a,b| -a["value"] <=> -b["value"]}
#then trim it
contributors[c] = contributors[c][0..(count - 1)]
#now output #now output
puts "\n\n\n"
puts c puts c
contributors[c].each do |p| puts "*********************"
contributors.each do |p|
url = ROOT + "/courses/#{c}/discussion/forum/users/#{p['_id']}" url = ROOT + "/courses/#{c}/discussion/forum/users/#{p['_id']}"
count_string = "#{p['value'].to_i} contributions:".rjust(25) count_string = "#{p['value'].to_i} contributions:".rjust(25)
puts "#{count_string} #{url} " puts "#{count_string} #{url} "
end end
puts "\n"
end
end
end end
end end
class Content class Content
include Mongoid::Document include Mongoid::Document
def author_with_anonymity(attr=nil, attr_when_anonymous=nil) def author_with_anonymity(attr=nil, attr_when_anonymous=nil)
if not attr if not attr
(anonymous || anonymous_to_peers) ? nil : author (anonymous || anonymous_to_peers) ? nil : author
...@@ -9,5 +9,31 @@ class Content ...@@ -9,5 +9,31 @@ class Content
(anonymous || anonymous_to_peers) ? attr_when_anonymous : author.send(attr) (anonymous || anonymous_to_peers) ? attr_when_anonymous : author.send(attr)
end end
end end
def self.prolific_metric what
#take a hash of criteria (where) and return a hash of hashes
#course => user => count
count = 10
contributors = {}
map = "function(){emit(this.author_id,1)}"
reduce = "function(k, vals) { var sum = 0; for(var i in vals) sum += vals[i]; return sum; }"
contributors = []
self.where(what).map_reduce(map,reduce).out(replace: "results").each do |d|
contributors << d
end
#now sort and limit them
#first sort destructively
contributors.sort! {|a,b| -a["value"] <=> -b["value"]}
#then trim it
contributors = contributors[0..(count - 1)]
contributors
end
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