| @@ -31,6 +31,7 @@ class Organizations::OrganizationsController < Organizations::BaseController | |||||
| Organizations::CreateForm.new(organization_params.merge(original_name: "")).validate! | Organizations::CreateForm.new(organization_params.merge(original_name: "")).validate! | ||||
| @organization = Organizations::CreateService.call(current_user, organization_params) | @organization = Organizations::CreateService.call(current_user, organization_params) | ||||
| Util.write_file(@image, avatar_path(@organization)) if params[:image].present? | Util.write_file(@image, avatar_path(@organization)) if params[:image].present? | ||||
| Cache::V2::OwnerCommonService.new(@organization.id).reset | |||||
| end | end | ||||
| rescue Exception => e | rescue Exception => e | ||||
| uid_logger_error(e.message) | uid_logger_error(e.message) | ||||
| @@ -48,6 +49,7 @@ class Organizations::OrganizationsController < Organizations::BaseController | |||||
| Gitea::Organization::UpdateService.call(current_user.gitea_token, login, @organization.reload) | Gitea::Organization::UpdateService.call(current_user.gitea_token, login, @organization.reload) | ||||
| Util.write_file(@image, avatar_path(@organization)) if params[:image].present? | Util.write_file(@image, avatar_path(@organization)) if params[:image].present? | ||||
| Cache::V2::OwnerCommonService.new(@organization.id).reset | |||||
| end | end | ||||
| rescue Exception => e | rescue Exception => e | ||||
| uid_logger_error(e.message) | uid_logger_error(e.message) | ||||
| @@ -3,4 +3,15 @@ class CommitLog < ApplicationRecord | |||||
| belongs_to :project | belongs_to :project | ||||
| belongs_to :repository | belongs_to :repository | ||||
| after_create :incre_project_common | |||||
| after_destroy :decre_project_common | |||||
| def incre_project_common | |||||
| CacheAsyncSetJob.perform_later("project_common_service", {commits: 1}, self.project_id) | |||||
| end | |||||
| def decre_project_common | |||||
| CacheAsyncSetJob.perform_later("project_common_service", {commits: -1}, self.project_id) | |||||
| end | |||||
| end | end | ||||
| @@ -81,15 +81,15 @@ class Organization < Owner | |||||
| scope :with_visibility, ->(visibility) { joins(:organization_extension).where(organization_extensions: {visibility: visibility}) if visibility.present? } | scope :with_visibility, ->(visibility) { joins(:organization_extension).where(organization_extensions: {visibility: visibility}) if visibility.present? } | ||||
| after_save :reset_cache_data | |||||
| # after_save :reset_cache_data | |||||
| def gitea_token | def gitea_token | ||||
| team_users.joins(:team).where(teams: {authorize: "owner"}).take&.user&.gitea_token | team_users.joins(:team).where(teams: {authorize: "owner"}).take&.user&.gitea_token | ||||
| end | end | ||||
| def reset_cache_data | |||||
| Cache::V2::OwnerCommonService.new(self.id).reset | |||||
| end | |||||
| # def reset_cache_data | |||||
| # Cache::V2::OwnerCommonService.new(self.id).reset | |||||
| # end | |||||
| def self.build(name, nickname, gitea_token=nil) | def self.build(name, nickname, gitea_token=nil) | ||||
| self.create!(login: name, nickname: nickname, gitea_token: gitea_token) | self.create!(login: name, nickname: nickname, gitea_token: gitea_token) | ||||
| @@ -1,5 +1,5 @@ | |||||
| class Cache::V2::ProjectCommonService < ApplicationService | class Cache::V2::ProjectCommonService < ApplicationService | ||||
| attr_reader :project_id, :owner_id, :name, :identifier, :description, :visits, :watchers, :praises, :forks, :issues, :pullrequests | |||||
| attr_reader :project_id, :owner_id, :name, :identifier, :description, :visits, :watchers, :praises, :forks, :issues, :pullrequests, :commits | |||||
| attr_accessor :project | attr_accessor :project | ||||
| def initialize(project_id, params={}) | def initialize(project_id, params={}) | ||||
| @@ -14,6 +14,7 @@ class Cache::V2::ProjectCommonService < ApplicationService | |||||
| @forks = params[:forks] | @forks = params[:forks] | ||||
| @issues = params[:issues] | @issues = params[:issues] | ||||
| @pullrequests = params[:pullrequests] | @pullrequests = params[:pullrequests] | ||||
| @commits = params[:commits] | |||||
| end | end | ||||
| def read | def read | ||||
| @@ -81,6 +82,10 @@ class Cache::V2::ProjectCommonService < ApplicationService | |||||
| "pullrequests" | "pullrequests" | ||||
| end | end | ||||
| def commits_key | |||||
| "commits" | |||||
| end | |||||
| def project_common | def project_common | ||||
| result = $redis_cache.hgetall(project_common_key) | result = $redis_cache.hgetall(project_common_key) | ||||
| result.blank? ? reset_project_common : result | result.blank? ? reset_project_common : result | ||||
| @@ -149,6 +154,11 @@ class Cache::V2::ProjectCommonService < ApplicationService | |||||
| Cache::V2::ProjectRankService.call(@project_id, {pullrequests: @pullrequests}) | Cache::V2::ProjectRankService.call(@project_id, {pullrequests: @pullrequests}) | ||||
| Cache::V2::ProjectDateRankService.call(@project_id, Date.today, {pullrequests: @pullrequests}) | Cache::V2::ProjectDateRankService.call(@project_id, Date.today, {pullrequests: @pullrequests}) | ||||
| end | end | ||||
| if @commits.present? | |||||
| $redis_cache.hincrby(project_common_key, commits_key, @commits) | |||||
| Cache::V2::ProjectRankService.call(@project_id, {commits: @commits}) | |||||
| Cache::V2::ProjectDateRankService.call(@project_id, Date.today, {commits: @commits}) | |||||
| end | |||||
| end | end | ||||
| $redis_cache.hgetall(project_common_key) | $redis_cache.hgetall(project_common_key) | ||||
| @@ -194,6 +204,10 @@ class Cache::V2::ProjectCommonService < ApplicationService | |||||
| $redis_cache.hset(project_common_key, pullrequests_key, PullRequest.where(project_id: @project_id).count) | $redis_cache.hset(project_common_key, pullrequests_key, PullRequest.where(project_id: @project_id).count) | ||||
| end | end | ||||
| def reset_project_commits | |||||
| $redis_cache.hset(project_common_key, commits_key, CommitLog.where(project_id: @project_id).count) | |||||
| end | |||||
| def reset_project_common | def reset_project_common | ||||
| load_project | load_project | ||||
| return unless @project.present? | return unless @project.present? | ||||
| @@ -209,6 +223,7 @@ class Cache::V2::ProjectCommonService < ApplicationService | |||||
| reset_project_forks | reset_project_forks | ||||
| reset_project_issues | reset_project_issues | ||||
| reset_project_pullrequests | reset_project_pullrequests | ||||
| reset_project_commits | |||||
| $redis_cache.hgetall(project_common_key) | $redis_cache.hgetall(project_common_key) | ||||
| end | end | ||||
| @@ -1,5 +1,6 @@ | |||||
| # 项目日活跃度计算存储 | |||||
| class Cache::V2::ProjectDateRankService < ApplicationService | class Cache::V2::ProjectDateRankService < ApplicationService | ||||
| attr_reader :project_id, :rank_date, :visits, :praises, :forks, :issues, :pullrequests | |||||
| attr_reader :project_id, :rank_date, :visits, :praises, :forks, :issues, :pullrequests, :commits | |||||
| attr_accessor :project_common | attr_accessor :project_common | ||||
| def initialize(project_id, rank_date=Date.today, params={}) | def initialize(project_id, rank_date=Date.today, params={}) | ||||
| @@ -10,6 +11,7 @@ class Cache::V2::ProjectDateRankService < ApplicationService | |||||
| @forks = params[:forks] | @forks = params[:forks] | ||||
| @issues = params[:issues] | @issues = params[:issues] | ||||
| @pullrequests = params[:pullrequests] | @pullrequests = params[:pullrequests] | ||||
| @commits = params[:commits] | |||||
| end | end | ||||
| def read | def read | ||||
| @@ -37,7 +39,7 @@ class Cache::V2::ProjectDateRankService < ApplicationService | |||||
| $redis_cache.zincrby(project_rank_key, @praises.to_i * 5, @project_id) | $redis_cache.zincrby(project_rank_key, @praises.to_i * 5, @project_id) | ||||
| end | end | ||||
| if @forks.present? | if @forks.present? | ||||
| $redis_cache.zincrby(project_rank_key, @forks.to_i * 5, @project_id) | |||||
| $redis_cache.zincrby(project_rank_key, @forks.to_i * 10, @project_id) | |||||
| end | end | ||||
| if @issues.present? | if @issues.present? | ||||
| $redis_cache.zincrby(project_rank_key, @issues.to_i * 10, @project_id) | $redis_cache.zincrby(project_rank_key, @issues.to_i * 10, @project_id) | ||||
| @@ -45,6 +47,9 @@ class Cache::V2::ProjectDateRankService < ApplicationService | |||||
| if @pullrequests.present? | if @pullrequests.present? | ||||
| $redis_cache.zincrby(project_rank_key, @pullrequests.to_i * 10, @project_id) | $redis_cache.zincrby(project_rank_key, @pullrequests.to_i * 10, @project_id) | ||||
| end | end | ||||
| if @commits.present? | |||||
| $redis_cache.zincrby(project_rank_key, @commits.to_i * 1, @project_id) | |||||
| end | |||||
| $redis_cache.zscore(project_rank_key, @project_id) | $redis_cache.zscore(project_rank_key, @project_id) | ||||
| end | end | ||||
| @@ -1,5 +1,5 @@ | |||||
| class Cache::V2::ProjectRankService < ApplicationService | class Cache::V2::ProjectRankService < ApplicationService | ||||
| attr_reader :project_id, :visits, :praises, :forks, :issues, :pullrequests | |||||
| attr_reader :project_id, :visits, :praises, :forks, :issues, :pullrequests, :commits | |||||
| attr_accessor :project_common | attr_accessor :project_common | ||||
| def initialize(project_id, params={}) | def initialize(project_id, params={}) | ||||
| @@ -9,6 +9,7 @@ class Cache::V2::ProjectRankService < ApplicationService | |||||
| @forks = params[:forks] | @forks = params[:forks] | ||||
| @issues = params[:issues] | @issues = params[:issues] | ||||
| @pullrequests = params[:pullrequests] | @pullrequests = params[:pullrequests] | ||||
| @commits = params[:commits] | |||||
| end | end | ||||
| def read | def read | ||||
| @@ -54,7 +55,7 @@ class Cache::V2::ProjectRankService < ApplicationService | |||||
| $redis_cache.zincrby(project_rank_key, @praises.to_i * 5, @project_id) | $redis_cache.zincrby(project_rank_key, @praises.to_i * 5, @project_id) | ||||
| end | end | ||||
| if @forks.present? | if @forks.present? | ||||
| $redis_cache.zincrby(project_rank_key, @forks.to_i * 5, @project_id) | |||||
| $redis_cache.zincrby(project_rank_key, @forks.to_i * 10, @project_id) | |||||
| end | end | ||||
| if @issues.present? | if @issues.present? | ||||
| $redis_cache.zincrby(project_rank_key, @issues.to_i * 10, @project_id) | $redis_cache.zincrby(project_rank_key, @issues.to_i * 10, @project_id) | ||||
| @@ -62,6 +63,9 @@ class Cache::V2::ProjectRankService < ApplicationService | |||||
| if @pullrequests.present? | if @pullrequests.present? | ||||
| $redis_cache.zincrby(project_rank_key, @pullrequests.to_i * 10, @project_id) | $redis_cache.zincrby(project_rank_key, @pullrequests.to_i * 10, @project_id) | ||||
| end | end | ||||
| if @commits.present? | |||||
| $redis_cache.zincrby(project_rank_key, @commits.to_i * 1, @project_id) | |||||
| end | |||||
| reset_user_project_rank | reset_user_project_rank | ||||
| end | end | ||||
| @@ -70,7 +74,7 @@ class Cache::V2::ProjectRankService < ApplicationService | |||||
| def reset_project_rank | def reset_project_rank | ||||
| load_project_common | load_project_common | ||||
| score = @project_common["visits"].to_i * 1 + @project_common["praises"].to_i * 5 + @project_common["forks"].to_i * 5 + @project_common["issues"].to_i * 10 + @project_common["pullrequests"].to_i * 10 | |||||
| score = @project_common["visits"].to_i * 1 + @project_common["praises"].to_i * 5 + @project_common["forks"].to_i * 10 + @project_common["issues"].to_i * 10 + @project_common["pullrequests"].to_i * 10 + @project_common["commits"].to_i * 1 | |||||
| $redis_cache.zadd(project_rank_key, score, @project_id) | $redis_cache.zadd(project_rank_key, score, @project_id) | ||||
| reset_user_project_rank | reset_user_project_rank | ||||