/moderator/dashboard: optimize sql queries
* Converts queries to use active record instead of raw sql. This ensures that user objects are loaded by rails in the join, so that we don't have to issue `User.find` calls to load users one-by-one. * Use `.includes` to preload associations used in the view, to avoid additional N+1 query problems (primarily, calls to link_to_user also causing users to be loaded one-by-one).
This commit is contained in:
@@ -1,28 +1,16 @@
|
||||
module Moderator
|
||||
module Dashboard
|
||||
module Queries
|
||||
class Artist
|
||||
attr_reader :user, :count
|
||||
|
||||
class Artist < ::Struct.new(:user, :count)
|
||||
def self.all(min_date, max_level)
|
||||
sql = <<-EOS
|
||||
SELECT artist_versions.updater_id AS updater_id, count(*)
|
||||
FROM artist_versions
|
||||
JOIN users ON users.id = artist_versions.updater_id
|
||||
WHERE
|
||||
artist_versions.created_at > ?
|
||||
AND users.level <= ?
|
||||
GROUP BY artist_versions.updater_id
|
||||
ORDER BY count(*) DESC
|
||||
LIMIT 10
|
||||
EOS
|
||||
|
||||
ActiveRecord::Base.select_all_sql(sql, min_date, max_level).map {|x| new(x)}
|
||||
end
|
||||
|
||||
def initialize(hash)
|
||||
@user = ::User.find(hash["updater_id"])
|
||||
@count = hash["count"]
|
||||
::ArtistVersion.joins(:updater)
|
||||
.where("artist_versions.created_at > ?", min_date)
|
||||
.where("users.level <= ?", max_level)
|
||||
.group(:updater)
|
||||
.order("count(*) desc")
|
||||
.limit(10)
|
||||
.count
|
||||
.map { |user, count| new(user, count) }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,31 +1,18 @@
|
||||
module Moderator
|
||||
module Dashboard
|
||||
module Queries
|
||||
class Comment
|
||||
attr_reader :comment, :count
|
||||
|
||||
class Comment < ::Struct.new(:comment, :count)
|
||||
def self.all(min_date, max_level)
|
||||
sql = <<-EOS
|
||||
SELECT comment_votes.comment_id, count(*)
|
||||
FROM comment_votes
|
||||
JOIN comments ON comments.id = comment_id
|
||||
JOIN users ON users.id = comments.creator_id
|
||||
WHERE
|
||||
comment_votes.created_at > ?
|
||||
AND comments.score < 0
|
||||
AND users.level <= ?
|
||||
GROUP BY comment_votes.comment_id
|
||||
HAVING count(*) >= 3
|
||||
ORDER BY count(*) DESC
|
||||
LIMIT 10
|
||||
EOS
|
||||
|
||||
ActiveRecord::Base.select_all_sql(sql, min_date, max_level).map {|x| new(x)}
|
||||
end
|
||||
|
||||
def initialize(hash)
|
||||
@comment = ::Comment.find(hash["comment_id"])
|
||||
@count = hash["count"]
|
||||
::CommentVote.joins(comment: [:creator])
|
||||
.where("comments.score < 0")
|
||||
.where("comment_votes.created_at > ?", min_date)
|
||||
.where("users.level <= ?", max_level)
|
||||
.group(:comment)
|
||||
.having("count(*) >= 3")
|
||||
.order("count(*) desc")
|
||||
.limit(10)
|
||||
.count
|
||||
.map { |comment, count| new(comment, count) }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,7 +3,7 @@ module Moderator
|
||||
module Queries
|
||||
class ModAction
|
||||
def self.all
|
||||
::ModAction.order("id desc").limit(10)
|
||||
::ModAction.includes(:creator).order("id desc").limit(10)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,28 +1,16 @@
|
||||
module Moderator
|
||||
module Dashboard
|
||||
module Queries
|
||||
class Note
|
||||
attr_reader :user, :count
|
||||
|
||||
class Note < ::Struct.new(:user, :count)
|
||||
def self.all(min_date, max_level)
|
||||
sql = <<-EOS
|
||||
SELECT note_versions.updater_id, count(*)
|
||||
FROM note_versions
|
||||
JOIN users ON users.id = note_versions.updater_id
|
||||
WHERE
|
||||
note_versions.created_at > ?
|
||||
AND users.level <= ?
|
||||
GROUP BY note_versions.updater_id
|
||||
ORDER BY count(*) DESC
|
||||
LIMIT 10
|
||||
EOS
|
||||
|
||||
ActiveRecord::Base.select_all_sql(sql, min_date, max_level).map {|x| new(x)}
|
||||
end
|
||||
|
||||
def initialize(hash)
|
||||
@user = ::User.find(hash["updater_id"])
|
||||
@count = hash["count"]
|
||||
::NoteVersion.joins(:updater)
|
||||
.where("note_versions.created_at > ?", min_date)
|
||||
.where("users.level <= ?", max_level)
|
||||
.group(:updater)
|
||||
.order("count(*) desc")
|
||||
.limit(10)
|
||||
.count
|
||||
.map { |user, count| new(user, count) }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2,28 +2,13 @@ module Moderator
|
||||
module Dashboard
|
||||
module Queries
|
||||
class PostAppeal
|
||||
attr_reader :post, :count
|
||||
|
||||
def self.all(min_date)
|
||||
sql = <<-EOS
|
||||
SELECT post_appeals.post_id, count(*)
|
||||
FROM post_appeals
|
||||
JOIN posts ON posts.id = post_appeals.post_id
|
||||
WHERE
|
||||
post_appeals.created_at > ?
|
||||
and posts.is_deleted = true
|
||||
and posts.is_pending = false
|
||||
GROUP BY post_appeals.post_id
|
||||
ORDER BY count(*) DESC
|
||||
LIMIT 10
|
||||
EOS
|
||||
|
||||
ActiveRecord::Base.select_all_sql(sql, min_date).map {|x| new(x)}
|
||||
end
|
||||
|
||||
def initialize(hash)
|
||||
@post = ::Post.find(hash["post_id"])
|
||||
@count = hash["count"]
|
||||
::Post.joins(:appeals).includes(:uploader, :flags, appeals: [:creator])
|
||||
.deleted
|
||||
.where("post_appeals.created_at > ?", min_date)
|
||||
.group(:id)
|
||||
.order("count(*) desc")
|
||||
.limit(10)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
module Moderator
|
||||
module Dashboard
|
||||
module Queries
|
||||
class Tag
|
||||
attr_reader :user, :count
|
||||
|
||||
class Tag < ::Struct.new(:user, :count)
|
||||
def self.all(min_date, max_level)
|
||||
return [] unless PostArchive.enabled?
|
||||
|
||||
@@ -13,10 +11,6 @@ module Moderator
|
||||
|
||||
records.select { |rec| rec.user.level <= max_level }.sort_by(&:count).reverse.take(10)
|
||||
end
|
||||
|
||||
def initialize(user, count)
|
||||
@user, @count = user, count
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,28 +1,16 @@
|
||||
module Moderator
|
||||
module Dashboard
|
||||
module Queries
|
||||
class Upload
|
||||
attr_reader :user, :count
|
||||
|
||||
class Upload < ::Struct.new(:user, :count)
|
||||
def self.all(min_date, max_level)
|
||||
sql = <<-EOS
|
||||
select uploader_id, count(*)
|
||||
from posts
|
||||
join users on uploader_id = users.id
|
||||
where
|
||||
posts.created_at > ?
|
||||
and level <= ?
|
||||
group by posts.uploader_id
|
||||
order by count(*) desc
|
||||
limit 10
|
||||
EOS
|
||||
|
||||
ActiveRecord::Base.select_all_sql(sql, min_date, max_level).map {|x| new(x)}
|
||||
end
|
||||
|
||||
def initialize(hash)
|
||||
@user = ::User.find(hash["uploader_id"])
|
||||
@count = hash["count"]
|
||||
::Post.joins(:uploader)
|
||||
.where("posts.created_at > ?", min_date)
|
||||
.where("users.level <= ?", max_level)
|
||||
.group(:uploader)
|
||||
.order("count(*) desc")
|
||||
.limit(10)
|
||||
.count
|
||||
.map { |user, count| new(user, count) }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,7 +3,7 @@ module Moderator
|
||||
module Queries
|
||||
class UserFeedback
|
||||
def self.all
|
||||
::UserFeedback.order("id desc").limit(10)
|
||||
::UserFeedback.includes(:user).order("id desc").limit(10)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,28 +1,16 @@
|
||||
module Moderator
|
||||
module Dashboard
|
||||
module Queries
|
||||
class WikiPage
|
||||
attr_reader :user, :count
|
||||
|
||||
class WikiPage < ::Struct.new(:user, :count)
|
||||
def self.all(min_date, max_level)
|
||||
sql = <<-EOS
|
||||
SELECT wiki_page_versions.updater_id, count(*)
|
||||
FROM wiki_page_versions
|
||||
JOIN users ON users.id = wiki_page_versions.updater_id
|
||||
WHERE
|
||||
wiki_page_versions.created_at > ?
|
||||
AND users.level <= ?
|
||||
GROUP BY wiki_page_versions.updater_id
|
||||
ORDER BY count(*) DESC
|
||||
LIMIT 10
|
||||
EOS
|
||||
|
||||
ActiveRecord::Base.select_all_sql(sql, min_date, max_level).map {|x| new(x)}
|
||||
end
|
||||
|
||||
def initialize(hash)
|
||||
@user = ::User.find(hash["updater_id"])
|
||||
@count = hash["count"]
|
||||
::WikiPageVersion.joins(:updater)
|
||||
.where("wiki_page_versions.created_at > ?", min_date)
|
||||
.where("users.level <= ?", max_level)
|
||||
.group(:updater)
|
||||
.order("count(*) desc")
|
||||
.limit(10)
|
||||
.count
|
||||
.map { |user, count| new(user, count) }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -10,13 +10,13 @@
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<% @dashboard.appeals.each do |appeal| %>
|
||||
<% @dashboard.appeals.each do |post| %>
|
||||
<tr>
|
||||
<td><%= link_to image_tag(appeal.post.preview_file_url), post_path(appeal.post) %></td>
|
||||
<td><%= mod_link_to_user appeal.post.uploader, :negative %></td>
|
||||
<td><%= post_flag_reasons(appeal.post) %></td>
|
||||
<td><%= post_appeal_reasons(appeal.post) %></td>
|
||||
<td><%= appeal.post.score %></td>
|
||||
<td><%= link_to image_tag(post.preview_file_url), post_path(post) %></td>
|
||||
<td><%= mod_link_to_user post.uploader, :negative %></td>
|
||||
<td><%= post_flag_reasons(post) %></td>
|
||||
<td><%= post_appeal_reasons(post) %></td>
|
||||
<td><%= post.score %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
|
||||
Reference in New Issue
Block a user