FEATURE: Add Top Uploads report (#6825)

Co-Authored-By: I am very Pro-Grammer. <khalilovcmded@users.noreply.github.com>
This commit is contained in:
Joffrey JAFFEUX
2018-12-28 20:48:54 +01:00
committed by GitHub
parent 608abd4c08
commit f1269fa807
5 changed files with 136 additions and 2 deletions

View File

@@ -1433,6 +1433,72 @@ class Report
}
end
def self.report_top_uploads(report)
report.modes = [:table]
report.labels = [
{
type: :link,
properties: [
:file_url,
:file_name,
],
title: I18n.t("reports.top_uploads.labels.filename")
},
{
type: :user,
properties: {
username: :author_username,
id: :author_id,
avatar: :author_avatar_template,
},
title: I18n.t("reports.top_uploads.labels.author")
},
{
type: :text,
property: :extension,
title: I18n.t("reports.top_uploads.labels.extension")
},
{
type: :bytes,
property: :filesize,
title: I18n.t("reports.top_uploads.labels.filesize")
},
]
report.data = []
sql = <<~SQL
SELECT
u.id as user_id,
u.username,
u.uploaded_avatar_id,
up.filesize,
up.original_filename,
up.extension,
up.url
FROM uploads up
JOIN users u
ON u.id = up.user_id
WHERE up.created_at >= '#{report.start_date}' AND up.created_at <= '#{report.end_date}'
ORDER BY up.filesize DESC
LIMIT #{report.limit || 250}
SQL
DB.query(sql).each do |row|
data = {}
data[:author_id] = row.user_id
data[:author_username] = row.username
data[:author_avatar_template] = User.avatar_template(row.username, row.uploaded_avatar_id)
data[:filesize] = row.filesize
data[:extension] = row.extension
data[:file_url] = Discourse.store.cdn_url(row.url)
data[:file_name] = row.original_filename.truncate(25)
report.data << data
end
end
DiscourseEvent.on(:site_setting_saved) do |site_setting|
if ["backup_location", "s3_backup_bucket"].include?(site_setting.name.to_s)
clear_cache(:storage_stats)