diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index e32b31017..1d98b17f4 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -22,6 +22,12 @@ class RepositoriesController < ApplicationController def detail @user = current_user @result = Repositories::DetailService.call(@owner, @repository, @user) + cache_total_forks = $redis_cache.get("ProjectSpecialForks:#{@project.id}") + if cache_total_forks.present? + @project_forked_count = cache_total_forks.to_i + else + @project_forked_count = @project.forked_count.to_i + end @project_fork_id = @project.try(:forked_from_project_id) if @project_fork_id.present? @fork_project = Project.find_by(id: @project_fork_id) @@ -166,8 +172,9 @@ class RepositoriesController < ApplicationController if params[:filepath].present? || @project.educoder? @contributors = [] else - result = Gitea::Repository::Contributors::GetService.call(@owner, @repository.identifier) - @contributors = result.is_a?(Hash) && result.key?(:status) ? [] : result + result = Gitea::Repository::Contributors::GetService.call(@owner, @repository.identifier, {page: params[:page], limit: params[:limit]}) + @total_count = result[:total_count] + @contributors = result.is_a?(Hash) ? result[:body] : [] end rescue @contributors = [] @@ -321,7 +328,12 @@ class RepositoriesController < ApplicationController def get_latest_commit latest_commit = @project.educoder? ? nil : project_commits @latest_commit = latest_commit.present? ? latest_commit[:body][0] : nil - @commits_count = latest_commit.present? ? latest_commit[:total_count] : 0 + cache_total_commits = $redis_cache.get("ProjectSpecialCommit:#{@project.id}") + if cache_total_commits.present? + @commits_count = cache_total_commits.to_i + else + @commits_count = latest_commit.present? ? latest_commit[:total_count] : 0 + end end def content_params diff --git a/app/services/gitea/repository/contributors/get_service.rb b/app/services/gitea/repository/contributors/get_service.rb index 1ee1c3955..ea121a50d 100644 --- a/app/services/gitea/repository/contributors/get_service.rb +++ b/app/services/gitea/repository/contributors/get_service.rb @@ -1,22 +1,38 @@ class Gitea::Repository::Contributors::GetService < Gitea::ClientService - attr_reader :owner, :repo_name + attr_reader :owner, :repo_name, :page, :limit - def initialize(owner, repo_name) + def initialize(owner, repo_name, params) @owner = owner @repo_name = repo_name + @page = params[:page] || 1 + @limit = params[:limit] || 20 end def call response = get(url, params) - render_status(response) + render_result(response) end private def params - Hash.new.merge(token: owner.gitea_token) + Hash.new.merge(token: owner.gitea_token, page: page, limit: limit) end def url "/repos/#{owner.login}/#{repo_name}/contributors" end + + def render_result(response) + case response.status + when 200 + result = {} + headers = response.headers.to_hash + body = JSON.parse(response.body) + total_count = headers["x-total"] + result.merge(total_count: total_count.to_i, body: body) + else + nil + # {status: -1, message: "#{body['message']}"} + end + end end \ No newline at end of file diff --git a/app/views/repositories/contributors.json.jbuilder b/app/views/repositories/contributors.json.jbuilder index 4534de693..3947fb967 100644 --- a/app/views/repositories/contributors.json.jbuilder +++ b/app/views/repositories/contributors.json.jbuilder @@ -1,7 +1,6 @@ -total_count = @contributors.size json.list @contributors.each do |contributor| json.partial! 'contributor', locals: { contributor: contributor, project: @project } end -json.total_count total_count +json.total_count @total_count diff --git a/app/views/repositories/detail.json.jbuilder b/app/views/repositories/detail.json.jbuilder index 508d4c658..2498379da 100644 --- a/app/views/repositories/detail.json.jbuilder +++ b/app/views/repositories/detail.json.jbuilder @@ -11,7 +11,7 @@ json.issues_count @project.issues.issue_issue.size - @project.issues.issue_issue json.pull_requests_count @project.pull_requests.opening.size json.project_identifier render_identifier(@project) json.praises_count @project.praises_count.to_i -json.forked_count @project.forked_count.to_i +json.forked_count @project_forked_count.to_i json.watchers_count @project.watchers_count.to_i json.versions_count @project.versions.opening.size #里程碑数量 json.version_releases_count @project.releases_size(@user.try(:id), "all") diff --git a/lib/tasks/special_commit.rake b/lib/tasks/special_commit.rake new file mode 100644 index 000000000..ab6c63767 --- /dev/null +++ b/lib/tasks/special_commit.rake @@ -0,0 +1,44 @@ +# 执行示例 bundle exec rake "special_commit:load[yystopf, pig]" +# RAILS_ENV=production bundle exec rake "special_commit:load[yystopf, pig]" +# +namespace :special_commit do + desc "Sync Special Commit to Cache" + task :load, [:login, :identifier] => :environment do |t, args| + owner = Owner.find_by(login: args.login) + project = Project.find_by(user_id: owner&.id, identifier: args.identifier) + if owner.nil? || project.nil? + puts "====Project is not found.====" + else + puts "====Sync Project: #{owner.real_name}/#{project.name}====" + puts "====Sync Count Project Self Commit====" + self_commit_list_result = $gitea_client.get_repos_commits_by_owner_repo(owner.login, project.identifier) + total_commits = self_commit_list_result[:total_data].to_i + puts "====Sync Count Project Submodule Commit====" + entries = $gitea_client.get_repos_contents_by_owner_repo(owner.login, project.identifier) + entries.each do |entry| + next if entry["submodule_git_url"].nil? + submodule_git_url = entry["submodule_git_url"].gsub(Rails.application.config_for(:configuration)['platform_url'], '').gsub(".git", '') + real_relative_path = File.expand_path(submodule_git_url, "#{owner.login}/#{project.identifier}").gsub("#{Rails.root}/", '') + sub_project_owner_login = real_relative_path.split("/")[0] + sub_project_identifier = real_relative_path.split("/")[1] + sub_owner = Owner.find_by(login: sub_project_owner_login) + sub_project = sub_owner.projects.find_by(identifier: sub_project_identifier) + next if sub_owner.nil? || sub_project.nil? + sub_commit_list_result = $gitea_client.get_repos_commits_by_owner_repo(sub_project_owner_login, sub_project_identifier) + total_commits += sub_commit_list_result[:total_data].to_i + puts "====Sync Count Project Submodule forkproject Commit====" + sub_project.forked_projects.each do |p| + forked_project_owner_login = p.owner&.login + forked_project_identifier = p.identifier + next if forked_project_owner_login.nil? || forked_project_owner_login.nil? + forked_commit_list_result = $gitea_client.get_repos_commits_by_owner_repo(forked_project_owner_login, forked_project_identifier) + total_commits += forked_commit_list_result[:total_data].to_i + end + end + + puts "====Write total commits to cache====" + $redis_cache.set("ProjectSpecialCommit:#{project.id}", total_commits) + $redis_cache.expireat("ProjectSpecialCommit:#{project.id}", (Date.today+30.days).to_time.to_i) + end + end +end \ No newline at end of file