mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FIX: Correct query for post_edits dashboard report
- Use query builder to improve readability - Remove subquery, so that all `where` filters happen alongside the limit - Add 'edited at' column to the report
This commit is contained in:
parent
6a65e5312b
commit
572e928cba
@ -7,6 +7,11 @@ Report.add_report('post_edits') do |report|
|
|||||||
report.modes = [:table]
|
report.modes = [:table]
|
||||||
|
|
||||||
report.labels = [
|
report.labels = [
|
||||||
|
{
|
||||||
|
type: :date,
|
||||||
|
property: :created_at,
|
||||||
|
title: I18n.t("reports.post_edits.labels.edited_at")
|
||||||
|
},
|
||||||
{
|
{
|
||||||
type: :post,
|
type: :post,
|
||||||
properties: {
|
properties: {
|
||||||
@ -43,55 +48,49 @@ Report.add_report('post_edits') do |report|
|
|||||||
|
|
||||||
report.data = []
|
report.data = []
|
||||||
|
|
||||||
sql = <<~SQL
|
builder = DB.build <<~SQL
|
||||||
WITH period_revisions AS (
|
SELECT
|
||||||
SELECT pr.user_id AS editor_id,
|
pr.user_id AS editor_id,
|
||||||
pr.number AS revision_version,
|
editor.username AS editor_username,
|
||||||
pr.created_at,
|
editor.uploaded_avatar_id AS editor_avatar_id,
|
||||||
pr.post_id,
|
|
||||||
u.username AS editor_username,
|
|
||||||
u.uploaded_avatar_id as editor_avatar_id
|
|
||||||
FROM post_revisions pr
|
|
||||||
JOIN users u
|
|
||||||
ON u.id = pr.user_id
|
|
||||||
WHERE u.id > 0
|
|
||||||
AND pr.created_at >= '#{report.start_date}'
|
|
||||||
AND pr.created_at <= '#{report.end_date}'
|
|
||||||
ORDER BY pr.created_at DESC
|
|
||||||
LIMIT #{report.limit || 20}
|
|
||||||
)
|
|
||||||
SELECT pr.editor_id,
|
|
||||||
pr.editor_username,
|
|
||||||
pr.editor_avatar_id,
|
|
||||||
p.user_id AS author_id,
|
p.user_id AS author_id,
|
||||||
u.username AS author_username,
|
author.username AS author_username,
|
||||||
u.uploaded_avatar_id AS author_avatar_id,
|
author.uploaded_avatar_id AS author_avatar_id,
|
||||||
pr.revision_version,
|
pr.number AS revision_version,
|
||||||
p.version AS post_version,
|
p.version AS post_version,
|
||||||
pr.post_id,
|
pr.post_id,
|
||||||
left(p.raw, 40) AS post_raw,
|
LEFT(p.raw, 40) AS post_raw,
|
||||||
p.topic_id,
|
p.topic_id,
|
||||||
p.post_number,
|
p.post_number,
|
||||||
p.edit_reason,
|
p.edit_reason,
|
||||||
pr.created_at
|
pr.created_at
|
||||||
FROM period_revisions pr
|
FROM post_revisions pr
|
||||||
JOIN posts p
|
JOIN posts p
|
||||||
ON p.id = pr.post_id
|
ON p.id = pr.post_id
|
||||||
JOIN users u
|
JOIN users author
|
||||||
ON u.id = p.user_id
|
ON author.id = p.user_id
|
||||||
|
JOIN users editor
|
||||||
|
ON editor.id = pr.user_id
|
||||||
|
/*join*/
|
||||||
|
/*where*/
|
||||||
|
/*limit*/
|
||||||
SQL
|
SQL
|
||||||
|
|
||||||
if category_filter
|
if category_filter
|
||||||
sql += <<~SQL
|
builder.join "topics t ON t.id = p.topic_id"
|
||||||
JOIN topics t
|
builder.where("t.category_id = :category_id
|
||||||
ON t.id = p.topic_id
|
OR t.category_id IN (
|
||||||
WHERE p.user_id != editor_id AND t.category_id = ? OR t.category_id IN (SELECT id FROM categories WHERE categories.parent_category_id = ?)
|
SELECT id FROM categories
|
||||||
SQL
|
WHERE categories.parent_category_id = :category_id
|
||||||
else
|
)", category_id: category_filter)
|
||||||
sql += "WHERE p.user_id != editor_id"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
result = category_filter ? DB.query(sql, category_filter, category_filter) : DB.query(sql)
|
builder.where("editor.id > 0 AND editor.id != author.id")
|
||||||
|
builder.where("pr.created_at >= :start_date", start_date: report.start_date)
|
||||||
|
builder.where("pr.created_at <= :end_date", end_date: report.end_date)
|
||||||
|
builder.limit(report.limit || 20)
|
||||||
|
|
||||||
|
result = builder.query
|
||||||
|
|
||||||
result.each do |r|
|
result.each do |r|
|
||||||
revision = {}
|
revision = {}
|
||||||
|
@ -974,6 +974,7 @@ en:
|
|||||||
post_edits:
|
post_edits:
|
||||||
title: "Post Edits"
|
title: "Post Edits"
|
||||||
labels:
|
labels:
|
||||||
|
edited_at: Date
|
||||||
post: Post
|
post: Post
|
||||||
editor: Editor
|
editor: Editor
|
||||||
author: Author
|
author: Author
|
||||||
|
Loading…
Reference in New Issue
Block a user