FEATURE: basic disk space usage stats

This commit is contained in:
Sam 2015-02-04 18:05:17 +11:00
parent a4b96adfc5
commit 67eccee990
5 changed files with 94 additions and 1 deletions

View File

@ -24,7 +24,7 @@ export default Discourse.Route.extend({
c.set('top_referrers', topReferrers);
}
['admins', 'moderators', 'blocked', 'suspended', 'top_traffic_sources', 'top_referred_topics', 'updated_at'].forEach(function(x) {
['disk_space','admins', 'moderators', 'blocked', 'suspended', 'top_traffic_sources', 'top_referred_topics', 'updated_at'].forEach(function(x) {
c.set(x, d[x]);
});

View File

@ -100,6 +100,28 @@
{{/unless}}
</table>
</div>
<div class="dashboard-stats">
<table class="table table-condensed table-hover">
<thead>
<tr>
<th>&nbsp;</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
{{#unless loading}}
<tr>
<td>{{i18n 'admin.dashboard.uploads'}}</td>
<td>{{disk_space.uploads_used}} ({{i18n 'admin.dashboard.space_free' size=disk_space.uploads_free}})</td>
<td>{{i18n 'admin.dashboard.backups'}}</td>
<td>{{disk_space.backups_used}} ({{i18n 'admin.dashboard.space_free' size=disk_space.backups_free}} free)</td>
</tr>
{{/unless}}
</tbody>
</table>
</div>
</div>
<div class="dashboard-right">

View File

@ -1,7 +1,10 @@
require 'disk_space'
class Admin::DashboardController < Admin::AdminController
def index
dashboard_data = AdminDashboardData.fetch_cached_stats || Jobs::DashboardStats.new.execute({})
dashboard_data.merge!({version_check: DiscourseUpdates.check_version.as_json}) if SiteSetting.version_checks?
dashboard_data[:disk_space] = DiskSpace.cached_stats
render json: dashboard_data
end

View File

@ -1531,6 +1531,9 @@ en:
suspended: 'Suspended:'
private_messages_short: "PMs"
private_messages_title: "Private Messages"
space_free: "{{size}} free"
uploads: "uploads"
backups: "backups"
reports:
today: "Today"

65
lib/disk_space.rb Normal file
View File

@ -0,0 +1,65 @@
class DiskSpace
extend ActionView::Helpers::NumberHelper
def self.uploads_used_bytes
used(uploads_path)
end
def self.uploads_free_bytes
free(uploads_path)
end
def self.backups_used_bytes
used(backups_path)
end
def self.backups_free_bytes
free(backups_path)
end
def self.backups_path
Backup.base_directory
end
def self.uploads_path
"#{Rails.root}/public/uploads/#{RailsMultisite::ConnectionManagement.current_db}"
end
def self.stats
{
uploads_used: number_to_human_size(uploads_used_bytes),
uploads_free: number_to_human_size(uploads_free_bytes),
backups_used: number_to_human_size(backups_used_bytes),
backups_free: number_to_human_size(backups_free_bytes)
}
end
def self.cached_stats
stats = $redis.get('disk_space_stats')
updated_at = $redis.get('disk_space_stats_updated')
unless updated_at && (Time.now.to_i - updated_at.to_i) < 30.minutes
Scheduler::Defer.later "updated stats" do
$redis.set('disk_space_stats_updated', Time.now.to_i)
$redis.set('disk_space_stats', self.stats.to_json)
end
end
if stats
JSON.parse(stats)
end
end
protected
def self.free(path)
`df -Pk #{path} | awk 'NR==2 {print $4 * 1024;}'`.to_i
end
def self.used(path)
`du -s #{path}`.to_i * 1024
end
end