From a2cca2540efc9a5b7a2091a98fb2e5153d395ca5 Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 5 Apr 2013 17:43:48 +1100 Subject: [PATCH] some minimal site settings diags fix issue where days_visited was totally out of sync --- .../javascripts/admin/models/site_setting.js | 3 ++- .../admin/routes/admin_site_settings_route.js | 4 ++-- .../admin/templates/site_settings.js.handlebars | 3 +++ .../admin/site_settings_controller.rb | 5 +++-- app/models/user.rb | 4 ++-- app/models/user_visit.rb | 13 +++++++++++++ lib/jobs/ensure_db_consistency.rb | 1 + lib/site_setting_extension.rb | 9 ++++++++- spec/models/user_visit_spec.rb | 16 ++++++++++++++++ 9 files changed, 50 insertions(+), 8 deletions(-) diff --git a/app/assets/javascripts/admin/models/site_setting.js b/app/assets/javascripts/admin/models/site_setting.js index 617421a8cb6..79eeeca7d2b 100644 --- a/app/assets/javascripts/admin/models/site_setting.js +++ b/app/assets/javascripts/admin/models/site_setting.js @@ -91,10 +91,11 @@ Discourse.SiteSetting.reopenClass({ findAll: function() { var result = Em.A(); Discourse.ajax({url: Discourse.getURL("/admin/site_settings")}).then(function (settings) { - settings.each(function(s) { + settings.site_settings.each(function(s) { s.originalValue = s.value; result.pushObject(Discourse.SiteSetting.create(s)); }); + result.set('diags', settings.diags); }); return result; } diff --git a/app/assets/javascripts/admin/routes/admin_site_settings_route.js b/app/assets/javascripts/admin/routes/admin_site_settings_route.js index f572d53b3b2..1d6959ebe5e 100644 --- a/app/assets/javascripts/admin/routes/admin_site_settings_route.js +++ b/app/assets/javascripts/admin/routes/admin_site_settings_route.js @@ -1,7 +1,7 @@ /** Handles routes related to viewing and editing site settings. - @class AdminSiteSettingsRoute + @class AdminSiteSettingsRoute @extends Discourse.Route @namespace Discourse @module Discourse @@ -13,5 +13,5 @@ Discourse.AdminSiteSettingsRoute = Discourse.Route.extend({ renderTemplate: function() { this.render('admin/templates/site_settings', {into: 'admin/templates/admin'}); - } + } }); diff --git a/app/assets/javascripts/admin/templates/site_settings.js.handlebars b/app/assets/javascripts/admin/templates/site_settings.js.handlebars index ac24d512836..dfc10a508ba 100644 --- a/app/assets/javascripts/admin/templates/site_settings.js.handlebars +++ b/app/assets/javascripts/admin/templates/site_settings.js.handlebars @@ -12,3 +12,6 @@ {{collection contentBinding="filteredContent" classNames="form-horizontal settings" itemViewClass="Discourse.SiteSettingView"}} + + +

Diagnostics: last_message_processed {{content.diags.last_message_processed}}

diff --git a/app/controllers/admin/site_settings_controller.rb b/app/controllers/admin/site_settings_controller.rb index dbda5ecc6a7..5c3c8ccbccc 100644 --- a/app/controllers/admin/site_settings_controller.rb +++ b/app/controllers/admin/site_settings_controller.rb @@ -1,8 +1,9 @@ class Admin::SiteSettingsController < Admin::AdminController def index - @site_settings = SiteSetting.all_settings - render_json_dump(@site_settings.as_json) + site_settings = SiteSetting.all_settings + info = {site_settings: site_settings, diags: SiteSetting.diags } + render_json_dump(info.as_json) end def update diff --git a/app/models/user.rb b/app/models/user.rb index 0c83a1cff73..e80172d8f8b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -299,7 +299,7 @@ class User < ActiveRecord::Base unless seen?(date) || has_visit_record?(date) adding_visit_record(date) - User.increment_counter(:days_visited, 1) + User.update_all('days_visited = days_visited + 1', id: self.id) end end @@ -493,7 +493,7 @@ class User < ActiveRecord::Base .where(['id in ( SELECT topic_id FROM posts p JOIN topics t2 ON t2.id = p.topic_id - WHERE deleted_at IS NULL AND + WHERE p.deleted_at IS NULL AND t2.user_id <> p.user_id AND p.user_id = ? )', self.id]) diff --git a/app/models/user_visit.rb b/app/models/user_visit.rb index 8b13566dbf1..d310ddaa4ca 100644 --- a/app/models/user_visit.rb +++ b/app/models/user_visit.rb @@ -5,4 +5,17 @@ class UserVisit < ActiveRecord::Base def self.by_day(sinceDaysAgo=30) where("visited_at >= ?", sinceDaysAgo.days.ago).group(:visited_at).order(:visited_at).count end + + def self.ensure_consistency! + exec_sql < + ( + SELECT COUNT(*) FROM user_visits v WHERE v.user_id = u.id + ) +SQL + end end diff --git a/lib/jobs/ensure_db_consistency.rb b/lib/jobs/ensure_db_consistency.rb index 024787c324b..df414823bf3 100644 --- a/lib/jobs/ensure_db_consistency.rb +++ b/lib/jobs/ensure_db_consistency.rb @@ -3,6 +3,7 @@ module Jobs class EnsureDbConsistency < Jobs::Base def execute(args) TopicUser.ensure_consistency! + UserVisit.ensure_consistency! end end end diff --git a/lib/site_setting_extension.rb b/lib/site_setting_extension.rb index 301377d2aef..c3c573d86b0 100644 --- a/lib/site_setting_extension.rb +++ b/lib/site_setting_extension.rb @@ -115,6 +115,7 @@ module SiteSettingExtension message = msg.data if message["process"] != pid begin + @last_message_processed = msg.global_id # picks a db MessageBus.on_connect.call(msg.site_id) SiteSetting.refresh! @@ -127,6 +128,12 @@ module SiteSettingExtension end end + def diags + { + last_message_processed: @last_message_processed + } + end + def process_id @@process_id ||= SecureRandom.uuid end @@ -162,7 +169,7 @@ module SiteSettingExtension SiteSetting.create!(name: name, value: val, data_type: type) end - MessageBus.publish('/site_settings', {process: process_id}) + @last_message_sent = MessageBus.publish('/site_settings', {process: process_id}) end diff --git a/spec/models/user_visit_spec.rb b/spec/models/user_visit_spec.rb index 017a1b4e6dc..0a5e836e8b0 100644 --- a/spec/models/user_visit_spec.rb +++ b/spec/models/user_visit_spec.rb @@ -1,4 +1,20 @@ require 'spec_helper' describe UserVisit do + it 'can ensure consistency' do + u = Fabricate(:user) + u.update_visit_record!(2.weeks.ago.to_date) + u.last_seen_at = 2.weeks.ago + u.save + u.update_visit_record!(1.day.ago.to_date) + + u.reload + u.days_visited.should == 2 + + u.days_visited = 1 + UserVisit.ensure_consistency! + + u.reload + u.days_visited.should == 2 + end end