UNPKG

sheercms

Version:

Sheer Cliff CMS is a simple and powerful content management system (CMS) for Node JS.

144 lines (120 loc) 4.51 kB
app.controller('StatController', function($scope, $timeout, StatService, DialogService, CacheService) { $scope.data = { title: "System Statistics", message: "", messageType: "", //alert-success, alert-warning, alert-info, alert-danger isAuthorized: false, tab: "stats", resultText: null }; $scope.stats = null; $scope.backups = null; $scope.statsString = null; $scope.isJsonVisible = false; $scope.refresh = function() { loadStats(true); }; $scope.showJson = function() { $scope.isJsonVisible = !$scope.isJsonVisible; }; $scope.changeTab = function(tab) { $scope.data.tab = tab; }; $scope.backupDatabase = function() { setMessage("Starting database backup...", "alert-info", false); StatService.backupDatabase().then(function(data) { setMessage(data.message, data.success ? "alert-success" : "alert-danger"); if (data.success === true && data.backups) setBackups(data.backups); }); }; $scope.deleteBackup = function(backup) { if (confirm('Are you sure you want to delete this backup: ' + backup.name + '?')) { StatService.deleteBackup(backup.name).then(function(data) { setMessage(data.message, data.success ? "alert-success" : "alert-danger"); if (data.success === true && data.backups) setBackups(data.backups); }); } }; $scope.uploadBackup = function() { DialogService.showFileSelector(function(files) { if (files && files.length > 0) { setMessage("Uploading database backup file...", "alert-info"); StatService.uploadBackup(files[0]).then(function(data) { setMessage(data.message, data.success ? "alert-success" : "alert-danger"); if (data.success === true && data.backups) setBackups(data.backups); }); } }); }; $scope.restoreBackup = function(backup) { if (confirm('Are you sure you want to restore this backup? This will overwrite the existing database with the backup data.\n' + 'WARNING: This action cannot be undone. It is recommended that you backup your existing database before doing the restore.')) { clearResultText(); StatService.restoreBackup(backup.name).then(function(data) { CacheService.clearClientCache(); setMessage(data.message, data.success ? "alert-success" : "alert-danger"); if (data.text && data.text.length > 0) { //show the command result text $scope.data.resultText = data.text; } }); } }; function clearResultText() { $scope.data.resultText = null; } function setBackups(backups) { var arr = []; if (backups) { for(var i = 0; i < backups.length; i++) { var obj = { name: backups[i], url: "/cms/stats/downloadBackup?name=" + encodeURIComponent(backups[i]) }; arr.push(obj); } } $scope.backups = arr; } function clearMessage() { $scope.data.message = ""; $scope.data.messageType = ""; } function setMessage(msg, type) { $scope.data.messageType = type; $scope.data.message = msg; } function timeoutClearMessage() { $timeout(function() { clearMessage(); }, 4000); } function init() { $scope.$parent.resize('.stats-view'); if ($scope.global.isAdmin === true) { $scope.data.isAuthorized = true; loadStats(true); } else { $scope.data.isAuthorized = false; } } function loadStats(showMsg) { if (showMsg) { setMessage("Loading statistics...", "alert-success", false); } StatService.getSystemStats().then(function(data) { if (showMsg) { setMessage(data.message, data.success ? "alert-success" : "alert-danger"); } $scope.stats = data.stats; setBackups(data.backups); if (data.stats) $scope.statsString = JSON.stringify(data.stats); }); } init(); });