blob: 54ef0a68d1b426084bf0c4efef185d6243d916f7 [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])
Matteo Scandolo42e3fe22016-05-27 14:52:37 -07009.service('Diag', function($rootScope, $http, $q, $interval){
10
11 let isRunning = false;
12
13 this.getDiags = () => {
14 let d = $q.defer();
15 $http.get('/api/core/diags')
16 .then(res => {
17 d.resolve(res.data);
18 })
19 .catch(err => {
20 d.reject(err);
21 });
22
23 return d.promise;
24 };
25
26 this.sendEvents = (diags) => {
27 diags.forEach(d => {
28 let status = JSON.parse(d.backend_register);
29 status.last_run = new Date(status.last_run * 1000);
Matteo Scandolo56773f32016-05-31 16:57:50 -070030 status.last_duration = status.last_duration * 1000;
Matteo Scandolo1a5bf202016-05-31 14:26:26 -070031 status.last_synchronizer_start = new Date(status.last_synchronizer_start * 1000);
32 status.last_syncrecord_start = status.last_syncrecord_start ? new Date(status.last_syncrecord_start * 1000) : null;
Matteo Scandolo42e3fe22016-05-27 14:52:37 -070033 $rootScope.$broadcast(`diag`, {
34 name: d.name,
35 updated: d.updated,
36 info: status,
37 status: this.getSyncStatus(status)
38 });
39 });
40 };
41
42 this.start = () => {
43 isRunning = true;
44 this.getDiags()
45 .then(diags => {
46 this.sendEvents(diags);
47 });
48 return isRunning;
49 };
50
51 this.stop = () => {
52 isRunning = false;
53 return isRunning;
54 };
55
56 this.getSyncStatus = (status) => {
57
Matteo Scandolo56773f32016-05-31 16:57:50 -070058 // let gap = 5 * 60 * 1000; /* ms */
59 let gap = 1 * 60 * 1000;
60 // if(status.last_run > status.last_synchronizer_start){
61 // // the synchronizer has finished
62 // return true;
63 // }
64 // else {
Matteo Scandolo1a5bf202016-05-31 14:26:26 -070065 // the synchronizer is running
Matteo Scandolo56773f32016-05-31 16:57:50 -070066 // if(status.last_syncrecord_start){
67 // // but no step have been completed
68 // return false;
69 // }
70 // else
71 if (((new Date()) - status.last_syncrecord_start) > gap){
Matteo Scandolo1a5bf202016-05-31 14:26:26 -070072 return false;
73 }
74 else{
75 return true;
76 }
Matteo Scandolo56773f32016-05-31 16:57:50 -070077 // }
Matteo Scandolo42e3fe22016-05-27 14:52:37 -070078 }
79
80 $interval(() => {
81 if(isRunning){
82 this.getDiags()
83 .then(diags => {
84 this.sendEvents(diags);
85 });
86 }
Matteo Scandolo56773f32016-05-31 16:57:50 -070087 }, 10000);
Matteo Scandolo42e3fe22016-05-27 14:52:37 -070088})
89.directive('syncStatus', function() {
90 return {
91 restrict: 'E',
92 scope: {},
93 bindToController: true,
94 controllerAs: 'vm',
95 templateUrl: 'templates/sync-status.tpl.html',
Matteo Scandolo56773f32016-05-31 16:57:50 -070096 controller: function($log, $rootScope, Diag, xosNotification){
Matteo Scandolo42e3fe22016-05-27 14:52:37 -070097 Diag.start();
Matteo Scandolo1a5bf202016-05-31 14:26:26 -070098 this.showNotificationPanel = true;
Matteo Scandolo42e3fe22016-05-27 14:52:37 -070099 this.synchronizers = {};
100
Matteo Scandolo56773f32016-05-31 16:57:50 -0700101 const notified = {};
102
103 // xosNotification.notify('test', {icon: 'http://localhost:8888/static/cord-logo.png', body: 'Diag'});
104
Matteo Scandolo42e3fe22016-05-27 14:52:37 -0700105 this.showNoSync = true;
106
107 $rootScope.$on('diag', (e, d) => {
Matteo Scandolo56773f32016-05-31 16:57:50 -0700108 // console.log(d.name);
109 if(d.name === 'global'){
110 $log.info('Received event: ', d.info.last_syncrecord_start);
Matteo Scandolo1a5bf202016-05-31 14:26:26 -0700111 }
Matteo Scandolo42e3fe22016-05-27 14:52:37 -0700112 this.synchronizers[d.name] = d;
Matteo Scandolo56773f32016-05-31 16:57:50 -0700113
114 if(!d.status){
115
116 if(!notified[d.name]){
117 console.log('sent notify');
118 xosNotification.notify('CORD Synchronizer Error', {
119 icon: 'http://localhost:8888/static/cord-logo.png',
120 body: `[DEBUG] The ${d.name} synchronizer has stopped.`
121 });
122 }
123
124 notified[d.name] = true;
125 }
126 else {
127 notified[d.name] = false;
128 }
129
130 // hide list if empty
Matteo Scandolo42e3fe22016-05-27 14:52:37 -0700131 this.showNoSync = false;
132 if(Object.keys(this.synchronizers).length === 0){
133 this.showNoSync = true;
134 }
135 });
136
137 }
138 }
139});
140
141angular.element(document).ready(function() {
142 angular.bootstrap('#xosSynchronizerNotifier', ['xos.synchronizerNotifier']);
143});