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 | ]) |
Matteo Scandolo | 42e3fe2 | 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 | 56773f3 | 2016-05-31 16:57:50 -0700 | [diff] [blame] | 30 | status.last_duration = status.last_duration * 1000; |
Matteo Scandolo | 1a5bf20 | 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 | 42e3fe2 | 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 | 99ac4ae | 2016-06-01 08:35:15 -0700 | [diff] [blame] | 58 | const now = new Date(); |
Matteo Scandolo | 7c4ffa4 | 2016-06-01 12:15:42 -0700 | [diff] [blame] | 59 | const gap = 15 * 60 * 1000; /* ms */ |
Matteo Scandolo | 8fabb0a | 2016-06-01 11:47:22 -0700 | [diff] [blame] | 60 | // const gap = 1 * 60 * 1000; // for demo use 1 minute |
Matteo Scandolo | 99ac4ae | 2016-06-01 08:35:15 -0700 | [diff] [blame] | 61 | // if all of this values are older than 15 min, |
| 62 | // probably something is wrong |
| 63 | if ( |
| 64 | (now - status.last_synchronizer_start) > gap && |
| 65 | (now - status.last_syncrecord_start) > gap && |
| 66 | (now - status.last_run) > gap |
| 67 | ){ |
| 68 | return false; |
| 69 | } |
| 70 | else{ |
| 71 | return true; |
| 72 | } |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | $interval(() => { |
| 76 | if(isRunning){ |
| 77 | this.getDiags() |
| 78 | .then(diags => { |
| 79 | this.sendEvents(diags); |
| 80 | }); |
| 81 | } |
Matteo Scandolo | 7c4ffa4 | 2016-06-01 12:15:42 -0700 | [diff] [blame] | 82 | }, 5 * 60 * 1000); |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 83 | }) |
| 84 | .directive('syncStatus', function() { |
| 85 | return { |
| 86 | restrict: 'E', |
| 87 | scope: {}, |
| 88 | bindToController: true, |
| 89 | controllerAs: 'vm', |
| 90 | templateUrl: 'templates/sync-status.tpl.html', |
Matteo Scandolo | e0afc4e | 2016-06-01 10:58:25 -0700 | [diff] [blame] | 91 | controller: function($log, $rootScope, Diag, xosNotification, XosUserPrefs){ |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 92 | Diag.start(); |
Matteo Scandolo | 7c4ffa4 | 2016-06-01 12:15:42 -0700 | [diff] [blame] | 93 | // to debug set this to true, |
| 94 | // the panel will be opened by default |
| 95 | // this.showNotificationPanel = true; |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 96 | this.synchronizers = {}; |
| 97 | |
| 98 | this.showNoSync = true; |
| 99 | |
| 100 | $rootScope.$on('diag', (e, d) => { |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 101 | this.synchronizers[d.name] = d; |
Matteo Scandolo | 56773f3 | 2016-05-31 16:57:50 -0700 | [diff] [blame] | 102 | |
Matteo Scandolo | 99ac4ae | 2016-06-01 08:35:15 -0700 | [diff] [blame] | 103 | // if errored |
Matteo Scandolo | 56773f3 | 2016-05-31 16:57:50 -0700 | [diff] [blame] | 104 | if(!d.status){ |
Matteo Scandolo | 99ac4ae | 2016-06-01 08:35:15 -0700 | [diff] [blame] | 105 | // and not already notified |
Matteo Scandolo | e0afc4e | 2016-06-01 10:58:25 -0700 | [diff] [blame] | 106 | if(!XosUserPrefs.getSynchronizerNotificationStatus(d.name)){ |
| 107 | xosNotification.notify('CORD Synchronizer', { |
| 108 | icon: '/static/cord-logo.png', |
| 109 | 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] | 110 | }); |
| 111 | } |
Matteo Scandolo | e0afc4e | 2016-06-01 10:58:25 -0700 | [diff] [blame] | 112 | XosUserPrefs.setSynchronizerNotificationStatus(d.name, true); |
Matteo Scandolo | 56773f3 | 2016-05-31 16:57:50 -0700 | [diff] [blame] | 113 | } |
| 114 | else { |
Matteo Scandolo | e0afc4e | 2016-06-01 10:58:25 -0700 | [diff] [blame] | 115 | XosUserPrefs.setSynchronizerNotificationStatus(d.name, false); |
Matteo Scandolo | 56773f3 | 2016-05-31 16:57:50 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | // hide list if empty |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 119 | this.showNoSync = false; |
| 120 | if(Object.keys(this.synchronizers).length === 0){ |
| 121 | this.showNoSync = true; |
| 122 | } |
| 123 | }); |
| 124 | |
| 125 | } |
| 126 | } |
| 127 | }); |
| 128 | |
| 129 | angular.element(document).ready(function() { |
| 130 | angular.bootstrap('#xosSynchronizerNotifier', ['xos.synchronizerNotifier']); |
| 131 | }); |