Fetch the list of problems more frequently on the admin dashboard

This commit is contained in:
Neil Lalonde 2013-03-29 15:48:26 -04:00
parent df85201298
commit 25073e873f
6 changed files with 96 additions and 4 deletions

View File

@ -1,6 +1,23 @@
/**
A model that stores all or some data that is displayed on the dashboard.
@class AdminDashboard
@extends Discourse.Model
@namespace Discourse
@module Discourse
**/
Discourse.AdminDashboard = Discourse.Model.extend({});
Discourse.AdminDashboard.reopenClass({
/**
Fetch all dashboard data. This can be an expensive request when the cached data
has expired and the server must collect the data again.
@method find
@return {jqXHR} a jQuery Promise object
**/
find: function() {
var model = Discourse.AdminDashboard.create();
return $.ajax(Discourse.getURL("/admin/dashboard"), {
@ -11,5 +28,24 @@ Discourse.AdminDashboard.reopenClass({
model.set('loaded', true);
}
});
},
/**
Only fetch the list of problems that should be rendered on the dashboard.
The model will only have its "problems" attribute set.
@method fetchProblems
@return {jqXHR} a jQuery Promise object
**/
fetchProblems: function() {
var model = Discourse.AdminDashboard.create();
return $.ajax(Discourse.getURL("/admin/dashboard/problems"), {
type: 'GET',
dataType: 'json',
success: function(json) {
model.mergeAttributes(json);
model.set('loaded', true);
}
});
}
});

View File

@ -19,6 +19,7 @@ Discourse.AdminDashboardRoute = Discourse.Route.extend({
fetchDashboardData: function(c) {
if( !c.get('dashboardFetchedAt') || Date.create('1 hour ago', 'en') > c.get('dashboardFetchedAt') ) {
c.set('dashboardFetchedAt', new Date());
c.set('problemsFetchedAt', new Date());
Discourse.AdminDashboard.find().then(function(d) {
if( Discourse.SiteSettings.version_checks ){
c.set('versionCheck', Discourse.VersionCheck.create(d.version_check));
@ -31,6 +32,12 @@ Discourse.AdminDashboardRoute = Discourse.Route.extend({
c.set('problems', d.problems);
c.set('loading', false);
});
} else if( !c.get('problemsFetchedAt') || Date.create('1 minute ago', 'en') > c.get('problemsFetchedAt') ) {
c.set('problemsFetchedAt', new Date());
Discourse.AdminDashboard.fetchProblems().then(function(d) {
c.set('problems', d.problems);
c.set('loading', false);
});
}
},

View File

@ -3,7 +3,10 @@ class Admin::DashboardController < Admin::AdminController
caches_action :index, expires_in: 1.hour
def index
render_json_dump(AdminDashboardData.fetch)
render_json_dump(AdminDashboardData.fetch_all)
end
def problems
render_json_dump({problems: AdminDashboardData.fetch_problems})
end
end

View File

@ -4,14 +4,18 @@ class AdminDashboardData
REPORTS = ['visits', 'signups', 'topics', 'posts', 'flags', 'users_by_trust_level', 'likes', 'emails']
def self.fetch
def self.fetch_all
AdminDashboardData.new
end
def self.fetch_problems
AdminDashboardData.new.problems
end
def as_json
@json ||= {
reports: REPORTS.map { |type| Report.find(type) },
problems: [rails_env_check, host_names_check, gc_checks, sidekiq_check || clockwork_check, ram_check, facebook_config_check, twitter_config_check, github_config_check].compact,
problems: problems,
admins: User.admins.count,
moderators: User.moderators.count
}.merge(
@ -19,6 +23,10 @@ class AdminDashboardData
)
end
def problems
[rails_env_check, host_names_check, gc_checks, sidekiq_check || clockwork_check, ram_check, facebook_config_check, twitter_config_check, github_config_check].compact
end
def rails_env_check
I18n.t("dashboard.rails_env_warning", env: Rails.env) unless Rails.env == 'production'
end

View File

@ -56,7 +56,11 @@ Discourse::Application.routes.draw do
resources :site_customizations
resources :export
get 'version_check' => 'versions#show'
resources :dashboard, only: [:index]
resources :dashboard, only: [:index] do
collection do
get 'problems'
end
end
resources :api, only: [:index] do
collection do
post 'generate_key'

View File

@ -46,5 +46,39 @@ describe Admin::DashboardController do
json['reports'].should be_a(Array)
end
end
context '.problems' do
it 'should be successful' do
AdminDashboardData.stubs(:fetch_problems).returns([])
xhr :get, :problems
response.should be_successful
end
context 'when there are no problems' do
before do
AdminDashboardData.stubs(:fetch_problems).returns([])
end
it 'returns an empty array' do
xhr :get, :problems
json = JSON.parse(response.body)
json['problems'].should have(0).problems
end
end
context 'when there are problems' do
before do
AdminDashboardData.stubs(:fetch_problems).returns(['Not enough awesome', 'Too much sass'])
end
it 'returns an array of strings' do
xhr :get, :problems
json = JSON.parse(response.body)
json['problems'].should have(2).problems
json['problems'][0].should be_a(String)
json['problems'][1].should be_a(String)
end
end
end
end
end