Skip to content

Commit

Permalink
Use node views cache (#5338)
Browse files Browse the repository at this point in the history
* 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.

* Added a migration which updates node.views to the count of unique visits
  • Loading branch information
alonpeer authored and jywarren committed Apr 18, 2019
1 parent 209b761 commit 6eb5897
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 deletions.
6 changes: 2 additions & 4 deletions app/models/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,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)
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20190401093400_update_node_view_count.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class UpdateNodeViewCount < ActiveRecord::Migration[5.2]
def up
Node.ids.each do |id|
node = Node.find(id)
node.update_columns(views: node.impressionist_count(filter: :ip_address))
end
end

def down
end
end
2 changes: 1 addition & 1 deletion db/schema.rb.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_03_30_043340) do
ActiveRecord::Schema.define(version: 2019_04_01_093400) do

create_table "answer_selections", force: true do |t|
t.integer "user_id"
Expand Down
6 changes: 3 additions & 3 deletions test/functional/notes_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions test/integration/node_unique_views_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 6eb5897

Please sign in to comment.