新增:项目排行榜新增提交数算法

This commit is contained in:
yystopf 2022-09-15 09:44:12 +08:00
parent 383660c778
commit 733f64eaec
4 changed files with 41 additions and 6 deletions

View File

@ -3,4 +3,15 @@ class CommitLog < ApplicationRecord
belongs_to :project
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

View File

@ -1,5 +1,5 @@
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
def initialize(project_id, params={})
@ -14,6 +14,7 @@ class Cache::V2::ProjectCommonService < ApplicationService
@forks = params[:forks]
@issues = params[:issues]
@pullrequests = params[:pullrequests]
@commits = params[:commits]
end
def read
@ -81,6 +82,10 @@ class Cache::V2::ProjectCommonService < ApplicationService
"pullrequests"
end
def commits_key
"commits"
end
def project_common
result = $redis_cache.hgetall(project_common_key)
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::ProjectDateRankService.call(@project_id, Date.today, {pullrequests: @pullrequests})
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
$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)
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
load_project
return unless @project.present?
@ -209,6 +223,7 @@ class Cache::V2::ProjectCommonService < ApplicationService
reset_project_forks
reset_project_issues
reset_project_pullrequests
reset_project_commits
$redis_cache.hgetall(project_common_key)
end

View File

@ -1,5 +1,6 @@
# 项目日活跃度计算存储
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
def initialize(project_id, rank_date=Date.today, params={})
@ -10,6 +11,7 @@ class Cache::V2::ProjectDateRankService < ApplicationService
@forks = params[:forks]
@issues = params[:issues]
@pullrequests = params[:pullrequests]
@commits = params[:commits]
end
def read
@ -37,7 +39,7 @@ class Cache::V2::ProjectDateRankService < ApplicationService
$redis_cache.zincrby(project_rank_key, @praises.to_i * 5, @project_id)
end
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
if @issues.present?
$redis_cache.zincrby(project_rank_key, @issues.to_i * 10, @project_id)
@ -45,6 +47,9 @@ class Cache::V2::ProjectDateRankService < ApplicationService
if @pullrequests.present?
$redis_cache.zincrby(project_rank_key, @pullrequests.to_i * 10, @project_id)
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)
end

View File

@ -1,5 +1,5 @@
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
def initialize(project_id, params={})
@ -9,6 +9,7 @@ class Cache::V2::ProjectRankService < ApplicationService
@forks = params[:forks]
@issues = params[:issues]
@pullrequests = params[:pullrequests]
@commits = params[:commits]
end
def read
@ -54,7 +55,7 @@ class Cache::V2::ProjectRankService < ApplicationService
$redis_cache.zincrby(project_rank_key, @praises.to_i * 5, @project_id)
end
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
if @issues.present?
$redis_cache.zincrby(project_rank_key, @issues.to_i * 10, @project_id)
@ -62,6 +63,9 @@ class Cache::V2::ProjectRankService < ApplicationService
if @pullrequests.present?
$redis_cache.zincrby(project_rank_key, @pullrequests.to_i * 10, @project_id)
end
if @commits.present?
$redis_cache.zincrby(project_rank_key, @commits.to_i * 1, @project_id)
end
reset_user_project_rank
end
@ -70,7 +74,7 @@ class Cache::V2::ProjectRankService < ApplicationService
def reset_project_rank
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)
reset_user_project_rank