|
- # encoding=utf-8
- class StatisticsController < ApplicationController
- # GET /statistics
- # GET /statistics.json
-
- include StatisticsHelper
- include ApplicationHelper
- layout 'base_statistic'
- before_filter :get_date, :only => [:index, :new]
- before_filter :require_login
- before_filter :find_statistic, :only => [:show, :edit, :update, :destroy]
- before_filter :require_manager, :only =>[:edit, :update, :destroy]
- before_filter :only => [:index, :new, :show, :edit] do
- set_nav_status(NavStatues::STATISTIC)
- end
-
- if RUBY_PLATFORM =~ /linux/
- require 'simple_xlsx_reader'
- end
-
- def index
- type = (params[:type] == "reorder_popu" ? "file_size" : "created_at")
- order = (params[:order] == "asc" ? "asc" : "desc")
- if params[:sub_category_id].present?
- @statistics = Statistic.where(:sub_category_id => params[:sub_category_id])
- sub_category_id = '(' + params[:sub_category_id].to_a.join(",") + ')'
- @statistics = Statistic.find_by_sql("SELECT statistics.* from statistics where sub_category_id in #{sub_category_id} order by #{type} #{order}")
- else
- if params[:main_category_id].present?
- @statistics = Statistic.find_by_sql("SELECT statistics.* from statistics where main_category_id = #{params[:main_category_id]} order by #{type} #{order}")
- @main_category_id = params[:main_category_id]
- else
- @statistics = Statistic.find_by_sql("SELECT statistics.* from statistics order by #{type} #{order}")
- end
- end
- @statistics_count = @statistics.size
- @data_size = data_format_string @statistics.select{|c| !c.file_size.nil?}.map{|s| s.file_size}.sum
- @limit = 10
- @is_remote = true
- @statistics_pages = Paginator.new @statistics_count, @limit, params['page'] || 1
- @offset ||= @statistics_pages.offset
- @statistics = paginateHelper @statistics, @limit
- respond_to do |format|
- format.js
- format.html # index.html.erb
- end
- end
-
- 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
- r_index= description.rindex("|").to_i
- 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
-
- # GET /statistics/1
- # GET /statistics/1.json
- def show
- @attachments = @statistic.attachments
- 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
- @sub_categories = @main_categories.first.sub_categories
- 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
- end
-
- # POST /statistics
- # POST /statistics.json
- def create
- @statistic = Statistic.new(params[:statistic])
- @statistic.user_id = User.current.id
- @statistic.save_attachments_containers(params[:attachments], User.current, true)
- respond_to do |format|
- if @statistic.save
- 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: '创建成功' }
- 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
-
- respond_to do |format|
- if @statistic.update_attributes(params[:statistic])
- # @statistic.save_attachments_containers(params[:attachments], User.current, true)
- 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
-
- 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: '更新成功' }
- 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
- if params[:main_category_id].present?
- main_category = MainCategory.find(params[:main_category_id])
- @sub_categories = main_category.sub_categories
- else
- @sub_categories = SubCategory.all
- end
- if params[:come_from] == "statistics_index"
- 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])
- else
- respond_to do |format|
- 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
-
- def find_statistic
- @statistic = Statistic.find(params[:id])
- rescue ActiveRecord::RecordNotFound
- render_404
- end
-
- def require_manager
- render_403 unless (User.current.id == @statistic.user_id || User.current.admin?)
- end
- end
|