From ef43488861d632bd2dfea1177a848fdff423efbb Mon Sep 17 00:00:00 2001 From: huang Date: Thu, 3 Nov 2016 15:35:09 +0800 Subject: [PATCH] =?UTF-8?q?admin=E6=B7=BB=E5=8A=A0=E5=AF=BC=E5=87=BA?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E6=89=80=E6=9C=89=E4=BB=A3=E7=A0=81=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/admin_controller.rb | 69 +++++++++++++++++++ app/controllers/repositories_controller.rb | 18 +++-- app/helpers/application_helper.rb | 29 -------- app/views/admin/projects.html.erb | 6 +- config/locales/projects/zh.yml | 8 +++ config/routes.rb | 1 + .../lib/gitlab/client/repositories.rb | 16 +++++ 7 files changed, 111 insertions(+), 36 deletions(-) diff --git a/app/controllers/admin_controller.rb b/app/controllers/admin_controller.rb index dce558ac4..0eec8dbc8 100644 --- a/app/controllers/admin_controller.rb +++ b/app/controllers/admin_controller.rb @@ -1,3 +1,4 @@ + # Redmine - project management software # Copyright (C) 2006-2013 Jean-Philippe Lang # @@ -31,6 +32,74 @@ class AdminController < ApplicationController @no_configuration_data = Redmine::DefaultData::Loader::no_data? end + def export_rep_static_users + rev = params[:rev] + cycle = params[:cycle] + cycle_name = (params[:cycle] == "admin_week" ? l(:label_per_week) : (params[:cycle] == "admin_month" ? l(:label_per_month) : l(:label_per_all))) + respond_to do |format| + format.html + format.xls{ + filename = "#{l(:label_user_rep_xls)}_#{cycle_name}.xls" + send_data(admin_export_rep_xls(:rev => rev, :cycle => "1"), :type => 'application/octet-stream', :filename => filename_for_content_disposition(filename)) + } + end + end + + # 管理员界面导出所有项目代码、提交次数统计 + def admin_export_rep_xls (options = {}) + cycle = params[:cycle] + g = Gitlab.client + users_results = [] + User.first(13).each do |user| + unless user.mail.nil? + user_mail = user.mail + user.projects.where("gpid is not null").each do |project| + begin + g.branches(project.gpid).each do |branch| + if cycle == "admin_week" + statics = g.admin_rep_stats_week(project.gpid, :rev => branch.name, :user_mail => user_mail) + elsif cycle == "admin_month" + statics = g.admin_rep_stats_month(project.gpid, :rev => branch.name, :user_mail => user_mail) + elsif cycle == "admin_all" + statics = g.admin_rep_stats_all(project.gpid, :rev => branch.name, :user_mail => user_mail) + end + unless statics.first.commits_num == "0" + user_details = {:user_id => user.id, :user_name => user.show_name, :project_id => project.id, :project_name => project.name, + :commits_num => statics.first.commits_num, :add => statics.first.add, :del => statics.first.del, :changes => statics.first.changes, :branch => branch.name} + users_results << user_details + end + end + rescue Exception => e + puts e + end + end + end + end + + xls_report = StringIO.new + book = Spreadsheet::Workbook.new + sheet1 = book.create_worksheet :name => l(:project_module_repository) + blue = Spreadsheet::Format.new :color => :blue, :weight => :bold, :size => 10 + sheet1.row(0).default_format = blue + sheet1.row(0).concat([l(:label_rep_user_id),l(:label_rep_user_name),l(:label_rep_project_id),l(:label_rep_project_name),l(:rep_branch),l(:rep_changeset),l(:rep_code_add),l(:rep_code_delete),l(:rep_code_modified),l(:rep_sode_time)]) + count_row = 1 + users_results.each do |static| + sheet1[count_row,0] = static[:user_id] + sheet1[count_row,1] = static[:user_name] + sheet1[count_row,2] = static[:project_id] + sheet1[count_row,3] = static[:project_name] + sheet1[count_row,4] = static[:branch] + sheet1[count_row,5] = static[:commits_num] + sheet1[count_row,6] = static[:add] + sheet1[count_row,7] = static[:del] + sheet1[count_row,8] = static[:changes] + sheet1[count_row,9] = Time.now.strftime('%Y-%m-%d %H:%M:%S') + count_row += 1 + end + book.write xls_report + xls_report.string + end + # 管理员界面 项目列表 def projects =begin diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 1a2613d03..d2a712f32 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -74,9 +74,19 @@ class RepositoriesController < ApplicationController def export_rep_static # 管理员界面导出所有项目 if params[:cycle] == "admin_week" || params[:cycle] == "admin_month" || params[:cycle] == "admin_all" - if User.current.admin - projects = Project.where(:gpid => !nil) - + if User.current.admin? + @project = Project.find(params[:id]) + gpid = @project.gpid + rev = params[:rev] + cycle = params[:cycle] + cycle_name = (params[:cycle] == "admin_week" ? "每周" : (params[:cycle] == "admin_month" ? "每月" : "所有")) + respond_to do |format| + format.html + format.xls{ + filename = "#{l(:label_user_rep_xls)}_#{l(:cycle_name)}_.xls" + send_data(admin_export_rep_xls(gpid, :rev => rev, :cycle => "1"), :type => 'application/octet-stream', :filename => filename_for_content_disposition(filename)) + } + end else render_403 end @@ -89,7 +99,7 @@ class RepositoriesController < ApplicationController format.html format.xls{ filename = "#{@project.name.to_s}_#{l(:label_rep_xls)}.xls" - send_data(export_rep_xls(gpid, :rev => rev, :cycle => "1"), :type => 'application/octet-stream', :filename => filename_for_content_disposition(filename)) + send_data(export_rep_xls(gpid, :rev => rev, :cycle => cycle), :type => 'application/octet-stream', :filename => filename_for_content_disposition(filename)) } end end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 20173d5c1..f97696b67 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -115,35 +115,6 @@ module ApplicationHelper xls_report.string end - # 管理员界面导出所有项目代码、提交次数统计 - def admin_export_rep_xls - users = User.all - user_details = {} - results = [] - users.each do |user| - user.projects.each do |project| - g = Gitlab.client - cycle = params[:cycle] - rev = params[:rev] - if cycle == "week" - statics = g.rep_stats_week(project.gpid, :rev => rev) - elsif cycle == "month" - statics = g.rep_stats_month(project.gpid, :rev => rev) - end. - user_details = {:user_id => user.id, :user_name => user.show_name, :project_id => project.id, :project_name => project.name, - :commits_num => static.commits_num, :add => static.add, :del => static.del, :changes => static.changes} - results << user_details - end - - static.commits_num - sheet1[count_row,3] = static.add - sheet1[count_row,4] = static.del - sheet1[count_row,5] = static.changes - - end - end - end - # 项目issue列表导出Excel功能 def issue_list_xls issues xls_report = StringIO.new diff --git a/app/views/admin/projects.html.erb b/app/views/admin/projects.html.erb index 34e716a8f..46a5e5f2a 100644 --- a/app/views/admin/projects.html.erb +++ b/app/views/admin/projects.html.erb @@ -78,9 +78,9 @@ <% html_title(l(:label_project_plural)) -%>
项目代码/提交数统计: - <%= link_to "最近一周", {:controller => 'repositories', :action => 'export_rep_static', :format => 'xls', :rev => @rev, :cycle => "admin_week" }, :class => "linkBlue2" %> | - <%= link_to "最近一月", {:controller => 'repositories', :action => 'export_rep_static', :format => 'xls', :rev => @rev, :cycle => "admin_month" }, :class => "linkBlue2" %> - <%= link_to "所有", {:controller => 'repositories', :action => 'export_rep_static', :format => 'xls', :rev => @rev, :cycle => "admin_all" }, :class => "linkBlue2" %> + <%= link_to "最近一周", {:controller => 'admin', :action => 'export_rep_static_users', :format => 'xls', :cycle => "admin_week" }, :class => "linkBlue2" %> | + <%= link_to "最近一月", {:controller => 'admin', :action => 'export_rep_static_users', :format => 'xls', :cycle => "admin_month" }, :class => "linkBlue2" %>| + <%= link_to "所有", {:controller => 'admin', :action => 'export_rep_static_users', :format => 'xls', :cycle => "admin_all" }, :class => "linkBlue2" %>