Merge branch 'pre_trustie_server' into trustie_server

This commit is contained in:
yystopf 2023-02-03 14:12:23 +08:00
commit a0f386950b
14 changed files with 220 additions and 12 deletions

View File

@ -0,0 +1,16 @@
class Admins::ProjectsRankController < Admins::BaseController
def index
@rank_date = rank_date
deleted_data = $redis_cache.smembers("v2-project-rank-deleted")
$redis_cache.zrem("v2-project-rank-#{rank_date}", deleted_data) unless deleted_data.blank?
@date_rank = $redis_cache.zrevrange("v2-project-rank-#{rank_date}", 0, -1, withscores: true)
end
private
def rank_date
params.fetch(:date, Date.today.to_s)
end
end

View File

@ -0,0 +1,15 @@
class Admins::UsersRankController < Admins::BaseController
def index
@rank_date = rank_date
@date_rank = $redis_cache.zrevrange("v2-user-rank-#{rank_date}", 0, -1, withscores: true)
end
private
def rank_date
params.fetch(:date, Date.today.to_s)
end
end

View File

@ -53,7 +53,7 @@ class ProjectsController < ApplicationController
ActiveRecord::Base.transaction do
Projects::CreateForm.new(project_params).validate!
@project = Projects::CreateService.new(current_user, project_params).call
OpenProjectDevOpsJob.perform_later(@project&.id, current_user.id)
OpenProjectDevOpsJob.set(wait: 5.seconds).perform_later(@project&.id, current_user.id)
end
rescue Exception => e
uid_logger_error(e.message)

View File

@ -15,7 +15,7 @@ class MigrateRemoteRepositoryJob < ApplicationJob
## open jianmu devops
project_id = repo&.project&.id
puts "############ mirror project_id,user_id: #{project_id},#{user_id} ############"
OpenProjectDevOpsJob.perform_later(project_id, user_id) if project_id.present? && user_id.present?
OpenProjectDevOpsJob.set(wait: 5.seconds).perform_later(project_id, user_id) if project_id.present? && user_id.present?
puts "############ mirror status: #{repo.mirror.status} ############"
else
repo&.mirror&.failed!

View File

@ -188,7 +188,7 @@ class Project < ApplicationRecord
forked_project = self.forked_from_project
if forked_project.present?
forked_project.decrement(:forked_count, 1)
forked_project.update_column(:forked_count, forked_project.forked_count)
forked_project.update_attribute(:forked_count, forked_project.forked_count)
end
end

View File

@ -22,10 +22,12 @@ class Projects::ListQuery < ApplicationQuery
sort_direction = params[:sort_direction] || "desc"
collection = optimize_sorting(collection, sort) if params[:category_id].present?
custom_sort(collection, sort, sort_direction)
# scope = scope.reorder("projects.#{sort} #{sort_direction}")
# scope
# 如果有搜索关键字根据ES搜索结果排序
if params[:search].present? && @ids.present?
collection.reorder(Arel.sql("FIELD(projects.id,#{@ids.join(',')})"))
else
custom_sort(collection, sort, sort_direction)
end
end
def filter_projects(collection)
@ -38,10 +40,10 @@ class Projects::ListQuery < ApplicationQuery
end
def by_search(items)
ids = Projects::ElasticsearchService.call(params[:search])
@ids = Projects::ElasticsearchService.call(params[:search])
items = items.where(platform: 'forge')
if ids.present?
items = items.where(id: ids).by_name_or_identifier(params[:search])
if @ids.present?
items = items.where(id: @ids).by_name_or_identifier(params[:search])
else
items = items.by_name_or_identifier(params[:search])
end

View File

@ -1,6 +1,6 @@
class Api::V1::Projects::CodeStats::ListService < ApplicationService
attr_reader :project, :ref, :owner, :repo, :token
attr_reader :project, :ref, :owner, :repo, :token, :page, :limit
attr_accessor :gitea_data
def initialize(project, params, token=nil)

View File

@ -28,6 +28,10 @@ class Cache::V2::ProjectDateRankService < ApplicationService
"v2-project-rank-#{@rank_date.to_s}"
end
def project_rank_statistic_key
"v2-project-statistic:#{@project_id}-#{@rank_date.to_s}"
end
def project_rank
$redis_cache.zscore(project_rank_key, @project_id)
end
@ -35,26 +39,37 @@ class Cache::V2::ProjectDateRankService < ApplicationService
def set_project_rank
if @visits.present?
$redis_cache.zincrby(project_rank_key, @visits.to_i * 1, @project_id)
$redis_cache.hincrby(project_rank_statistic_key, "visits", @visits.to_i)
end
if @watchers.present?
$redis_cache.zincrby(project_rank_key, @watchers.to_i * 5, @project_id)
$redis_cache.hincrby(project_rank_statistic_key, "watchers", @watchers.to_i)
end
if @praises.present?
$redis_cache.zincrby(project_rank_key, @praises.to_i * 5, @project_id)
$redis_cache.hincrby(project_rank_statistic_key, "praises", @praises.to_i)
end
if @forks.present?
$redis_cache.zincrby(project_rank_key, @forks.to_i * 10, @project_id)
$redis_cache.hincrby(project_rank_statistic_key, "forks", @forks.to_i)
end
if @issues.present?
$redis_cache.zincrby(project_rank_key, @issues.to_i * 5, @project_id)
$redis_cache.hincrby(project_rank_statistic_key, "issues", @issues.to_i)
end
if @pullrequests.present?
$redis_cache.zincrby(project_rank_key, @pullrequests.to_i * 10, @project_id)
$redis_cache.hincrby(project_rank_statistic_key, "pullrequests", @pullrequests.to_i)
end
if @commits.present?
$redis_cache.zincrby(project_rank_key, @commits.to_i * 5, @project_id)
$redis_cache.hincrby(project_rank_statistic_key, "commits", @commits.to_i)
end
$redis_cache.zscore(project_rank_key, @project_id)
# 设置过期时间(一个月)
$redis_cache.expireat(project_rank_key, (@rank_date+30.days).to_time.to_i)
$redis_cache.expireat(project_rank_statistic_key, (@rank_date+30.days).to_time.to_i)
end
end

View File

@ -115,5 +115,9 @@ class Cache::V2::UserDateRankService < ApplicationService
$redis_cache.zadd(user_rank_key, score-300, @user_id) if score > 300
$redis_cache.zscore(user_rank_key, @user_id)
# 设置过期时间(一个月)
$redis_cache.expireat(user_rank_key, (@rank_date+30.days).to_time.to_i)
$redis_cache.expireat(user_date_statistic_key, (@rank_date+30.days).to_time.to_i)
end
end

View File

@ -29,7 +29,7 @@ class Projects::ForkService < ApplicationService
ProjectUnit.init_types(clone_project.id)
@project.update_column('forked_count', @project&.forked_count.to_i + 1)
@project.update_attribute('forked_count', @project&.forked_count.to_i + 1)
new_repository.update_column('url', result['clone_url']) if result
ForkUser.create(project_id: @project.id, fork_project_id: clone_project.id, user_id: clone_project.user_id)

View File

@ -0,0 +1,74 @@
<% define_admin_breadcrumbs do %>
<% add_admin_breadcrumb('项目排行榜', admins_path) %>
<% end %>
<div class="box search-form-container user-list-form">
<%= form_tag(admins_projects_rank_index_path, method: :get, class: 'form-inline search-form flex-1', id: 'project-rank-date-form') do %>
<div class="form-group mr-2">
<label for="status">日期:</label>
<% dates_array = (0..30).to_a.map { |item| [(Date.today-item.days).to_s, (Date.today-item.days).to_s] } %>
<%= select_tag(:date, options_for_select(dates_array, params[:date]), class:"form-control",id: "project-rank-date-select")%>
</div>
<% end %>
</div>
<div class="box admin-list-container project-language-list-container">
<table class="table table-hover text-center subject-list-table">
<thead class="thead-light">
<tr>
<th width="20%">排名</th>
<th width="20%">项目</th>
<th width="20%">得分</th>
<th width="20%">访问数</th>
<th width="20%">关注数</th>
<th width="20%">点赞数</th>
<th width="20%">fork数</th>
<th width="20%">疑修数</th>
<th width="20%">合并请求数</th>
<th width="20%">提交数</th>
</tr>
</thead>
<tbody>
<% @date_rank.each_with_index do |item, index| %>
<tr class="">
<td><%= index + 1%></td>
<% project_common = $redis_cache.hgetall("v2-project-common:#{item[0]}") %>
<% owner_common = $redis_cache.hgetall("v2-owner-common:#{project_common["owner_id"]}")%>
<td>
<a href="/<%= owner_common["login"] %>/<%= project_common["identifier"]%>">
<%= project_common["name"] %>
</a>
</td>
<td><%= item[1] %></td>
<% project_date_statistic_key = "v2-project-statistic:#{item[0]}-#{@rank_date}"%>
<% if $redis_cache.exists(project_date_statistic_key)%>
<% visits = $redis_cache.hget(project_date_statistic_key, "visits") %>
<td><%= visits || 0 %></td>
<% watchers = $redis_cache.hget(project_date_statistic_key, "watchers") %>
<td><%= watchers || 0 %></td>
<% praises = $redis_cache.hget(project_date_statistic_key, "praises") %>
<td><%= praises || 0 %></td>
<% forks = $redis_cache.hget(project_date_statistic_key, "forks") %>
<td><%= forks || 0 %></td>
<% issues = $redis_cache.hget(project_date_statistic_key, "issues") %>
<td><%= issues || 0 %></td>
<% pullrequests = $redis_cache.hget(project_date_statistic_key, "pullrequests") %>
<td><%= pullrequests || 0 %></td>
<% commits = $redis_cache.hget(project_date_statistic_key, "commits") %>
<td><%= commits || 0 %></td>
<% else %>
<td colspan="7">暂无数据</td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
</div>
<script>
$("#project-rank-date-select").on('change', function() {
$("#project-rank-date-form").submit()
});
</script>

View File

@ -14,6 +14,12 @@
<!-- Sidebar Links -->
<ul class="list-unstyled components">
<li><%= sidebar_item(admins_path, '概览', icon: 'dashboard', controller: 'admins-dashboards') %></li>
<li>
<%= sidebar_item_group('#user-submenu', '排行榜', icon: 'user') do %>
<li><%= sidebar_item(admins_users_rank_index_path, '用户排行榜', icon: 'user', controller: 'admins-users_rank') %></li>
<li><%= sidebar_item(admins_projects_rank_index_path, '项目排行榜', icon: 'user', controller: 'admins-projects_rank') %></li>
<% end %>
</li>
<li>
<%= sidebar_item_group('#user-submenu', '用户', icon: 'user') do %>
<li><%= sidebar_item(admins_users_path, '用户列表', icon: 'user', controller: 'admins-users') %></li>

View File

@ -0,0 +1,74 @@
<% define_admin_breadcrumbs do %>
<% add_admin_breadcrumb('用户排行榜', admins_path) %>
<% end %>
<div class="box search-form-container user-list-form">
<%= form_tag(admins_users_rank_index_path, method: :get, class: 'form-inline search-form flex-1', id: 'user-rank-date-form') do %>
<div class="form-group mr-2">
<label for="status">日期:</label>
<% dates_array = (0..30).to_a.map { |item| [(Date.today-item.days).to_s, (Date.today-item.days).to_s] } %>
<%= select_tag(:date, options_for_select(dates_array, params[:date]), class:"form-control",id: "user-rank-date-select")%>
</div>
<% end %>
</div>
<div class="box admin-list-container project-language-list-container">
<table class="table table-hover text-center subject-list-table">
<thead class="thead-light">
<tr>
<th width="20%">排名</th>
<th width="20%">用户</th>
<th width="20%">得分</th>
<th width="20%">影响力</th>
<th width="20%">贡献度</th>
<th width="20%">活跃度</th>
<th width="20%">项目经验</th>
<th width="20%">语言能力</th>
</tr>
</thead>
<tbody>
<% @date_rank.each_with_index do |item, index| %>
<tr class="">
<td><%= index + 1%></td>
<% owner_common = $redis_cache.hgetall("v2-owner-common:#{item[0]}")%>
<td>
<a href="/<%= owner_common["login"] %>">
<%= owner_common["name"] %>
</a>
</td>
<% user_date_statistic_key = "v2-user-statistic:#{item[0]}-#{@rank_date}"%>
<% follow_count = $redis_cache.hget(user_date_statistic_key, "follow-count") || 0 %>
<% pullrequest_count = $redis_cache.hget(user_date_statistic_key, "pullrequest-count") || 0 %>
<% issues_count = $redis_cache.hget(user_date_statistic_key, "issue-count") || 0 %>
<% project_count = $redis_cache.hget(user_date_statistic_key, "project-count") || 0 %>
<% fork_count = $redis_cache.hget(user_date_statistic_key, "fork-count") || 0 %>
<% project_watchers_count = $redis_cache.hget(user_date_statistic_key, "project-watcher-count") || 0 %>
<% project_praises_count = $redis_cache.hget(user_date_statistic_key, "project-praise-count") || 0 %>
<% project_language = $redis_cache.hget(user_date_statistic_key, "project-language") %>
<% project_languages_count = project_language.nil? || project_language == "{}" ? 0 : JSON.parse(project_language).length %>
<% influence = (60.0 + follow_count.to_i / (follow_count.to_i + 20.0) * 40.0).to_i %>
<% contribution = (60.0 + pullrequest_count.to_i / (pullrequest_count.to_i + 20.0) * 40.0).to_i %>
<% activity = (60.0 + issues_count.to_i / (issues_count.to_i + 80.0) * 40.0).to_i %>
<% experience = 10 * project_count.to_i + 5 * fork_count.to_i + project_watchers_count.to_i + project_praises_count.to_i %>
<% experience = (60.0 + experience / (experience + 100.0) * 40.0).to_i %>
<% language = (60.0 + project_languages_count.to_i / (project_languages_count.to_i + 5.0) * 40.0).to_i %>
<% score = influence+ contribution + activity + experience + language%>
<td><%= score %></td>
<td><%= influence%></td>
<td><%= contribution%></td>
<td><%= activity%></td>
<td><%= experience%></td>
<td><%= language%></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<script>
$("#user-rank-date-select").on('change', function() {
$("#user-rank-date-form").submit()
});
</script>

View File

@ -761,6 +761,8 @@ Rails.application.routes.draw do
get :visits_static
end
end
resources :users_rank, only: [:index]
resources :projects_rank, only: [:index]
resources :sites
resources :edu_settings
resources :project_languages