diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 1c6e9868c..b1ab9d010 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -190,6 +190,8 @@ class ProjectsController < ApplicationController end def simple + # 为了缓存活跃项目的基本信息,后续删除 + Cache::V2::ProjectCommonService.new(@project.id).read json_response(@project, current_user) end diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 35f11f476..9b94f0d5d 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -48,7 +48,7 @@ class RepositoriesController < ApplicationController def entries @project.increment!(:visits) - + CacheAsyncSetJob.perform_later("project_common_service", {visits: 1}, @project.id) if @project.educoder? @entries = Educoder::Repository::Entries::ListService.call(@project&.project_educoder.repo_name) else diff --git a/app/jobs/cache_async_set_job.rb b/app/jobs/cache_async_set_job.rb index 1c67f91a6..f617b1316 100644 --- a/app/jobs/cache_async_set_job.rb +++ b/app/jobs/cache_async_set_job.rb @@ -1,7 +1,7 @@ -class CacheAsyncResetJob < ApplicationJob +class CacheAsyncSetJob < ApplicationJob queue_as :cache - def perform(type, id=nil, params={}) + def perform(type, params={}, id=nil) case type when "platform_statistic_service" Cache::V2::PlatformStatisticService.new(params).call diff --git a/app/models/fork_user.rb b/app/models/fork_user.rb index 0365af72b..bddf8f75c 100644 --- a/app/models/fork_user.rb +++ b/app/models/fork_user.rb @@ -20,12 +20,30 @@ class ForkUser < ApplicationRecord belongs_to :user belongs_to :fork_project, class_name: 'Project', foreign_key: :fork_project_id - after_save :reset_cache_data - after_destroy :reset_cache_data + after_create :incre_project_common, :incre_user_statistic, :incre_platform_statistic + after_destroy :decre_project_common, :decre_user_statistic, :decre_platform_statistic - def reset_cache_data - CacheAsyncResetJob.perform_later("platform_statistic_service") - CacheAsyncResetJob.perform_later("user_statistic_service", self.project&.user_id) + def incre_project_common + CacheAsyncSetJob.perform_later("project_common_service", {forks: 1}, self.project_id) end + def decre_project_common + CacheAsyncSetJob.perform_later("project_common_service", {forks: -1}, self.project_id) + end + + def incre_user_statistic + CacheAsyncSetJob.perform_later("user_statistic_service", {fork_count: 1}, self.project&.user_id) + end + + def decre_user_statistic + CacheAsyncSetJob.perform_later("user_statistic_service", {fork_count: -1}, self.project&.user_id) + end + + def incre_platform_statistic + CacheAsyncSetJob.perform_later("platform_statistic_service", {fork_count: 1}) + end + + def decre_platform_statistic + CacheAsyncSetJob.perform_later("platform_statistic_service", {fork_count: -1}) + end end diff --git a/app/models/issue.rb b/app/models/issue.rb index d195837a7..c642e642b 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -74,13 +74,32 @@ class Issue < ApplicationRecord scope :issue_pull_request, ->{where(issue_classify: "pull_request")} scope :issue_index_includes, ->{includes(:tracker, :priority, :version, :issue_status, :journals,:issue_tags,user: :user_extension)} scope :closed, ->{where(status_id: 5)} + after_create :incre_project_common, :incre_user_statistic, :incre_platform_statistic after_update :change_versions_count - after_save :reset_cache_data - after_destroy :update_closed_issues_count_in_project!, :reset_cache_data + after_destroy :update_closed_issues_count_in_project!, :decre_project_common, :decre_user_statistic, :decre_platform_statistic - def reset_cache_data - CacheAsyncResetJob.perform_later("platform_statistic_service") - CacheAsyncResetJob.perform_later("user_statistic_service", self.author_id) + def incre_project_common + CacheAsyncSetJob.perform_later("project_common_service", {issues: 1}, self.project_id) + end + + def decre_project_common + CacheAsyncSetJob.perform_later("project_common_service", {issues: -1}, self.project_id) + end + + def incre_user_statistic + CacheAsyncSetJob.perform_later("user_statistic_service", {issue_count: 1}, self.author_id) + end + + def decre_user_statistic + CacheAsyncSetJob.perform_later("user_statistic_service", {issue_count: -1}, self.author_id) + end + + def incre_platform_statistic + CacheAsyncSetJob.perform_later("platform_statistic_service", {issue_count: 1}) + end + + def decre_platform_statistic + CacheAsyncSetJob.perform_later("platform_statistic_service", {issue_count: -1}) end def get_assign_user diff --git a/app/models/praise_tread.rb b/app/models/praise_tread.rb index 30d226735..5d4ae0d80 100644 --- a/app/models/praise_tread.rb +++ b/app/models/praise_tread.rb @@ -21,15 +21,31 @@ class PraiseTread < ApplicationRecord belongs_to :praise_tread_object, polymorphic: true, counter_cache: :praises_count has_many :tidings, :as => :container, :dependent => :destroy - after_create :send_tiding - after_save :reset_cache_data - after_destroy :reset_cache_data + after_create :send_tiding, :incre_project_common, :incre_user_statistic, :incre_platform_statistic + after_destroy :decre_project_common, :decre_user_statistic, :decre_platform_statistic - def reset_cache_data - CacheAsyncResetJob.perform_later("platform_statistic_service") - if self.praise_tread_object.is_a?(Project) - CacheAsyncResetJob.perform_later("user_statistic_service", self.praise_tread_object&.user_id) - end + def incre_project_common + CacheAsyncSetJob.perform_later("project_common_service", {praises: 1}, self.praise_tread_object_id) if self.praise_tread_object_type == "Project" + end + + def decre_project_common + CacheAsyncSetJob.perform_later("project_common_service", {praises: -1}, self.praise_tread_object_id) if self.praise_tread_object_type == "Project" + end + + def incre_user_statistic + CacheAsyncSetJob.perform_later("user_statistic_service", {project_praise_count: 1}, self.praise_tread_object&.user_id) if self.praise_tread_object_type == "Project" + end + + def decre_user_statistic + CacheAsyncSetJob.perform_later("user_statistic_service", {project_praise_count: -1}, self.praise_tread_object&.user_id) if self.praise_tread_object_type == "Project" + end + + def incre_platform_statistic + CacheAsyncSetJob.perform_later("platform_statistic_service", {project_praise_count: 1}) if self.praise_tread_object_type == "Project" + end + + def decre_platform_statistic + CacheAsyncSetJob.perform_later("platform_statistic_service", {project_praise_count: -1}) if self.praise_tread_object_type == "Project" end def send_tiding diff --git a/app/models/project.rb b/app/models/project.rb index d9933e13d..78c049ab2 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -126,10 +126,10 @@ class Project < ApplicationRecord has_many :pinned_projects, dependent: :destroy has_many :has_pinned_users, through: :pinned_projects, source: :user has_many :webhooks, class_name: "Gitea::Webhook", primary_key: :gpid, foreign_key: :repo_id - + after_create :init_project_common, :incre_user_statistic, :incre_platform_statistic after_save :check_project_members before_save :set_invite_code, :reset_cache_data, :reset_unmember_followed - after_destroy :reset_cache_data + after_destroy :decre_project_common, :decre_user_statistic, :decre_platform_statistic scope :project_statics_select, -> {select(:id,:name, :is_public, :identifier, :status, :project_type, :user_id, :forked_count, :visits, :project_category_id, :project_language_id, :license_id, :ignore_id, :watchers_count, :created_on)} scope :no_anomory_projects, -> {where("projects.user_id is not null and projects.user_id != ?", 2)} scope :recommend, -> { visible.project_statics_select.where(recommend: true) } @@ -150,13 +150,46 @@ class Project < ApplicationRecord end def reset_cache_data + CacheAsyncResetJob.perform_later("project_common_service", self.id) if changes[:user_id].present? - CacheAsyncResetJob.perform_later("user_statistic_service", changes[:user_id].first) + CacheAsyncSetJob.perform_later("user_statistic_service", {project_count: -1}, changes[:user_id].first) + CacheAsyncSetJob.perform_later("user_statistic_service", {project_count: 1}, changes[:user_id].last) + end + if changes[:project_language_id].present? + first_language = ProjectLanguage.find_by_id(changes[:project_language_id].first) + last_language = ProjectLanguage.find_by_id(changes[:project_language_id].last) + CacheAsyncSetJob.perform_later("user_statistic_service", {project_language_count_key: first_language&.name, project_language_count: -1}, self.user_id) + CacheAsyncSetJob.perform_later("user_statistic_service", {project_language_count_key: last_language&.name, project_language_count: 1}, self.user_id) + CacheAsyncSetJob.perform_later("platform_statistic_service", {project_language_count_key: first_language&.name, project_language_count: -1}) + CacheAsyncSetJob.perform_later("platform_statistic_service", {project_language_count_key: last_language&.name, project_language_count: 1}) end - CacheAsyncResetJob.perform_later("platform_statistic_service") - CacheAsyncResetJob.perform_later("user_statistic_service", self.user_id) end + def init_project_common + CacheAsyncResetJob.perform_later("project_common_service", self.id) + end + + def decre_project_common + $redis_cache.del("v2-project-common:#{self.id}") + end + + def incre_user_statistic + CacheAsyncSetJob.perform_later("user_statistic_service", {project_count: 1, project_language_count_key: self.project_language&.name, project_language_count: 1}, self.user_id) + end + + def decre_user_statistic + CacheAsyncSetJob.perform_later("user_statistic_service", {project_count: -1, project_language_count_key: self.project_language&.name, project_language_count: -1}, self.user_id) + end + + def incre_platform_statistic + CacheAsyncSetJob.perform_later("platform_statistic_service", {project_count: 1, project_language_count_key: self.project_language&.name, project_language_count: 1}) + end + + def decre_platform_statistic + CacheAsyncSetJob.perform_later("platform_statistic_service", {project_count: -1, project_language_count_key: self.project_language&.name, project_language_count: -1}) + end + + def reset_unmember_followed if changes[:is_public].present? && changes[:is_public] == [true, false] self.watchers.where.not(user_id: self.all_collaborators).destroy_all diff --git a/app/models/pull_request.rb b/app/models/pull_request.rb index 9c6825f27..7338a1d72 100644 --- a/app/models/pull_request.rb +++ b/app/models/pull_request.rb @@ -42,12 +42,31 @@ class PullRequest < ApplicationRecord scope :merged_and_closed, ->{where.not(status: 0)} scope :opening, -> {where(status: 0)} - after_save :reset_cache_data - after_destroy :reset_cache_data + after_create :incre_project_common, :incre_user_statistic, :incre_platform_statistic + after_destroy :decre_project_common, :decre_user_statistic, :decre_platform_statistic - def reset_cache_data - CacheAsyncResetJob.perform_later("platform_statistic_service") - CacheAsyncResetJob.perform_later("user_statistic_service", self.user_id) + def incre_project_common + CacheAsyncSetJob.perform_later("project_common_service", {pullrequests: 1}, self.project_id) + end + + def decre_project_common + CacheAsyncSetJob.perform_later("project_common_service", {pullrequests: -1}, self.project_id) + end + + def incre_user_statistic + CacheAsyncSetJob.perform_later("user_statistic_service", {pullrequest_count: 1}, self.user_id) + end + + def decre_user_statistic + CacheAsyncSetJob.perform_later("user_statistic_service", {pullrequest_count: -1}, self.user_id) + end + + def incre_platform_statistic + CacheAsyncSetJob.perform_later("platform_statistic_service", {pullrequest_count: 1}) + end + + def decre_platform_statistic + CacheAsyncSetJob.perform_later("platform_statistic_service", {pullrequest_count: -1}) end def fork_project diff --git a/app/models/user.rb b/app/models/user.rb index fc4c33618..7a172020c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -188,7 +188,7 @@ class User < Owner :show_email, :show_location, :show_department, :technical_title, :province, :city, :custom_department, to: :user_extension, allow_nil: true - before_save :update_hashed_password, :set_lastname + before_save :update_hashed_password, :set_lastname, :reset_cache_data after_create do SyncTrustieJob.perform_later("user", 1) if allow_sync_to_trustie? end @@ -205,6 +205,10 @@ class User < Owner validate :validate_sensitive_string validate :validate_password_length + def reset_cache_data + Cache::V2::OwnerCommonService.new(self.login, self.mail).reset + end + # 用户参与的所有项目 def full_member_projects normal_projects = Project.members_projects(self.id).to_sql diff --git a/app/models/watcher.rb b/app/models/watcher.rb index abbcf07bc..5a2cd96fb 100644 --- a/app/models/watcher.rb +++ b/app/models/watcher.rb @@ -22,18 +22,36 @@ class Watcher < ApplicationRecord scope :watching_users, ->(watchable_id){ where("watchable_type = ? and user_id = ?",'User',watchable_id)} - after_save :reset_cache_data - after_destroy :reset_cache_data - after_create :send_create_message_to_notice_system + after_create :send_create_message_to_notice_system, :incre_project_common, :incre_user_statistic, :incre_platform_statistic + after_destroy :decre_project_common, :decre_user_statistic, :decre_platform_statistic - def reset_cache_data - if self.watchable.is_a?(User) - CacheAsyncResetJob.perform_later("user_statistic_service", self.watchable_id) - end - if self.watchable.is_a?(Project) - CacheAsyncResetJob.perform_later("user_statistic_service", self.watchable&.user_id) - end - CacheAsyncResetJob.perform_later("platform_statistic_service") + + def incre_project_common + CacheAsyncSetJob.perform_later("project_common_service", {watchers: 1}, self.watchable_id) if self.watchable_type == "Project" + end + + def decre_project_common + CacheAsyncSetJob.perform_later("project_common_service", {watchers: -1}, self.watchable_id) if self.watchable_type == "Project" + end + + def incre_user_statistic + CacheAsyncSetJob.perform_later("user_statistic_service", {follow_count: 1}, self.watchable_id) if self.watchable_type == "User" + CacheAsyncSetJob.perform_later("user_statistic_service", {project_watcher_count: 1}, self.watchable&.user_id) if self.watchable_type == "Project" + end + + def decre_user_statistic + CacheAsyncSetJob.perform_later("user_statistic_service", {follow_count: -1}, self.watchable_id) if self.watchable_type == "User" + CacheAsyncSetJob.perform_later("user_statistic_service", {project_watcher_count: -1}, self.watchable&.user_id) if self.watchable_type == "Project" + end + + def incre_platform_statistic + CacheAsyncSetJob.perform_later("platform_statistic_service", {follow_count: 1}) if self.watchable_type == "User" + CacheAsyncSetJob.perform_later("platform_statistic_service", {project_watcher_count: 1}) if self.watchable_type == "Project" + end + + def decre_platform_statistic + CacheAsyncSetJob.perform_later("platform_statistic_service", {follow_count: -1}) if self.watchable_type == "User" + CacheAsyncSetJob.perform_later("platform_statistic_service", {project_watcher_count: -1}) if self.watchable_type == "Project" end def send_create_message_to_notice_system diff --git a/app/services/cache/v2/project_common_service.rb b/app/services/cache/v2/project_common_service.rb index 365f07a6e..6ea4bb3a9 100644 --- a/app/services/cache/v2/project_common_service.rb +++ b/app/services/cache/v2/project_common_service.rb @@ -1,5 +1,5 @@ class Cache::V2::ProjectCommonService < ApplicationService - attr_reader :project_id, :owner_id, :name, :identifier, :visits, :watchers, :praises, :forks, :issues, :pullrequests + attr_reader :project_id, :owner_id, :name, :identifier, :description, :visits, :watchers, :praises, :forks, :issues, :pullrequests attr_accessor :project def initialize(project_id, params={}) @@ -7,6 +7,7 @@ class Cache::V2::ProjectCommonService < ApplicationService @owner_id = params[:owner_id] @name = params[:name] @identifier = params[:identifier] + @description = params[:description] @visits = params[:visits] @watchers = params[:watchers] @praises = params[:praises] @@ -48,6 +49,10 @@ class Cache::V2::ProjectCommonService < ApplicationService "identifier" end + def description_key + "description" + end + def visits_key "visits" end @@ -102,13 +107,22 @@ class Cache::V2::ProjectCommonService < ApplicationService $redis_cache.hset(project_common_key, identifier_key, @identifier) end end + if @description.present? + if $redis_cache.hget(project_common_key, description_key).nil? + reset_project_description + else + $redis_cache.hset(project_common_key, description_key, @description) + end + end if @visits.present? if $redis_cache.hget(project_common_key, visits_key).nil? reset_project_visits + Cache::V2::ProjectRankService.call(@project_id, {visits: @visits}) + Cache::V2::ProjectDateRankService.call(@project_id, Date.today, {visits: @visits}) else $redis_cache.hincrby(project_common_key, visits_key, @visits) Cache::V2::ProjectRankService.call(@project_id, {visits: @visits}) - Cache::V2::ProjectDateRankService.call(@project_id, {visits: @visits}) + Cache::V2::ProjectDateRankService.call(@project_id, Date.today, {visits: @visits}) end end if @watchers.present? @@ -121,37 +135,45 @@ class Cache::V2::ProjectCommonService < ApplicationService if @praises.present? if $redis_cache.hget(project_common_key, praises_key).nil? reset_project_praises + Cache::V2::ProjectRankService.call(@project_id, {praises: @praises}) + Cache::V2::ProjectDateRankService.call(@project_id, Date.today, {praises: @praises}) else $redis_cache.hincrby(project_common_key, praises_key, @praises) Cache::V2::ProjectRankService.call(@project_id, {praises: @praises}) - Cache::V2::ProjectDateRankService.call(@project_id, {praises: @praises}) + Cache::V2::ProjectDateRankService.call(@project_id, Date.today, {praises: @praises}) end end if @forks.present? if $redis_cache.hget(project_common_key, forks_key).nil? reset_project_forks + Cache::V2::ProjectRankService.call(@project_id, {forks: @forks}) + Cache::V2::ProjectDateRankService.call(@project_id, Date.today, {forks: @forks}) else $redis_cache.hincrby(project_common_key, forks_key, @forks) Cache::V2::ProjectRankService.call(@project_id, {forks: @forks}) - Cache::V2::ProjectDateRankService.call(@project_id, {forks: @forks}) + Cache::V2::ProjectDateRankService.call(@project_id, Date.today, {forks: @forks}) end end if @issues.present? if $redis_cache.hget(project_common_key, issues_key).nil? reset_project_issues + Cache::V2::ProjectRankService.call(@project_id, {issues: @issues}) + Cache::V2::ProjectDateRankService.call(@project_id, Date.today, {issues: @issues}) else $redis_cache.hincrby(project_common_key, issues_key, @issues) Cache::V2::ProjectRankService.call(@project_id, {issues: @issues}) - Cache::V2::ProjectDateRankService.call(@project_id, {issues: @issues}) + Cache::V2::ProjectDateRankService.call(@project_id, Date.today, {issues: @issues}) end end if @pullrequests.present? if $redis_cache.hget(project_common_key, pullrequests_key).nil? reset_project_pullrequests + Cache::V2::ProjectRankService.call(@project_id, {pullrequests: @pullrequests}) + Cache::V2::ProjectDateRankService.call(@project_id, Date.today, {pullrequests: @pullrequests}) else $redis_cache.hincrby(project_common_key, pullrequests_key, @pullrequests) Cache::V2::ProjectRankService.call(@project_id, {pullrequests: @pullrequests}) - Cache::V2::ProjectDateRankService.call(@project_id, {pullrequests: @pullrequests}) + Cache::V2::ProjectDateRankService.call(@project_id, Date.today, {pullrequests: @pullrequests}) end end $redis_cache.hgetall(project_common_key) @@ -169,6 +191,10 @@ class Cache::V2::ProjectCommonService < ApplicationService $redis_cache.hset(project_common_key, identifier_key, @project&.identifier) end + def reset_project_description + $redis_cache.hset(project_common_key, description_key, @project&.description) + end + def reset_project_visits $redis_cache.hset(project_common_key, visits_key, @project&.visits) end @@ -186,7 +212,7 @@ class Cache::V2::ProjectCommonService < ApplicationService end def reset_project_issues - $redis_cache.hset(project_common_key, issues_key, Issue.where(project_id: @project_id).count) + $redis_cache.hset(project_common_key, issues_key, Issue.issue_issue.where(project_id: @project_id).count) end def reset_project_pullrequests @@ -199,6 +225,7 @@ class Cache::V2::ProjectCommonService < ApplicationService reset_project_owner_id reset_project_name reset_project_identifier + reset_project_description reset_project_visits reset_project_watchers reset_project_praises diff --git a/app/services/cache/v2/project_date_rank_service.rb b/app/services/cache/v2/project_date_rank_service.rb index 38c11c9b9..092aff796 100644 --- a/app/services/cache/v2/project_date_rank_service.rb +++ b/app/services/cache/v2/project_date_rank_service.rb @@ -4,6 +4,7 @@ class Cache::V2::ProjectDateRankService < ApplicationService def initialize(project_id, rank_date=Date.today, params={}) @project_id = project_id + @rank_date = rank_date @visits = params[:visits] @praises = params[:praises] @forks = params[:forks] @@ -21,7 +22,7 @@ class Cache::V2::ProjectDateRankService < ApplicationService private def project_rank_key - "v2-project-rank-#{rank_date.to_s}" + "v2-project-rank-#{@rank_date.to_s}" end def project_rank diff --git a/app/services/cache/v2/user_date_rank_service.rb b/app/services/cache/v2/user_date_rank_service.rb new file mode 100644 index 000000000..583f03a22 --- /dev/null +++ b/app/services/cache/v2/user_date_rank_service.rb @@ -0,0 +1,118 @@ +class Cache::V2::UserDateRankService < ApplicationService + attr_reader :user_id, :rank_date, :follow_count, :fork_count, :issue_count, :project_count, :project_language_count_key, :project_language_count, :project_praise_count, :project_watcher_count, :pullrequest_count + + def initialize(user_id, rank_date=Date.today, params={}) + @user_id = user_id + @rank_date = rank_date + @follow_count = params[:follow_count] + @fork_count = params[:fork_count] + @issue_count = params[:issue_count] + @project_count = params[:project_count] + @project_language_count_key = params[:project_language_count_key] + @project_language_count = params[:project_language_count] + @project_praise_count = params[:project_praise_count] + @project_watcher_count = params[:project_watcher_count] + @pullrequest_count = params[:pullrequest_count] + end + + def read_rank + user_rank + end + + def read_statistic + user_statistic + end + + def call + set_user_rank + end + + private + def user_rank_key + "v2-user-rank-#{@rank_date.to_s}" + end + + def user_date_statistic_key + "v2-user-statistic:#{@user_id}-#{@rank_date.to_s}" + end + + def user_rank + $redis_cache.zscore(user_rank_key, @user_id) + end + + def user_statistic + $redis_cache.hgetall(user_date_statistic_key) + end + + def set_user_statistic + if @follow_count.present? + $redis_cache.hincrby(user_date_statistic_key, "follow-count", @follow_count.to_i) + end + if @fork_count.present? + $redis_cache.hincrby(user_date_statistic_key, "fork-count", @fork_count.to_i) + end + if @issue_count.present? + $redis_cache.hincrby(user_date_statistic_key, "issue-count", @issue_count.to_i) + end + if @project_count.present? + $redis_cache.hincrby(user_date_statistic_key, "project-count", @project_count.to_i) + end + if project_language_count_key.present? && project_language_count.present? + if $redis_cache.hget(user_date_statistic_key, "project-language").nil? + result = {} + result[@project_language_count_key] = project_language_count.to_i + result.delete(@project_language_count_key) if result[@project_language_count_key] == 0 + $redis_cache.hset(user_date_statistic_key, "project-language", result.to_json) + else + result = JSON.parse($redis_cache.hget(user_date_statistic_key, "project-language")) + result[@project_language_count_key] ||= 0 + result[@project_language_count_key] += project_language_count.to_i + result.delete(@project_language_count_key) if result[@project_language_count_key] == 0 + $redis_cache.hset(user_date_statistic_key, "project-language", result.to_json) + end + end + if @project_praise_count.present? + $redis_cache.hincrby(user_date_statistic_key, "project-praise-count", @project_praise_count.to_i) + end + if @project_watcher_count.present? + $redis_cache.hincrby(user_date_statistic_key, "project-watcher-count", @project_watcher_count.to_i) + end + if @pullrequest_count.present? + $redis_cache.hincrby(user_date_statistic_key, "pullrequest-count", @pullrequest_count.to_i) + end + + $redis_cache.hgetall(user_date_statistic_key) + end + + def set_user_rank + set_user_statistic + follow_count = $redis_cache.hget(user_date_statistic_key, "follow-count") || 0 + pullrequest_count = $redis_cache.hget(user_date_statistic_key, "pullrequest-count") || 0 + issues_count = $redis_cache.hget(user_date_statistic_key, "issue-count") || 0 + project_count = $redis_cache.hget(user_date_statistic_key, "project-count") || 0 + fork_count = $redis_cache.hget(user_date_statistic_key, "fork-count") || 0 + project_watchers_count = $redis_cache.hget(user_date_statistic_key, "project-watcher-count") || 0 + project_praises_count = $redis_cache.hget(user_date_statistic_key, "project-praise-count") || 0 + project_languages_count = $redis_cache.hget(user_date_statistic_key, "project-language").nil? ? 0 : $redis_cache.hget(user_date_statistic_key, "project-language").length + # 影响力 + influence = (60.0 + follow_count.to_i / (follow_count.to_i + 20.0) * 40.0).to_i + + # 贡献度 + contribution = (60.0 + pullrequest_count.to_i / (pullrequest_count.to_i + 20.0) * 40.0).to_i + + # 活跃度 + activity = (60.0 + issues_count.to_i / (issues_count.to_i + 80.0) * 40.0).to_i + + # 项目经验 + experience = 10 * project_count.to_i + 5 * fork_count.to_i + project_watchers_count.to_i + project_praises_count.to_i + experience = (60.0 + experience / (experience + 100.0) * 40.0).to_i + # 语言能力 + language = (60.0 + project_languages_count.to_i / (project_languages_count.to_i + 5.0) * 40.0).to_i + + score = influence+ contribution + activity + experience + language + + $redis_cache.zadd(user_rank_key, score, @user_id) if score.to_i > 300 + + $redis_cache.zscore(user_rank_key, @user_id) + end +end \ No newline at end of file diff --git a/app/services/cache/v2/user_statistic_service.rb b/app/services/cache/v2/user_statistic_service.rb index 0df49c8ae..8caa66d88 100644 --- a/app/services/cache/v2/user_statistic_service.rb +++ b/app/services/cache/v2/user_statistic_service.rb @@ -76,60 +76,77 @@ class Cache::V2::UserStatisticService < ApplicationService if @follow_count.present? if $redis_cache.hget(user_statistic_key, follow_count_key).nil? reset_user_follow_count + Cache::V2::UserDateRankService.call(@user_id, Date.today, {follow_count: @follow_count}) else $redis_cache.hincrby(user_statistic_key, follow_count_key, @follow_count) + Cache::V2::UserDateRankService.call(@user_id, Date.today, {follow_count: @follow_count}) end end if @fork_count.present? if $redis_cache.hget(user_statistic_key, fork_count_key).nil? reset_user_fork_count + Cache::V2::UserDateRankService.call(@user_id, Date.today, {fork_count: @fork_count}) else $redis_cache.hincrby(user_statistic_key, fork_count_key, @fork_count) + Cache::V2::UserDateRankService.call(@user_id, Date.today, {fork_count: @fork_count}) end end if @issue_count.present? if $redis_cache.hget(user_statistic_key, issue_count_key).nil? reset_user_issue_count + Cache::V2::UserDateRankService.call(@user_id, Date.today, {issue_count: @issue_count}) else $redis_cache.hincrby(user_statistic_key, issue_count_key, @issue_count) + Cache::V2::UserDateRankService.call(@user_id, Date.today, {issue_count: @issue_count}) end end if @project_count.present? if $redis_cache.hget(user_statistic_key, project_count_key).nil? reset_user_project_count + Cache::V2::UserDateRankService.call(@user_id, Date.today, {project_count: @project_count}) else $redis_cache.hincrby(user_statistic_key, project_count_key, @project_count) + Cache::V2::UserDateRankService.call(@user_id, Date.today, {project_count: @project_count}) end end if @project_language_count_key.present? && project_language_count.present? if $redis_cache.hget(user_statistic_key, project_language_key).nil? reset_user_project_language + Cache::V2::UserDateRankService.call(@user_id, Date.today, {project_language_count_key: @project_language_count_key, project_language_count: @project_language_count}) else result = JSON.parse($redis_cache.hget(user_statistic_key, project_language_key)) result[@project_language_count_key] ||= 0 result[@project_language_count_key] += project_language_count.to_i + result.delete(@project_language_count_key) if result[@project_language_count_key] == 0 $redis_cache.hset(user_statistic_key, project_language_key, result.to_json) + Cache::V2::UserDateRankService.call(@user_id, Date.today, {project_language_count_key: @project_language_count_key, project_language_count: @project_language_count}) end end if @project_praise_count.present? if $redis_cache.hget(user_statistic_key, project_praise_count_key).nil? reset_user_project_praise_count + Cache::V2::UserDateRankService.call(@user_id, Date.today, {project_praise_count: @project_praise_count}) else $redis_cache.hincrby(user_statistic_key, project_praise_count_key, @project_praise_count) + Cache::V2::UserDateRankService.call(@user_id, Date.today, {project_praise_count: @project_praise_count}) end end if @project_watcher_count.present? if $redis_cache.hget(user_statistic_key, project_watcher_count_key).nil? reset_user_project_watcher_count + Cache::V2::UserDateRankService.call(@user_id, Date.today, {project_watcher_count: @project_watcher_count}) else $redis_cache.hincrby(user_statistic_key, project_watcher_count_key, @project_watcher_count) + Cache::V2::UserDateRankService.call(@user_id, Date.today, {project_watcher_count: @project_watcher_count}) end end if @pullrequest_count.present? if $redis_cache.hget(user_statistic_key, pullrequest_count_key).nil? reset_user_pullrequest_count + Cache::V2::UserDateRankService.call(@user_id, Date.today, {pullrequest_count: @pullrequest_count}) else $redis_cache.hincrby(user_statistic_key, pullrequest_count_key, @pullrequest_count) + Cache::V2::UserDateRankService.call(@user_id, Date.today, {pullrequest_count: @pullrequest_count}) end end $redis_cache.hgetall(user_statistic_key)