forgeplus/app/controllers/admins/projects_rank_controller.rb

57 lines
2.4 KiB
Ruby
Raw Permalink Normal View History

2023-02-01 14:25:55 +08:00
class Admins::ProjectsRankController < Admins::BaseController
2024-07-01 09:55:20 +08:00
before_action :require_admin
2023-02-01 14:25:55 +08:00
def index
@statistics = DailyProjectStatistic.where("date >= ? AND date <= ?", begin_date, end_date)
@statistics = @statistics.group(:project_id).select("project_id,
sum(score) as score,
sum(visits) as visits,
sum(watchers) as watchers,
sum(praises) as praises,
sum(forks) as forks,
sum(issues) as issues,
sum(pullrequests) as pullrequests,
sum(commits) as commits").includes(:project)
2024-06-27 09:13:24 +08:00
@statistics = paginate @statistics.order("#{sort_by} #{sort_direction}")
export_excel(@statistics.limit(50))
2023-02-01 14:25:55 +08:00
end
private
2023-12-18 15:49:41 +08:00
def begin_date
2024-01-16 16:27:18 +08:00
params.fetch(:begin_date, (Date.yesterday-7.days).to_s)
2023-12-18 15:49:41 +08:00
end
def end_date
2024-01-16 16:27:18 +08:00
params.fetch(:end_date, Date.yesterday.to_s)
2023-12-18 15:49:41 +08:00
end
def sort_by
2023-12-19 15:05:00 +08:00
DailyProjectStatistic.column_names.include?(params.fetch(:sort_by, "score")) ? params.fetch(:sort_by, "score") : "score"
end
def sort_direction
2023-12-19 15:05:00 +08:00
%w(desc asc).include?(params.fetch(:sort_direction, "desc")) ? params.fetch(:sort_direction, "desc") : "desc"
end
def export_excel(data)
book = Spreadsheet::Workbook.new
sheet = book.create_worksheet :name => "项目活跃度排行"
sheet.row(0).concat %w(排名 项目全称 项目地址 得分 访问数 关注数 点赞数 fork数 疑修数 合并请求数 提交数)
data.each_with_index do |d, index|
sheet[index+1,0] = index+1
sheet[index+1,1] = "#{d&.project&.owner&.real_name}/#{d&.project&.name}"
sheet[index+1,2] = "#{Rails.application.config_for(:configuration)['platform_url']}/#{d&.project&.owner&.login}/#{d&.project&.identifier}"
sheet[index+1,3] = d.score
sheet[index+1,4] = d.visits
sheet[index+1,5] = d.watchers
sheet[index+1,6] = d.praises
sheet[index+1,7] = d.forks
sheet[index+1,8] = d.issues
sheet[index+1,9] = d.pullrequests
sheet[index+1,10] = d.commits
2023-12-18 15:49:41 +08:00
end
book.write "#{Rails.root}/public/项目活跃度排行.xls"
2023-02-01 14:25:55 +08:00
end
end