mirror of
https://github.com/discourse/discourse.git
synced 2025-05-23 15:14:16 +08:00
Use one request to fetch dashboard report data and check version
This commit is contained in:
14
app/assets/javascripts/admin/models/admin_dashboard.js
Normal file
14
app/assets/javascripts/admin/models/admin_dashboard.js
Normal 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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
@ -8,8 +8,7 @@
|
|||||||
**/
|
**/
|
||||||
Discourse.AdminDashboardRoute = Discourse.Route.extend({
|
Discourse.AdminDashboardRoute = Discourse.Route.extend({
|
||||||
setupController: function(c) {
|
setupController: function(c) {
|
||||||
this.checkVersion(c);
|
this.fetchDashboardData(c);
|
||||||
this.fetchReports(c);
|
|
||||||
this.fetchGithubCommits(c);
|
this.fetchGithubCommits(c);
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -17,26 +16,21 @@ Discourse.AdminDashboardRoute = Discourse.Route.extend({
|
|||||||
this.render({into: 'admin/templates/admin'});
|
this.render({into: 'admin/templates/admin'});
|
||||||
},
|
},
|
||||||
|
|
||||||
checkVersion: function(c) {
|
fetchDashboardData: function(c) {
|
||||||
if( Discourse.SiteSettings.version_checks && (!c.get('versionCheckedAt') || Date.create('12 hours ago', 'en') > c.get('versionCheckedAt')) ) {
|
if( !c.get('dashboardFetchedAt') || Date.create('1 hour ago', 'en') > c.get('dashboardFetchedAt') ) {
|
||||||
c.set('versionCheckedAt', new Date());
|
c.set('dashboardFetchedAt', new Date());
|
||||||
Discourse.VersionCheck.find().then(function(vc) {
|
Discourse.AdminDashboard.find().then(function(d) {
|
||||||
c.set('versionCheck', vc);
|
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);
|
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) {
|
fetchGithubCommits: function(c) {
|
||||||
if( !c.get('commitsCheckedAt') || Date.create('1 hour ago', 'en') > c.get('commitsCheckedAt') ) {
|
if( !c.get('commitsCheckedAt') || Date.create('1 hour ago', 'en') > c.get('commitsCheckedAt') ) {
|
||||||
c.set('commitsCheckedAt', new Date());
|
c.set('commitsCheckedAt', new Date());
|
||||||
|
@ -49,10 +49,12 @@
|
|||||||
<th>{{i18n admin.dashboard.reports.last_30_days}}</th>
|
<th>{{i18n admin.dashboard.reports.last_30_days}}</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
{{ render 'admin_report_signups' signups }}
|
{{#unless loading}}
|
||||||
{{ render 'admin_report_topics' topics }}
|
{{ render 'admin_report_signups' signups }}
|
||||||
{{ render 'admin_report_posts' posts }}
|
{{ render 'admin_report_topics' topics }}
|
||||||
{{ render 'admin_report_flags' flags }}
|
{{ render 'admin_report_posts' posts }}
|
||||||
|
{{ render 'admin_report_flags' flags }}
|
||||||
|
{{/unless}}
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@ -67,8 +69,10 @@
|
|||||||
<th>{{i18n admin.dashboard.reports.30_days_ago}}</th>
|
<th>{{i18n admin.dashboard.reports.30_days_ago}}</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
{{ render 'admin_report_total_users' total_users }}
|
{{#unless loading}}
|
||||||
{{ render 'admin_report_visits' visits }}
|
{{ render 'admin_report_total_users' total_users }}
|
||||||
|
{{ render 'admin_report_visits' visits }}
|
||||||
|
{{/unless}}
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -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}}
|
|
@ -1,9 +1,7 @@
|
|||||||
{{#if loaded}}
|
<tr>
|
||||||
<tr>
|
<td class="title">{{title}}</td>
|
||||||
<td class="title">{{title}}</td>
|
<td class="value">{{valueAtDaysAgo data 0}}</td>
|
||||||
<td class="value">{{valueAtDaysAgo data 0}}</td>
|
<td class="value">{{valueAtDaysAgo data 1}}</td>
|
||||||
<td class="value">{{valueAtDaysAgo data 1}}</td>
|
<td class="value">{{valueAtDaysAgo data 7}}</td>
|
||||||
<td class="value">{{valueAtDaysAgo data 7}}</td>
|
<td class="value">{{valueAtDaysAgo data 30}}</td>
|
||||||
<td class="value">{{valueAtDaysAgo data 30}}</td>
|
</tr>
|
||||||
</tr>
|
|
||||||
{{/if}}
|
|
@ -1,9 +1,7 @@
|
|||||||
{{#if loaded}}
|
<tr>
|
||||||
<tr>
|
<td class="title">{{title}}</td>
|
||||||
<td class="title">{{title}}</td>
|
<td class="value">{{valueAtDaysAgo data 0}}</td>
|
||||||
<td class="value">{{valueAtDaysAgo data 0}}</td>
|
<td class="value">{{valueAtDaysAgo data 1}}</td>
|
||||||
<td class="value">{{valueAtDaysAgo data 1}}</td>
|
<td class="value">{{sumLast data 7}}</td>
|
||||||
<td class="value">{{sumLast data 7}}</td>
|
<td class="value">{{sumLast data 30}}</td>
|
||||||
<td class="value">{{sumLast data 30}}</td>
|
</tr>
|
||||||
</tr>
|
|
||||||
{{/if}}
|
|
11
app/controllers/admin/dashboard_controller.rb
Normal file
11
app/controllers/admin/dashboard_controller.rb
Normal 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
|
@ -55,6 +55,7 @@ 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]
|
||||||
end
|
end
|
||||||
|
|
||||||
get 'email_preferences' => 'email#preferences_redirect'
|
get 'email_preferences' => 'email#preferences_redirect'
|
||||||
|
50
spec/controllers/admin/dashboard_controller_spec.rb
Normal file
50
spec/controllers/admin/dashboard_controller_spec.rb
Normal 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
|
Reference in New Issue
Block a user