diff --git a/Gemfile.lock b/Gemfile.lock index c298d66ad..c1f63c7cd 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -157,7 +157,7 @@ GEM thor (~> 0.15) libv8 (3.16.14.3) listen (0.7.3) - logster (0.1.2) + logster (0.1.3) lru_redux (0.8.1) mail (2.5.4) mime-types (~> 1.16) diff --git a/app/models/topic_view_item.rb b/app/models/topic_view_item.rb index a3bbef6d0..07fdd5204 100644 --- a/app/models/topic_view_item.rb +++ b/app/models/topic_view_item.rb @@ -6,7 +6,7 @@ class TopicViewItem < ActiveRecord::Base belongs_to :user validates_presence_of :topic_id, :ip_address, :viewed_at - def self.add(topic_id, ip, user_id, at=nil) + def self.add(topic_id, ip, user_id=nil, at=nil, skip_redis=false) # Only store a view once per day per thing per user per ip redis_key = "view:#{topic_id}:#{Date.today.to_s}" if user_id @@ -15,11 +15,13 @@ class TopicViewItem < ActiveRecord::Base redis_key << ":ip-#{ip}" end - if $redis.setnx(redis_key, "1") - $redis.expire(redis_key, 1.day.to_i) + if skip_redis || $redis.setnx(redis_key, "1") + skip_redis || $redis.expire(redis_key, 1.day.to_i) TopicViewItem.transaction do at ||= Date.today + + # AR likes logging failures here, we don't need that TopicViewItem.create!(topic_id: topic_id, ip_address: ip, viewed_at: at, user_id: user_id) # Update the views count in the parent, if it exists. diff --git a/config/initializers/logster.rb b/config/initializers/logster.rb index 633dffaf0..9176d258a 100644 --- a/config/initializers/logster.rb +++ b/config/initializers/logster.rb @@ -1,10 +1,13 @@ if Rails.env.production? - # Logster.store.ignore = [ - # # honestly, Rails should not be logging this, its real noisy - # /^ActionController::RoutingError \(No route matches/, - # # suppress trackback spam bots - # Logster::IgnorePattern.new("Can't verify CSRF token authenticity", { REQUEST_URI: /\/trackback\/$/ }) - # ] + Logster.store.ignore = [ + # honestly, Rails should not be logging this, its real noisy + /^ActionController::RoutingError \(No route matches/, + + /^PG::Error: ERROR:\s+duplicate key/, + + # suppress trackback spam bots + Logster::IgnorePattern.new("Can't verify CSRF token authenticity", { REQUEST_URI: /\/trackback\/$/ }) + ] Logster.config.authorize_callback = lambda{|env| user = CurrentUser.lookup_from_env(env) diff --git a/spec/models/topic_view_item_spec.rb b/spec/models/topic_view_item_spec.rb index d6ae9531f..14c980f9a 100644 --- a/spec/models/topic_view_item_spec.rb +++ b/spec/models/topic_view_item_spec.rb @@ -1,5 +1,20 @@ require 'spec_helper' -describe TopicView do +describe TopicViewItem do + + def add(topic_id, ip, user_id=nil) + skip_redis = true + TopicViewItem.add(topic_id, ip, user_id, nil, skip_redis) + end + + it "raises nothing for dupes" do + add(2, "1.1.1.1") + add(2, "1.1.1.1", 1) + + TopicViewItem.create!(topic_id: 1, ip_address: "1.1.1.1", viewed_at: 1.day.ago) + add(1, "1.1.1.1") + + TopicViewItem.count.should == 3 + end end