FEATURE: Improve backup stats on admin dashboard

* Dashboard doesn't timeout anymore when Amazon S3 is used for backups
* Storage stats are now a proper report with the same caching rules
* Changing the backup_location, s3_backup_bucket or creating and deleting backups removes the report from the cache
* It shows the number of backups and the backup location
* It shows the used space for the correct backup location instead of always showing used space on local storage
* It shows the date of the last backup as relative date
This commit is contained in:
Gerhard Schlager
2018-12-14 23:14:46 +01:00
parent 040ddec63d
commit 1a8ca68ea3
20 changed files with 223 additions and 173 deletions

View File

@ -1,10 +1,4 @@
class DiskSpace
extend ActionView::Helpers::NumberHelper
DISK_SPACE_STATS_CACHE_KEY ||= 'disk_space_stats'.freeze
DISK_SPACE_STATS_UPDATED_CACHE_KEY ||= 'disk_space_stats_updated'.freeze
def self.uploads_used_bytes
# used(uploads_path)
# temporary (on our internal setup its just too slow to iterate)
@ -15,51 +9,6 @@ class DiskSpace
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
BackupRestore::LocalBackupStore.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.reset_cached_stats
Discourse.cache.delete(DISK_SPACE_STATS_UPDATED_CACHE_KEY)
Discourse.cache.delete(DISK_SPACE_STATS_CACHE_KEY)
end
def self.cached_stats
stats = Discourse.cache.read(DISK_SPACE_STATS_CACHE_KEY)
updated_at = Discourse.cache.read(DISK_SPACE_STATS_UPDATED_CACHE_KEY)
unless updated_at && (Time.now.to_i - updated_at.to_i) < 30.minutes
Jobs.enqueue(:update_disk_space)
end
if stats
JSON.parse(stats)
end
end
protected
def self.free(path)
`df -Pk #{path} | awk 'NR==2 {print $4;}'`.to_i * 1024
end
@ -67,4 +16,9 @@ class DiskSpace
def self.used(path)
`du -s #{path}`.to_i * 1024
end
def self.uploads_path
"#{Rails.root}/public/uploads/#{RailsMultisite::ConnectionManagement.current_db}"
end
private_class_method :uploads_path
end