数据增加数据源大小

This commit is contained in:
cxt 2018-05-04 17:17:07 +08:00
parent 6e95dc6a37
commit 075091beda
6 changed files with 38 additions and 14 deletions

View File

@ -9,20 +9,17 @@ class StatisticsController < ApplicationController
before_filter :require_manager, :only =>[:edit, :update, :destroy]
def index
type = (params[:type] == "reorder_popu" ? "size" : "created_at")
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.*, (select sum(filesize) from attachments where attachments.container_type='Statistic' and
attachments.container_id = statistics.id group by attachments.container_id) as size from statistics where sub_category_id in #{sub_category_id} order by #{type} #{order}")
@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.*, (select sum(filesize) from attachments where attachments.container_type='Statistic' and
attachments.container_id = statistics.id group by attachments.container_id) as size from statistics where main_category_id = #{params[:main_category_id]} order by #{type} #{order}")
@statistics = Statistic.find_by_sql("SELECT statistics.* from statistics where main_category_id = #{params[:main_category_id]} order by #{type} #{order}")
else
@statistics = Statistic.find_by_sql("SELECT statistics.*, (select sum(filesize) from attachments where attachments.container_type='Statistic' and
attachments.container_id = statistics.id group by attachments.container_id) as size from statistics order by #{type} #{order}")
@statistics = Statistic.find_by_sql("SELECT statistics.* from statistics order by #{type} #{order}")
end
end
@statistics_count = @statistics.count
@ -78,6 +75,9 @@ class StatisticsController < ApplicationController
@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
@ -103,6 +103,10 @@ class StatisticsController < ApplicationController
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

View File

@ -1,5 +1,5 @@
class Statistic < ActiveRecord::Base
attr_accessible :description, :name, :status, :user_id, :main_category_id, :sub_category_id
attr_accessible :description, :name, :status, :user_id, :main_category_id, :sub_category_id, :file_size
acts_as_attachable
validates_presence_of :main_category_id

View File

@ -31,9 +31,9 @@
</div>
</div>
<div class="field line_field">
<label class="field_lab"><span>*</span>数据大小:</label>
<%= f.text_field :name, :class => "wb85" %>
<p class="cl" style="padding-left: 10%;color: red;height: 20px"><span id="input_name_notice" class="none">请输入数据名称</span></p>
<label class="field_lab">数据大小:</label>
<%= f.text_field :file_size, :placeholder => "单位M", :class => "wb85" %>
<p class="cl" style="padding-left: 10%;color: red;height: 20px"><span id="input_file_size_notice" class="none">请输入正数</span></p>
</div>
<div class="field line_field">
@ -112,10 +112,14 @@
function submit_cate(){
var name=$("#statistic_name").val();
var file_size=$("#statistic_file_size").val().trim();
var cate=$("#statistic_main_category_id option:selected").text();
if(name == ""){
$("#input_name_notice").removeClass("none");
return;
} else if(file_size != "" && !/^(?:[1-9][0-9]*\.[0-9]+|0\.(?!0+$)[0-9]+|[1-9]+\d*)$/.test(file_size)){
$("#input_file_size_notice").show();
return;
}
$("#new_statistic").submit();
$("#edit_statistic_<%= @statistic.id %>").submit();

View File

@ -25,8 +25,8 @@
<% if statistic.sub_category.present? %>
<span><%= statistic.sub_category.try(:name) %></span>
<% end %>
<% if statistic.size.present? %>
<span><%= number_to_human_size statistic.size %>&nbsp;Bytes</span>
<% if statistic.file_size.present? %>
<span><%= statistic.file_size %>&nbsp;MB</span>
<% end %>
<span><%= format_time statistic.created_at %></span>
</div>

View File

@ -0,0 +1,11 @@
class AddFileSizeToStatistics < ActiveRecord::Migration
def change
add_column :statistics, :file_size, :float
Statistic.all.each do |statistic|
if statistic.attachments.count > 0
statistic.update_attribute('file_size', format("%0.3f",statistic.attachments.map{|c| c.filesize}.sum.to_f/(1024*1024)))
end
end
end
end

View File

@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20180207065530) do
ActiveRecord::Schema.define(:version => 20180504075834) do
create_table "activities", :force => true do |t|
t.integer "act_id", :null => false
@ -1133,6 +1133,9 @@ ActiveRecord::Schema.define(:version => 20180207065530) do
t.integer "score", :default => -1
end
add_index "exercise_answers", ["exercise_choice_id"], :name => "ec"
add_index "exercise_answers", ["exercise_question_id", "user_id"], :name => "ea"
create_table "exercise_bank_choices", :force => true do |t|
t.integer "exercise_bank_question_id"
t.text "choice_text"
@ -1221,6 +1224,8 @@ ActiveRecord::Schema.define(:version => 20180207065530) do
t.integer "subjective_score", :default => -1
end
add_index "exercise_users", ["exercise_id"], :name => "index_exercise_users_on_exercise_id"
create_table "exercises", :force => true do |t|
t.text "exercise_name"
t.text "exercise_description"