| @@ -85,19 +85,19 @@ class Users::StatisticsController < Users::BaseController | |||
| for key in @project_languages_count.keys do | |||
| @languages_percent[key] = (@project_languages_count[key].to_f / @project_languages_count.values.sum).round(2) | |||
| end | |||
| @platform_languages_percent = Hash.new | |||
| for key in @platform_project_languages_count.keys do | |||
| @platform_languages_percent[key] = (@platform_project_languages_count[key].to_f / @platform_project_languages_count.values.sum).round(2) | |||
| end | |||
| # @platform_languages_percent = Hash.new | |||
| # for key in @platform_project_languages_count.keys do | |||
| # @platform_languages_percent[key] = (@platform_project_languages_count[key].to_f / @platform_project_languages_count.values.sum).round(2) | |||
| # end | |||
| # 各门语言分数 | |||
| @each_language_score = Hash.new | |||
| for key in @project_languages_count.keys do | |||
| @each_language_score[key] = (60.0 + @project_languages_count[key] / (@project_languages_count[key] + 5.0) * 40.0).to_i | |||
| end | |||
| @platform_each_language_score = Hash.new | |||
| for key in @platform_project_languages_count.keys do | |||
| @platform_each_language_score[key] = (60.0 + @platform_project_languages_count[key] / (@platform_project_languages_count[key] + 5.0) * 40.0).to_i | |||
| end | |||
| # @platform_each_language_score = Hash.new | |||
| # for key in @platform_project_languages_count.keys do | |||
| # @platform_each_language_score[key] = (60.0 + @platform_project_languages_count[key] / (@platform_project_languages_count[key] + 5.0) * 40.0).to_i | |||
| # end | |||
| end | |||
| end | |||
| @@ -164,124 +164,54 @@ class Users::StatisticsController < Users::BaseController | |||
| def preload_develop_data | |||
| if params[:start_time].present? && params[:end_time].present? | |||
| # 用户被follow数量 | |||
| @follow_count = Rails.cache.fetch("user-follow-count-#{observed_user.id}-start:#{params[:start_time]}-end:#{params[:end_time]}-#{Watcher.where(watchable: observed_user).maximum('created_at').to_i}") do | |||
| time_filter(Watcher.where(watchable: observed_user), 'created_at').count | |||
| end | |||
| @platform_follow_count = Rails.cache.fetch("platform-follow-count-start:#{params[:start_time]}-end:#{params[:end_time]}-#{Watcher.where(watchable_type: 'User').maximum('created_at').to_i}") do | |||
| time_filter(Watcher.where(watchable_type: 'User'), 'created_at').count | |||
| end | |||
| @follow_count = time_filter(Watcher.where(watchable: observed_user), 'created_at').count | |||
| @platform_follow_count = time_filter(Watcher.where(watchable_type: 'User'), 'created_at').count | |||
| # 用户pr数量 | |||
| @pullrequest_count = Rails.cache.fetch("user-pullrequest-count-#{observed_user.id}-start:#{params[:start_time]}-end:#{params[:end_time]}-#{PullRequest.where(user_id: observed_user.id).maximum('created_at').to_i}") do | |||
| time_filter(PullRequest.where(user_id: observed_user.id), 'created_at').count | |||
| end | |||
| @platform_pullrequest_count = Rails.cache.fetch("platform-pullrequest-count-start:#{params[:start_time]}-end:#{params[:end_time]}-#{PullRequest.maximum('created_at').to_i}") do | |||
| time_filter(PullRequest, 'created_at').count | |||
| end | |||
| @pullrequest_count = time_filter(PullRequest.where(user_id: observed_user.id), 'created_at').count | |||
| @platform_pullrequest_count = time_filter(PullRequest, 'created_at').count | |||
| # 用户issue数量 | |||
| @issues_count = Rails.cache.fetch("user-issue-count-#{observed_user.id}-start:#{params[:start_time]}-end:#{params[:end_time]}-#{Issue.where(author_id: observed_user.id).maximum('created_on').to_i}") do | |||
| time_filter(Issue.where(author_id: observed_user.id), 'created_on').count | |||
| end | |||
| @platform_issues_count = Rails.cache.fetch("platform-issue-count-start:#{params[:start_time]}-end:#{params[:end_time]}-#{Issue.maximum('created_on').to_i}") do | |||
| time_filter(Issue, 'created_on').count | |||
| end | |||
| @issues_count = time_filter(Issue.where(author_id: observed_user.id), 'created_on').count | |||
| @platform_issues_count = time_filter(Issue, 'created_on').count | |||
| # 用户总项目数 | |||
| @project_count = Rails.cache.fetch("user-project-count-#{observed_user.id}-start:#{params[:start_time]}-end:#{params[:end_time]}-#{Project.where(user_id:observed_user.id).maximum('created_on').to_i}") do | |||
| time_filter(Project.where(user_id:observed_user.id), 'created_on').count | |||
| end | |||
| @platform_project_count = Rails.cache.fetch("platform-project-count-start:#{params[:start_time]}-end:#{params[:end_time]}-#{Project.maximum('created_on').to_i}") do | |||
| time_filter(Project, 'created_on').count | |||
| end | |||
| @project_count = time_filter(Project.where(user_id:observed_user.id), 'created_on').count | |||
| @platform_project_count = time_filter(Project, 'created_on').count | |||
| # 用户项目被fork数量 | |||
| @fork_count = Rails.cache.fetch("user-fork-count-#{observed_user.id}-start:#{params[:start_time]}-end:#{params[:end_time]}-#{ForkUser.joins(:project).where(projects: {user_id: observed_user.id}).maximum('created_at').to_i}") do | |||
| time_filter(Project.where(user_id:observed_user.id), 'created_on').sum("forked_count") | |||
| end | |||
| @platform_fork_count = Rails.cache.fetch("platform-fork-count-start:#{params[:start_time]}-end:#{params[:end_time]}-#{ForkUser.maximum('created_at').to_i}") do | |||
| time_filter(Project, 'created_on').sum("forked_count") | |||
| end | |||
| @fork_count = time_filter(Project.where(user_id:observed_user.id), 'created_on').sum("forked_count") | |||
| @platform_fork_count = time_filter(Project, 'created_on').sum("forked_count") | |||
| # 用户项目关注数 | |||
| @project_watchers_count = Rails.cache.fetch("user-watchers-count-start:#{params[:start_time]}-end:#{params[:end_time]}-#{observed_user.id}-#{Watcher.where(watchable_type: 'Project', watchable_id: observed_user.projects).maximum('created_at').to_i}") do | |||
| time_filter(Project.where(user_id: observed_user.id), 'created_on').sum("watchers_count") | |||
| end | |||
| @platform_project_watchers_count = Rails.cache.fetch("platform-watchers-count-start:#{params[:start_time]}-end:#{params[:end_time]}-#{Watcher.where(watchable_type: 'Project').maximum('created_at').to_i}") do | |||
| time_filter(Project, 'created_on').sum("watchers_count") | |||
| end | |||
| @project_watchers_count = time_filter(Project.where(user_id: observed_user.id), 'created_on').sum("watchers_count") | |||
| @platform_project_watchers_count = time_filter(Project, 'created_on').sum("watchers_count") | |||
| # 用户项目点赞数 | |||
| @project_praises_count = Rails.cache.fetch("user-praises-count-#{observed_user.id}-start:#{params[:start_time]}-end:#{params[:end_time]}-#{PraiseTread.where(praise_tread_object_type: 'Project', praise_tread_object_id: observed_user.projects).maximum('created_at').to_i}") do | |||
| time_filter(Project.where(user_id: observed_user.id), 'created_on').sum("praises_count") | |||
| end | |||
| @platform_project_praises_count = Rails.cache.fetch("user-praises-count-#{observed_user.id}-start:#{params[:start_time]}-end:#{params[:end_time]}-#{PraiseTread.where(praise_tread_object_type: 'Project', praise_tread_object_id: observed_user.projects).maximum('created_at').to_i}") do | |||
| time_filter(Project, 'created_on').sum("praises_count") | |||
| end | |||
| @project_praises_count = time_filter(Project.where(user_id: observed_user.id), 'created_on').sum("praises_count") | |||
| @platform_project_praises_count = time_filter(Project, 'created_on').sum("praises_count") | |||
| # 用户不同语言项目数量 | |||
| @project_languages_count = Rails.cache.fetch("user-languages-count-#{observed_user.id}-start:#{params[:start_time]}-end:#{params[:end_time]}-#{Project.where(user_id:observed_user.id).maximum('created_on').to_i}") do | |||
| time_filter(Project.where(user_id: observed_user.id), 'created_on').joins(:project_language).group("project_languages.name").count | |||
| end | |||
| @platform_project_languages_count = Rails.cache.fetch("platform-languages-count-start:#{params[:start_time]}-end:#{params[:end_time]}-#{Project.maximum('created_on').to_i}") do | |||
| time_filter(Project, 'created_on').joins(:project_language).group("project_languages.name").count | |||
| end | |||
| @project_languages_count = time_filter(Project.where(user_id: observed_user.id), 'created_on').joins(:project_language).group("project_languages.name").count | |||
| @platform_project_languages_count = time_filter(Project, 'created_on').joins(:project_language).group("project_languages.name").count | |||
| else | |||
| # 用户被follow数量 | |||
| @follow_count = Rails.cache.fetch("user-follow-count-#{observed_user.id}-#{Watcher.where(watchable: observed_user).maximum('created_at').to_i}") do | |||
| time_filter(Watcher.where(watchable: observed_user), 'created_at').count | |||
| end | |||
| @platform_follow_count = Rails.cache.fetch("platform-follow-count-#{Watcher.where(watchable_type: 'User').maximum('created_at').to_i}") do | |||
| time_filter(Watcher.where(watchable_type: 'User'), 'created_at').count | |||
| end | |||
| @follow_count = Cache::UserFollowCountService.call(observed_user) | |||
| @platform_follow_count = Cache::PlatformFollowCountService.call | |||
| # 用户pr数量 | |||
| @pullrequest_count = Rails.cache.fetch("user-pullrequest-count-#{observed_user.id}-#{PullRequest.where(user_id: observed_user.id).maximum('created_at').to_i}") do | |||
| time_filter(PullRequest.where(user_id: observed_user.id), 'created_at').count | |||
| end | |||
| @platform_pullrequest_count = Rails.cache.fetch("platform-pullrequest-count-#{PullRequest.maximum('created_at').to_i}") do | |||
| time_filter(PullRequest, 'created_at').count | |||
| end | |||
| @pullrequest_count = Cache::UserPullrequestCountService.call(observed_user) | |||
| @platform_pullrequest_count = Cache::PlatformPullrequestCountService.call | |||
| # 用户issue数量 | |||
| @issues_count = Rails.cache.fetch("user-issue-count-#{observed_user.id}-#{Issue.where(author_id: observed_user.id).maximum('created_on').to_i}") do | |||
| time_filter(Issue.where(author_id: observed_user.id), 'created_on').count | |||
| end | |||
| @platform_issues_count = Rails.cache.fetch("platform-issue-count-#{Issue.maximum('created_on').to_i}") do | |||
| time_filter(Issue, 'created_on').count | |||
| end | |||
| @issues_count = Cache::UserIssueCountService.call(observed_user) | |||
| @platform_issues_count = Cache::PlatformIssueCountService.call | |||
| # 用户总项目数 | |||
| @project_count = Rails.cache.fetch("user-project-count-#{observed_user.id}-#{Project.where(user_id:observed_user.id).maximum('created_on').to_i}") do | |||
| time_filter(Project.where(user_id:observed_user.id), 'created_on').count | |||
| end | |||
| @platform_project_count = Rails.cache.fetch("platform-project-count-#{Project.maximum('created_on').to_i}") do | |||
| time_filter(Project, 'created_on').count | |||
| end | |||
| @project_count = Cache::UserProjectCountService.call(observed_user) | |||
| @platform_project_count = Cache::PlatformProjectCountService.call | |||
| # 用户项目被fork数量 | |||
| @fork_count = Rails.cache.fetch("user-fork-count-#{observed_user.id}-#{ForkUser.joins(:project).where(projects: {user_id: observed_user.id}).maximum('created_at').to_i}") do | |||
| time_filter(Project.where(user_id:observed_user.id), 'created_on').sum("forked_count") | |||
| end | |||
| @platform_fork_count = Rails.cache.fetch("platform-fork-count-#{ForkUser.maximum('created_at').to_i}") do | |||
| time_filter(Project, 'created_on').sum("forked_count") | |||
| end | |||
| @fork_count = Cache::UserProjectForkCountService.call(observed_user) | |||
| @platform_fork_count = Cache::PlatformProjectForkCountService.call | |||
| # 用户项目关注数 | |||
| @project_watchers_count = Rails.cache.fetch("user-watchers-count-#{observed_user.id}-#{Watcher.where(watchable_type: 'Project', watchable_id: observed_user.projects).maximum('created_at').to_i}") do | |||
| time_filter(Project.where(user_id: observed_user.id), 'created_on').sum("watchers_count") | |||
| end | |||
| @platform_project_watchers_count = Rails.cache.fetch("platform-watchers-count-#{Watcher.where(watchable_type: 'Project').maximum('created_at').to_i}") do | |||
| time_filter(Project, 'created_on').sum("watchers_count") | |||
| end | |||
| @project_watchers_count = Cache::UserProjectWatchersCountService.call(observed_user) | |||
| @platform_project_watchers_count = Cache::PlatformProjectWatchersCountService.call | |||
| # 用户项目点赞数 | |||
| @project_praises_count = Rails.cache.fetch("user-praises-count-#{observed_user.id}-#{PraiseTread.where(praise_tread_object_type: 'Project', praise_tread_object_id: observed_user.projects).maximum('created_at').to_i}") do | |||
| time_filter(Project.where(user_id: observed_user.id), 'created_on').sum("praises_count") | |||
| end | |||
| @platform_project_praises_count = Rails.cache.fetch("user-praises-count-#{observed_user.id}-#{PraiseTread.where(praise_tread_object_type: 'Project', praise_tread_object_id: observed_user.projects).maximum('created_at').to_i}") do | |||
| time_filter(Project, 'created_on').sum("praises_count") | |||
| end | |||
| @project_praises_count = Cache::UserProjectPraisesCountService.call(observed_user) | |||
| @platform_project_praises_count = Cache::PlatformProjectPraisesCountService.call | |||
| # 用户不同语言项目数量 | |||
| @project_languages_count = Rails.cache.fetch("user-languages-count-#{observed_user.id}-#{Project.where(user_id:observed_user.id).maximum('created_on').to_i}") do | |||
| time_filter(Project.where(user_id: observed_user.id), 'created_on').joins(:project_language).group("project_languages.name").count | |||
| end | |||
| @platform_project_languages_count = Rails.cache.fetch("platform-languages-count-#{Project.maximum('created_on').to_i}") do | |||
| time_filter(Project, 'created_on').joins(:project_language).group("project_languages.name").count | |||
| end | |||
| @project_languages_count = Cache::UserProjectLanguagesCountService.call(observed_user) | |||
| @platform_project_languages_count = Cache::PlatformProjectLanguagesCountService.call | |||
| end | |||
| end | |||
| end | |||
| @@ -0,0 +1,14 @@ | |||
| class ResetPlatformCacheJob < ApplicationJob | |||
| queue_as :cache | |||
| def perform | |||
| Cache::PlatformFollowCountService.new.reset | |||
| Cache::PlatformIssueCountService.new.reset | |||
| Cache::PlatformProjectCountService.new.reset | |||
| Cache::PlatformProjectForkCountService.new.reset | |||
| Cache::PlatformProjectLanguagesCountService.new.reset | |||
| Cache::PlatformProjectPraisesCountService.new.reset | |||
| Cache::PlatformProjectWatchersCountService.new.reset | |||
| Cache::PlatformPullrequestCountService.new.reset | |||
| end | |||
| end | |||
| @@ -0,0 +1,14 @@ | |||
| class ResetUserCacheJob < ApplicationJob | |||
| queue_as :cache | |||
| def perform(user) | |||
| Cache::UserFollowCountService.new(user).reset | |||
| Cache::UserIssueCountService.new(user).reset | |||
| Cache::UserProjectCountService.new(user).reset | |||
| Cache::UserProjectForkCountService.new(user).reset | |||
| Cache::UserProjectLanguagesCountService.new(user).reset | |||
| Cache::UserProjectPraisesCountService.new(user).reset | |||
| Cache::UserProjectWatchersCountService.new(user).reset | |||
| Cache::UserPullrequestCountService.new(user).reset | |||
| end | |||
| end | |||
| @@ -16,4 +16,12 @@ class ApplicationRecord < ActiveRecord::Base | |||
| def allow_sync_to_trustie? | |||
| Rails.env.production? && EduSetting.get('host_name') == 'https://www.educoder.net' | |||
| end | |||
| def reset_user_cache_async_job(user) | |||
| ResetUserCacheJob.perform_later(user) | |||
| end | |||
| def reset_platform_cache_async_job | |||
| ResetPlatformCacheJob.perform_later | |||
| end | |||
| end | |||
| @@ -20,4 +20,12 @@ 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 | |||
| def reset_cache_data | |||
| self.reset_platform_cache_async_job | |||
| self.reset_user_cache_async_job(self.project.owner) | |||
| end | |||
| end | |||
| @@ -76,8 +76,13 @@ class Issue < ApplicationRecord | |||
| scope :issue_index_includes, ->{includes(:tracker, :priority, :version, :issue_status, :journals,:issue_tags,user: :user_extension)} | |||
| after_update :change_versions_count | |||
| after_destroy :update_closed_issues_count_in_project! | |||
| after_save :reset_cache_data | |||
| after_destroy :update_closed_issues_count_in_project!, :reset_cache_data | |||
| def reset_cache_data | |||
| self.reset_platform_cache_async_job | |||
| self.reset_user_cache_async_job(self.user) | |||
| end | |||
| def get_assign_user | |||
| User&.find_by_id(self.assigned_to_id) if self.assigned_to_id.present? | |||
| @@ -1,26 +1,35 @@ | |||
| # == Schema Information | |||
| # | |||
| # Table name: praise_treads | |||
| # | |||
| # id :integer not null, primary key | |||
| # user_id :integer not null | |||
| # praise_tread_object_id :integer | |||
| # praise_tread_object_type :string(255) | |||
| # praise_or_tread :integer default("1") | |||
| # created_at :datetime not null | |||
| # updated_at :datetime not null | |||
| # | |||
| # Indexes | |||
| # | |||
| # praise_tread (praise_tread_object_id,praise_tread_object_type) | |||
| # | |||
| # == Schema Information | |||
| # | |||
| # Table name: praise_treads | |||
| # | |||
| # id :integer not null, primary key | |||
| # user_id :integer not null | |||
| # praise_tread_object_id :integer | |||
| # praise_tread_object_type :string(255) | |||
| # praise_or_tread :integer default("1") | |||
| # created_at :datetime not null | |||
| # updated_at :datetime not null | |||
| # | |||
| # Indexes | |||
| # | |||
| # praise_tread (praise_tread_object_id,praise_tread_object_type) | |||
| # | |||
| class PraiseTread < ApplicationRecord | |||
| belongs_to :user | |||
| 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 | |||
| def reset_cache_data | |||
| self.reset_platform_cache_async_job | |||
| if self.praise_tread_object.is_a?(Project) | |||
| self.reset_user_cache_async_job(self.praise_tread_object&.owner) | |||
| end | |||
| end | |||
| def send_tiding | |||
| case self.praise_tread_object_type | |||
| @@ -105,7 +105,8 @@ class Project < ApplicationRecord | |||
| has_many :pinned_projects, dependent: :destroy | |||
| has_many :has_pinned_users, through: :pinned_projects, source: :user | |||
| after_save :check_project_members | |||
| after_save :check_project_members, :reset_cache_data | |||
| after_destroy :reset_cache_data | |||
| 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) } | |||
| @@ -113,6 +114,14 @@ class Project < ApplicationRecord | |||
| delegate :content, to: :project_detail, allow_nil: true | |||
| delegate :name, to: :license, prefix: true, allow_nil: true | |||
| def reset_cache_data | |||
| if changes[:user_id].present? | |||
| first_owner = Owner.find_by_id(changes[:user_id].first) | |||
| self.reset_user_cache_async_job(first_owner) | |||
| end | |||
| self.reset_platform_cache_async_job | |||
| self.reset_user_cache_async_job(self.owner) | |||
| end | |||
| def self.search_project(search) | |||
| ransack(name_or_identifier_cont: search) | |||
| @@ -33,6 +33,14 @@ class PullRequest < ApplicationRecord | |||
| has_many :project_trends, as: :trend, dependent: :destroy | |||
| has_many :attachments, as: :container, dependent: :destroy | |||
| after_save :reset_cache_data | |||
| after_destroy :reset_cache_data | |||
| def reset_cache_data | |||
| self.reset_platform_cache_async_job | |||
| self.reset_user_cache_async_job(self.user) | |||
| end | |||
| def fork_project | |||
| Project.find_by(id: self.fork_project_id) | |||
| end | |||
| @@ -15,10 +15,24 @@ | |||
| # watchers_user_id_type (user_id,watchable_type) | |||
| # | |||
| class Watcher < ApplicationRecord | |||
| belongs_to :user | |||
| belongs_to :watchable, polymorphic: true, counter_cache: :watchers_count | |||
| scope :watching_users, ->(watchable_id){ where("watchable_type = ? and user_id = ?",'User',watchable_id)} | |||
| end | |||
| class Watcher < ApplicationRecord | |||
| belongs_to :user | |||
| belongs_to :watchable, polymorphic: true, counter_cache: :watchers_count | |||
| scope :watching_users, ->(watchable_id){ where("watchable_type = ? and user_id = ?",'User',watchable_id)} | |||
| after_save :reset_cache_data | |||
| after_destroy :reset_cache_data | |||
| def reset_cache_data | |||
| if self.watchable.is_a?(User) | |||
| self.reset_user_cache_async_job(self.watchable) | |||
| end | |||
| if self.watchable.is_a?(Project) | |||
| self.reset_user_cache_async_job(self.watchable&.owner) | |||
| end | |||
| self.reset_platform_cache_async_job | |||
| end | |||
| end | |||
| @@ -0,0 +1,41 @@ | |||
| class Cache::PlatformFollowCountService < ApplicationService | |||
| attr_reader :increment_count | |||
| def initialize(increment_count=0) | |||
| @increment_count = increment_count | |||
| end | |||
| def call | |||
| set_platform_follow_count | |||
| platform_follow_count | |||
| end | |||
| def reset | |||
| reset_platform_follow_count | |||
| platform_follow_count | |||
| end | |||
| private | |||
| def platform_follow_count_key | |||
| "platform-follow-count" | |||
| end | |||
| def platform_follow_count | |||
| $redis_cache.get(platform_follow_count_key).to_i | |||
| end | |||
| def set_platform_follow_count | |||
| if $redis_cache.exists(platform_follow_count_key) | |||
| $redis_cache.incrby(platform_follow_count_key, increment_count) | |||
| else | |||
| reset_platform_follow_count | |||
| end | |||
| end | |||
| def reset_platform_follow_count | |||
| $redis_cache.set(platform_follow_count_key, Watcher.where(watchable_type: 'User').count) | |||
| end | |||
| end | |||
| @@ -0,0 +1,41 @@ | |||
| class Cache::PlatformIssueCountService < ApplicationService | |||
| attr_reader :increment_count | |||
| def initialize(increment_count=0) | |||
| @increment_count = increment_count | |||
| end | |||
| def call | |||
| set_platform_issue_count | |||
| platform_issue_count | |||
| end | |||
| def reset | |||
| reset_platform_issue_count | |||
| platform_issue_count | |||
| end | |||
| private | |||
| def platform_issue_count_key | |||
| "platform-issue-count" | |||
| end | |||
| def platform_issue_count | |||
| $redis_cache.get(platform_issue_count_key).to_i | |||
| end | |||
| def set_platform_issue_count | |||
| if $redis_cache.exists(platform_issue_count_key) | |||
| $redis_cache.incrby(platform_issue_count_key, increment_count) | |||
| else | |||
| reset_platform_issue_count | |||
| end | |||
| end | |||
| def reset_platform_issue_count | |||
| $redis_cache.set(platform_issue_count_key, Issue.count) | |||
| end | |||
| end | |||
| @@ -0,0 +1,41 @@ | |||
| class Cache::PlatformProjectCountService < ApplicationService | |||
| attr_reader :increment_count | |||
| def initialize(increment_count=0) | |||
| @increment_count = increment_count | |||
| end | |||
| def call | |||
| set_platform_project_count | |||
| platform_project_count | |||
| end | |||
| def reset | |||
| reset_platform_project_count | |||
| platform_project_count | |||
| end | |||
| private | |||
| def platform_project_count_key | |||
| "platform-project-count" | |||
| end | |||
| def platform_project_count | |||
| $redis_cache.get(platform_project_count_key).to_i | |||
| end | |||
| def set_platform_project_count | |||
| if $redis_cache.exists(platform_project_count_key) | |||
| $redis_cache.incrby(platform_project_count_key, increment_count) | |||
| else | |||
| reset_platform_project_count | |||
| end | |||
| end | |||
| def reset_platform_project_count | |||
| $redis_cache.set(platform_project_count_key, Project.count) | |||
| end | |||
| end | |||
| @@ -0,0 +1,41 @@ | |||
| class Cache::PlatformProjectForkCountService < ApplicationService | |||
| attr_reader :increment_count | |||
| def initialize(increment_count=0) | |||
| @increment_count = increment_count | |||
| end | |||
| def call | |||
| set_platform_project_fork_count | |||
| platform_project_fork_count | |||
| end | |||
| def reset | |||
| reset_platform_project_fork_count | |||
| platform_project_fork_count | |||
| end | |||
| private | |||
| def platform_project_fork_count_key | |||
| "platform-project-fork-count" | |||
| end | |||
| def platform_project_fork_count | |||
| $redis_cache.get(platform_project_fork_count_key).to_i | |||
| end | |||
| def set_platform_project_fork_count | |||
| if $redis_cache.exists(platform_project_fork_count_key) | |||
| $redis_cache.incrby(platform_project_fork_count_key, increment_count) | |||
| else | |||
| reset_platform_project_fork_count | |||
| end | |||
| end | |||
| def reset_platform_project_fork_count | |||
| $redis_cache.set(platform_project_fork_count_key, ForkUser.count) | |||
| end | |||
| end | |||
| @@ -0,0 +1,57 @@ | |||
| class Cache::PlatformProjectLanguagesCountService < ApplicationService | |||
| attr_reader :key, :increment_count | |||
| def initialize(key=nil, increment_count=0) | |||
| @key = key | |||
| @increment_count = increment_count | |||
| end | |||
| def call | |||
| set_platform_project_language_count | |||
| platform_project_language_count | |||
| end | |||
| def reset_by_key | |||
| reset_platform_project_language_count_by_key | |||
| platform_project_language_count | |||
| end | |||
| def reset | |||
| reset_platform_project_language_count | |||
| platform_project_language_count | |||
| end | |||
| private | |||
| def platform_project_language_count_key | |||
| "platform-project-language-count" | |||
| end | |||
| def platform_project_language_count | |||
| $redis_cache.hgetall(platform_project_language_count_key).transform_values(&:to_i) | |||
| end | |||
| def set_platform_project_language_count | |||
| if $redis_cache.hlen(platform_project_language_count_key) == 0 | |||
| reset_platform_project_language_count | |||
| elsif $redis_cache.hget(platform_project_language_count_key, key).nil? | |||
| reset_platform_project_language_count_by_key | |||
| else | |||
| $redis_cache.hincrby(platform_project_language_count_key, key, increment_count) | |||
| end | |||
| end | |||
| def reset_platform_project_language_count_by_key | |||
| return if key.nil? | |||
| $redis_cache.hset(platform_project_language_count_key, key, Project.joins(:project_language).where(project_languages: {name: key}).count) | |||
| end | |||
| def reset_platform_project_language_count | |||
| Project.joins(:project_language).group("project_languages.name").count.each do |k, v| | |||
| $redis_cache.hset(platform_project_language_count_key, k, v) | |||
| end | |||
| end | |||
| end | |||
| @@ -0,0 +1,41 @@ | |||
| class Cache::PlatformProjectPraisesCountService < ApplicationService | |||
| attr_reader :increment_count | |||
| def initialize(increment_count=0) | |||
| @increment_count = increment_count | |||
| end | |||
| def call | |||
| set_platform_project_praises_count | |||
| platform_project_praises_count | |||
| end | |||
| def reset | |||
| reset_platform_project_praises_count | |||
| platform_project_praises_count | |||
| end | |||
| private | |||
| def platform_project_praises_count_key | |||
| "platform-project-praises-count" | |||
| end | |||
| def platform_project_praises_count | |||
| $redis_cache.get(platform_project_praises_count_key).to_i | |||
| end | |||
| def set_platform_project_praises_count | |||
| if $redis_cache.exists(platform_project_praises_count_key) | |||
| $redis_cache.incrby(platform_project_praises_count_key, increment_count) | |||
| else | |||
| reset_platform_project_praises_count | |||
| end | |||
| end | |||
| def reset_platform_project_praises_count | |||
| $redis_cache.set(platform_project_praises_count_key, PraiseTread.where(praise_tread_object_type: "Project").count) | |||
| end | |||
| end | |||
| @@ -0,0 +1,41 @@ | |||
| class Cache::PlatformProjectWatchersCountService < ApplicationService | |||
| attr_reader :increment_count | |||
| def initialize(increment_count=0) | |||
| @increment_count = increment_count | |||
| end | |||
| def call | |||
| set_platform_project_watchers_count | |||
| platform_project_watchers_count | |||
| end | |||
| def reset | |||
| reset_platform_project_watchers_count | |||
| platform_project_watchers_count | |||
| end | |||
| private | |||
| def platform_project_watchers_count_key | |||
| "platform-project-watchers-count" | |||
| end | |||
| def platform_project_watchers_count | |||
| $redis_cache.get(platform_project_watchers_count_key).to_i | |||
| end | |||
| def set_platform_project_watchers_count | |||
| if $redis_cache.exists(platform_project_watchers_count_key) | |||
| $redis_cache.incrby(platform_project_watchers_count_key, increment_count) | |||
| else | |||
| reset_platform_project_watchers_count | |||
| end | |||
| end | |||
| def reset_platform_project_watchers_count | |||
| $redis_cache.set(platform_project_watchers_count_key, Watcher.where(watchable_type: 'Project').count) | |||
| end | |||
| end | |||
| @@ -0,0 +1,41 @@ | |||
| class Cache::PlatformPullrequestCountService < ApplicationService | |||
| attr_reader :increment_count | |||
| def initialize(increment_count=0) | |||
| @increment_count = increment_count | |||
| end | |||
| def call | |||
| set_platform_pullrequest_count | |||
| platform_pullrequest_count | |||
| end | |||
| def reset | |||
| reset_platform_pullrequest_count | |||
| platform_pullrequest_count | |||
| end | |||
| private | |||
| def platform_pullrequest_count_key | |||
| "platform-pullrequest-count" | |||
| end | |||
| def platform_pullrequest_count | |||
| $redis_cache.get(platform_pullrequest_count_key).to_i | |||
| end | |||
| def set_platform_pullrequest_count | |||
| if $redis_cache.exists(platform_pullrequest_count_key) | |||
| $redis_cache.incrby(platform_pullrequest_count_key, increment_count) | |||
| else | |||
| reset_platform_pullrequest_count | |||
| end | |||
| end | |||
| def reset_platform_pullrequest_count | |||
| $redis_cache.set(platform_pullrequest_count_key, PullRequest.count) | |||
| end | |||
| end | |||
| @@ -0,0 +1,43 @@ | |||
| class Cache::UserFollowCountService < ApplicationService | |||
| attr_reader :user, :increment_count | |||
| def initialize(user, increment_count=0) | |||
| @user = user | |||
| @increment_count = increment_count | |||
| end | |||
| def call | |||
| set_user_follow_count | |||
| user_follow_count | |||
| end | |||
| def reset | |||
| reset_user_follow_count | |||
| user_follow_count | |||
| end | |||
| private | |||
| def user_follow_count_key | |||
| "user-follow-count-#{user.id}" | |||
| end | |||
| def user_follow_count | |||
| $redis_cache.get(user_follow_count_key).to_i | |||
| end | |||
| def set_user_follow_count | |||
| if $redis_cache.exists(user_follow_count_key) | |||
| $redis_cache.incrby(user_follow_count_key, increment_count) | |||
| else | |||
| reset_user_follow_count | |||
| end | |||
| end | |||
| def reset_user_follow_count | |||
| return if user.nil? | |||
| $redis_cache.set(user_follow_count_key, Watcher.where(watchable: user).count) | |||
| end | |||
| end | |||
| @@ -0,0 +1,43 @@ | |||
| class Cache::UserIssueCountService < ApplicationService | |||
| attr_reader :user, :increment_count | |||
| def initialize(user, increment_count=0) | |||
| @user = user | |||
| @increment_count = increment_count | |||
| end | |||
| def call | |||
| set_user_issue_count | |||
| user_issue_count | |||
| end | |||
| def reset | |||
| reset_user_issue_count | |||
| user_issue_count | |||
| end | |||
| private | |||
| def user_issue_count_key | |||
| "user-issue-count-#{user.id}" | |||
| end | |||
| def user_issue_count | |||
| $redis_cache.get(user_issue_count_key).to_i | |||
| end | |||
| def set_user_issue_count | |||
| if $redis_cache.exists(user_issue_count_key) | |||
| $redis_cache.incrby(user_issue_count_key, increment_count) | |||
| else | |||
| reset_user_issue_count | |||
| end | |||
| end | |||
| def reset_user_issue_count | |||
| return if user.nil? | |||
| $redis_cache.set(user_issue_count_key, Issue.where(author_id: user.id).count) | |||
| end | |||
| end | |||
| @@ -0,0 +1,43 @@ | |||
| class Cache::UserProjectCountService < ApplicationService | |||
| attr_reader :user, :increment_count | |||
| def initialize(user, increment_count=0) | |||
| @user = user | |||
| @increment_count = increment_count | |||
| end | |||
| def call | |||
| set_user_project_count | |||
| user_project_count | |||
| end | |||
| def reset | |||
| reset_user_project_count | |||
| user_project_count | |||
| end | |||
| private | |||
| def user_project_count_key | |||
| "user-project-count-#{user.id}" | |||
| end | |||
| def user_project_count | |||
| $redis_cache.get(user_project_count_key).to_i | |||
| end | |||
| def set_user_project_count | |||
| if $redis_cache.exists(user_project_count_key) | |||
| $redis_cache.incrby(user_project_count_key, increment_count) | |||
| else | |||
| reset_user_project_count | |||
| end | |||
| end | |||
| def reset_user_project_count | |||
| return if user.nil? | |||
| $redis_cache.set(user_project_count_key, user.projects.count) | |||
| end | |||
| end | |||
| @@ -0,0 +1,43 @@ | |||
| class Cache::UserProjectForkCountService < ApplicationService | |||
| attr_reader :user, :increment_count | |||
| def initialize(user, increment_count=0) | |||
| @user = user | |||
| @increment_count = increment_count | |||
| end | |||
| def call | |||
| set_user_project_fork_count | |||
| user_project_fork_count | |||
| end | |||
| def reset | |||
| reset_user_project_fork_count | |||
| user_project_fork_count | |||
| end | |||
| private | |||
| def user_project_fork_count_key | |||
| "user-project-fork-count-#{user.id}" | |||
| end | |||
| def user_project_fork_count | |||
| $redis_cache.get(user_project_fork_count_key).to_i | |||
| end | |||
| def set_user_project_fork_count | |||
| if $redis_cache.exists(user_project_fork_count_key) | |||
| $redis_cache.incrby(user_project_fork_count_key, increment_count) | |||
| else | |||
| reset_user_project_fork_count | |||
| end | |||
| end | |||
| def reset_user_project_fork_count | |||
| return if user.nil? | |||
| $redis_cache.set(user_project_fork_count_key, ForkUser.joins(:project).where(projects: {user_id: user.id}).count) | |||
| end | |||
| end | |||
| @@ -0,0 +1,60 @@ | |||
| class Cache::UserProjectLanguagesCountService < ApplicationService | |||
| attr_reader :user, :key, :increment_count | |||
| def initialize(user, key=nil, increment_count=0) | |||
| @user = user | |||
| @key = key | |||
| @increment_count = increment_count | |||
| end | |||
| def call | |||
| set_user_project_language_count | |||
| user_project_language_count | |||
| end | |||
| def reset_by_key | |||
| reset_user_project_language_count_by_key | |||
| user_project_language_count | |||
| end | |||
| def reset | |||
| reset_user_project_language_count | |||
| user_project_language_count | |||
| end | |||
| private | |||
| def user_project_language_count_key | |||
| "user-project-language-count-#{user.id}" | |||
| end | |||
| def user_project_language_count | |||
| $redis_cache.hgetall(user_project_language_count_key).transform_values(&:to_i) | |||
| end | |||
| def set_user_project_language_count | |||
| if $redis_cache.hlen(user_project_language_count_key) == 0 | |||
| reset_user_project_language_count | |||
| elsif $redis_cache.hget(user_project_language_count_key, key).nil? | |||
| reset_user_project_language_count_by_key | |||
| else | |||
| $redis_cache.hincrby(user_project_language_count_key, key, increment_count) | |||
| end | |||
| end | |||
| def reset_user_project_language_count_by_key | |||
| return if user.nil? | |||
| return if key.nil? | |||
| $redis_cache.hset(user_project_language_count_key, key, user.projects.joins(:project_language).where(project_languages: {name: key}).count) | |||
| end | |||
| def reset_user_project_language_count | |||
| return if user.nil? | |||
| user.projects.joins(:project_language).group("project_languages.name").count.each do |k, v| | |||
| $redis_cache.hset(user_project_language_count_key, k, v) | |||
| end | |||
| end | |||
| end | |||
| @@ -0,0 +1,43 @@ | |||
| class Cache::UserProjectPraisesCountService < ApplicationService | |||
| attr_reader :user, :increment_count | |||
| def initialize(user, increment_count=0) | |||
| @user = user | |||
| @increment_count = increment_count | |||
| end | |||
| def call | |||
| set_user_project_praises_count | |||
| user_project_praises_count | |||
| end | |||
| def reset | |||
| reset_user_project_praises_count | |||
| user_project_praises_count | |||
| end | |||
| private | |||
| def user_project_praises_count_key | |||
| "user-project-praises-count-#{user.id}" | |||
| end | |||
| def user_project_praises_count | |||
| $redis_cache.get(user_project_praises_count_key).to_i | |||
| end | |||
| def set_user_project_praises_count | |||
| if $redis_cache.exists(user_project_praises_count_key) | |||
| $redis_cache.incrby(user_project_praises_count_key, increment_count) | |||
| else | |||
| reset_user_project_praises_count | |||
| end | |||
| end | |||
| def reset_user_project_praises_count | |||
| return if user.nil? | |||
| $redis_cache.set(user_project_praises_count_key, PraiseTread.where(praise_tread_object_type: 'Project', praise_tread_object_id: user.projects).count) | |||
| end | |||
| end | |||
| @@ -0,0 +1,43 @@ | |||
| class Cache::UserProjectWatchersCountService < ApplicationService | |||
| attr_reader :user, :increment_count | |||
| def initialize(user, increment_count=0) | |||
| @user = user | |||
| @increment_count = increment_count | |||
| end | |||
| def call | |||
| set_user_project_watchers_count | |||
| user_project_watchers_count | |||
| end | |||
| def reset | |||
| reset_user_project_watchers_count | |||
| user_project_watchers_count | |||
| end | |||
| private | |||
| def user_project_watchers_count_key | |||
| "user-project-watchers-count-#{user.id}" | |||
| end | |||
| def user_project_watchers_count | |||
| $redis_cache.get(user_project_watchers_count_key).to_i | |||
| end | |||
| def set_user_project_watchers_count | |||
| if $redis_cache.exists(user_project_watchers_count_key) | |||
| $redis_cache.incrby(user_project_watchers_count_key, increment_count) | |||
| else | |||
| reset_user_project_watchers_count | |||
| end | |||
| end | |||
| def reset_user_project_watchers_count | |||
| return if user.nil? | |||
| $redis_cache.set(user_project_watchers_count_key, Watcher.where(watchable_type: 'Project', watchable_id: user.projects).count) | |||
| end | |||
| end | |||
| @@ -0,0 +1,43 @@ | |||
| class Cache::UserPullrequestCountService < ApplicationService | |||
| attr_reader :user, :increment_count | |||
| def initialize(user, increment_count=0) | |||
| @user = user | |||
| @increment_count = increment_count | |||
| end | |||
| def call | |||
| set_user_pullrequest_count | |||
| user_pullrequest_count | |||
| end | |||
| def reset | |||
| reset_user_pullrequest_count | |||
| user_pullrequest_count | |||
| end | |||
| private | |||
| def user_pullrequest_count_key | |||
| "user-pullrequest-count-#{user.id}" | |||
| end | |||
| def user_pullrequest_count | |||
| $redis_cache.get(user_pullrequest_count_key).to_i | |||
| end | |||
| def set_user_pullrequest_count | |||
| if $redis_cache.exists(user_pullrequest_count_key) | |||
| $redis_cache.incrby(user_pullrequest_count_key, increment_count) | |||
| else | |||
| reset_user_pullrequest_count | |||
| end | |||
| end | |||
| def reset_user_pullrequest_count | |||
| return if user.nil? | |||
| $redis_cache.set(user_pullrequest_count_key, PullRequest.where(user_id: user.id).count) | |||
| end | |||
| end | |||
| @@ -4,8 +4,8 @@ json.platform do | |||
| json.activity @platform_activity | |||
| json.experience @platform_experience | |||
| json.language @platform_language | |||
| json.languages_percent @platform_languages_percent | |||
| json.each_language_score @platform_each_language_score | |||
| # json.languages_percent @platform_languages_percent | |||
| # json.each_language_score @platform_each_language_score | |||
| end | |||
| json.user do | |||
| @@ -0,0 +1,3 @@ | |||
| redis_config = Rails.application.config_for(:redis) | |||
| cache_url = redis_config["url"] || 'redis://localhost:6379' | |||
| $redis_cache = Redis.new(url: cache_url, db: 2) | |||
| @@ -7,3 +7,4 @@ | |||
| - [searchkick, 10] | |||
| - [notify, 100] | |||
| - [mailers, 101] | |||
| - [cache, 10] | |||