socialforge/app/controllers/statistics_controller.rb

237 lines
9.3 KiB
Ruby
Raw Permalink Normal View History

# encoding=utf-8
2018-02-05 12:03:35 +08:00
class StatisticsController < ApplicationController
# GET /statistics
# GET /statistics.json
2018-05-08 16:25:59 +08:00
include StatisticsHelper
2018-06-05 14:58:45 +08:00
include ApplicationHelper
2018-02-06 17:41:39 +08:00
layout 'base_statistic'
before_filter :get_date, :only => [:index, :new]
2018-02-09 11:27:32 +08:00
before_filter :require_login
before_filter :find_statistic, :only => [:show, :edit, :update, :destroy]
before_filter :require_manager, :only =>[:edit, :update, :destroy]
2018-06-05 14:58:45 +08:00
if RUBY_PLATFORM =~ /linux/
require 'simple_xlsx_reader'
end
2018-02-06 17:41:39 +08:00
2018-02-05 12:03:35 +08:00
def index
2018-05-04 17:17:07 +08:00
type = (params[:type] == "reorder_popu" ? "file_size" : "created_at")
2018-02-09 09:55:21 +08:00
order = (params[:order] == "asc" ? "asc" : "desc")
2018-02-08 15:12:36 +08:00
if params[:sub_category_id].present?
@statistics = Statistic.where(:sub_category_id => params[:sub_category_id])
2018-02-09 17:00:12 +08:00
sub_category_id = '(' + params[:sub_category_id].to_a.join(",") + ')'
2018-05-04 17:17:07 +08:00
@statistics = Statistic.find_by_sql("SELECT statistics.* from statistics where sub_category_id in #{sub_category_id} order by #{type} #{order}")
2018-02-08 15:12:36 +08:00
else
2018-02-09 18:12:53 +08:00
if params[:main_category_id].present?
2018-05-04 17:17:07 +08:00
@statistics = Statistic.find_by_sql("SELECT statistics.* from statistics where main_category_id = #{params[:main_category_id]} order by #{type} #{order}")
2018-05-09 10:35:33 +08:00
@main_category_id = params[:main_category_id]
2018-02-09 18:12:53 +08:00
else
2018-05-04 17:17:07 +08:00
@statistics = Statistic.find_by_sql("SELECT statistics.* from statistics order by #{type} #{order}")
2018-02-09 18:12:53 +08:00
end
2018-02-08 15:12:36 +08:00
end
2018-05-09 09:49:00 +08:00
@statistics_count = @statistics.size
2018-05-08 16:48:07 +08:00
@data_size = data_format_string @statistics.select{|c| !c.file_size.nil?}.map{|s| s.file_size}.sum
2018-02-08 15:12:36 +08:00
@limit = 10
@is_remote = true
@statistics_pages = Paginator.new @statistics_count, @limit, params['page'] || 1
@offset ||= @statistics_pages.offset
@statistics = paginateHelper @statistics, @limit
2018-02-05 12:03:35 +08:00
respond_to do |format|
2018-02-08 15:12:36 +08:00
format.js
2018-02-05 12:03:35 +08:00
format.html # index.html.erb
end
end
2018-06-05 14:58:45 +08:00
def sync_peking_data
attachment = Attachment.where(:container_id => 57, :container_type => "Statistic").first
if attachment.present?
path = attachment.disk_directory
name = attachment.disk_filename
if name.split(".").last == "xls" || name.split(".").last == "xlsx"
lists = readData("files/#{path}/#{name}")
exp_statistic = Statistic.where(:id => 247).first
if exp_statistic
# @list_count += lists.count
lists.each_with_index do |list, index|
# logger.info("#{list[0]}---#{list[1]}---#{list[2]}")
if index > 0 && list[0]
if index == 1
description = exp_statistic.description.sub("https://www.trustie.net/statistics", "http://softwareasset.org.cn")
Statistic.create(:description => description, :name => list[0], :status => exp_statistic.status, :user_id => 21502, :main_category_id => exp_statistic.main_category_id, :sub_category_id => exp_statistic.sub_category_id, :file_size => exp_statistic.file_size)
else
file_size = file_size_revert list[2]
s_type = list[0][0,list[0].size - 4]
description = exp_statistic.description.sub("APR", s_type)
description = description.sub("https://www.trustie.net/statistics", "http://softwareasset.org.cn")
lindex = list[3].index("").to_i
rindex= list[3].index("其中").to_i - 1
description = description.sub("问答数据和API介绍数据。", list[3][lindex..rindex])
# 数据列表
data_list = "| 数据类型 | 数据来源 | 数据格式 | 数据规模 |
| ------------ | ------------ | ------------ | ------------ |
"
if list[4].present?
data_list += "| 软件源代码 | http://www-eu.apache.org/dist/ | `.java`文件 | `#{list[4][0, list[4].size - 2]}`#{list[4][-2, 2]} |
"
end
if list[5].present?
data_list += "| 软件问答数据 | https://stackoverflow.com/ | `.xml`文件 | `#{list[5][0, list[5].size - 2]}`#{list[5][-2, 2]} |
"
end
if list[6].present?
data_list += "| 版本提交历史 | https://git.apache.org/ | `git`库 | `#{list[6][0, list[6].size - 2]}`#{list[6][-2, 2]} |
"
end
if list[7].present?
data_list += "| 缺陷报告 | https://issues.apache.org/jira/secure/BrowseProjects.jspa?selectedCategory=all&selectedProjectType=all | `.json`文件 | `#{list[7][0, list[7].size - 2]}`#{list[7][-2, 2]} |
"
end
if list[8].present?
data_list += "| 开发邮件 | http://mail-archives.apache.org/mod_mbox/ | `.mbox`文件 | `#{list[8][0, list[8].size - 2]}`#{list[8][-2, 2]} |
"
end
if list[9].present?
data_list += "| API使用介绍 | 项目主页 | `网页`文件 | `#{list[9][0, list[9].size - 2]}`#{list[9][-2, 2]} |"
end
# logger.info("data-list-------------#{data_list}")
l_index = description.index("|").to_i
2018-06-05 15:57:48 +08:00
r_index= description.rindex("|").to_i
2018-06-05 14:58:45 +08:00
description = description.sub(description[l_index..r_index], data_list)
Statistic.create(:description => description, :name => list[0], :status => exp_statistic.status, :user_id => 21502, :main_category_id => exp_statistic.main_category_id, :sub_category_id => exp_statistic.sub_category_id, :file_size => file_size)
end
end
end
end
end
end
redirect_to statistics_path
end
def search
end
2018-02-05 12:03:35 +08:00
# GET /statistics/1
# GET /statistics/1.json
def show
2018-02-05 15:13:03 +08:00
@attachments = @statistic.attachments
2018-02-05 12:03:35 +08:00
respond_to do |format|
format.html # show.html.erb
format.json { render json: @statistic }
end
end
# GET /statistics/new
# GET /statistics/new.json
def new
@statistic = Statistic.new
2018-02-08 10:28:45 +08:00
@sub_categories = @main_categories.first.sub_categories
2018-02-05 12:03:35 +08:00
respond_to do |format|
format.html # new.html.erb
format.json { render json: @statistic }
end
end
# GET /statistics/1/edit
def edit
@main_categories = MainCategory.all
main_category = MainCategory.find(@statistic.main_category_id)
@sub_categories = main_category.sub_categories
2018-02-05 12:03:35 +08:00
end
# POST /statistics
# POST /statistics.json
def create
@statistic = Statistic.new(params[:statistic])
2018-02-08 11:24:04 +08:00
@statistic.user_id = User.current.id
2018-02-05 15:13:03 +08:00
@statistic.save_attachments_containers(params[:attachments], User.current, true)
2018-02-05 12:03:35 +08:00
respond_to do |format|
if @statistic.save
2018-05-04 17:17:07 +08:00
if @statistic.file_size.nil? && @statistic.attachments.count > 0
@statistic.update_attribute('file_size', format("%0.3f", @statistic.attachments.map{|c| c.filesize}.sum.to_f/(1024*1024)))
end
format.html { redirect_to @statistic, notice: '创建成功' }
2018-02-05 12:03:35 +08:00
format.json { render json: @statistic, status: :created, location: @statistic }
else
format.html { render action: "new" }
format.json { render json: @statistic.errors, status: :unprocessable_entity }
end
end
end
# PUT /statistics/1
# PUT /statistics/1.json
def update
2018-02-05 12:03:35 +08:00
respond_to do |format|
if @statistic.update_attributes(params[:statistic])
# @statistic.save_attachments_containers(params[:attachments], User.current, true)
2018-02-09 16:08:52 +08:00
if params[:attachments].present?
params[:attachments].each_value do |attachment|
temp_attachment = Attachment.where(:id => attachment['attachment_id']).first
if temp_attachment.present? && temp_attachment.container_id.blank?
temp_attachment.update_attributes(:container_id => @statistic.id, :container_type => "Statistic")
end
end
end
2018-05-04 17:17:07 +08:00
if @statistic.file_size.nil? && @statistic.attachments.count > 0
@statistic.update_attribute('file_size', format("%0.3f", @statistic.attachments.map{|c| c.filesize}.sum.to_f/(1024*1024)))
end
2018-02-09 16:08:52 +08:00
format.html { redirect_to @statistic, notice: '更新成功' }
2018-02-05 12:03:35 +08:00
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @statistic.errors, status: :unprocessable_entity }
end
end
end
# DELETE /statistics/1
# DELETE /statistics/1.json
def destroy
@statistic.destroy
respond_to do |format|
format.html { redirect_to statistics_url }
format.json { head :no_content }
end
end
def get_sub_category
2018-02-08 16:25:49 +08:00
if params[:main_category_id].present?
2018-02-08 15:37:30 +08:00
main_category = MainCategory.find(params[:main_category_id])
@sub_categories = main_category.sub_categories
2018-02-08 16:25:49 +08:00
else
@sub_categories = SubCategory.all
2018-02-08 15:37:30 +08:00
end
2018-02-09 18:34:47 +08:00
if params[:come_from] == "statistics_index"
2018-02-09 18:38:07 +08:00
redirect_to statistics_url(:come_from => "statistics_index", :sub_category_id => (@sub_categories.present? ? nil : @sub_categories.map(&:id)), :main_category_id => params[:main_category_id])
2018-02-09 18:34:47 +08:00
else
respond_to do |format|
2018-02-09 17:00:12 +08:00
format.js
end
end
end
private
def get_date
@main_categories = MainCategory.all
@sub_categories = params[:main_category_id].present? ? SubCategory.where(:main_category_id => params[:main_category_id]) : SubCategory.all
end
2018-02-09 11:27:32 +08:00
def find_statistic
@statistic = Statistic.find(params[:id])
rescue ActiveRecord::RecordNotFound
render_404
2018-02-09 11:27:32 +08:00
end
def require_manager
render_403 unless (User.current.id == @statistic.user_id || User.current.admin?)
end
2018-02-05 12:03:35 +08:00
end