Merge pull request '确权相关' (#37) from yystopf/forgeplus:blockchain_sync_data into trustie_server

This commit is contained in:
yystopf 2023-03-23 16:10:05 +08:00
commit e93656a1e8
5 changed files with 1283 additions and 1 deletions

View File

@ -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

View File

@ -438,4 +438,35 @@ 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
user = User.find_by(login: username, mail: email)
user = User.find_by(login: username) if user.nil?
user = User.find_by(mail: email) if user.nil?
# next if user.nil?
search_contributor = contributors.select{|con| con["id"]==user.id}[0]
if search_contributor.present?
search_contributor["contributions"] += commits
else
contributors << {contributions: commits, name: username, login: username, email: email, id: user&.id}.stringify_keys
end
end
file.close
$redis_cache.set("ProjectMindsporeContributors", contributors.to_json)
return contributors
else
return JSON.parse(cache_result)
end
end
end end

View File

@ -14,5 +14,7 @@ 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?
json.contribution_perc db_user.contribution_perc(project)
end
end end

View File

@ -0,0 +1,79 @@
# 执行示例 bundle exec rake "sync_mindspore:contributor_to_user"
# RAILS_ENV=production bundle exec rake "sync_mindspore:contributor_to_user"
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
# 执行示例 bundle exec rake "sync_mindspore:init_project_blockchain[1,2]"
# RAILS_ENV=production bundle exec rake "sync_mindspore:init_project_blockchain[1,2]"
desc "初始化区块链项目"
task :init_project_blockchain, [:id, :init_id] => :environment do |t, args|
puts "=====Init Project Blockchain: #{args.id}====="
project = Project.find_by_id(args.id)
project.update_column(:use_blockchain, true)
username = project.user_id.to_s
token_name = project.id.to_s
total_supply = 10000
token_balance = [[init_id.to_s, 100]]
contributions = Project.mindspore_contributors
total_contributions = contributions.sum{|i| i["contributions"]}
contributions.each do |con|
cont_balance = Float(con["contributions"]*9900/total_contributions).round(0)
token_balance << [con["id"], cont_balance] if cont_balance > 0
end
params = {
"request-type": "create repo",
"username": username,
"token_name": token_name,
"total_supply": total_supply,
"token_balance": token_balance
}.to_json
resp_body = Blockchain::InvokeBlockchainApi.call(params)
end
end

1151
public/mindspore_authors Normal file

File diff suppressed because it is too large Load Diff