userscore 事务

This commit is contained in:
yanxd 2014-06-16 10:54:52 +08:00
parent 47e6c3eedd
commit d2b5560d5e
1 changed files with 47 additions and 33 deletions

View File

@ -4,9 +4,10 @@ task :user_score do
end
namespace :user_score do
desc "calculating user score"
# Rails.env = 'production'
task :calculate => :environment do
desc "calculating user score"
task :calculate => :environment do
puts "truncating table...#{Rails.env}"
UserScore.destroy_all
puts "loading..."
# collaboration 协同得分
users = {}
@ -40,10 +41,13 @@ namespace :user_score do
end
puts ":reply_posting calculate Completed. collaboration users count: #{users.count}"
users.each do |user_id, score|
UserScore.find_or_create_by_user_id(user_id).update_attribute(:collaboration, score)
UserScore.transaction do
users.each do |user_id, score|
UserScore.find_or_create_by_user_id(user_id).update_attribute(:collaboration, score)
end
end
puts "=== UserScore#collaboration calculate Completed. collaboration users count: #{users.count}"
puts ""
users.clear
@ -55,23 +59,26 @@ namespace :user_score do
end
puts ":followed_by calculate Completed. influence users count: #{users_influence.count}"
users_influence.each do |user_id, score|
UserScore.find_or_create_by_user_id(user_id).update_attribute(:influence, score)
UserScore.transaction do
users_influence.each do |user_id, score|
UserScore.find_or_create_by_user_id(user_id).update_attribute(:influence, score)
end
end
puts "=== UserScore#influence calculate Completed. influence users count: #{users_influence.count}"
puts ""
# skill 技术得分
# 顶踩帖
users_skill = {}
PraiseTread.where("praise_tread_object_type = 'Memo' || praise_tread_object_type = 'Message'").all.each do |pt|
obj = PraiseTread.find_object_by_type_and_id(pt.praise_tread_object_type, pt.praise_tread_object_id)
if obj.nil?
next
next
end
target_user = obj.author
level = pt.user.get_level
if pt.praise_or_tread == 0
#踩帖
users_skill[pt.user.id] = users_skill[pt.user.id].to_i - 2 #踩别人帖-2分
#踩帖
users_skill[pt.user.id] = users_skill[pt.user.id].to_i - 2 #踩别人帖-2分
if level == 1
users_skill[target_user.id] = users_skill[target_user.id].to_i - 2 #帖子被一级会员踩-2分 add praise_tread
elsif level == 2
@ -80,7 +87,7 @@ namespace :user_score do
users_skill[target_user.id] = users_skill[target_user.id].to_i - 6 #帖子被三级会员踩-6分 add praise_tread
end
elsif pt.praise_or_tread == 1
#顶贴
#顶贴
if level == 1
users_skill[target_user.id] = users_skill[target_user.id].to_i + 4 #帖子被一级会员顶+4分 add praise_tread
elsif level == 2
@ -91,10 +98,13 @@ namespace :user_score do
end
end
puts ":praised_by_user and :treaded_by_user calculate Completed. skill users count: #{users_skill.count}"
UserScore.transaction do
users_skill.each do |user_id, score|
UserScore.find_or_create_by_user_id(user_id).update_attribute(:skill, score)
end
puts "=== UserScore#skill calculate Completed. skill users count: #{users_skill.count}"
end
puts "=== UserScore#skill calculate Completed. skill users count: #{users_skill.count}"
puts ""
# active 项目贡献得分
users_active = {}
@ -115,20 +125,20 @@ namespace :user_score do
end
puts ":push_document calculate Completed. active users count: #{users_active.count}"
#提交附件
Attachment.includes(:author).all.each do |attachment|
#if attachment.container_id_changed?
# type = attachment.container_type
# types = %w|Document News Version Project Issue Message WikiPage|
# if types.include?(type)
if attachment.author.nil?
next
end
users_active[attachment.author.id] = users_active[attachment.author.id].to_i + 4
# end
#end
end
puts ":push_file calculate Completed. active users count: #{users_active.count}"
#更新完成度
Attachment.includes(:author).all.each do |attachment|
#if attachment.container_id_changed?
# type = attachment.container_type
# types = %w|Document News Version Project Issue Message WikiPage|
# if types.include?(type)
if attachment.author.nil?
next
end
users_active[attachment.author.id] = users_active[attachment.author.id].to_i + 4
# end
#end
end
puts ":push_file calculate Completed. active users count: #{users_active.count}"
#更新完成度
Journal.joins(:details, :user).where("#{JournalDetail.table_name}.prop_key = 'done_ratio'").each do |j|
users_active[j.user.id] = users_active[j.user.id].to_i + 2
end
@ -139,10 +149,14 @@ namespace :user_score do
end
puts ":post_issue calculate Completed. active users count: #{users_active.count}"
users_active.each do |user_id, score|
UserScore.find_or_create_by_user_id(user_id).update_attribute(:active, score)
UserScore.transaction do
users_active.each do |user_id, score|
UserScore.find_or_create_by_user_id(user_id).update_attribute(:active, score)
end
end
puts "=== UserScore#active calculate Completed. active users count: #{users_active.count}"
puts ""
end
end
@ -151,11 +165,11 @@ desc "create tmp file, to test"
file 'tmp/test.yml' do
require 'yaml'
var = {
:name => "name",
:age => "age",
:agent => "agent"
:name => "name",
:age => "age",
:agent => "agent"
}
File.open('tmp/test.yml', 'w') do |f|
f.write YAML.dump({'conf' => var })
f.write YAML.dump({'conf' => var})
end
end