From 7227c7109e62a684d681f9227f72e8ba5568aa23 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Fri, 18 Jan 2019 15:39:58 +0100 Subject: [PATCH] admin: add a progression indicator for the deep synchronization --- admin/api/theme.go | 8 ++++++++ admin/static/js/app.js | 17 ++++++++++++++++- admin/static/views/settings.html | 3 +++ admin/sync/full.go | 18 ++++++++++++++++-- 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/admin/api/theme.go b/admin/api/theme.go index e95f0993..e7803963 100644 --- a/admin/api/theme.go +++ b/admin/api/theme.go @@ -44,6 +44,14 @@ func init() { router.GET("/api/remote/themes/:thid/exercices", apiHandler(themeHandler(sync.ApiListRemoteExercices))) // Synchronize + router.GET("/api/sync/deep", apiHandler( + func(_ httprouter.Params, _ []byte) (interface{}, error) { + if sync.DeepSyncProgress == 0 { + return nil, errors.New("Pas de synchronisation en cours") + } else { + return map[string]interface{}{"progress": sync.DeepSyncProgress}, nil + } + })) router.POST("/api/sync/deep", apiHandler( func(_ httprouter.Params, _ []byte) (interface{}, error) { return sync.SyncDeep(sync.GlobalImporter), nil diff --git a/admin/static/js/app.js b/admin/static/js/app.js index d725b113..b30c97cb 100644 --- a/admin/static/js/app.js +++ b/admin/static/js/app.js @@ -422,7 +422,7 @@ angular.module("FICApp") $interval(updBox, 750); }) - .controller("SettingsController", function($scope, $rootScope, Settings, ROSettings, $location, $http) { + .controller("SettingsController", function($scope, $rootScope, Settings, ROSettings, $location, $http, $interval) { $scope.config = Settings.get(); $scope.config.$promise.then(function(response) { $rootScope.settings.start = new Date(response.start); @@ -433,6 +433,21 @@ angular.module("FICApp") $scope.configro = ROSettings.get(); $scope.duration = 240; + var progressInterval = $interval(function() { + $http.get("/api/sync/deep").then(function(response) { + if (response.data && response.data.progress) + $scope.syncProgress = Math.floor(response.data.progress * 100 / 255) + " %"; + else + $scope.syncProgress = response.data; + }, function(response) { + if (response.data && response.data.errmsg) + $scope.syncProgress = response.data.errmsg; + else + $scope.syncProgress = response.data; + }) + }, 1500); + $scope.$on('$destroy', function () { $interval.cancel(progressInterval); }); + $scope.saveSettings = function(msg) { if (msg === undefined) { msg = 'New settings saved!'; } var nStart = this.config.start; diff --git a/admin/static/views/settings.html b/admin/static/views/settings.html index 192d4573..01cfdb58 100644 --- a/admin/static/views/settings.html +++ b/admin/static/views/settings.html @@ -161,6 +161,9 @@
+
+ {{ syncProgress }} +
diff --git a/admin/sync/full.go b/admin/sync/full.go index 9814a09d..070b3120 100644 --- a/admin/sync/full.go +++ b/admin/sync/full.go @@ -18,10 +18,14 @@ var DeepReportPath = "full_import_report.json" // oneDeepSync ensure there is no more than one running deep sync. var oneDeepSync sync.Mutex +// DeepSyncProgress expose the progression of the depp synchronization (0 = 0%, 255 = 100%). +var DeepSyncProgress uint8 + // SyncDeep performs a recursive synchronisation: from themes to challenge items. func SyncDeep(i Importer) (errs map[string][]string) { oneDeepSync.Lock() defer oneDeepSync.Unlock() + DeepSyncProgress = 1 errs = map[string][]string{} @@ -29,19 +33,28 @@ func SyncDeep(i Importer) (errs map[string][]string) { errs["_themes"] = SyncThemes(i) if themes, err := fic.GetThemes(); err == nil { - for _, theme := range themes { + DeepSyncProgress = 2 + var themeStep uint8 = uint8(250) / uint8(len(themes)) + + for tid, theme := range themes { + DeepSyncProgress = 3 + uint8(tid) * themeStep errs[theme.Name] = SyncExercices(i, theme) if exercices, err := theme.GetExercices(); err == nil { - for _, exercice := range exercices { + var exerciceStep uint8 = themeStep / uint8(len(exercices)) + for eid, exercice := range exercices { + DeepSyncProgress = 3 + uint8(tid) * themeStep + uint8(eid) * exerciceStep errs[theme.Name] = append(errs[theme.Name], SyncExerciceFiles(i, exercice)...) + DeepSyncProgress += exerciceStep / 3 errs[theme.Name] = append(errs[theme.Name], SyncExerciceFlags(i, exercice)...) + DeepSyncProgress += exerciceStep / 3 errs[theme.Name] = append(errs[theme.Name], SyncExerciceHints(i, exercice)...) } } } } + DeepSyncProgress = 254 errs["_date"] = append(errs["_date"], fmt.Sprintf("%v", time.Now())) errs["_regeneration"] = []string{} @@ -66,5 +79,6 @@ func SyncDeep(i Importer) (errs map[string][]string) { errs["_regeneration"] = append(errs["_regeneration"], err.Error()) } + DeepSyncProgress = 255 return }