mirror of
https://github.com/discourse/discourse.git
synced 2025-01-30 03:21:02 +08:00
Fetch the list of problems more frequently on the admin dashboard
This commit is contained in:
parent
df85201298
commit
25073e873f
|
@ -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 = Discourse.Model.extend({});
|
||||||
|
|
||||||
Discourse.AdminDashboard.reopenClass({
|
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() {
|
find: function() {
|
||||||
var model = Discourse.AdminDashboard.create();
|
var model = Discourse.AdminDashboard.create();
|
||||||
return $.ajax(Discourse.getURL("/admin/dashboard"), {
|
return $.ajax(Discourse.getURL("/admin/dashboard"), {
|
||||||
|
@ -11,5 +28,24 @@ Discourse.AdminDashboard.reopenClass({
|
||||||
model.set('loaded', true);
|
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);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -19,6 +19,7 @@ Discourse.AdminDashboardRoute = Discourse.Route.extend({
|
||||||
fetchDashboardData: function(c) {
|
fetchDashboardData: function(c) {
|
||||||
if( !c.get('dashboardFetchedAt') || Date.create('1 hour ago', 'en') > c.get('dashboardFetchedAt') ) {
|
if( !c.get('dashboardFetchedAt') || Date.create('1 hour ago', 'en') > c.get('dashboardFetchedAt') ) {
|
||||||
c.set('dashboardFetchedAt', new Date());
|
c.set('dashboardFetchedAt', new Date());
|
||||||
|
c.set('problemsFetchedAt', new Date());
|
||||||
Discourse.AdminDashboard.find().then(function(d) {
|
Discourse.AdminDashboard.find().then(function(d) {
|
||||||
if( Discourse.SiteSettings.version_checks ){
|
if( Discourse.SiteSettings.version_checks ){
|
||||||
c.set('versionCheck', Discourse.VersionCheck.create(d.version_check));
|
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('problems', d.problems);
|
||||||
c.set('loading', false);
|
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);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,10 @@ class Admin::DashboardController < Admin::AdminController
|
||||||
caches_action :index, expires_in: 1.hour
|
caches_action :index, expires_in: 1.hour
|
||||||
|
|
||||||
def index
|
def index
|
||||||
render_json_dump(AdminDashboardData.fetch)
|
render_json_dump(AdminDashboardData.fetch_all)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def problems
|
||||||
|
render_json_dump({problems: AdminDashboardData.fetch_problems})
|
||||||
|
end
|
||||||
end
|
end
|
|
@ -4,14 +4,18 @@ class AdminDashboardData
|
||||||
|
|
||||||
REPORTS = ['visits', 'signups', 'topics', 'posts', 'flags', 'users_by_trust_level', 'likes', 'emails']
|
REPORTS = ['visits', 'signups', 'topics', 'posts', 'flags', 'users_by_trust_level', 'likes', 'emails']
|
||||||
|
|
||||||
def self.fetch
|
def self.fetch_all
|
||||||
AdminDashboardData.new
|
AdminDashboardData.new
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.fetch_problems
|
||||||
|
AdminDashboardData.new.problems
|
||||||
|
end
|
||||||
|
|
||||||
def as_json
|
def as_json
|
||||||
@json ||= {
|
@json ||= {
|
||||||
reports: REPORTS.map { |type| Report.find(type) },
|
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,
|
admins: User.admins.count,
|
||||||
moderators: User.moderators.count
|
moderators: User.moderators.count
|
||||||
}.merge(
|
}.merge(
|
||||||
|
@ -19,6 +23,10 @@ class AdminDashboardData
|
||||||
)
|
)
|
||||||
end
|
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
|
def rails_env_check
|
||||||
I18n.t("dashboard.rails_env_warning", env: Rails.env) unless Rails.env == 'production'
|
I18n.t("dashboard.rails_env_warning", env: Rails.env) unless Rails.env == 'production'
|
||||||
end
|
end
|
||||||
|
|
|
@ -56,7 +56,11 @@ Discourse::Application.routes.draw do
|
||||||
resources :site_customizations
|
resources :site_customizations
|
||||||
resources :export
|
resources :export
|
||||||
get 'version_check' => 'versions#show'
|
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
|
resources :api, only: [:index] do
|
||||||
collection do
|
collection do
|
||||||
post 'generate_key'
|
post 'generate_key'
|
||||||
|
|
|
@ -46,5 +46,39 @@ describe Admin::DashboardController do
|
||||||
json['reports'].should be_a(Array)
|
json['reports'].should be_a(Array)
|
||||||
end
|
end
|
||||||
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
|
||||||
end
|
end
|
Loading…
Reference in New Issue
Block a user