Use one request to fetch dashboard report data and check version

This commit is contained in:
Neil Lalonde 2013-03-14 18:26:12 -04:00
parent 6690a2ba3e
commit 6c4d9ecfdc
9 changed files with 111 additions and 50 deletions

View File

@ -0,0 +1,14 @@
Discourse.AdminDashboard = Discourse.Model.extend({});
Discourse.AdminDashboard.reopenClass({
find: function() {
var model = Discourse.AdminDashboard.create();
return $.ajax("/admin/dashboard", {
type: 'GET',
success: function(json) {
model.mergeAttributes(json);
model.set('loaded', true);
}
});
}
});

View File

@ -8,8 +8,7 @@
**/
Discourse.AdminDashboardRoute = Discourse.Route.extend({
setupController: function(c) {
this.checkVersion(c);
this.fetchReports(c);
this.fetchDashboardData(c);
this.fetchGithubCommits(c);
},
@ -17,26 +16,21 @@ Discourse.AdminDashboardRoute = Discourse.Route.extend({
this.render({into: 'admin/templates/admin'});
},
checkVersion: function(c) {
if( Discourse.SiteSettings.version_checks && (!c.get('versionCheckedAt') || Date.create('12 hours ago', 'en') > c.get('versionCheckedAt')) ) {
c.set('versionCheckedAt', new Date());
Discourse.VersionCheck.find().then(function(vc) {
c.set('versionCheck', vc);
fetchDashboardData: function(c) {
if( !c.get('dashboardFetchedAt') || Date.create('1 hour ago', 'en') > c.get('dashboardFetchedAt') ) {
c.set('dashboardFetchedAt', new Date());
Discourse.AdminDashboard.find().then(function(d) {
if( Discourse.SiteSettings.version_checks ){
c.set('versionCheck', Discourse.VersionCheck.create(d.version_check));
}
d.reports.each(function(report){
c.set(report.type, Discourse.Report.create(report));
});
c.set('loading', false);
});
}
},
fetchReports: function(c) {
if( !c.get('reportsCheckedAt') || Date.create('1 hour ago', 'en') > c.get('reportsCheckedAt') ) {
// TODO: use one request to get all reports, or maybe one request for all dashboard data including version check.
c.set('reportsCheckedAt', new Date());
['visits', 'signups', 'topics', 'posts', 'total_users', 'flags'].each(function(reportType){
c.set(reportType, Discourse.Report.find(reportType));
});
}
},
fetchGithubCommits: function(c) {
if( !c.get('commitsCheckedAt') || Date.create('1 hour ago', 'en') > c.get('commitsCheckedAt') ) {
c.set('commitsCheckedAt', new Date());

View File

@ -49,10 +49,12 @@
<th>{{i18n admin.dashboard.reports.last_30_days}}</th>
</tr>
</thead>
{{ render 'admin_report_signups' signups }}
{{ render 'admin_report_topics' topics }}
{{ render 'admin_report_posts' posts }}
{{ render 'admin_report_flags' flags }}
{{#unless loading}}
{{ render 'admin_report_signups' signups }}
{{ render 'admin_report_topics' topics }}
{{ render 'admin_report_posts' posts }}
{{ render 'admin_report_flags' flags }}
{{/unless}}
</table>
</div>
@ -67,8 +69,10 @@
<th>{{i18n admin.dashboard.reports.30_days_ago}}</th>
</tr>
</thead>
{{ render 'admin_report_total_users' total_users }}
{{ render 'admin_report_visits' visits }}
{{#unless loading}}
{{ render 'admin_report_total_users' total_users }}
{{ render 'admin_report_visits' visits }}
{{/unless}}
</table>
</div>
</div>

View File

@ -1,9 +0,0 @@
{{#if loaded}}
<tr>
<td class="title">{{title}}</td>
<td class="value">{{valueAtDaysAgo data 0}}</td>
<td class="value">{{valueAtDaysAgo data 1}}</td>
<td class="value">{{sumLast data 7}}</td>
<td class="value">{{sumLast data 30}}</td>
</tr>
{{/if}}

View File

@ -1,9 +1,7 @@
{{#if loaded}}
<tr>
<td class="title">{{title}}</td>
<td class="value">{{valueAtDaysAgo data 0}}</td>
<td class="value">{{valueAtDaysAgo data 1}}</td>
<td class="value">{{valueAtDaysAgo data 7}}</td>
<td class="value">{{valueAtDaysAgo data 30}}</td>
</tr>
{{/if}}
<tr>
<td class="title">{{title}}</td>
<td class="value">{{valueAtDaysAgo data 0}}</td>
<td class="value">{{valueAtDaysAgo data 1}}</td>
<td class="value">{{valueAtDaysAgo data 7}}</td>
<td class="value">{{valueAtDaysAgo data 30}}</td>
</tr>

View File

@ -1,9 +1,7 @@
{{#if loaded}}
<tr>
<td class="title">{{title}}</td>
<td class="value">{{valueAtDaysAgo data 0}}</td>
<td class="value">{{valueAtDaysAgo data 1}}</td>
<td class="value">{{sumLast data 7}}</td>
<td class="value">{{sumLast data 30}}</td>
</tr>
{{/if}}
<tr>
<td class="title">{{title}}</td>
<td class="value">{{valueAtDaysAgo data 0}}</td>
<td class="value">{{valueAtDaysAgo data 1}}</td>
<td class="value">{{sumLast data 7}}</td>
<td class="value">{{sumLast data 30}}</td>
</tr>

View File

@ -0,0 +1,11 @@
class Admin::DashboardController < Admin::AdminController
def index
render_json_dump({
reports: ['visits', 'signups', 'topics', 'posts', 'total_users', 'flags'].map { |type| Report.find(type) }
}.merge(
SiteSetting.version_checks? ? {version_check: DiscourseUpdates.check_version} : {}
))
end
end

View File

@ -55,6 +55,7 @@ Discourse::Application.routes.draw do
resources :site_customizations
resources :export
get 'version_check' => 'versions#show'
resources :dashboard, only: [:index]
end
get 'email_preferences' => 'email#preferences_redirect'

View File

@ -0,0 +1,50 @@
require 'spec_helper'
describe Admin::DashboardController do
it "is a subclass of AdminController" do
(Admin::DashboardController < Admin::AdminController).should be_true
end
context 'while logged in as an admin' do
let!(:admin) { log_in(:admin) }
context '.index' do
it 'should be successful' do
xhr :get, :index
response.should be_successful
end
context 'version checking is enabled' do
before do
SiteSetting.stubs(:version_checks).returns(true)
end
it 'returns discourse version info' do
xhr :get, :index
json = JSON.parse(response.body)
json['version_check'].should be_present
end
end
context 'version checking is disabled' do
before do
SiteSetting.stubs(:version_checks).returns(false)
end
it 'does not return discourse version info' do
xhr :get, :index
json = JSON.parse(response.body)
json['version_check'].should_not be_present
end
end
it 'returns report data' do
xhr :get, :index
json = JSON.parse(response.body)
json.should have_key('reports')
json['reports'].should be_a(Array)
end
end
end
end