From 828f8dfd58684e12483cc0f76f9f0067a43dba8b Mon Sep 17 00:00:00 2001 From: Alon Pe'er Date: Sun, 24 Mar 2019 19:51:33 +0100 Subject: [PATCH] Restore using node.views for unique impressions count This should improve performance of various page loads, as it avoids running COUNT() queries on the impressions table, and instead uses the views value, which the impressions gem keeps up-to-date. --- app/models/node.rb | 6 ++---- test/functional/notes_controller_test.rb | 6 +++--- test/integration/node_unique_views_test.rb | 4 ++-- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/app/models/node.rb b/app/models/node.rb index 1b364d7ade..8ba28a8380 100644 --- a/app/models/node.rb +++ b/app/models/node.rb @@ -174,12 +174,10 @@ def setup public - is_impressionable counter_cache: true, column_name: :views + is_impressionable counter_cache: true, column_name: :views, unique: :ip_address def totalviews - # this doesn't filter out duplicate ip addresses as the line below does: - # self.views + self.legacy_views - impressionist_count(filter: :ip_address) + legacy_views + views + legacy_views end def self.weekly_tallies(type = 'note', span = 52, time = Time.now) diff --git a/test/functional/notes_controller_test.rb b/test/functional/notes_controller_test.rb index 68e0467585..0e0898e22d 100644 --- a/test/functional/notes_controller_test.rb +++ b/test/functional/notes_controller_test.rb @@ -81,7 +81,7 @@ def teardown assert_equal '0.0.0.0', Impression.last.ip_address Impression.last.update_attribute('ip_address', '0.0.0.1') - assert_difference 'note.totalviews', 1 do + assert_difference 'note.reload.totalviews', 1 do get :show, params: { author: note.author.name, @@ -90,10 +90,10 @@ def teardown } end - assert_equal 2, note.totalviews + assert_equal 2, note.reload.totalviews # same IP won't add to views twice - assert_difference 'note.totalviews', 0 do + assert_difference 'note.reload.totalviews', 0 do get :show, params: { author: note.author.name, diff --git a/test/integration/node_unique_views_test.rb b/test/integration/node_unique_views_test.rb index 92805f866e..d7d32673de 100644 --- a/test/integration/node_unique_views_test.rb +++ b/test/integration/node_unique_views_test.rb @@ -12,7 +12,7 @@ class NodeInsertExtrasTest < ActionDispatch::IntegrationTest assert_response :success end - assert_difference 'nodes(:about).totalviews', 0 do + assert_difference 'nodes(:about).reload.totalviews', 0 do assert_difference 'Impression.count', 0 do get "/wiki/#{nodes(:about).slug}" assert_response :success @@ -22,7 +22,7 @@ class NodeInsertExtrasTest < ActionDispatch::IntegrationTest assert_equal '127.0.0.1', Impression.last.ip_address assert Impression.last.update_attributes(ip_address: '0.0.0.0') - assert_difference 'nodes(:about).totalviews', 1 do + assert_difference 'nodes(:about).reload.totalviews', 1 do assert_difference 'Impression.count', 1 do get "/wiki/#{nodes(:about).slug}" assert_response :success