Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Use one request to fetch dashboard report data and check version

  • Loading branch information...
commit 6c4d9ecfdca389d89de7de21fafcf3ff228e52c6 1 parent 6690a2b
@nlalonde nlalonde authored
View
14 app/assets/javascripts/admin/models/admin_dashboard.js
@@ -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
28 app/assets/javascripts/admin/routes/admin_dashboard_route.js
@@ -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
16 app/assets/javascripts/admin/templates/dashboard.js.handlebars
@@ -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
9 app/assets/javascripts/admin/templates/report.js.handlebars
@@ -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
16 app/assets/javascripts/admin/templates/reports/per_day_counts_report.js.handlebars
@@ -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
16 app/assets/javascripts/admin/templates/reports/summed_counts_report.js.handlebars
@@ -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
11 app/controllers/admin/dashboard_controller.rb
@@ -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
1  config/routes.rb
@@ -55,6 +55,7 @@
resources :site_customizations
resources :export
get 'version_check' => 'versions#show'
+ resources :dashboard, only: [:index]
end
get 'email_preferences' => 'email#preferences_redirect'
View
50 spec/controllers/admin/dashboard_controller_spec.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.