优化积分计算速度问题

This commit is contained in:
z9hang 2014-08-19 17:03:26 +08:00
parent 9039755fc5
commit 1e234a9adf
3 changed files with 183 additions and 29 deletions

View File

@ -683,8 +683,8 @@ class ProjectsController < ApplicationController
else # @project.project_type == Project::ProjectType_project
roles = Role.find_all_givable
@subPage_title = l :label_member_list
@members = @project.member_principals.includes(:roles, :principal).all
@members = sort_project_members(@project, @members)
@members = @project.member_principals.includes(:roles, :principal).joins("LEFT JOIN #{OptionNumber.table_name} ON #{OptionNumber.table_name}.user_id = #{Member.table_name}.user_id and #{OptionNumber.table_name}.score_type = 2").order("#{OptionNumber.table_name}.total_score DESC").all
#@members = sort_project_members(@project, @members)
@applied_members = appied_project_members(@project, @members)
end
@members = paginateHelper @members

View File

@ -418,11 +418,26 @@ module UserScoreHelper
#发帖数
def memo_num(user,project=nil)
if project.nil?
Message.includes(:author).joins(:board).where("#{Message.table_name}.parent_id IS NULL and #{Message.table_name}.author_id = '#{user.id}' and #{Board.table_name}.project_id != -1").all.count #+ Memo.includes(:author).where("parent_id IS NULL and author_id = '#{user.id}'").all.count
#Message.includes(:author).joins(:board).where("#{Message.table_name}.parent_id IS NULL and #{Message.table_name}.author_id = '#{user.id}' and #{Board.table_name}.project_id != -1").all.count #+ Memo.includes(:author).where("parent_id IS NULL and author_id = '#{user.id}'").all.count
users = User.find_by_sql("SELECT `users`.id,(SELECT COUNT(*) FROM `messages` JOIN `boards` ON boards.project_id != -1 AND messages.board_id = boards.id
WHERE messages.parent_id IS NULL AND messages.author_id = `users`.id) AS m_count
FROM `users` where id = #{user.id}")
result = 0
users.each do |user|
result = user.m_count
end
result
#user.messages.where("parent_id IS NULL").count
else
Message.includes(:author).joins(:board).where("#{Message.table_name}.parent_id IS NULL and #{Message.table_name}.author_id = '#{user.id}' and #{Board.table_name}.project_id = #{project.id}").all.count
#Message.includes(:author).joins(:board).where("#{Message.table_name}.parent_id IS NULL and #{Message.table_name}.author_id = '#{user.id}' and #{Board.table_name}.project_id = #{project.id}").all.count
users = User.find_by_sql("SELECT `users`.id,(SELECT COUNT(*) FROM `messages` JOIN `boards` ON boards.project_id = '#{project.id}' AND messages.board_id = boards.id
WHERE messages.parent_id IS NULL AND messages.author_id = `users`.id) AS m_count
FROM `users` where id = #{user.id}")
result = 0
users.each do |user|
result = user.m_count
end
result
end
end
@ -448,9 +463,21 @@ FROM `users` where id = #{user.id}")
def messges_for_issue_num(user,project=nil)
if project.nil?
Journal.includes(:user).where("user_id = '#{user.id}' and notes is not null and notes != ''").all.count
#Journal.includes(:user).where("user_id = '#{user.id}' and notes is not null and notes != ''").all.count
users = User.find_by_sql("SELECT users.id,(SELECT COUNT(*) FROM journals WHERE journals.user_id = users.id AND journals.notes IS NOT NULL AND journals.notes != '') AS m_count FROM users where id = #{user.id}")
result = 0
if users.count > 0
result = users.first.m_count
end
result
else
Journal.includes(:user).joins(:issue).where("#{Journal.table_name}.user_id = '#{user.id}' and #{Issue.table_name}.project_id = '#{project.id}' and #{Journal.table_name}.notes != '' and #{Journal.table_name}.notes is not null").all.count
#Journal.includes(:user).joins(:issue).where("#{Journal.table_name}.user_id = '#{user.id}' and #{Issue.table_name}.project_id = '#{project.id}' and #{Journal.table_name}.notes != '' and #{Journal.table_name}.notes is not null").all.count
users = User.find_by_sql("SELECT users.id,(SELECT COUNT(*) FROM journals join issues on #{Journal.table_name}.journalized_type = 'Issue' and #{Journal.table_name}.journalized_id = #{Issue.table_name}.id WHERE journals.user_id = users.id AND journals.notes IS NOT NULL AND journals.notes != ''and #{Issue.table_name}.project_id = '#{project.id}') AS m_count FROM users where id = #{user.id}")
result = 0
if users.count > 0
result = users.first.m_count
end
result
end
end
@ -471,9 +498,21 @@ FROM `users` where id = #{user.id}")
def issues_status_num(user,project=nil)
if project.nil?
Journal.joins(:details, :user).where("#{JournalDetail.table_name}.prop_key = 'status_id' and #{User.table_name}.id = '#{user.id}'").count
#Journal.joins(:details, :user).where("#{JournalDetail.table_name}.prop_key = 'status_id' and #{User.table_name}.id = '#{user.id}'").count
users = User.find_by_sql("SELECT users.id,(SELECT COUNT(*) FROM journals JOIN journal_details on journals.id = journal_details.journal_id WHERE journal_details.prop_key = 'status_id' and journals.user_id = users.id) AS m_count FROM users WHERE users.id = '#{user.id}'")
result = 0
if users.count > 0
result = users.first.m_count
end
result
else
Journal.joins(:issue,:details,:user).where("#{Issue.table_name}.project_id = '#{project.id}' and #{JournalDetail.table_name}.prop_key = 'status_id' and #{User.table_name}.id = '#{user.id}'").count
#Journal.joins(:issue,:details,:user).where("#{Issue.table_name}.project_id = '#{project.id}' and #{JournalDetail.table_name}.prop_key = 'status_id' and #{User.table_name}.id = '#{user.id}'").count
User.find_by_sql("SELECT users.id,(SELECT COUNT(*) FROM journals JOIN journal_details on journals.id = journal_details.journal_id JOIN issues ON issues.id = journals.journalized_id and journalized_type = 'Issue' WHERE journal_details.prop_key = 'status_id' and journals.user_id = users.id and issues.project_id = '#{project.id}') AS m_count FROM users WHERE users.id = '#{user.id}'")
result = 0
if users.count > 0
result = users.first.m_count
end
result
end
end
@ -481,10 +520,10 @@ FROM `users` where id = #{user.id}")
def issues_status_score(user,project=nil)
if project.nil?
#Journal.joins(:details, :user).where("#{JournalDetail.table_name}.prop_key = 'status_id' and #{User.table_name}.id = '#{user.id}'").count
User.find_by_sql("SELECT users.id,(SELECT COUNT(*) FROM journals JOIN journal_details on journals.id = journal_details.journal_id WHERE journal_details.prop_key = 'status_id' and journals.user_id = users.id) AS m_score FROM users WHERE users.id = '#{user.id}'")
User.find_by_sql("SELECT users.id,(SELECT COUNT(*) FROM journals JOIN journal_details on journals.id = journal_details.journal_id WHERE journal_details.prop_key = 'status_id' and journals.user_id = users.id) AS m_count FROM users WHERE users.id = '#{user.id}'")
else
#Journal.joins(:issue,:details,:user).where("#{Issue.table_name}.project_id = '#{project.id}' and #{JournalDetail.table_name}.prop_key = 'status_id' and #{User.table_name}.id = '#{user.id}'").count
User.find_by_sql("SELECT users.id,(SELECT COUNT(*) FROM journals JOIN journal_details on journals.id = journal_details.journal_id JOIN issues ON issues.id = journals.journalized_id and journalized_type = 'Issue' WHERE journal_details.prop_key = 'status_id' and journals.user_id = users.id and issues.project_id = '#{project.id}') AS m_score FROM users WHERE users.id = '#{user.id}'")
User.find_by_sql("SELECT users.id,(SELECT COUNT(*) FROM journals JOIN journal_details on journals.id = journal_details.journal_id JOIN issues ON issues.id = journals.journalized_id and journalized_type = 'Issue' WHERE journal_details.prop_key = 'status_id' and journals.user_id = users.id and issues.project_id = '#{project.id}') AS m_count FROM users WHERE users.id = '#{user.id}'")
end
end
@ -497,9 +536,21 @@ FROM `users` where id = #{user.id}")
def replay_for_message_num(user,project=nil)
if project.nil?
JournalsForMessage.includes(:user).where("m_parent_id IS NOT NULL and user_id = #{user.id} and jour_type = 'Project'").count
#JournalsForMessage.includes(:user).where("m_parent_id IS NOT NULL and user_id = #{user.id} and jour_type = 'Project'").count
users = User.find_by_sql("SELECT users.id,(SELECT COUNT(*) From journals_for_messages WHERE m_parent_id IS NOT NULL and user_id = users.id and jour_type = 'Project') as m_count FROM users WHERE users.id = '#{user.id}'")
result = 0
if users.count > 0
result = users.first.m_count
end
result
else
JournalsForMessage.includes(:user).where("m_parent_id IS NOT NULL and user_id = #{user.id} and jour_type = 'Project' and jour_id = '#{project.id}'").count
#JournalsForMessage.includes(:user).where("m_parent_id IS NOT NULL and user_id = #{user.id} and jour_type = 'Project' and jour_id = '#{project.id}'").count
users = User.find_by_sql("SELECT users.id,(SELECT COUNT(*) From journals_for_messages WHERE m_parent_id IS NOT NULL and user_id = users.id and jour_type = 'Project' and jour_id = '#{project.id}') as m_count FROM users WHERE users.id = '#{user.id}'")
result = 0
if users.count > 0
result = users.first.m_count
end
result
end
end
@ -508,7 +559,7 @@ FROM `users` where id = #{user.id}")
if project.nil?
User.find_by_sql("SELECT users.id,(SELECT COUNT(*) From journals_for_messages WHERE m_parent_id IS NOT NULL and user_id = users.id and jour_type = 'Project') as m_score FROM users WHERE users.id = '#{user.id}'")
else
User.find_by_sql("SELECT users.id,(SELECT COUNT(*) From journals_for_messages WHERE m_parent_id IS NOT NULL and user_id = users.id and jour_type = 'Project' and jour_id = '#{project.id}') as m_score FROM users WHERE users.id = '#{user.id}'")
User.find_by_sql("SELECT users.id,(SELECT COUNT(*) From journals_for_messages WHERE m_parent_id IS NOT NULL and user_id = users.id and jour_type = 'Project' and jour_id = '#{project.id}') as m_score FROM users WHERE users.id = '#{user.id}'")
end
end
@ -521,9 +572,21 @@ FROM `users` where id = #{user.id}")
def replay_for_memo_num(user,project=nil)
if project.nil?
Message.includes(:author).joins(:board).where("#{Message.table_name}.parent_id IS NOT NULL and #{Message.table_name}.author_id = '#{user.id}' and #{Board.table_name}.project_id != -1").all.count
#Message.includes(:author).joins(:board).where("#{Message.table_name}.parent_id IS NOT NULL and #{Message.table_name}.author_id = '#{user.id}' and #{Board.table_name}.project_id != -1").all.count
users = User.find_by_sql("SELECT users.id,(SELECT COUNT(*) FROM messages JOIN boards on messages.board_id = boards.id WHERE messages.parent_id IS NOT NULL AND messages.author_id = users.id AND boards.project_id != -1) as m_count FROM users WHERE users.id = #{user.id}")
result = 0
if users.count > 0
result = users.first.m_count
end
result
else
Message.includes(:author).joins(:board).where("#{Message.table_name}.parent_id IS NOT NULL and #{Message.table_name}.author_id = '#{user.id}' and #{Board.table_name}.project_id = #{project.id}").all.count
#Message.includes(:author).joins(:board).where("#{Message.table_name}.parent_id IS NOT NULL and #{Message.table_name}.author_id = '#{user.id}' and #{Board.table_name}.project_id = #{project.id}").all.count
users = User.find_by_sql("SELECT users.id,(SELECT COUNT(*) FROM messages JOIN boards on messages.board_id = boards.id WHERE messages.parent_id IS NOT NULL AND messages.author_id = users.id AND boards.project_id = #{project.id}) FROM users WHERE users.id = #{user.id}")
result = 0
if users.count > 0
result = users.first.m_count
end
result
end
end
@ -538,16 +601,21 @@ FROM `users` where id = #{user.id}")
#更新被关注的人数
def update_follow(user,type)
option_number = get_option_number(user,type)
option_number.follow = Watcher.includes(:watchable).where("watchable_type = 'Principal' and watchable_id = '#{user.id}'").count
option_number.follow = follow_num(user)#Watcher.includes(:watchable).where("watchable_type = 'Principal' and watchable_id = '#{user.id}'").count
update_score(option_number)
end
def follow_num(user)
Watcher.includes(:watchable).where("watchable_type = 'Principal' and watchable_id = '#{user.id}'").count
#Watcher.includes(:watchable).where("watchable_type = 'Principal' and watchable_id = '#{user.id}'").count
users = User.find_by_sql("SELECT users.id, (SELECT COUNT(*) FROM #{Watcher.table_name} WHERE watchable_type = 'Principal' and watchable_id = users.id) as m_count FROM users WHERE users.id = '#{user.id}'")
result = 0
if users.count > 0
result = users.first.m_count
end
end
def follow_score(user)
User.find_by_sql("SELECT users.id, (SELECT COUNT(*) * 2 FROM #{Watcher.table_name} WHERE watchable_type = 'Principal' and watchable_id = '#{user.id}') FROM users WHERE users.id = '#{user.id}'")
User.find_by_sql("SELECT users.id, (SELECT COUNT(*) * 2 FROM #{Watcher.table_name} WHERE watchable_type = 'Principal' and watchable_id = users.id) FROM users WHERE users.id = '#{user.id}'")
end
#更新帖子踩各项数量
@ -689,9 +757,21 @@ FROM `users` where id = #{user.id}")
def changeset_num(user,project=nil)
if project.nil?
Changeset.includes(:user).where("user_id = '#{user.id}'").all.count
#Changeset.includes(:user).where("user_id = '#{user.id}'").all.count
users = User.find_by_sql("SELECT users.id, (SELECT COUNT(*) FROM #{Changeset.table_name} WHERE user_id = users.id) as m_count FROM users WHERE users.id = '#{user.id}'")
result = 0
if users.count > 0
result = users.first.m_count
end
result
else
Changeset.includes(:user).joins(:repository).where("#{Changeset.table_name}.user_id = '#{user.id}' and #{Repository.table_name}.project_id = #{project.id}").all.count
#Changeset.includes(:user).joins(:repository).where("#{Changeset.table_name}.user_id = '#{user.id}' and #{Repository.table_name}.project_id = #{project.id}").all.count
users = User.find_by_sql("SELECT users.id, (SELECT COUNT(*) FROM #{Changeset.table_name} JOIN #{Repository.table_name} ON #{Changeset.table_name}.repository_id = #{Repository.table_name}.id WHERE #{Changeset.table_name}.user_id = users.id and #{Repository.table_name}.project_id = #{project.id}) as m_count FROM users WHERE users.id = '#{user.id}'")
result = 0
if users.count > 0
result = users.first.m_count
end
result
end
end
@ -705,9 +785,21 @@ FROM `users` where id = #{user.id}")
def document_num(user,project=nil)
if project.nil?
Document.includes(:user).where("user_id = '#{user.id}'").all.count
#Document.includes(:user).where("user_id = '#{user.id}'").all.count
users = User.find_by_sql("SELECT #{User.table_name}.id,(SELECT COUNT(*) FROM #{Document.table_name} WHERE #{Document.table_name}.user_id = #{User.table_name}.id) as m_count FROM #{User.table_name} where #{User.table_name}.id = #{user.id}")
result = 0
if users.count > 0
result = users.first.m_count
end
result
else
Document.includes(:user,:project).where("user_id = '#{user.id}' and project_id = '#{project.id}'").all.count
#Document.includes(:user,:project).where("user_id = '#{user.id}' and project_id = '#{project.id}'").all.count
users = User.find_by_sql("SELECT #{User.table_name}.id,(SELECT COUNT(*) FROM #{Document.table_name} WHERE #{Document.table_name}.user_id = #{User.table_name}.id and #{Document.table_name}.project_id = '#{project.id}') as m_count FROM #{User.table_name} where #{User.table_name}.id = #{user.id}")
result = 0
if users.count > 0
result = users.first.m_count
end
result
end
end
@ -721,9 +813,21 @@ FROM `users` where id = #{user.id}")
def attachment_num(user,project=nil)
if project.nil?
Attachment.includes(:author,:container).where("author_id = '#{user.id}' and container_type = 'Project'").all.count
#Attachment.includes(:author,:container).where("author_id = '#{user.id}' and container_type = 'Project'").all.count
users = User.find_by_sql("SELECT #{User.table_name}.id,(SELECT COUNT(*) FROM #{Attachment.table_name} WHERE author_id = '#{user.id}' and container_type = 'Project') as m_count FROM #{User.table_name} WHERE #{User.table_name}.id = #{user.id}")
result = 0
if users.count > 0
result = users.first.m_count
end
result
else
Attachment.includes(:author,:container).where("author_id = '#{user.id}' and container_type = 'Project' and container_id = #{project.id}").all.count
#Attachment.includes(:author,:container).where("author_id = '#{user.id}' and container_type = 'Project' and container_id = #{project.id}").all.count
User.find_by_sql("SELECT #{User.table_name}.id,(SELECT COUNT(*) FROM #{Attachment.table_name} WHERE author_id = '#{user.id}' and container_type = 'Project' and container_id = #{project.id}) as m_count FROM #{User.table_name} WHERE #{User.table_name}.id = #{user.id}")
result = 0
if users.count > 0
result = users.first.m_coumt
end
result
end
end
@ -736,9 +840,21 @@ FROM `users` where id = #{user.id}")
def issue_done_ratio_num(user,project=nil)
if project.nil?
Journal.joins(:details, :user).where("#{JournalDetail.table_name}.prop_key = 'done_ratio' and #{User.table_name}.id = '#{user.id}'").count
#Journal.joins(:details, :user).where("#{JournalDetail.table_name}.prop_key = 'done_ratio' and #{User.table_name}.id = '#{user.id}'").count
users = User.find_by_sql("SELECT #{User.table_name}.id,(SELECT COUNT(*) FROM #{Journal.table_name} JOIN #{JournalDetail.table_name} ON #{JournalDetail.table_name}.journal_id = #{Journal.table_name}.id WHERE #{JournalDetail.table_name}.prop_key = 'done_ratio' and #{Journal.table_name}.user_id = #{User.table_name}.id) as m_count FROM #{User.table_name} WHERE #{User.table_name}.id = #{user.id}")
result = 0
if users.count > 0
result = users.first.m_count
end
result
else
Journal.joins(:issue,:details,:user).where("#{Issue.table_name}.project_id = '#{project.id}' and #{JournalDetail.table_name}.prop_key = 'done_ratio' and #{User.table_name}.id = '#{user.id}'").count
#Journal.joins(:issue,:details,:user).where("#{Issue.table_name}.project_id = '#{project.id}' and #{JournalDetail.table_name}.prop_key = 'done_ratio' and #{User.table_name}.id = '#{user.id}'").count
users = User.find_by_sql("SELECT #{User.table_name}.id,(SELECT COUNT(*) FROM #{Journal.table_name} JOIN #{JournalDetail.table_name} ON #{JournalDetail.table_name}.journal_id = #{Journal.table_name}.id JOIN issues ON issues.id = journals.journalized_id and journalized_type = 'Issue' WHERE #{JournalDetail.table_name}.prop_key = 'done_ratio' and #{Journal.table_name}.user_id = #{User.table_name}.id and #{Issue.table_name}.project_id = '#{project.id}') as m_count FROM #{User.table_name} WHERE #{User.table_name}.id = #{user.id}")
result = 0
if users.count > 0
result = users.first.m_count
end
result
end
end
@ -752,9 +868,21 @@ FROM `users` where id = #{user.id}")
def post_issue_num(user,project=nil)
if project.nil?
Issue.includes(:author).where("author_id = '#{user.id}'").all.count
#Issue.includes(:author).where("author_id = '#{user.id}'").all.count
users = User.find_by_sql("SELECT #{User.table_name}.id,(SELECT COUNT(*) FROM #{Issue.table_name} WHERE author_id = #{User.table_name}.id ) as m_count FROM #{User.table_name} WHERE #{User.table_name}.id = #{user.id}")
result = 0
if users.count > 0
result = users.first.m_count
end
result
else
Issue.includes(:author).where("author_id = '#{user.id}' and project_id = '#{project.id}'").all.count
#Issue.includes(:author).where("author_id = '#{user.id}' and project_id = '#{project.id}'").all.count
users = User.find_by_sql("SELECT #{User.table_name}.id,(SELECT COUNT(*) FROM #{Issue.table_name} WHERE author_id = #{User.table_name}.id and project_id = '#{project.id}' ) as m_count FROM #{User.table_name} WHERE #{User.table_name}.id = #{user.id}")
result = 0
if users.count > 0
result = users.first.m_count
end
result
end
end
@ -778,6 +906,9 @@ FROM `users` where id = #{user.id}")
update_attachment(user,type,project)
update_issue_done_ratio(user,type,project)
update_post_issue(user,type,project)
if project.nil?
update_follow(user,type)
end
update_score(ooption_num)
ooption_num
end

View File

@ -0,0 +1,23 @@
desc "User New Score description"
task :user_new_score do
puts "user_score sync."
end
namespace :user_new_score do
desc "calculating user score"
task :calculate => :environment do
include UserScoreHelper
User.all.each do |user|
result = user_scores(user,1)
puts "score of #{user.login} is #{result}"
end
Project.where("project_type != 1").all.each do |project|
project.member_principals.includes(:roles, :principal).all.each do |member|
result = user_scores(member.user,2,project)
puts "#{user.login}/'s score in #{project.name} is #{result}"
end
end
puts "calculate completed"
end
end