diff --git a/app/controllers/api/v1/projects/portrait_controller.rb b/app/controllers/api/v1/projects/portrait_controller.rb new file mode 100644 index 000000000..e77e7f8e3 --- /dev/null +++ b/app/controllers/api/v1/projects/portrait_controller.rb @@ -0,0 +1,52 @@ +class Api::V1::Projects::PortraitController < Api::V1::BaseController + before_action :require_public_and_member_above + + def index + platform_statistic = $redis_cache.hgetall("v2-platform-statistic") + + # 社区影响力 + praise_count = PraiseTread.where(praise_tread_object_type: "Project", praise_tread_object_id: @project.id).count + watcher_count = Watcher.where(watchable_type:"Project", watchable_id: @project.id).count + fork_count = ForkUser.where(project_id: @project.id).count + community_impact_praise = platform_statistic['max-praise-count'].to_i == 0 ? 0 : 30*(praise_count.to_f/platform_statistic['max-praise-count'].to_i) + community_impact_watcher = platform_statistic['max-watcher-count'].to_i == 0 ? 0 : 30*(watcher_count.to_f/platform_statistic['max-watcher-count'].to_i) + community_impact_fork = platform_statistic['max-fork-count'].to_i == 0 ? 0 : 40*(fork_count.to_f/platform_statistic['max-fork-count'].to_i) + community_impact = community_impact_praise + community_impact_watcher + community_impact_fork + + # 项目成熟度 + pullrequest_count = PullRequest.where(project_id: @project.id).count + issue_count = Issue.issue_issue.where(project_id: @project.id).count + commit_count = CommitLog.joins(:project).merge(Project.common).where(project_id: @project.id).count + project_maturity_pullrequest = platform_statistic['max-pullrequest-count'].to_i == 0 ? 0 : 30*(pullrequest_count.to_f/platform_statistic['max-pullrequest-count'].to_i) + project_maturity_issue = platform_statistic['max-issue-count'].to_i == 0 ? 0 : 30*(issue_count.to_f/platform_statistic['max-issue-count'].to_i) + project_maturity_commit = platform_statistic['max-commit-count'].to_i == 0 ? 0 : 40*(commit_count.to_f/platform_statistic['max-commit-count'].to_i) + project_maturity = project_maturity_pullrequest + project_maturity_issue + project_maturity_commit + + # 项目健康度 + closed_pullrequest_count = PullRequest.where(project_id: @project.id).merged_and_closed.count + closed_issue_count = Issue.issue_issue.where(project_id: @project.id).closed.count + has_license = @project.license.present? ? 1 : 0 + project_health_issue = (issue_count < 10 || closed_issue_count < 10) ? 0 : 40*(closed_issue_count-10).to_f/(issue_count-10) + project_health_pullrequest = (pullrequest_count < 5 || closed_pullrequest_count < 5) ? 0 : 30*(closed_pullrequest_count-5).to_f/(pullrequest_count-5) + project_health_license = 20*has_license + project_health = project_health_issue + project_health_pullrequest + project_health_license + + # 团队影响度 + member_count = Member.where(project_id: @project.id).count + recent_one_month_member_count = Member.where(project_id:@project.id).where("created_on > ?", Time.now - 30.days).count + team_impact_member = platform_statistic['max-member-count'].to_i == 0 ? 0 : 40*(member_count.to_f/platform_statistic['max-member-count'].to_i) + team_impact_recent_member = platform_statistic['max-recent-one-month-member-count'].to_i == 0 ? 0 : 60*(recent_one_month_member_count.to_f/platform_statistic['max-recent-one-month-member-count'].to_i) + team_impact = team_impact_member + team_impact_recent_member + + # 开发活跃度 + recent_one_month_pullrequest_count = PullRequest.where(project_id: @project.id).where("created_at > ?", Time.now - 30.days).count + recent_one_month_issue_count = Issue.issue_issue.where(project_id: @project.id).where("created_on > ?", Time.now - 30.days).count + recent_one_month_commit_count = CommitLog.joins(:project).merge(Project.common).where(project_id: @project.id).where("created_at > ?", Time.now - 30.days).count + develop_activity_pullrequest = platform_statistic['max-recent-one-month-pullrequest-count'].to_i == 0 ? 0 : 20*(recent_one_month_pullrequest_count.to_f/platform_statistic['max-recent-one-month-pullrequest-count'].to_i) + develop_activity_issue = platform_statistic['max-recent-one-month-issue-count'].to_i == 0 ? 0 : 20*(recent_one_month_issue_count.to_f/platform_statistic['max-recent-one-month-issue-count'].to_i) + develop_activity_commit = platform_statistic['max-recent-one-month-commit-count'].to_i == 0 ? 0 : 40*(recent_one_month_commit_count.to_f/platform_statistic['max-recent-one-month-commit-count'].to_i) + develop_activity = 20 + develop_activity_pullrequest + develop_activity_issue + develop_activity_commit + + render :json => {community_impact: community_impact, project_maturity: project_maturity, project_health: project_health, team_impact: team_impact, develop_activity: develop_activity} + end +end \ No newline at end of file diff --git a/app/services/cache/v2/platform_statistic_service.rb b/app/services/cache/v2/platform_statistic_service.rb index 5bf4f4a74..a7d9437a0 100644 --- a/app/services/cache/v2/platform_statistic_service.rb +++ b/app/services/cache/v2/platform_statistic_service.rb @@ -31,6 +31,61 @@ class Cache::V2::PlatformStatisticService < ApplicationService "v2-platform-statistic" end + # 平台最高关注数 + def max_watcher_count_key + "max-watcher-count" + end + + # 平台最高点赞数 + def max_praise_count_key + "max-praise-count" + end + + # 平台最高fork数 + def max_fork_count_key + "max-fork-count" + end + + # 平台最高pr数 + def max_pullrequest_count_key + "max-pullrequest-count" + end + + # 平台最高issue数 + def max_issue_count_key + "max-issue-count" + end + + # 平台最高commit数 + def max_commit_count_key + "max-commit-count" + end + + # 平台最高仓库人数 + def max_member_count_key + "max-member-count" + end + + # 平台近一个月新增成员最大值 + def max_recent_one_month_member_count_key + "max-recent-one-month-member-count" + end + + # 平台近一个月合并请求最大值 + def max_recent_one_month_pullrequest_count_key + "max-recent-one-month-pullrequest-count" + end + + # 平台近一个月issue最大值 + def max_recent_one_month_issue_count_key + "max-recent-one-month-issue-count" + end + + # 平台近一个月commit最大值 + def max_recent_one_month_commit_count_key + "max-recent-one-month-commit-count" + end + def follow_count_key "follow-count" end @@ -136,6 +191,61 @@ class Cache::V2::PlatformStatisticService < ApplicationService $redis_cache.hgetall(platform_statistic_key) end + def reset_platform_max_watcher_count + max_watcher = Watcher.where(watchable_type:"Project").group(:watchable_type, :watchable_id).count.sort_by{|i|i[1]}.last || [] + $redis_cache.hset(platform_statistic_key, max_watcher_count_key, max_watcher[1].nil? ? 0 : max_watcher[1]) + end + + def reset_platform_max_praise_count + max_praise = PraiseTread.where(praise_tread_object_type: "Project").group(:praise_tread_object_type, :praise_tread_object_id).count.sort_by{|i|i[1]}.last || [] + $redis_cache.hset(platform_statistic_key, max_praise_count_key, max_praise[1].nil? ? 0 : max_praise[1]) + end + + def reset_platform_max_fork_count + max_fork = ForkUser.group(:project_id).count.sort_by{|i|i[1]}.last || [] + $redis_cache.hset(platform_statistic_key, max_fork_count_key, max_fork[1].nil? ? 0 : max_fork[1]) + end + + def reset_platform_max_pullrequest_count + max_pullrequest = PullRequest.group(:project_id).count.sort_by{|i|i[1]}.last || [] + $redis_cache.hset(platform_statistic_key, max_pullrequest_count_key, max_pullrequest[1].nil? ? 0 : max_pullrequest[1]) + end + + def reset_platform_max_issue_count + max_issue = Issue.issue_issue.group(:project_id).count.sort_by{|i|i[1]}.last || [] + $redis_cache.hset(platform_statistic_key, max_issue_count_key, max_issue[1].nil? ? 0 : max_issue[1]) + end + + def reset_platform_max_commit_count + max_commit = CommitLog.joins(:project).merge(Project.common).group(:project_id).count.sort_by{|i|i[1]}.last || [] + $redis_cache.hset(platform_statistic_key, max_commit_count_key, max_commit[1].nil? ? 0 : max_commit[1]) + end + + def reset_platform_max_member_count + max_member = Member.where.not(project_id: [0,-1]).group(:project_id).count.sort_by{|i|i[1]}.last || [] + $redis_cache.hset(platform_statistic_key, max_member_count_key, max_member[1].nil? ? 0 : max_member[1]) + end + + def reset_platform_max_recent_one_month_member_count + max_recent_one_month_member = Member.where("created_on > ?", Time.now - 30.days).group(:project_id).count.sort_by{|i|i[1]}.last || [] + $redis_cache.hset(platform_statistic_key, max_recent_one_month_member_count_key, max_recent_one_month_member[1].nil? ? 0 : max_recent_one_month_member[1]) + end + + def reset_platform_max_recent_one_month_pullrequest_count + max_recent_one_month_pullrequest = PullRequest.where("created_at > ?", Time.now - 30.days).group(:project_id).count.sort_by{|i|i[1]}.last || [] + $redis_cache.hset(platform_statistic_key, max_recent_one_month_pullrequest_count_key, max_recent_one_month_pullrequest[1].nil? ? 0 : max_recent_one_month_pullrequest[1]) + end + + def reset_platform_max_recent_one_month_issue_count + max_recent_one_month_issue = Issue.issue_issue.where("created_on > ?", Time.now - 30.days).group(:project_id).count.sort_by{|i|i[1]}.last || [] + $redis_cache.hset(platform_statistic_key, max_recent_one_month_issue_count_key, max_recent_one_month_issue[1].nil? ? 0 : max_recent_one_month_issue[1]) + end + + def reset_platform_max_recent_one_month_commit_count + max_recent_one_month_commit = CommitLog.joins(:project).merge(Project.common).where("created_at > ?", Time.now - 30.days).group(:project_id).count.sort_by{|i|i[1]}.last || [] + $redis_cache.hset(platform_statistic_key, max_recent_one_month_commit_count_key, max_recent_one_month_commit[1].nil? ? 0 : max_recent_one_month_commit[1]) + end + def reset_platform_follow_count $redis_cache.hset(platform_statistic_key, follow_count_key, Watcher.where(watchable_type: 'User').count) end @@ -178,6 +288,17 @@ class Cache::V2::PlatformStatisticService < ApplicationService reset_platform_project_praise_count reset_platform_project_watcher_count reset_platform_pullrequest_count + reset_platform_max_watcher_count + reset_platform_max_praise_count + reset_platform_max_fork_count + reset_platform_max_pullrequest_count + reset_platform_max_issue_count + reset_platform_max_commit_count + reset_platform_max_member_count + reset_platform_max_recent_one_month_member_count + reset_platform_max_recent_one_month_pullrequest_count + reset_platform_max_recent_one_month_issue_count + reset_platform_max_recent_one_month_commit_count $redis_cache.hgetall(platform_statistic_key) end diff --git a/config/routes/api.rb b/config/routes/api.rb index 34751cf50..5f4691a24 100644 --- a/config/routes/api.rb +++ b/config/routes/api.rb @@ -78,6 +78,7 @@ defaults format: :json do # projects文件夹下的 scope module: :projects do + resources :portrait, only: [:index] resources :sync_repositories, only: [:create, :index] do collection do post :update_info diff --git a/config/sidekiq_cron.yml b/config/sidekiq_cron.yml index 72a69bbad..40c655a74 100644 --- a/config/sidekiq_cron.yml +++ b/config/sidekiq_cron.yml @@ -16,4 +16,11 @@ create_daily_project_statistics: daily_platform_statistics: cron: "0 1 * * *" class: "DailyPlatformStatisticsJob" - queue: default \ No newline at end of file + queue: default + +cache_async_reset: + cron: "0 2 * * *" + class: "CacheAsyncResetJob" + queue: cache + args: + - "platform_statistic_service" \ No newline at end of file