Skip to content

Commit 6c4d9ec

Browse files
committed
Use one request to fetch dashboard report data and check version
1 parent 6690a2b commit 6c4d9ec

9 files changed

Lines changed: 111 additions & 50 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Discourse.AdminDashboard = Discourse.Model.extend({});
2+
3+
Discourse.AdminDashboard.reopenClass({
4+
find: function() {
5+
var model = Discourse.AdminDashboard.create();
6+
return $.ajax("/admin/dashboard", {
7+
type: 'GET',
8+
success: function(json) {
9+
model.mergeAttributes(json);
10+
model.set('loaded', true);
11+
}
12+
});
13+
}
14+
});

app/assets/javascripts/admin/routes/admin_dashboard_route.js

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,29 @@
88
**/
99
Discourse.AdminDashboardRoute = Discourse.Route.extend({
1010
setupController: function(c) {
11-
this.checkVersion(c);
12-
this.fetchReports(c);
11+
this.fetchDashboardData(c);
1312
this.fetchGithubCommits(c);
1413
},
1514

1615
renderTemplate: function() {
1716
this.render({into: 'admin/templates/admin'});
1817
},
1918

20-
checkVersion: function(c) {
21-
if( Discourse.SiteSettings.version_checks && (!c.get('versionCheckedAt') || Date.create('12 hours ago', 'en') > c.get('versionCheckedAt')) ) {
22-
c.set('versionCheckedAt', new Date());
23-
Discourse.VersionCheck.find().then(function(vc) {
24-
c.set('versionCheck', vc);
19+
fetchDashboardData: function(c) {
20+
if( !c.get('dashboardFetchedAt') || Date.create('1 hour ago', 'en') > c.get('dashboardFetchedAt') ) {
21+
c.set('dashboardFetchedAt', new Date());
22+
Discourse.AdminDashboard.find().then(function(d) {
23+
if( Discourse.SiteSettings.version_checks ){
24+
c.set('versionCheck', Discourse.VersionCheck.create(d.version_check));
25+
}
26+
d.reports.each(function(report){
27+
c.set(report.type, Discourse.Report.create(report));
28+
});
2529
c.set('loading', false);
2630
});
2731
}
2832
},
2933

30-
fetchReports: function(c) {
31-
if( !c.get('reportsCheckedAt') || Date.create('1 hour ago', 'en') > c.get('reportsCheckedAt') ) {
32-
// TODO: use one request to get all reports, or maybe one request for all dashboard data including version check.
33-
c.set('reportsCheckedAt', new Date());
34-
['visits', 'signups', 'topics', 'posts', 'total_users', 'flags'].each(function(reportType){
35-
c.set(reportType, Discourse.Report.find(reportType));
36-
});
37-
}
38-
},
39-
4034
fetchGithubCommits: function(c) {
4135
if( !c.get('commitsCheckedAt') || Date.create('1 hour ago', 'en') > c.get('commitsCheckedAt') ) {
4236
c.set('commitsCheckedAt', new Date());

app/assets/javascripts/admin/templates/dashboard.js.handlebars

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@
4949
<th>{{i18n admin.dashboard.reports.last_30_days}}</th>
5050
</tr>
5151
</thead>
52-
{{ render 'admin_report_signups' signups }}
53-
{{ render 'admin_report_topics' topics }}
54-
{{ render 'admin_report_posts' posts }}
55-
{{ render 'admin_report_flags' flags }}
52+
{{#unless loading}}
53+
{{ render 'admin_report_signups' signups }}
54+
{{ render 'admin_report_topics' topics }}
55+
{{ render 'admin_report_posts' posts }}
56+
{{ render 'admin_report_flags' flags }}
57+
{{/unless}}
5658
</table>
5759
</div>
5860

@@ -67,8 +69,10 @@
6769
<th>{{i18n admin.dashboard.reports.30_days_ago}}</th>
6870
</tr>
6971
</thead>
70-
{{ render 'admin_report_total_users' total_users }}
71-
{{ render 'admin_report_visits' visits }}
72+
{{#unless loading}}
73+
{{ render 'admin_report_total_users' total_users }}
74+
{{ render 'admin_report_visits' visits }}
75+
{{/unless}}
7276
</table>
7377
</div>
7478
</div>

app/assets/javascripts/admin/templates/report.js.handlebars

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
{{#if loaded}}
2-
<tr>
3-
<td class="title">{{title}}</td>
4-
<td class="value">{{valueAtDaysAgo data 0}}</td>
5-
<td class="value">{{valueAtDaysAgo data 1}}</td>
6-
<td class="value">{{valueAtDaysAgo data 7}}</td>
7-
<td class="value">{{valueAtDaysAgo data 30}}</td>
8-
</tr>
9-
{{/if}}
1+
<tr>
2+
<td class="title">{{title}}</td>
3+
<td class="value">{{valueAtDaysAgo data 0}}</td>
4+
<td class="value">{{valueAtDaysAgo data 1}}</td>
5+
<td class="value">{{valueAtDaysAgo data 7}}</td>
6+
<td class="value">{{valueAtDaysAgo data 30}}</td>
7+
</tr>
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
{{#if loaded}}
2-
<tr>
3-
<td class="title">{{title}}</td>
4-
<td class="value">{{valueAtDaysAgo data 0}}</td>
5-
<td class="value">{{valueAtDaysAgo data 1}}</td>
6-
<td class="value">{{sumLast data 7}}</td>
7-
<td class="value">{{sumLast data 30}}</td>
8-
</tr>
9-
{{/if}}
1+
<tr>
2+
<td class="title">{{title}}</td>
3+
<td class="value">{{valueAtDaysAgo data 0}}</td>
4+
<td class="value">{{valueAtDaysAgo data 1}}</td>
5+
<td class="value">{{sumLast data 7}}</td>
6+
<td class="value">{{sumLast data 30}}</td>
7+
</tr>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Admin::DashboardController < Admin::AdminController
2+
3+
def index
4+
render_json_dump({
5+
reports: ['visits', 'signups', 'topics', 'posts', 'total_users', 'flags'].map { |type| Report.find(type) }
6+
}.merge(
7+
SiteSetting.version_checks? ? {version_check: DiscourseUpdates.check_version} : {}
8+
))
9+
end
10+
11+
end

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
resources :site_customizations
5656
resources :export
5757
get 'version_check' => 'versions#show'
58+
resources :dashboard, only: [:index]
5859
end
5960

6061
get 'email_preferences' => 'email#preferences_redirect'
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
require 'spec_helper'
2+
3+
describe Admin::DashboardController do
4+
5+
it "is a subclass of AdminController" do
6+
(Admin::DashboardController < Admin::AdminController).should be_true
7+
end
8+
9+
context 'while logged in as an admin' do
10+
let!(:admin) { log_in(:admin) }
11+
12+
context '.index' do
13+
it 'should be successful' do
14+
xhr :get, :index
15+
response.should be_successful
16+
end
17+
18+
context 'version checking is enabled' do
19+
before do
20+
SiteSetting.stubs(:version_checks).returns(true)
21+
end
22+
23+
it 'returns discourse version info' do
24+
xhr :get, :index
25+
json = JSON.parse(response.body)
26+
json['version_check'].should be_present
27+
end
28+
end
29+
30+
context 'version checking is disabled' do
31+
before do
32+
SiteSetting.stubs(:version_checks).returns(false)
33+
end
34+
35+
it 'does not return discourse version info' do
36+
xhr :get, :index
37+
json = JSON.parse(response.body)
38+
json['version_check'].should_not be_present
39+
end
40+
end
41+
42+
it 'returns report data' do
43+
xhr :get, :index
44+
json = JSON.parse(response.body)
45+
json.should have_key('reports')
46+
json['reports'].should be_a(Array)
47+
end
48+
end
49+
end
50+
end

0 commit comments

Comments
 (0)