Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
| 3 | angular.module('xos.synchronizerNotifier', [ |
| 4 | 'ngResource', |
| 5 | 'ngCookies', |
| 6 | 'ui.router', |
| 7 | 'xos.helpers' |
| 8 | ]) |
| 9 | .config(function($provide) { |
| 10 | $provide.decorator('$rootScope', function($delegate) { |
| 11 | var Scope = $delegate.constructor; |
| 12 | // var origBroadcast = Scope.prototype.$broadcast; |
| 13 | // var origEmit = Scope.prototype.$emit; |
| 14 | var origOn = Scope.prototype.$on; |
| 15 | |
| 16 | // Scope.prototype.$broadcast = function() { |
| 17 | // // console.log("$broadcast was called on $scope " + $scope.$id + " with arguments:", arguments); |
| 18 | // return origBroadcast.apply(this, arguments); |
| 19 | // }; |
| 20 | // Scope.prototype.$emit = function() { |
| 21 | // // console.log("$emit was called on $scope " + $scope.$id + " with arguments:", arguments); |
| 22 | // return origEmit.apply(this, arguments); |
| 23 | // }; |
| 24 | |
| 25 | Scope.prototype.$on = function(){ |
| 26 | // console.log('$on', arguments, arguments[1].toString()); |
| 27 | return origOn.apply(this, arguments); |
| 28 | } |
| 29 | return $delegate; |
| 30 | }); |
| 31 | }) |
| 32 | .service('Diag', function($rootScope, $http, $q, $interval){ |
| 33 | |
| 34 | let isRunning = false; |
| 35 | |
| 36 | this.getDiags = () => { |
| 37 | let d = $q.defer(); |
| 38 | $http.get('/api/core/diags') |
| 39 | .then(res => { |
| 40 | d.resolve(res.data); |
| 41 | }) |
| 42 | .catch(err => { |
| 43 | d.reject(err); |
| 44 | }); |
| 45 | |
| 46 | return d.promise; |
| 47 | }; |
| 48 | |
| 49 | this.sendEvents = (diags) => { |
| 50 | diags.forEach(d => { |
| 51 | let status = JSON.parse(d.backend_register); |
| 52 | status.last_run = new Date(status.last_run * 1000); |
Matteo Scandolo | 1a5bf20 | 2016-05-31 14:26:26 -0700 | [diff] [blame^] | 53 | status.last_synchronizer_start = new Date(status.last_synchronizer_start * 1000); |
| 54 | status.last_syncrecord_start = status.last_syncrecord_start ? new Date(status.last_syncrecord_start * 1000) : null; |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 55 | $rootScope.$broadcast(`diag`, { |
| 56 | name: d.name, |
| 57 | updated: d.updated, |
| 58 | info: status, |
| 59 | status: this.getSyncStatus(status) |
| 60 | }); |
| 61 | }); |
| 62 | }; |
| 63 | |
| 64 | this.start = () => { |
| 65 | isRunning = true; |
| 66 | this.getDiags() |
| 67 | .then(diags => { |
| 68 | this.sendEvents(diags); |
| 69 | }); |
| 70 | return isRunning; |
| 71 | }; |
| 72 | |
| 73 | this.stop = () => { |
| 74 | isRunning = false; |
| 75 | return isRunning; |
| 76 | }; |
| 77 | |
| 78 | this.getSyncStatus = (status) => { |
| 79 | |
| 80 | let gap = 5 * 60 * 1000; /* ms */ |
| 81 | // let gap = 2; |
Matteo Scandolo | 1a5bf20 | 2016-05-31 14:26:26 -0700 | [diff] [blame^] | 82 | if(status.last_run > status.last_synchronizer_start){ |
| 83 | // the synchronizer has finished |
| 84 | return true; |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 85 | } |
Matteo Scandolo | 1a5bf20 | 2016-05-31 14:26:26 -0700 | [diff] [blame^] | 86 | else { |
| 87 | // the synchronizer is running |
| 88 | if(!status.last_syncrecord_start){ |
| 89 | // but no step have been completed |
| 90 | return false; |
| 91 | } |
| 92 | else if (((new Date()) - status.last_syncrecord_start) > gap){ |
| 93 | return false; |
| 94 | } |
| 95 | else{ |
| 96 | return true; |
| 97 | } |
| 98 | } |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | $interval(() => { |
| 102 | if(isRunning){ |
| 103 | this.getDiags() |
| 104 | .then(diags => { |
| 105 | this.sendEvents(diags); |
| 106 | }); |
| 107 | } |
| 108 | }, 25000); |
| 109 | }) |
| 110 | .run(function($log){ |
| 111 | $log.info('Listening for Syncronizers Events!'); |
| 112 | }) |
| 113 | .directive('syncStatus', function() { |
| 114 | return { |
| 115 | restrict: 'E', |
| 116 | scope: {}, |
| 117 | bindToController: true, |
| 118 | controllerAs: 'vm', |
| 119 | templateUrl: 'templates/sync-status.tpl.html', |
| 120 | controller: function($log, $rootScope, Diag){ |
| 121 | Diag.start(); |
Matteo Scandolo | 1a5bf20 | 2016-05-31 14:26:26 -0700 | [diff] [blame^] | 122 | this.showNotificationPanel = true; |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 123 | this.synchronizers = {}; |
| 124 | |
| 125 | this.showNoSync = true; |
| 126 | |
| 127 | $rootScope.$on('diag', (e, d) => { |
Matteo Scandolo | 1a5bf20 | 2016-05-31 14:26:26 -0700 | [diff] [blame^] | 128 | if(d.name === 'vcpe'){ |
| 129 | $log.info('Received event: ', d.info.last_run, d.info.last_synchronizer_start); |
| 130 | } |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 131 | this.synchronizers[d.name] = d; |
| 132 | this.showNoSync = false; |
| 133 | if(Object.keys(this.synchronizers).length === 0){ |
| 134 | this.showNoSync = true; |
| 135 | } |
| 136 | }); |
| 137 | |
| 138 | } |
| 139 | } |
| 140 | }); |
| 141 | |
| 142 | angular.element(document).ready(function() { |
| 143 | angular.bootstrap('#xosSynchronizerNotifier', ['xos.synchronizerNotifier']); |
| 144 | }); |