修改了contribution_perc函数,添加了对区块链确权项目的支持

This commit is contained in:
zhangxunhui 2023-02-02 14:00:13 +08:00
parent c5314f7439
commit ada6afa500
1 changed files with 101 additions and 90 deletions

View File

@ -86,107 +86,118 @@ class UsersController < ApplicationController
@project = Project.find(project_id) @project = Project.find(project_id)
user_id = params[:id] user_id = params[:id]
@user = User.find(user_id) @user = User.find(user_id)
# 获取所有行为对应的项目内记录总数和个人记录数
features = {
"requirement" => {},
"development" => {},
"review" => {}
}
def cal_perc(count_user, count_all) def cal_perc(count_user, count_all)
(count_user * 1.0 / (count_all + 0.000000001)).round(5) (count_user * 1.0 / (count_all + 0.000000001)).round(5)
end end
# 1. issue创建 if @project.use_blockchain == true or @project.use_blockchain == 1
issues = Issue.where(project_id: project_id, issue_classify: 'issue') balance_user = Blockchain::BalanceQueryOneProject.call({"user_id": user_id, "project_id": project_id})[:balance]
issue_all = issues.count balance_all = Blockchain::RepoBasicInfo.call({"project_id": project_id})["cur_supply"]
issue_user = issues.where(author_id: user_id).count scores = {
features["requirement"] = features["requirement"].merge({"issue" => {"all" => issue_all, "perc" => cal_perc(issue_user, issue_all)}}) "final" => cal_perc(balance_user, balance_all),
# 2. 里程碑创建 "blockchain" => true
milestones = Version.where(project_id: project_id) }
milestone_all = milestones.count else
milestone_user = milestones.where(user_id: user_id).count # 获取所有行为对应的项目内记录总数和个人记录数
features["requirement"] = features["requirement"].merge({"milestone" => {"all" => milestone_all, "perc" => milestone_user * 1.0 / milestone_all}}) features = {
# 3. issue评论 "requirement" => {},
issue_comments = Journal.joins("INNER JOIN issues on journals.journalized_id=issues.id").where("issues.project_id=#{project_id} and journalized_type='Issue' and issues.issue_classify='issue'") "development" => {},
issue_comment_all = issue_comments.count "review" => {}
issue_comment_user = issue_comments.where("journals.user_id=#{user_id}").count }
features["requirement"] = features["requirement"].merge({"issue_comment" => {"all" => issue_comment_all, "perc" => cal_perc(issue_comment_user, issue_comment_all)}})
# 4. 合并请求
prs = PullRequest.where(project_id: project_id)
pr_all = prs.count
pr_user = prs.where(user_id: user_id).count
features["development"] = features["development"].merge({"pr" => {"all" => pr_all, "perc" => pr_user * 1.0 / pr_all}})
# 5. pr评论
pr_comments = Journal.joins("INNER JOIN issues on journals.journalized_id=issues.id").where("issues.project_id=#{project_id} and journalized_type='Issue' and issues.issue_classify='pull_request'")
pr_comment_all = pr_comments.count
pr_comment_user = pr_comments.where("journals.user_id=#{user_id}").count
features["review"] = features["review"].merge({"pr_comment" => {"all" => pr_comment_all, "perc" => cal_perc(pr_comment_user, pr_comment_all)}})
# 6. 代码行评论
line_comments = Journal.joins("INNER JOIN pull_requests on journals.journalized_id=pull_requests.id").where("pull_requests.project_id=#{project_id} and journalized_type='PullRequest'")
line_comment_all = line_comments.count
line_comment_user = line_comments.where("journals.user_id=#{user_id}").count
features["review"] = features["review"].merge({"line_comment" => {"all" => line_comment_all, "perc" => cal_perc(line_comment_user, line_comment_all)}})
# 7. 代码行、commit贡献统计
code_contributions = Api::V1::Projects::CodeStats::ListService.call(@project, {ref: nil})
commit_all = code_contributions["commit_count"]
addition_all = code_contributions["additions"]
deletion_all = code_contributions["deletions"]
commit_user = 0 # 1. issue创建
addition_user = 0 issues = Issue.where(project_id: project_id, issue_classify: 'issue')
deletion_user = 0 issue_all = issues.count
code_contributions["authors"].each do |author| issue_user = issues.where(author_id: user_id).count
if author["name"] == @user.login features["requirement"] = features["requirement"].merge({"issue" => {"all" => issue_all, "perc" => cal_perc(issue_user, issue_all)}})
commit_user = author["commits"] # 2. 里程碑创建
addition_user = author["additions"] milestones = Version.where(project_id: project_id)
deletion_user = author["deletions"] milestone_all = milestones.count
end milestone_user = milestones.where(user_id: user_id).count
end features["requirement"] = features["requirement"].merge({"milestone" => {"all" => milestone_all, "perc" => cal_perc(milestone_user, milestone_all)}})
# 3. issue评论
issue_comments = Journal.joins("INNER JOIN issues on journals.journalized_id=issues.id").where("issues.project_id=#{project_id} and journalized_type='Issue' and issues.issue_classify='issue'")
issue_comment_all = issue_comments.count
issue_comment_user = issue_comments.where("journals.user_id=#{user_id}").count
features["requirement"] = features["requirement"].merge({"issue_comment" => {"all" => issue_comment_all, "perc" => cal_perc(issue_comment_user, issue_comment_all)}})
# 4. 合并请求
prs = PullRequest.where(project_id: project_id)
pr_all = prs.count
pr_user = prs.where(user_id: user_id).count
features["development"] = features["development"].merge({"pr" => {"all" => pr_all, "perc" => cal_perc(pr_user, pr_all)}})
# 5. pr评论
pr_comments = Journal.joins("INNER JOIN issues on journals.journalized_id=issues.id").where("issues.project_id=#{project_id} and journalized_type='Issue' and issues.issue_classify='pull_request'")
pr_comment_all = pr_comments.count
pr_comment_user = pr_comments.where("journals.user_id=#{user_id}").count
features["review"] = features["review"].merge({"pr_comment" => {"all" => pr_comment_all, "perc" => cal_perc(pr_comment_user, pr_comment_all)}})
# 6. 代码行评论
line_comments = Journal.joins("INNER JOIN pull_requests on journals.journalized_id=pull_requests.id").where("pull_requests.project_id=#{project_id} and journalized_type='PullRequest'")
line_comment_all = line_comments.count
line_comment_user = line_comments.where("journals.user_id=#{user_id}").count
features["review"] = features["review"].merge({"line_comment" => {"all" => line_comment_all, "perc" => cal_perc(line_comment_user, line_comment_all)}})
# 7. 代码行、commit贡献统计
code_contributions = Api::V1::Projects::CodeStats::ListService.call(@project, {ref: nil})
commit_all = code_contributions["commit_count"]
addition_all = code_contributions["additions"]
deletion_all = code_contributions["deletions"]
features["development"] = features["development"].merge({"commit" => {"all" => commit_all, "perc" => cal_perc(commit_user, commit_all)}}) commit_user = 0
features["development"] = features["development"].merge({"addition" => {"all" => addition_all, "perc" => cal_perc(addition_user, addition_all)}}) addition_user = 0
features["development"] = features["development"].merge({"deletion" => {"all" => deletion_all, "perc" => cal_perc(deletion_user, deletion_all)}}) deletion_user = 0
code_contributions["authors"].each do |author|
def cal_weight(features) if author["name"] == @user.login
weights = {} # 计算每一项的权重 commit_user = author["commits"]
categories = [] addition_user = author["additions"]
features.each do |key, _| deletion_user = author["deletions"]
categories << key
weights[key] = Hash.new
end
count_all = 0
counts = {}
categories.each do |category|
count_1 = 0
features[category].each do |_, value|
count_1 += value["all"]
end
count_all += count_1
counts[category] = count_1
features[category].each do |key, value|
weight = value["all"] * 1.0 / count_1
weights[category] = weights[category].merge({key => weight})
end end
end end
categories.each do |category|
weight = counts[category] * 1.0 / count_all
weights[category] = weights[category].merge({"category_weight" => weight})
end
return weights
end
weights_categories = cal_weight(features) features["development"] = features["development"].merge({"commit" => {"all" => commit_all, "perc" => cal_perc(commit_user, commit_all)}})
scores = { features["development"] = features["development"].merge({"addition" => {"all" => addition_all, "perc" => cal_perc(addition_user, addition_all)}})
"final" => 0.0 features["development"] = features["development"].merge({"deletion" => {"all" => deletion_all, "perc" => cal_perc(deletion_user, deletion_all)}})
}
features.each do |category, value_1| def cal_weight(features)
category_score = 0.0 weights = {} # 计算每一项的权重
value_1.each do |action, value_2| categories = []
category_score += weights_categories[category][action] * value_2["perc"] features.each do |key, _|
categories << key
weights[key] = Hash.new
end
count_all = 0
counts = {}
categories.each do |category|
count_1 = 0
features[category].each do |_, value|
count_1 += value["all"]
end
count_all += count_1
counts[category] = count_1
features[category].each do |key, value|
weight = cal_perc(value["all"], count_1)
weights[category] = weights[category].merge({key => weight})
end
end
categories.each do |category|
weight = cal_perc(counts[category], count_all)
weights[category] = weights[category].merge({"category_weight" => weight})
end
return weights
end
weights_categories = cal_weight(features)
scores = {
"final" => 0.0,
"blockchain" => false
}
features.each do |category, value_1|
category_score = 0.0
value_1.each do |action, value_2|
category_score += weights_categories[category][action] * value_2["perc"]
end
scores["final"] += weights_categories[category]["category_weight"] * category_score.round(4)
scores = scores.merge({category => category_score.round(4)})
end end
scores["final"] += weights_categories[category]["category_weight"] * category_score.round(4)
scores = scores.merge({category => category_score.round(4)})
end end
render json: { scores: scores } render json: { scores: scores }
end end