2017-04-06 09:20:52 +02:00
backupApp . controller ( 'StateController' , function ( $scope , $timeout , ServerStatus , BackupList , AppService , AppUtils , DialogService , gettextCatalog ) {
2015-08-21 22:04:39 +02:00
$scope . state = ServerStatus . watch ( $scope );
$scope . backups = BackupList . watch ( $scope );
2015-10-04 17:08:48 +02:00
$scope . ServerStatus = ServerStatus ;
2015-08-21 22:04:39 +02:00
$scope . activeTask = null ;
var updateActiveTask = function () {
2017-04-06 09:20:52 +02:00
$scope . activeTaskID = $scope . state . activeTask == null ? null : $scope . state . activeTask . Item1 ;
2015-10-04 17:08:48 +02:00
$scope . activeBackup = $scope . state . activeTask == null ? null : BackupList . lookup [ $scope . state . activeTask . Item2 ];
2015-12-12 14:28:52 +01:00
$scope . nextTask = ( $scope . state . schedulerQueueIds == null || $scope . state . schedulerQueueIds . length == 0 ) ? null : BackupList . lookup [ $scope . state . schedulerQueueIds [ 0 ]. Item2 ];
$scope . nextScheduledTask = ( $scope . state . proposedSchedule == null || $scope . state . proposedSchedule . length == 0 ) ? null : BackupList . lookup [ $scope . state . proposedSchedule [ 0 ]. Item1 ];
$scope . nextScheduledTime = ( $scope . state . proposedSchedule == null || $scope . state . proposedSchedule . length == 0 ) ? null : $scope . state . proposedSchedule [ 0 ]. Item2 ;
2015-08-21 22:04:39 +02:00
};
$scope . $on ( 'backuplistchanged' , updateActiveTask );
$scope . $on ( 'serverstatechanged' , updateActiveTask );
2015-09-23 21:01:43 +02:00
$scope . $on ( 'serverstatechanged.pauseTimeRemain' , function () { $timeout ( function () { $scope . $digest (); }) });
2015-08-21 22:04:39 +02:00
$scope . sendResume = function () {
2016-09-15 11:39:27 +02:00
ServerStatus . resume (). then ( function () {}, AppUtils . connectionError );
2015-08-21 22:04:39 +02:00
};
2015-10-04 17:08:48 +02:00
2015-12-03 22:01:30 +01:00
function updateStateDisplay () {
2019-09-02 17:03:02 +00:00
var text = gettextCatalog . getString ( 'Running …' );
2015-12-03 22:01:30 +01:00
var pg = - 1 ;
2017-06-20 13:05:09 +02:00
if ( $scope . state . lastPgEvent != null && $scope . state . activeTask != null )
2015-12-03 22:01:30 +01:00
{
text = ServerStatus . progress_state_text [ $scope . state . lastPgEvent . Phase || '' ] || $scope . state . lastPgEvent . Phase ;
2022-04-22 12:50:41 +05:30
if ( $scope . state . lastPgEvent . Phase == 'Backup_ProcessingFiles' || $scope . state . lastPgEvent . Phase == 'Restore_DownloadingRemoteFiles' ) {
2015-12-03 22:01:30 +01:00
if ( $scope . state . lastPgEvent . StillCounting ) {
2016-10-02 16:47:40 +02:00
text = gettextCatalog . getString ( 'Counting ({{files}} files found, {{size}})' , { files : $scope . state . lastPgEvent . TotalFileCount , size : AppUtils . formatSizeString ( $scope . state . lastPgEvent . TotalFileSize ) });
2015-12-03 22:01:30 +01:00
pg = 0 ;
} else {
2019-09-16 22:04:24 -07:00
var unaccountedbytes = ( $scope . state . lastPgEvent . CurrentFilecomplete ) ? 0 : $scope . state . lastPgEvent . CurrentFileoffset ;
2015-12-03 22:01:30 +01:00
var filesleft = $scope . state . lastPgEvent . TotalFileCount - $scope . state . lastPgEvent . ProcessedFileCount ;
2019-09-16 22:04:24 -07:00
var sizeleft = $scope . state . lastPgEvent . TotalFileSize - $scope . state . lastPgEvent . ProcessedFileSize - unaccountedbytes ;
pg = ( $scope . state . lastPgEvent . ProcessedFileSize + unaccountedbytes ) / $scope . state . lastPgEvent . TotalFileSize ;
2015-12-03 22:01:30 +01:00
if ( $scope . state . lastPgEvent . ProcessedFileCount == 0 )
pg = 0 ;
2016-09-28 20:13:49 +02:00
else if ( pg >= 0.90 )
pg = 0.90 ;
2017-08-31 00:42:44 -04:00
// If we have a speed append it
var speed_txt = ( $scope . state . lastPgEvent . BackendSpeed < 0 ) ? "" : " at " + AppUtils . formatSizeString ( $scope . state . lastPgEvent . BackendSpeed ) + "/s" ;
2022-04-22 12:50:41 +05:30
var restoring_text = $scope . state . lastPgEvent . Phase == 'Restore_DownloadingRemoteFiles' ? 'Restoring: ' : '' ;
2017-08-31 00:42:44 -04:00
// Finally construct the whole text
2022-04-22 12:50:41 +05:30
text = gettextCatalog . getString ( restoring_text + '{{files}} files ({{size}}) to go {{speed_txt}}' , { files : filesleft , size : AppUtils . formatSizeString ( sizeleft ), speed_txt : speed_txt });
2015-12-03 22:01:30 +01:00
}
}
2016-09-28 20:13:49 +02:00
else if ( $scope . state . lastPgEvent . Phase == 'Backup_Finalize' || $scope . state . lastPgEvent . Phase == 'Backup_WaitForUpload' )
{
pg = 0.90 ;
}
else if ( $scope . state . lastPgEvent . Phase == 'Backup_Delete' || $scope . state . lastPgEvent . Phase == 'Backup_Compact' )
{
pg = 0.95 ;
}
else if ( $scope . state . lastPgEvent . Phase == 'Backup_VerificationUpload' || $scope . state . lastPgEvent . Phase == 'Backup_PostBackupVerify' )
{
pg = 0.98 ;
}
else if ( $scope . state . lastPgEvent . Phase == 'Backup_Complete' || $scope . state . lastPgEvent . Phase == 'Backup_WaitForUpload' )
{
2015-12-03 22:01:30 +01:00
pg = 1 ;
2019-07-23 10:35:33 -04:00
}
2015-12-03 22:01:30 +01:00
else if ( $scope . state . lastPgEvent . OverallProgress > 0 ) {
pg = $scope . state . lastPgEvent . OverallProgress ;
}
}
$scope . StateText = text ;
$scope . Progress = pg ;
};
$scope . $watch ( 'state.lastPgEvent' , updateStateDisplay , true );
2017-06-20 13:05:09 +02:00
$scope . $on ( 'serverstatechanged' , updateStateDisplay );
2015-12-03 22:01:30 +01:00
2017-04-06 09:20:52 +02:00
$scope . stopDialog = function () {
if ( $scope . activeTaskID == null )
return ;
var taskId = $scope . activeTaskID ;
var txt = $scope . state . lastPgEvent == null ? '' : ( $scope . state . lastPgEvent . Phase || '' );
function handleClick ( ix ) {
if ( ix == 0 )
{
2019-07-23 10:35:33 -04:00
AppService . post ( '/task/' + taskId + '/stopaftercurrentfile' );
$scope . StopReqId = taskId ;
}
else if ( ix == 1 ) {
AppService . post ( '/task/' + taskId + '/stopnow' );
2017-04-06 09:20:52 +02:00
$scope . StopReqId = taskId ;
}
};
2015-12-12 15:12:25 +01:00
2017-04-06 09:20:52 +02:00
if ( txt . indexOf ( 'Backup_' ) == 0 )
{
DialogService . dialog (
2019-07-23 11:32:07 -04:00
gettextCatalog . getString ( "Stop running backup" ),
gettextCatalog . getString ( "You can stop the backup after any file uploads currently in progress have finished." ),
[ gettextCatalog . getString ( "Stop after current file" ), gettextCatalog . getString ( "Stop now" ), gettextCatalog . getString ( "Cancel" )],
2017-04-06 09:20:52 +02:00
handleClick
);
}
else
{
DialogService . dialog (
2019-07-23 11:32:07 -04:00
gettextCatalog . getString ( "Stop running task" ),
gettextCatalog . getString ( "You can stop the task immediately, or allow the process to continue its current file and then stop." ),
[ gettextCatalog . getString ( "Stop after the current file" ), gettextCatalog . getString ( "Stop now" ), gettextCatalog . getString ( "Cancel" )],
2017-04-06 09:20:52 +02:00
handleClick
);
}
2015-12-12 15:12:25 +01:00
};
2016-03-13 16:29:50 +01:00
updateStateDisplay ();
updateActiveTask ();
2016-09-15 11:39:27 +02:00
});