# Calculates average file size per attachment,
# And average total usage per imported row.

include ActionView::Helpers::NumberHelper

total_attachment_size = AttachmentData.sum(:file_size)
per_attachment = total_attachment_size / AttachmentData.count

puts "Total uploaded attachments: #{number_to_human_size(total_attachment_size)}"
puts "Size per attachment: #{number_to_human_size(per_attachment)}"

# Calculates average file size per attachment,
# And average total usage per imported row.

include ActionView::Helpers::NumberHelper

query = ActiveRecord::Base.connection.execute(%Q{
SELECT e.type, SUM(ad.file_size) FROM attachment_data ad
       LEFT JOIN attachments a ON a.attachment_data_id = ad.id
       LEFT JOIN edition_attachments ea ON ea.attachment_id = a.id
       LEFT JOIN editions e ON e.id = ea.edition_id
GROUP BY e.type;
                                              })
query.each do |row|
  next if row.first.nil?
  puts "#{row.first} -> #{number_to_human_size(row[1].to_f)}"
end

Speed up counting by using a JOIN
Speed up counting by using a JOIN

Note this will double count disk usage as it doesn't take into account
the fact that attachments are re-attached to each subsequent edition.
