PERF: Don't display days ago on timeline for megatopics.

Analysis using `pg_stat_statements` showed this query
to be eating up a significant portion of CPU.
This commit is contained in:
Guo Xiang Tan 2018-06-20 16:24:09 +08:00
parent cbdab71179
commit 9c925a66ff
3 changed files with 49 additions and 5 deletions

View File

@ -151,8 +151,14 @@ createWidget("timeline-scrollarea", {
const current = clamp(Math.floor(total * percentage) + 1, 1, total);
const daysAgo = postStream.closestDaysAgoFor(current);
const date = new Date();
date.setDate(date.getDate() - daysAgo || 0);
let date;
if (daysAgo !== null) {
date = new Date();
date.setDate(date.getDate() - daysAgo || 0);
} else {
date = null;
}
const result = {
current,

View File

@ -372,9 +372,18 @@ class TopicView
# Returns an array of [id, post_number, days_ago] tuples.
# `days_ago` is there for the timeline calculations.
def filtered_post_stream
@filtered_post_stream ||= @filtered_posts
.order(:sort_order)
.pluck(:id, :post_number, 'EXTRACT(DAYS FROM CURRENT_TIMESTAMP - created_at)::INT AS days_ago')
@filtered_post_stream ||= begin
posts = @filtered_posts
.order(:sort_order)
columns = [:id, :post_number]
if is_mega_topic?
columns << 'EXTRACT(DAYS FROM CURRENT_TIMESTAMP - created_at)::INT AS days_ago'
end
posts.pluck(*columns)
end
end
def filtered_post_ids
@ -554,4 +563,7 @@ class TopicView
filtered_post_ids.index(closest_post.first) || filtered_post_ids[0]
end
def is_mega_topic?
!@topic.private_message? && @topic.posts_count >= SiteSetting.auto_close_topics_post_count
end
end

View File

@ -549,4 +549,30 @@ describe TopicView do
end
end
end
describe '#filtered_post_stream' do
let!(:post) { Fabricate(:post, topic: topic, user: first_poster) }
let!(:post2) { Fabricate(:post, topic: topic, user: evil_trout) }
let!(:post3) { Fabricate(:post, topic: topic, user: first_poster) }
it 'should return the right columns' do
expect(topic_view.filtered_post_stream).to eq([
[post.id, post.post_number, 0],
[post2.id, post2.post_number, 0],
[post3.id, post3.post_number, 0]
])
end
describe 'for mega topics' do
it 'should return the right columns' do
SiteSetting.auto_close_topics_post_count = 2
expect(topic_view.filtered_post_stream).to eq([
[post.id, post.post_number],
[post2.id, post2.post_number],
[post3.id, post3.post_number]
])
end
end
end
end