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', |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 6 | 'xos.helpers' |
| 7 | ]) |
Matteo Scandolo | c0de7ed | 2016-06-06 13:09:51 -0700 | [diff] [blame] | 8 | .run(function($rootScope){ |
| 9 | $rootScope.$on('$locationChangeStart', function(event) { |
| 10 | event.preventDefault(); |
| 11 | }); |
| 12 | }) |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 13 | .service('Diag', function($rootScope, $http, $q, $interval){ |
| 14 | |
| 15 | let isRunning = false; |
| 16 | |
| 17 | this.getDiags = () => { |
| 18 | let d = $q.defer(); |
| 19 | $http.get('/api/core/diags') |
| 20 | .then(res => { |
| 21 | d.resolve(res.data); |
| 22 | }) |
| 23 | .catch(err => { |
| 24 | d.reject(err); |
| 25 | }); |
| 26 | |
| 27 | return d.promise; |
| 28 | }; |
| 29 | |
| 30 | this.sendEvents = (diags) => { |
| 31 | diags.forEach(d => { |
| 32 | let status = JSON.parse(d.backend_register); |
| 33 | status.last_run = new Date(status.last_run * 1000); |
Matteo Scandolo | 56773f3 | 2016-05-31 16:57:50 -0700 | [diff] [blame] | 34 | status.last_duration = status.last_duration * 1000; |
Matteo Scandolo | 1a5bf20 | 2016-05-31 14:26:26 -0700 | [diff] [blame] | 35 | status.last_synchronizer_start = new Date(status.last_synchronizer_start * 1000); |
| 36 | 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] | 37 | $rootScope.$broadcast(`diag`, { |
| 38 | name: d.name, |
| 39 | updated: d.updated, |
| 40 | info: status, |
| 41 | status: this.getSyncStatus(status) |
| 42 | }); |
| 43 | }); |
| 44 | }; |
| 45 | |
| 46 | this.start = () => { |
| 47 | isRunning = true; |
| 48 | this.getDiags() |
| 49 | .then(diags => { |
| 50 | this.sendEvents(diags); |
| 51 | }); |
| 52 | return isRunning; |
| 53 | }; |
| 54 | |
| 55 | this.stop = () => { |
| 56 | isRunning = false; |
| 57 | return isRunning; |
| 58 | }; |
| 59 | |
| 60 | this.getSyncStatus = (status) => { |
| 61 | |
Matteo Scandolo | 99ac4ae | 2016-06-01 08:35:15 -0700 | [diff] [blame] | 62 | const now = new Date(); |
Matteo Scandolo | 7c4ffa4 | 2016-06-01 12:15:42 -0700 | [diff] [blame] | 63 | const gap = 15 * 60 * 1000; /* ms */ |
Matteo Scandolo | 8fabb0a | 2016-06-01 11:47:22 -0700 | [diff] [blame] | 64 | // const gap = 1 * 60 * 1000; // for demo use 1 minute |
Matteo Scandolo | 99ac4ae | 2016-06-01 08:35:15 -0700 | [diff] [blame] | 65 | // if all of this values are older than 15 min, |
| 66 | // probably something is wrong |
| 67 | if ( |
| 68 | (now - status.last_synchronizer_start) > gap && |
| 69 | (now - status.last_syncrecord_start) > gap && |
| 70 | (now - status.last_run) > gap |
| 71 | ){ |
| 72 | return false; |
| 73 | } |
| 74 | else{ |
| 75 | return true; |
| 76 | } |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | $interval(() => { |
| 80 | if(isRunning){ |
| 81 | this.getDiags() |
| 82 | .then(diags => { |
| 83 | this.sendEvents(diags); |
| 84 | }); |
| 85 | } |
Matteo Scandolo | 7c4ffa4 | 2016-06-01 12:15:42 -0700 | [diff] [blame] | 86 | }, 5 * 60 * 1000); |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 87 | }) |
| 88 | .directive('syncStatus', function() { |
| 89 | return { |
| 90 | restrict: 'E', |
| 91 | scope: {}, |
| 92 | bindToController: true, |
| 93 | controllerAs: 'vm', |
| 94 | templateUrl: 'templates/sync-status.tpl.html', |
Matteo Scandolo | e0afc4e | 2016-06-01 10:58:25 -0700 | [diff] [blame] | 95 | controller: function($log, $rootScope, Diag, xosNotification, XosUserPrefs){ |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 96 | Diag.start(); |
Matteo Scandolo | 7c4ffa4 | 2016-06-01 12:15:42 -0700 | [diff] [blame] | 97 | // to debug set this to true, |
| 98 | // the panel will be opened by default |
| 99 | // this.showNotificationPanel = true; |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 100 | this.synchronizers = {}; |
| 101 | |
| 102 | this.showNoSync = true; |
| 103 | |
| 104 | $rootScope.$on('diag', (e, d) => { |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 105 | this.synchronizers[d.name] = d; |
Matteo Scandolo | 56773f3 | 2016-05-31 16:57:50 -0700 | [diff] [blame] | 106 | |
Matteo Scandolo | 99ac4ae | 2016-06-01 08:35:15 -0700 | [diff] [blame] | 107 | // if errored |
Matteo Scandolo | 56773f3 | 2016-05-31 16:57:50 -0700 | [diff] [blame] | 108 | if(!d.status){ |
Matteo Scandolo | 99ac4ae | 2016-06-01 08:35:15 -0700 | [diff] [blame] | 109 | // and not already notified |
Matteo Scandolo | e0afc4e | 2016-06-01 10:58:25 -0700 | [diff] [blame] | 110 | if(!XosUserPrefs.getSynchronizerNotificationStatus(d.name)){ |
| 111 | xosNotification.notify('CORD Synchronizer', { |
| 112 | icon: '/static/cord-logo.png', |
| 113 | body: `The ${d.name} synchronizer has not performed actions in the last 15 minutes.` |
Matteo Scandolo | 56773f3 | 2016-05-31 16:57:50 -0700 | [diff] [blame] | 114 | }); |
| 115 | } |
Matteo Scandolo | e0afc4e | 2016-06-01 10:58:25 -0700 | [diff] [blame] | 116 | XosUserPrefs.setSynchronizerNotificationStatus(d.name, true); |
Matteo Scandolo | 56773f3 | 2016-05-31 16:57:50 -0700 | [diff] [blame] | 117 | } |
| 118 | else { |
Matteo Scandolo | e0afc4e | 2016-06-01 10:58:25 -0700 | [diff] [blame] | 119 | XosUserPrefs.setSynchronizerNotificationStatus(d.name, false); |
Matteo Scandolo | 56773f3 | 2016-05-31 16:57:50 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | // hide list if empty |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 123 | this.showNoSync = false; |
| 124 | if(Object.keys(this.synchronizers).length === 0){ |
| 125 | this.showNoSync = true; |
| 126 | } |
| 127 | }); |
| 128 | |
| 129 | } |
| 130 | } |
| 131 | }); |
| 132 | |
| 133 | angular.element(document).ready(function() { |
| 134 | angular.bootstrap('#xosSynchronizerNotifier', ['xos.synchronizerNotifier']); |
| 135 | }); |