forked from Trustie/forgeplus
组织语言缓存处理
This commit is contained in:
parent
dc1d6ce219
commit
73d441397c
|
|
@ -46,7 +46,13 @@ class Organizations::OrganizationsController < Organizations::BaseController
|
|||
@organization.nickname = organization_params[:nickname] if organization_params[:nickname].present?
|
||||
@organization.save!
|
||||
sync_organization_extension!
|
||||
|
||||
# 更改组织可见性为私有,则需将该组织下的所有仓库同步更改为私有仓库
|
||||
if organization_extension_params[:visibility] == "privacy"
|
||||
Project.where(user_id: @organization.id).where(is_public: true).each do |project|
|
||||
update_project_private(project)
|
||||
end
|
||||
end
|
||||
|
||||
Gitea::Organization::UpdateService.call(current_user.gitea_token, login, @organization.reload)
|
||||
Util.write_file(@image, avatar_path(@organization)) if params[:image].present?
|
||||
Cache::V2::OwnerCommonService.new(@organization.id).reset
|
||||
|
|
@ -81,28 +87,14 @@ class Organizations::OrganizationsController < Organizations::BaseController
|
|||
def languages
|
||||
projects = @organization.projects
|
||||
projects_count = @organization.projects.count
|
||||
languages_hash ={}
|
||||
projects.each do |p|
|
||||
result = Gitea::Repository::Languages::ListService.call(p.owner.login,
|
||||
p.identifier, current_user&.gitea_token)
|
||||
next unless result[:status] === :success
|
||||
total_byte_size = result[:body].values.sum
|
||||
hash = result[:body].transform_values { |v|
|
||||
(v * 100.0 / total_byte_size).to_f
|
||||
}
|
||||
|
||||
hash.each do |key,value|
|
||||
# Rails.logger.info "key=============#{key}:#{value}"
|
||||
if languages_hash.has_key?(key)
|
||||
languages_hash[key] = languages_hash[key] + value
|
||||
else
|
||||
languages_hash[key] = value
|
||||
end
|
||||
end
|
||||
languages_hash = Rails.cache.fetch("query/organizations/languages/#{@organization.id}", :expires_in => 1.days) do
|
||||
total_languages(projects)
|
||||
end
|
||||
|
||||
languages_hash = languages_hash.sort { |x, y| y[1] <=> x[1] }
|
||||
sort_hash = Hash[*languages_hash.flatten]
|
||||
Rails.logger.info "languages_hash=============#{sort_hash}"
|
||||
# Rails.logger.info "languages_hash=============#{sort_hash}"
|
||||
sort_hash= sort_hash.transform_values { |v|
|
||||
ActionController::Base.helpers.number_to_percentage((v / projects_count), precision: 1)
|
||||
}.select { |k, v| v != "0.0%" }
|
||||
|
|
@ -176,5 +168,43 @@ class Organizations::OrganizationsController < Organizations::BaseController
|
|||
def sync_organization_extension!
|
||||
@organization.organization_extension.update_attributes!(organization_extension_params)
|
||||
end
|
||||
|
||||
|
||||
def update_project_private(project)
|
||||
project.update_attributes!(is_public: false)
|
||||
project.forked_projects.update_all(is_public: project.is_public)
|
||||
gitea_params = {
|
||||
private: true,
|
||||
default_branch: project.default_branch,
|
||||
website: project.website,
|
||||
name: project.identifier
|
||||
}
|
||||
gitea_repo = Gitea::Repository::UpdateService.call(@organization, project&.repository&.identifier, gitea_params)
|
||||
project.repository.update_attributes({hidden: gitea_repo["private"], identifier: gitea_repo["name"]})
|
||||
# 更新对应所属分类下的项目数量(私有)
|
||||
project.project_category.decrement!(:private_projects_count, 1) if project.project_category.present?
|
||||
end
|
||||
|
||||
def total_languages(projects)
|
||||
languages_hash ={}
|
||||
projects.each do |p|
|
||||
result = Gitea::Repository::Languages::ListService.call(p.owner.login,
|
||||
p.identifier, current_user&.gitea_token)
|
||||
next unless result[:status] === :success
|
||||
total_byte_size = result[:body].values.sum
|
||||
hash = result[:body].transform_values { |v|
|
||||
(v * 100.0 / total_byte_size).to_f
|
||||
}
|
||||
|
||||
hash.each do |key,value|
|
||||
# Rails.logger.info "key=============#{key}:#{value}"
|
||||
if languages_hash.has_key?(key)
|
||||
languages_hash[key] = languages_hash[key] + value
|
||||
else
|
||||
languages_hash[key] = value
|
||||
end
|
||||
end
|
||||
end
|
||||
languages_hash
|
||||
end
|
||||
|
||||
end
|
||||
Loading…
Reference in New Issue