Matteo Scandolo | 2906911 | 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 | ]) |
Matteo Scandolo | 2906911 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 9 | .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 Scandolo | 0c989e0 | 2016-05-31 16:57:50 -0700 | [diff] [blame^] | 30 | status.last_duration = status.last_duration * 1000; |
Matteo Scandolo | 24464a6 | 2016-05-31 14:26:26 -0700 | [diff] [blame] | 31 | 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 Scandolo | 2906911 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 33 | $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 Scandolo | 0c989e0 | 2016-05-31 16:57:50 -0700 | [diff] [blame^] | 58 | // 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 Scandolo | 24464a6 | 2016-05-31 14:26:26 -0700 | [diff] [blame] | 65 | // the synchronizer is running |
Matteo Scandolo | 0c989e0 | 2016-05-31 16:57:50 -0700 | [diff] [blame^] | 66 | // 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 Scandolo | 24464a6 | 2016-05-31 14:26:26 -0700 | [diff] [blame] | 72 | return false; |
| 73 | } |
| 74 | else{ |
| 75 | return true; |
| 76 | } |
Matteo Scandolo | 0c989e0 | 2016-05-31 16:57:50 -0700 | [diff] [blame^] | 77 | // } |
Matteo Scandolo | 2906911 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | $interval(() => { |
| 81 | if(isRunning){ |
| 82 | this.getDiags() |
| 83 | .then(diags => { |
| 84 | this.sendEvents(diags); |
| 85 | }); |
| 86 | } |
Matteo Scandolo | 0c989e0 | 2016-05-31 16:57:50 -0700 | [diff] [blame^] | 87 | }, 10000); |
Matteo Scandolo | 2906911 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 88 | }) |
| 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 Scandolo | 0c989e0 | 2016-05-31 16:57:50 -0700 | [diff] [blame^] | 96 | controller: function($log, $rootScope, Diag, xosNotification){ |
Matteo Scandolo | 2906911 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 97 | Diag.start(); |
Matteo Scandolo | 24464a6 | 2016-05-31 14:26:26 -0700 | [diff] [blame] | 98 | this.showNotificationPanel = true; |
Matteo Scandolo | 2906911 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 99 | this.synchronizers = {}; |
| 100 | |
Matteo Scandolo | 0c989e0 | 2016-05-31 16:57:50 -0700 | [diff] [blame^] | 101 | const notified = {}; |
| 102 | |
| 103 | // xosNotification.notify('test', {icon: 'http://localhost:8888/static/cord-logo.png', body: 'Diag'}); |
| 104 | |
Matteo Scandolo | 2906911 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 105 | this.showNoSync = true; |
| 106 | |
| 107 | $rootScope.$on('diag', (e, d) => { |
Matteo Scandolo | 0c989e0 | 2016-05-31 16:57:50 -0700 | [diff] [blame^] | 108 | // console.log(d.name); |
| 109 | if(d.name === 'global'){ |
| 110 | $log.info('Received event: ', d.info.last_syncrecord_start); |
Matteo Scandolo | 24464a6 | 2016-05-31 14:26:26 -0700 | [diff] [blame] | 111 | } |
Matteo Scandolo | 2906911 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 112 | this.synchronizers[d.name] = d; |
Matteo Scandolo | 0c989e0 | 2016-05-31 16:57:50 -0700 | [diff] [blame^] | 113 | |
| 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 Scandolo | 2906911 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 131 | this.showNoSync = false; |
| 132 | if(Object.keys(this.synchronizers).length === 0){ |
| 133 | this.showNoSync = true; |
| 134 | } |
| 135 | }); |
| 136 | |
| 137 | } |
| 138 | } |
| 139 | }); |
| 140 | |
| 141 | angular.element(document).ready(function() { |
| 142 | angular.bootstrap('#xosSynchronizerNotifier', ['xos.synchronizerNotifier']); |
| 143 | }); |