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