| @@ -86,107 +86,118 @@ class UsersController < ApplicationController | |||
| @project = Project.find(project_id) | |||
| user_id = params[:id] | |||
| @user = User.find(user_id) | |||
| # 获取所有行为对应的项目内记录总数和个人记录数 | |||
| features = { | |||
| "requirement" => {}, | |||
| "development" => {}, | |||
| "review" => {} | |||
| } | |||
| def cal_perc(count_user, count_all) | |||
| (count_user * 1.0 / (count_all + 0.000000001)).round(5) | |||
| end | |||
| # 1. issue创建 | |||
| issues = Issue.where(project_id: project_id, issue_classify: 'issue') | |||
| issue_all = issues.count | |||
| issue_user = issues.where(author_id: user_id).count | |||
| features["requirement"] = features["requirement"].merge({"issue" => {"all" => issue_all, "perc" => cal_perc(issue_user, issue_all)}}) | |||
| # 2. 里程碑创建 | |||
| milestones = Version.where(project_id: project_id) | |||
| milestone_all = milestones.count | |||
| 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}}) | |||
| # 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" => 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 | |||
| addition_user = 0 | |||
| deletion_user = 0 | |||
| code_contributions["authors"].each do |author| | |||
| if author["name"] == @user.login | |||
| commit_user = author["commits"] | |||
| addition_user = author["additions"] | |||
| deletion_user = author["deletions"] | |||
| if @project.use_blockchain == true or @project.use_blockchain == 1 | |||
| balance_user = Blockchain::BalanceQueryOneProject.call({"user_id": user_id, "project_id": project_id})[:balance] | |||
| balance_all = Blockchain::RepoBasicInfo.call({"project_id": project_id})["cur_supply"] | |||
| scores = { | |||
| "final" => cal_perc(balance_user, balance_all), | |||
| "blockchain" => true | |||
| } | |||
| else | |||
| # 获取所有行为对应的项目内记录总数和个人记录数 | |||
| features = { | |||
| "requirement" => {}, | |||
| "development" => {}, | |||
| "review" => {} | |||
| } | |||
| # 1. issue创建 | |||
| issues = Issue.where(project_id: project_id, issue_classify: 'issue') | |||
| issue_all = issues.count | |||
| issue_user = issues.where(author_id: user_id).count | |||
| features["requirement"] = features["requirement"].merge({"issue" => {"all" => issue_all, "perc" => cal_perc(issue_user, issue_all)}}) | |||
| # 2. 里程碑创建 | |||
| milestones = Version.where(project_id: project_id) | |||
| milestone_all = milestones.count | |||
| milestone_user = milestones.where(user_id: user_id).count | |||
| 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"] | |||
| commit_user = 0 | |||
| addition_user = 0 | |||
| deletion_user = 0 | |||
| code_contributions["authors"].each do |author| | |||
| if author["name"] == @user.login | |||
| commit_user = author["commits"] | |||
| addition_user = author["additions"] | |||
| deletion_user = author["deletions"] | |||
| end | |||
| end | |||
| end | |||
| features["development"] = features["development"].merge({"commit" => {"all" => commit_all, "perc" => cal_perc(commit_user, commit_all)}}) | |||
| features["development"] = features["development"].merge({"addition" => {"all" => addition_all, "perc" => cal_perc(addition_user, addition_all)}}) | |||
| features["development"] = features["development"].merge({"deletion" => {"all" => deletion_all, "perc" => cal_perc(deletion_user, deletion_all)}}) | |||
| features["development"] = features["development"].merge({"commit" => {"all" => commit_all, "perc" => cal_perc(commit_user, commit_all)}}) | |||
| features["development"] = features["development"].merge({"addition" => {"all" => addition_all, "perc" => cal_perc(addition_user, addition_all)}}) | |||
| features["development"] = features["development"].merge({"deletion" => {"all" => deletion_all, "perc" => cal_perc(deletion_user, deletion_all)}}) | |||
| def cal_weight(features) | |||
| weights = {} # 计算每一项的权重 | |||
| categories = [] | |||
| 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"] | |||
| def cal_weight(features) | |||
| weights = {} # 计算每一项的权重 | |||
| categories = [] | |||
| features.each do |key, _| | |||
| categories << key | |||
| weights[key] = Hash.new | |||
| 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}) | |||
| 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 | |||
| 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) | |||
| scores = { | |||
| "final" => 0.0 | |||
| } | |||
| 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"] | |||
| 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 | |||
| scores["final"] += weights_categories[category]["category_weight"] * category_score.round(4) | |||
| scores = scores.merge({category => category_score.round(4)}) | |||
| end | |||
| render json: { scores: scores } | |||
| end | |||