From d5a65112ca46edb72be908970af58cd95c205c59 Mon Sep 17 00:00:00 2001 From: "vilet.yy" Date: Mon, 7 Jun 2021 14:17:12 +0800 Subject: [PATCH 1/3] fix: statistics develop load slowly --- .../users/statistics_controller.rb | 145 +++++++++++++++--- 1 file changed, 121 insertions(+), 24 deletions(-) diff --git a/app/controllers/users/statistics_controller.rb b/app/controllers/users/statistics_controller.rb index d508176e2..255f0e948 100644 --- a/app/controllers/users/statistics_controller.rb +++ b/app/controllers/users/statistics_controller.rb @@ -162,29 +162,126 @@ class Users::StatisticsController < Users::BaseController end def preload_develop_data - # 用户被follow数量 - @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 = time_filter(PullRequest.where(user_id: observed_user.id), 'created_at').count - @platform_pullrequest_count = time_filter(PullRequest, 'created_at').count - # 用户issue数量 - @issues_count = time_filter(Issue.where(author_id: observed_user.id), 'created_on').count - @platform_issues_count = time_filter(Issue, 'created_on').count - # 用户总项目数 @fork_count + @project_watchers_count + @project_praises_count - @project_count = filter_member_projects_by_role("Owner").count - @platform_project_count = time_filter(Project, 'created_on').count - # 用户项目被fork数量 - @fork_count = filter_member_projects_by_role("Owner").sum("forked_count") - @platform_fork_count = time_filter(Project, 'created_on').sum("forked_count") - # 用户项目关注数 - @project_watchers_count = filter_member_projects_by_role("Owner").sum("watchers_count") - @platform_project_watchers_count = time_filter(Project, 'created_on').sum("watchers_count") - # 用户项目点赞数 - @project_praises_count = filter_member_projects_by_role("Owner").sum("praises_count") - @platform_project_praises_count = time_filter(Project, 'created_on').sum("praises_count") - # 用户不同语言项目数量 - @project_languages_count = filter_member_projects_by_role("Owner").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 + 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 + # 用户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 + # 用户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 + # 用户总项目数 + @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 + # 用户项目被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 + + # 用户项目关注数 + @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_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_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 + 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 + # 用户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 + # 用户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 + # 用户总项目数 + @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 + # 用户项目被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 + + # 用户项目关注数 + @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_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_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 + end end end \ No newline at end of file From a5b98bcad442a3c7fa6137c27ec4fad0df73134c Mon Sep 17 00:00:00 2001 From: "vilet.yy" Date: Mon, 7 Jun 2021 14:34:51 +0800 Subject: [PATCH 2/3] fix: concat nickname --- .../admins/auth_schools_controller.rb | 2 +- .../admins/laboratories_controller.rb | 4 +- app/controllers/members_controller.rb | 2 +- .../organization_users_controller.rb | 2 +- app/controllers/users_controller.rb | 2 +- .../users_for_private_messages_controller.rb | 2 +- app/controllers/weapps/courses_controller.rb | 4 +- app/controllers/zips_controller.rb | 4 +- app/models/attachment.rb | 80 +++++++++---------- app/models/user.rb | 2 +- app/queries/admins/apply_item_bank_query.rb | 2 +- .../admins/apply_user_authentication_query.rb | 2 +- app/queries/admins/course_list_query.rb | 2 +- app/queries/admins/course_query.rb | 2 +- app/queries/admins/laboratory_shixun_query.rb | 2 +- .../admins/laboratory_subject_query.rb | 2 +- app/queries/admins/subject_query.rb | 2 +- app/queries/admins/user_query.rb | 4 +- app/queries/user_query.rb | 2 +- app/services/admins/import_user_service.rb | 2 +- 20 files changed, 63 insertions(+), 63 deletions(-) diff --git a/app/controllers/admins/auth_schools_controller.rb b/app/controllers/admins/auth_schools_controller.rb index e7c0253b6..825c04cdc 100644 --- a/app/controllers/admins/auth_schools_controller.rb +++ b/app/controllers/admins/auth_schools_controller.rb @@ -32,7 +32,7 @@ class Admins::AuthSchoolsController < Admins::BaseController def search_manager school = School.find_by(id: params[:school_id]) user_ids = school&.ec_school_users&.pluck(:user_id) - @users = User.where.not(id: user_ids).where("concat(lastname, firstname) like ?", "%#{params[:name].strip.to_s}%").limit(10) + @users = User.where.not(id: user_ids).where("CONCAT_WS(lastname, firstname, nickname) like ?", "%#{params[:name].strip.to_s}%").limit(10) end # 添加认证学校管理员 diff --git a/app/controllers/admins/laboratories_controller.rb b/app/controllers/admins/laboratories_controller.rb index f397b0f34..46463fed0 100644 --- a/app/controllers/admins/laboratories_controller.rb +++ b/app/controllers/admins/laboratories_controller.rb @@ -32,7 +32,7 @@ class Admins::LaboratoriesController < Admins::BaseController keyword = params[:keyword].to_s.strip if keyword.present? - like_sql = 'shixuns.name LIKE :keyword OR CONCAT(users.lastname, users.firstname) LIKE :keyword '\ + like_sql = 'shixuns.name LIKE :keyword OR CONCAT_WS(users.lastname, users.firstname, users.nickname) LIKE :keyword '\ 'OR mirror_repositories.name LIKE :keyword' shixuns = shixuns.joins(:user, :mirror_repositories).where(like_sql, keyword: "%#{keyword}%") end @@ -48,7 +48,7 @@ class Admins::LaboratoriesController < Admins::BaseController keyword = params[:keyword].to_s.strip if keyword.present? - like_sql = 'subjects.name LIKE :keyword OR CONCAT(users.lastname, users.firstname) LIKE :keyword' + like_sql = 'subjects.name LIKE :keyword OR CONCAT_WS(users.lastname, users.firstname, users.nickname) LIKE :keyword' subjects = subjects.joins(:user).where(like_sql, keyword: "%#{keyword}%") end diff --git a/app/controllers/members_controller.rb b/app/controllers/members_controller.rb index bea4a0d29..fea57fa72 100644 --- a/app/controllers/members_controller.rb +++ b/app/controllers/members_controller.rb @@ -18,7 +18,7 @@ class MembersController < ApplicationController scope = @project.members.includes(:roles, user: :user_extension) search = params[:search].to_s.downcase role = params[:role].to_s - scope = scope.joins(:user).where("LOWER(concat(users.lastname, users.firstname, users.login, users.mail)) LIKE ?", "%#{search.split(" ").join('|')}%") if search.present? + scope = scope.joins(:user).where("LOWER(CONCAT_WS(users.lastname, users.firstname, users.login, users.mail, users.nickname)) LIKE ?", "%#{search.split(" ").join('|')}%") if search.present? scope = scope.joins(:roles).where("roles.name LIKE ?", "%#{role}%") if role.present? @total_count = scope.size diff --git a/app/controllers/organizations/organization_users_controller.rb b/app/controllers/organizations/organization_users_controller.rb index 8a7755a1b..d2d6963a4 100644 --- a/app/controllers/organizations/organization_users_controller.rb +++ b/app/controllers/organizations/organization_users_controller.rb @@ -5,7 +5,7 @@ class Organizations::OrganizationUsersController < Organizations::BaseController def index @organization_users = @organization.organization_users.includes(:user) search = params[:search].to_s.downcase - @organization_users = @organization_users.joins(:user).where("LOWER(CONCAT_WS(users.lastname, users.firstname, users.login, users.mail)) LIKE ?", "%#{search.split(" ").join('|')}%") if search.present? + @organization_users = @organization_users.joins(:user).where("LOWER(CONCAT_WS(users.lastname, users.firstname, users.login, users.mail, users.nickname)) LIKE ?", "%#{search.split(" ").join('|')}%") if search.present? @organization_users = kaminari_paginate(@organization_users) end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 237ce9e6a..163b5849f 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -63,7 +63,7 @@ class UsersController < ApplicationController def fan_users watchers = @user.watchers.includes(:user).order("watchers.created_at desc") - watchers = watchers.joins(:user).where("LOWER(concat(users.lastname, users.firstname, users.login)) LIKE ?", "%#{params[:search].split(" ").join('|')}%") if params[:search].present? + watchers = watchers.joins(:user).where("LOWER(CONCAT_WS(users.lastname, users.firstname, users.login, users.nickname)) LIKE ?", "%#{params[:search].split(" ").join('|')}%") if params[:search].present? @watchers_count = watchers.size @watchers = paginate(watchers) diff --git a/app/controllers/users_for_private_messages_controller.rb b/app/controllers/users_for_private_messages_controller.rb index 610bf5d52..3a53a1af8 100644 --- a/app/controllers/users_for_private_messages_controller.rb +++ b/app/controllers/users_for_private_messages_controller.rb @@ -11,7 +11,7 @@ class UsersForPrivateMessagesController < ApplicationController return end - users = users.where('LOWER(concat(lastname, firstname, nickname)) LIKE ?', "%#{keyword}%") + users = users.where('LOWER(CONCAT_WS(lastname, firstname, nickname)) LIKE ?', "%#{keyword}%") @users = users.limit(10).includes(:user_extension) end diff --git a/app/controllers/weapps/courses_controller.rb b/app/controllers/weapps/courses_controller.rb index ccd5d1bcf..72782f5cc 100644 --- a/app/controllers/weapps/courses_controller.rb +++ b/app/controllers/weapps/courses_controller.rb @@ -86,7 +86,7 @@ class Weapps::CoursesController < Weapps::BaseController end if search.present? - @teacher_list = @teacher_list.joins(:user).where("LOWER(CONCAT(users.lastname, users.firstname)) like ?", "%#{search}%") + @teacher_list = @teacher_list.joins(:user).where("LOWER(CONCAT_WS(users.lastname, users.firstname, users.nickname)) like ?", "%#{search}%") end @teacher_list_size = @teacher_list.size @@ -127,7 +127,7 @@ class Weapps::CoursesController < Weapps::BaseController @students = CourseMember.students(@course) if search.present? - @students = @students.joins(user: :user_extension).where("LOWER(CONCAT(users.lastname, users.firstname)) like ? or + @students = @students.joins(user: :user_extension).where("LOWER(CONCAT_WS(users.lastname, users.firstname, users.nickname)) like ? or user_extensions.student_id like ?", "%#{search}%", "%#{search}%") end diff --git a/app/controllers/zips_controller.rb b/app/controllers/zips_controller.rb index 3ed1eb8b3..0c226211f 100644 --- a/app/controllers/zips_controller.rb +++ b/app/controllers/zips_controller.rb @@ -86,7 +86,7 @@ class ZipsController < ApplicationController #搜索 if params[:search].present? - @ex_users = @ex_users.joins(user: :user_extension).where("CONCAT(lastname, firstname) like ? OR student_id like ?", "%#{params[:search]}%", "%#{params[:search]}%") + @ex_users = @ex_users.joins(user: :user_extension).where("CONCAT_WS(lastname, firstname, nickname) like ? OR student_id like ?", "%#{params[:search]}%", "%#{params[:search]}%") end default_ex_users_size = @ex_users&.size @@ -130,7 +130,7 @@ class ZipsController < ApplicationController end unless params[:search].blank? - @all_student_works = @all_student_works.joins(user: :user_extension).where("concat(lastname, firstname) like ? + @all_student_works = @all_student_works.joins(user: :user_extension).where("CONCAT_WS(lastname, firstname, nickname) like ? or student_id like ?", "%#{params[:search]}%", "%#{params[:search]}%") end diff --git a/app/models/attachment.rb b/app/models/attachment.rb index 461c835fe..3bfc9a8ce 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -1,42 +1,42 @@ -# == Schema Information -# -# Table name: attachments -# -# id :integer not null, primary key -# container_id :integer -# container_type :string(30) -# filename :string(255) default(""), not null -# disk_filename :string(255) default(""), not null -# filesize :integer default("0"), not null -# content_type :string(255) default("") -# digest :string(60) default(""), not null -# downloads :integer default("0"), not null -# author_id :integer default("0"), not null -# created_on :datetime -# description :text(65535) -# disk_directory :string(255) -# attachtype :integer default("1") -# is_public :integer default("1") -# copy_from :integer -# quotes :integer default("0") -# is_publish :integer default("1") -# publish_time :datetime -# resource_bank_id :integer -# unified_setting :boolean default("1") -# cloud_url :string(255) default("") -# course_second_category_id :integer default("0") -# delay_publish :boolean default("0") -# -# Indexes -# -# index_attachments_on_author_id (author_id) -# index_attachments_on_container_id_and_container_type (container_id,container_type) -# index_attachments_on_course_second_category_id (course_second_category_id) -# index_attachments_on_created_on (created_on) -# index_attachments_on_is_public (is_public) -# index_attachments_on_quotes (quotes) -# - +# == Schema Information +# +# Table name: attachments +# +# id :integer not null, primary key +# container_id :integer +# container_type :string(30) +# filename :string(255) default(""), not null +# disk_filename :string(255) default(""), not null +# filesize :integer default("0"), not null +# content_type :string(255) default("") +# digest :string(60) default(""), not null +# downloads :integer default("0"), not null +# author_id :integer default("0"), not null +# created_on :datetime +# description :text(65535) +# disk_directory :string(255) +# attachtype :integer default("1") +# is_public :integer default("1") +# copy_from :integer +# quotes :integer default("0") +# is_publish :integer default("1") +# publish_time :datetime +# resource_bank_id :integer +# unified_setting :boolean default("1") +# cloud_url :string(255) default("") +# course_second_category_id :integer default("0") +# delay_publish :boolean default("0") +# +# Indexes +# +# index_attachments_on_author_id (author_id) +# index_attachments_on_container_id_and_container_type (container_id,container_type) +# index_attachments_on_course_second_category_id (course_second_category_id) +# index_attachments_on_created_on (created_on) +# index_attachments_on_is_public (is_public) +# index_attachments_on_quotes (quotes) +# + class Attachment < ApplicationRecord include BaseModel include Publicable @@ -51,7 +51,7 @@ class Attachment < ApplicationRecord # 二级目录 # belongs_to :course_second_category, optional: true - scope :by_filename_or_user_name, -> (keywords) { joins(:author).where("filename like :search or LOWER(concat(users.lastname, users.firstname)) LIKE :search", + scope :by_filename_or_user_name, -> (keywords) { joins(:author).where("filename like :search or LOWER(CONCAT_WS(users.lastname, users.firstname, users.nickname)) LIKE :search", :search => "%#{keywords.split(" ").join('|')}%") unless keywords.blank? } scope :by_keywords, -> (keywords) { where("filename LIKE ?", "%#{keywords.split(" ").join('|')}%") unless keywords.blank? } scope :ordered, -> (opts = {}) { order("#{opts[:sort_type]} #{opts[:sort] == 1 ? 'asc': 'desc'}") } diff --git a/app/models/user.rb b/app/models/user.rb index 99b1e7cd7..9d917f4c8 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -170,7 +170,7 @@ class User < Owner # Groups and active users scope :active, lambda { where(status: STATUS_ACTIVE) } scope :like, lambda { |keywords| - sql = "CONCAT(lastname, firstname) LIKE :search OR login LIKE :search OR mail LIKE :search OR nickname LIKE :search" + sql = "CONCAT_WS(lastname, firstname, nickname) LIKE :search OR login LIKE :search OR mail LIKE :search OR nickname LIKE :search" where(sql, :search => "%#{keywords.split(" ").join('|')}%") unless keywords.blank? } diff --git a/app/queries/admins/apply_item_bank_query.rb b/app/queries/admins/apply_item_bank_query.rb index 37b6d25a6..922d136ef 100644 --- a/app/queries/admins/apply_item_bank_query.rb +++ b/app/queries/admins/apply_item_bank_query.rb @@ -26,7 +26,7 @@ class Admins::ApplyItemBankQuery < ApplicationQuery keyword = params[:keyword].to_s.strip if keyword.present? applies = applies.joins(user: { user_extension: :school }) - .where('CONCAT(lastname,firstname) LIKE :keyword OR schools.name LIKE :keyword', keyword: "%#{keyword}%") + .where('CONCAT_WS(lastname,firstname, nickname) LIKE :keyword OR schools.name LIKE :keyword', keyword: "%#{keyword}%") end custom_sort(applies, params[:sort_by], params[:sort_direction]) diff --git a/app/queries/admins/apply_user_authentication_query.rb b/app/queries/admins/apply_user_authentication_query.rb index a4d1c2a4a..60b781d1c 100644 --- a/app/queries/admins/apply_user_authentication_query.rb +++ b/app/queries/admins/apply_user_authentication_query.rb @@ -26,7 +26,7 @@ class Admins::ApplyUserAuthenticationQuery < ApplicationQuery keyword = params[:keyword].to_s.strip if keyword.present? applies = applies.joins(user: { user_extension: :school }) - .where('CONCAT(lastname,firstname) LIKE :keyword OR schools.name LIKE :keyword', keyword: "%#{keyword}%") + .where('CONCAT_WS(lastname,firstname,nickname) LIKE :keyword OR schools.name LIKE :keyword', keyword: "%#{keyword}%") end custom_sort(applies, params[:sort_by], params[:sort_direction]) diff --git a/app/queries/admins/course_list_query.rb b/app/queries/admins/course_list_query.rb index 24eedaf1b..b97a9cebe 100644 --- a/app/queries/admins/course_list_query.rb +++ b/app/queries/admins/course_list_query.rb @@ -19,7 +19,7 @@ class Admins::CourseListQuery < ApplicationQuery case search_type when "0" course_lists = course_lists.joins(:user) - .where('CONCAT(lastname, firstname) like :keyword', keyword: "%#{keyword}%") + .where('CONCAT_WS(lastname, firstname, nickname) like :keyword', keyword: "%#{keyword}%") when "1" course_lists = course_lists.where('name like :keyword', keyword: "%#{keyword}%") end diff --git a/app/queries/admins/course_query.rb b/app/queries/admins/course_query.rb index e883650d0..b1466e22f 100644 --- a/app/queries/admins/course_query.rb +++ b/app/queries/admins/course_query.rb @@ -35,7 +35,7 @@ class Admins::CourseQuery < ApplicationQuery # 关键字 keyword = params[:keyword].to_s.strip if keyword - sql = 'CONCAT(lastname, firstname) LIKE :keyword OR courses.name LIKE :keyword OR course_lists.name LIKE :keyword' + sql = 'CONCAT_WS(lastname, firstname, nickname) LIKE :keyword OR courses.name LIKE :keyword OR course_lists.name LIKE :keyword' courses = courses.joins(:teacher, :course_list).where(sql, keyword: "%#{keyword}%") end diff --git a/app/queries/admins/laboratory_shixun_query.rb b/app/queries/admins/laboratory_shixun_query.rb index d489aed21..9b9bf6a21 100644 --- a/app/queries/admins/laboratory_shixun_query.rb +++ b/app/queries/admins/laboratory_shixun_query.rb @@ -11,7 +11,7 @@ class Admins::LaboratoryShixunQuery < ApplicationQuery keyword = params[:keyword].to_s.strip if keyword.present? - like_sql = 'shixuns.name LIKE :keyword OR CONCAT(users.lastname, users.firstname) LIKE :keyword' + like_sql = 'shixuns.name LIKE :keyword OR CONCAT_WS(users.lastname, users.firstname, users.nickname) LIKE :keyword' laboratory_shixuns = laboratory_shixuns.joins(shixun: :user).where(like_sql, keyword: "%#{keyword}%") end diff --git a/app/queries/admins/laboratory_subject_query.rb b/app/queries/admins/laboratory_subject_query.rb index 316ea9743..619aeb2c2 100644 --- a/app/queries/admins/laboratory_subject_query.rb +++ b/app/queries/admins/laboratory_subject_query.rb @@ -11,7 +11,7 @@ class Admins::LaboratorySubjectQuery < ApplicationQuery keyword = params[:keyword].to_s.strip if keyword.present? - like_sql = 'subjects.name LIKE :keyword OR CONCAT(users.lastname, users.firstname) LIKE :keyword' + like_sql = 'subjects.name LIKE :keyword OR CONCAT_WS(users.lastname, users.firstname, users.nickname) LIKE :keyword' laboratory_subjects = laboratory_subjects.joins(subject: :user).where(like_sql, keyword: "%#{keyword}%") end diff --git a/app/queries/admins/subject_query.rb b/app/queries/admins/subject_query.rb index 0415deb9f..df2ac40ef 100644 --- a/app/queries/admins/subject_query.rb +++ b/app/queries/admins/subject_query.rb @@ -40,7 +40,7 @@ class Admins::SubjectQuery < ApplicationQuery # 关键字 keyword = params[:keyword].to_s.strip if keyword - sql = 'CONCAT(lastname, firstname) LIKE :keyword OR subjects.name LIKE :keyword' + sql = 'CONCAT_WS(lastname, firstname, nickname) LIKE :keyword OR subjects.name LIKE :keyword' subjects = subjects.joins(:user).where(sql, keyword: "%#{keyword}%") end diff --git a/app/queries/admins/user_query.rb b/app/queries/admins/user_query.rb index cdf158c67..670eef58d 100644 --- a/app/queries/admins/user_query.rb +++ b/app/queries/admins/user_query.rb @@ -30,14 +30,14 @@ class Admins::UserQuery < ApplicationQuery # 关键字检索 keyword = params[:keyword].to_s.strip.presence if keyword - sql = 'CONCAT(lastname, firstname) LIKE :keyword OR login LIKE :keyword OR mail LIKE :keyword OR phone LIKE :keyword' + sql = 'CONCAT_WS(lastname, firstname, nickname) LIKE :keyword OR login LIKE :keyword OR mail LIKE :keyword OR phone LIKE :keyword' users = users.where(sql, keyword: "%#{keyword}%") end # 姓名 name = params[:name].to_s.strip.presence if name.present? - users = users.where('CONCAT(lastname, firstname) LIKE :name', name: "%#{name}%") + users = users.where('CONCAT_WS(lastname, firstname, nickname) LIKE :name', name: "%#{name}%") end # 单位ID diff --git a/app/queries/user_query.rb b/app/queries/user_query.rb index 326665fe9..f2964c7dc 100644 --- a/app/queries/user_query.rb +++ b/app/queries/user_query.rb @@ -10,7 +10,7 @@ class UserQuery < ApplicationQuery # 真实姓名 if name = strip_param(:name) - users = users.where('LOWER(CONCAT(users.lastname, users.firstname)) LIKE ?', "%#{name.downcase}%") + users = users.where('LOWER(CONCAT_WS(users.lastname, users.firstname, users.nickname)) LIKE ?', "%#{name.downcase}%") end # 单位名称 diff --git a/app/services/admins/import_user_service.rb b/app/services/admins/import_user_service.rb index f3408051b..298bf1e16 100644 --- a/app/services/admins/import_user_service.rb +++ b/app/services/admins/import_user_service.rb @@ -84,7 +84,7 @@ class Admins::ImportUserService < ApplicationService if data.identity.to_i == 1 users = users.where(user_extensions: { student_id: data.student_id }) else - users = users.where(user_extensions: { technical_title: data.technical_title }).where('CONCAT(users.lastname,users.firstname) = ?', data.name) + users = users.where(user_extensions: { technical_title: data.technical_title }).where('CONCAT_WS(users.lastname,users.firstname,users.nickname) = ?', data.name) end users.first From 02fe8a8966d8cdc895875bd5586fcd0c84c96f40 Mon Sep 17 00:00:00 2001 From: "vilet.yy" Date: Tue, 8 Jun 2021 11:51:46 +0800 Subject: [PATCH 3/3] add: user and platform cache --- .../users/statistics_controller.rb | 150 +++++------------- app/jobs/reset_platform_cache_job.rb | 14 ++ app/jobs/reset_user_cache_job.rb | 14 ++ app/models/application_record.rb | 8 + app/models/fork_user.rb | 8 + app/models/issue.rb | 7 +- app/models/praise_tread.rb | 43 +++-- app/models/project.rb | 11 +- app/models/pull_request.rb | 8 + app/models/watcher.rb | 28 +++- .../cache/platform_follow_count_service.rb | 41 +++++ .../cache/platform_issue_count_service.rb | 41 +++++ .../cache/platform_project_count_service.rb | 41 +++++ .../platform_project_fork_count_service.rb | 41 +++++ ...latform_project_languages_count_service.rb | 57 +++++++ .../platform_project_praises_count_service.rb | 41 +++++ ...platform_project_watchers_count_service.rb | 41 +++++ .../platform_pullrequest_count_service.rb | 41 +++++ .../cache/user_follow_count_service.rb | 43 +++++ .../cache/user_issue_count_service.rb | 43 +++++ .../cache/user_project_count_service.rb | 43 +++++ .../cache/user_project_fork_count_service.rb | 43 +++++ .../user_project_languages_count_service.rb | 60 +++++++ .../user_project_praises_count_service.rb | 43 +++++ .../user_project_watchers_count_service.rb | 43 +++++ .../cache/user_pullrequest_count_service.rb | 43 +++++ .../users/statistics/develop.json.jbuilder | 4 +- config/initializers/redis_cache.rb | 3 + config/sidekiq.yml | 1 + 29 files changed, 866 insertions(+), 138 deletions(-) create mode 100644 app/jobs/reset_platform_cache_job.rb create mode 100644 app/jobs/reset_user_cache_job.rb create mode 100644 app/services/cache/platform_follow_count_service.rb create mode 100644 app/services/cache/platform_issue_count_service.rb create mode 100644 app/services/cache/platform_project_count_service.rb create mode 100644 app/services/cache/platform_project_fork_count_service.rb create mode 100644 app/services/cache/platform_project_languages_count_service.rb create mode 100644 app/services/cache/platform_project_praises_count_service.rb create mode 100644 app/services/cache/platform_project_watchers_count_service.rb create mode 100644 app/services/cache/platform_pullrequest_count_service.rb create mode 100644 app/services/cache/user_follow_count_service.rb create mode 100644 app/services/cache/user_issue_count_service.rb create mode 100644 app/services/cache/user_project_count_service.rb create mode 100644 app/services/cache/user_project_fork_count_service.rb create mode 100644 app/services/cache/user_project_languages_count_service.rb create mode 100644 app/services/cache/user_project_praises_count_service.rb create mode 100644 app/services/cache/user_project_watchers_count_service.rb create mode 100644 app/services/cache/user_pullrequest_count_service.rb create mode 100644 config/initializers/redis_cache.rb diff --git a/app/controllers/users/statistics_controller.rb b/app/controllers/users/statistics_controller.rb index 255f0e948..c6cc0d889 100644 --- a/app/controllers/users/statistics_controller.rb +++ b/app/controllers/users/statistics_controller.rb @@ -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 \ No newline at end of file diff --git a/app/jobs/reset_platform_cache_job.rb b/app/jobs/reset_platform_cache_job.rb new file mode 100644 index 000000000..bdc5b6aaa --- /dev/null +++ b/app/jobs/reset_platform_cache_job.rb @@ -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 \ No newline at end of file diff --git a/app/jobs/reset_user_cache_job.rb b/app/jobs/reset_user_cache_job.rb new file mode 100644 index 000000000..3562475ce --- /dev/null +++ b/app/jobs/reset_user_cache_job.rb @@ -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 \ No newline at end of file diff --git a/app/models/application_record.rb b/app/models/application_record.rb index 888c65423..3ab2b11e8 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -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 diff --git a/app/models/fork_user.rb b/app/models/fork_user.rb index d0915f152..0936f6bfa 100644 --- a/app/models/fork_user.rb +++ b/app/models/fork_user.rb @@ -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 diff --git a/app/models/issue.rb b/app/models/issue.rb index 58607ebdd..b43f5a3d2 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -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? diff --git a/app/models/praise_tread.rb b/app/models/praise_tread.rb index d13c17ddd..04008eaf6 100644 --- a/app/models/praise_tread.rb +++ b/app/models/praise_tread.rb @@ -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 diff --git a/app/models/project.rb b/app/models/project.rb index f41e980d7..b61f05689 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -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) diff --git a/app/models/pull_request.rb b/app/models/pull_request.rb index c3eefb0db..97f4a9164 100644 --- a/app/models/pull_request.rb +++ b/app/models/pull_request.rb @@ -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 diff --git a/app/models/watcher.rb b/app/models/watcher.rb index 76a82702b..ccc8eefaa 100644 --- a/app/models/watcher.rb +++ b/app/models/watcher.rb @@ -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 diff --git a/app/services/cache/platform_follow_count_service.rb b/app/services/cache/platform_follow_count_service.rb new file mode 100644 index 000000000..5398da5a8 --- /dev/null +++ b/app/services/cache/platform_follow_count_service.rb @@ -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 \ No newline at end of file diff --git a/app/services/cache/platform_issue_count_service.rb b/app/services/cache/platform_issue_count_service.rb new file mode 100644 index 000000000..95511f480 --- /dev/null +++ b/app/services/cache/platform_issue_count_service.rb @@ -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 \ No newline at end of file diff --git a/app/services/cache/platform_project_count_service.rb b/app/services/cache/platform_project_count_service.rb new file mode 100644 index 000000000..d807682f1 --- /dev/null +++ b/app/services/cache/platform_project_count_service.rb @@ -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 \ No newline at end of file diff --git a/app/services/cache/platform_project_fork_count_service.rb b/app/services/cache/platform_project_fork_count_service.rb new file mode 100644 index 000000000..21d6c0636 --- /dev/null +++ b/app/services/cache/platform_project_fork_count_service.rb @@ -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 \ No newline at end of file diff --git a/app/services/cache/platform_project_languages_count_service.rb b/app/services/cache/platform_project_languages_count_service.rb new file mode 100644 index 000000000..2b4f0fae4 --- /dev/null +++ b/app/services/cache/platform_project_languages_count_service.rb @@ -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 \ No newline at end of file diff --git a/app/services/cache/platform_project_praises_count_service.rb b/app/services/cache/platform_project_praises_count_service.rb new file mode 100644 index 000000000..6c78b8022 --- /dev/null +++ b/app/services/cache/platform_project_praises_count_service.rb @@ -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 \ No newline at end of file diff --git a/app/services/cache/platform_project_watchers_count_service.rb b/app/services/cache/platform_project_watchers_count_service.rb new file mode 100644 index 000000000..70c834d2e --- /dev/null +++ b/app/services/cache/platform_project_watchers_count_service.rb @@ -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 \ No newline at end of file diff --git a/app/services/cache/platform_pullrequest_count_service.rb b/app/services/cache/platform_pullrequest_count_service.rb new file mode 100644 index 000000000..c497c488a --- /dev/null +++ b/app/services/cache/platform_pullrequest_count_service.rb @@ -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 \ No newline at end of file diff --git a/app/services/cache/user_follow_count_service.rb b/app/services/cache/user_follow_count_service.rb new file mode 100644 index 000000000..c583c32b6 --- /dev/null +++ b/app/services/cache/user_follow_count_service.rb @@ -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 \ No newline at end of file diff --git a/app/services/cache/user_issue_count_service.rb b/app/services/cache/user_issue_count_service.rb new file mode 100644 index 000000000..9e5f2d491 --- /dev/null +++ b/app/services/cache/user_issue_count_service.rb @@ -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 \ No newline at end of file diff --git a/app/services/cache/user_project_count_service.rb b/app/services/cache/user_project_count_service.rb new file mode 100644 index 000000000..677184161 --- /dev/null +++ b/app/services/cache/user_project_count_service.rb @@ -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 \ No newline at end of file diff --git a/app/services/cache/user_project_fork_count_service.rb b/app/services/cache/user_project_fork_count_service.rb new file mode 100644 index 000000000..134e4ed65 --- /dev/null +++ b/app/services/cache/user_project_fork_count_service.rb @@ -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 \ No newline at end of file diff --git a/app/services/cache/user_project_languages_count_service.rb b/app/services/cache/user_project_languages_count_service.rb new file mode 100644 index 000000000..35b8a418e --- /dev/null +++ b/app/services/cache/user_project_languages_count_service.rb @@ -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 \ No newline at end of file diff --git a/app/services/cache/user_project_praises_count_service.rb b/app/services/cache/user_project_praises_count_service.rb new file mode 100644 index 000000000..0c7d49e3c --- /dev/null +++ b/app/services/cache/user_project_praises_count_service.rb @@ -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 \ No newline at end of file diff --git a/app/services/cache/user_project_watchers_count_service.rb b/app/services/cache/user_project_watchers_count_service.rb new file mode 100644 index 000000000..50733cad7 --- /dev/null +++ b/app/services/cache/user_project_watchers_count_service.rb @@ -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 \ No newline at end of file diff --git a/app/services/cache/user_pullrequest_count_service.rb b/app/services/cache/user_pullrequest_count_service.rb new file mode 100644 index 000000000..da478b2af --- /dev/null +++ b/app/services/cache/user_pullrequest_count_service.rb @@ -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 \ No newline at end of file diff --git a/app/views/users/statistics/develop.json.jbuilder b/app/views/users/statistics/develop.json.jbuilder index 9d530fbbe..85f66f5c9 100644 --- a/app/views/users/statistics/develop.json.jbuilder +++ b/app/views/users/statistics/develop.json.jbuilder @@ -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 diff --git a/config/initializers/redis_cache.rb b/config/initializers/redis_cache.rb new file mode 100644 index 000000000..67f52ec7b --- /dev/null +++ b/config/initializers/redis_cache.rb @@ -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) \ No newline at end of file diff --git a/config/sidekiq.yml b/config/sidekiq.yml index dda6a32eb..a50c1c1aa 100644 --- a/config/sidekiq.yml +++ b/config/sidekiq.yml @@ -7,3 +7,4 @@ - [searchkick, 10] - [notify, 100] - [mailers, 101] + - [cache, 10]