blob: 1d7de328d0ecd1840c981b039a6f27269b058a8c [file] [log] [blame]
Matteo Scandolo42e3fe22016-05-27 14:52:37 -07001'use strict';
2
3angular.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 Scandolo1a5bf202016-05-31 14:26:26 -070053 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 Scandolo42e3fe22016-05-27 14:52:37 -070055 $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 Scandolo1a5bf202016-05-31 14:26:26 -070082 if(status.last_run > status.last_synchronizer_start){
83 // the synchronizer has finished
84 return true;
Matteo Scandolo42e3fe22016-05-27 14:52:37 -070085 }
Matteo Scandolo1a5bf202016-05-31 14:26:26 -070086 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 Scandolo42e3fe22016-05-27 14:52:37 -070099 }
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 Scandolo1a5bf202016-05-31 14:26:26 -0700122 this.showNotificationPanel = true;
Matteo Scandolo42e3fe22016-05-27 14:52:37 -0700123 this.synchronizers = {};
124
125 this.showNoSync = true;
126
127 $rootScope.$on('diag', (e, d) => {
Matteo Scandolo1a5bf202016-05-31 14:26:26 -0700128 if(d.name === 'vcpe'){
129 $log.info('Received event: ', d.info.last_run, d.info.last_synchronizer_start);
130 }
Matteo Scandolo42e3fe22016-05-27 14:52:37 -0700131 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
142angular.element(document).ready(function() {
143 angular.bootstrap('#xosSynchronizerNotifier', ['xos.synchronizerNotifier']);
144});