| @@ -31,6 +31,25 @@ module Watchable | |||||
| following.size | following.size | ||||
| end | end | ||||
| def mindspore_contribution_perc(project) | |||||
| @project = project | |||||
| @user = self | |||||
| def cal_perc(count_user, count_all) | |||||
| (count_user * 1.0 / (count_all + 0.000000001)).round(5) | |||||
| end | |||||
| if @project['use_blockchain'] == true or @project['use_blockchain'] == 1 | |||||
| balance_user = Blockchain::BalanceQueryOneProject.call({"user_id": @user.id, "project_id": @project.id}) | |||||
| balance_all = Blockchain::RepoBasicInfo.call({"project_id": @project.id})["cur_supply"] | |||||
| score = cal_perc(balance_user, balance_all) | |||||
| else | |||||
| commits_all = Project.mindspore_contributors.map{|i| i['contributions']}.sum | |||||
| commit_user = Project.mindspore_contributors.select{|i| i['login'] == @user.login}.map{|i| i['contributions']}.sum | |||||
| score = cal_perc(commit_user, commits_all) | |||||
| end | |||||
| end | |||||
| def contribution_perc(project) | def contribution_perc(project) | ||||
| @project = project | @project = project | ||||
| @user = self | @user = self | ||||
| @@ -438,4 +438,32 @@ class Project < ApplicationRecord | |||||
| def del_project_issue_cache_delete_count | def del_project_issue_cache_delete_count | ||||
| $redis_cache.hdel("issue_cache_delete_count", self.id) | $redis_cache.hdel("issue_cache_delete_count", self.id) | ||||
| end | end | ||||
| def self.mindspore_contributors | |||||
| cache_result = $redis_cache.get("ProjectMindsporeContributors") | |||||
| if cache_result.nil? | |||||
| contributors = [] | |||||
| file = File.open('public/mindspore_authors', 'r') | |||||
| file.each_line do |l| | |||||
| itemArray = l.gsub("\r\n", "").split("|:|") | |||||
| email = itemArray[0] | |||||
| username = itemArray[1] | |||||
| commits = itemArray[2].to_i | |||||
| if username =~ CustomRegexp::LOGIN && email =~ CustomRegexp::EMAIL | |||||
| user = User.find_by(login: username, mail: email) | |||||
| else | |||||
| user = User.find_by(mail: email) unless username =~ CustomRegexp::LOGIN | |||||
| user = User.find_by(login: username) unless email =~ CustomRegexp::EMAIL | |||||
| end | |||||
| contributors << {contributions: commits, name: username, login: username, email: email, id: user&.id}.stringify_keys | |||||
| end | |||||
| file.close | |||||
| $redis_cache.set("ProjectMindsporeContributors", contributors.to_json) | |||||
| return contributors | |||||
| else | |||||
| return JSON.parse(cache_result) | |||||
| end | |||||
| end | |||||
| end | end | ||||
| @@ -14,5 +14,11 @@ else | |||||
| json.name user["name"] | json.name user["name"] | ||||
| json.image_url user["avatar_url"] | json.image_url user["avatar_url"] | ||||
| db_user = User.find_by_id(user["id"]) | db_user = User.find_by_id(user["id"]) | ||||
| json.contribution_perc db_user.contribution_perc(project) if db_user.present? | |||||
| if db_user.present? | |||||
| if @project&.id == 1428586 | |||||
| json.contribution_perc db_user.mindspore_contribution_perc(project) | |||||
| else | |||||
| json.contribution_perc db_user.contribution_perc(project) | |||||
| end | |||||
| end | |||||
| end | end | ||||
| @@ -0,0 +1,54 @@ | |||||
| desc "mindspore项目贡献者数据" | |||||
| # 同步mindspre贡献者数据至用户表 | |||||
| namespace :sync_mindspore do | |||||
| desc "同步用户信息" | |||||
| task contributor_to_user: :environment do | |||||
| i = 1 | |||||
| fail_users = [] | |||||
| file = File.open('public/mindspore_authors', 'r') | |||||
| file.each_line do |l| | |||||
| itemArray = l.gsub("\r\n", "").split("|:|") | |||||
| email = itemArray[0] | |||||
| username = itemArray[1] | |||||
| password = '12345678' | |||||
| puts "=======Generate:[#{i}] Username: #{username}, Password: #{password}, Email: #{email}=======" | |||||
| puts "=======Create User Begin====== " | |||||
| user = User.new(admin: false, login: username, mail: email, type: "User", is_test: 1) | |||||
| user.password = password | |||||
| user.platform = 'forge' | |||||
| user.activate | |||||
| unless user.valid? | |||||
| puts "=======Create User Fail!====== " | |||||
| fail_users << [username, email] | |||||
| next | |||||
| end | |||||
| interactor = Gitea::RegisterInteractor.call({username: username, email: email, password: password}) | |||||
| if interactor.success? | |||||
| gitea_user = interactor.result | |||||
| result = Gitea::User::GenerateTokenService.call(username, password) | |||||
| user.gitea_token = result['sha1'] | |||||
| user.gitea_uid = gitea_user[:body]['id'] | |||||
| if user.save! | |||||
| UserExtension.create!(user_id: user.id) | |||||
| end | |||||
| end | |||||
| i += 1 | |||||
| puts "=======Create User End====== " | |||||
| end | |||||
| puts "=======Fail Users:#{fail_users}====== " | |||||
| file.close | |||||
| end | |||||
| desc "初始化区块链项目" | |||||
| task init_project_blockchain: :environment do | |||||
| end | |||||
| end | |||||