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 | c806574 | 2016-06-01 08:35:15 -0700 | [diff] [blame] | 58 | const now = new Date(); |
Matteo Scandolo | 9b6d11f | 2016-06-01 11:47:22 -0700 | [diff] [blame^] | 59 | let gap = 15 * 60 * 1000; /* ms */ |
| 60 | // const gap = 1 * 60 * 1000; // for demo use 1 minute |
Matteo Scandolo | c806574 | 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 | 2906911 | 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 | f0958b1 | 2016-06-01 10:58:25 -0700 | [diff] [blame] | 82 | }, 6 * 1000); |
Matteo Scandolo | 2906911 | 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 | f0958b1 | 2016-06-01 10:58:25 -0700 | [diff] [blame] | 91 | controller: function($log, $rootScope, Diag, xosNotification, XosUserPrefs){ |
Matteo Scandolo | 2906911 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 92 | Diag.start(); |
Matteo Scandolo | 24464a6 | 2016-05-31 14:26:26 -0700 | [diff] [blame] | 93 | this.showNotificationPanel = true; |
Matteo Scandolo | 2906911 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 94 | this.synchronizers = {}; |
| 95 | |
| 96 | this.showNoSync = true; |
| 97 | |
| 98 | $rootScope.$on('diag', (e, d) => { |
Matteo Scandolo | 2906911 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 99 | this.synchronizers[d.name] = d; |
Matteo Scandolo | 0c989e0 | 2016-05-31 16:57:50 -0700 | [diff] [blame] | 100 | |
Matteo Scandolo | c806574 | 2016-06-01 08:35:15 -0700 | [diff] [blame] | 101 | // if errored |
Matteo Scandolo | 0c989e0 | 2016-05-31 16:57:50 -0700 | [diff] [blame] | 102 | if(!d.status){ |
Matteo Scandolo | c806574 | 2016-06-01 08:35:15 -0700 | [diff] [blame] | 103 | // and not already notified |
Matteo Scandolo | f0958b1 | 2016-06-01 10:58:25 -0700 | [diff] [blame] | 104 | if(!XosUserPrefs.getSynchronizerNotificationStatus(d.name)){ |
| 105 | xosNotification.notify('CORD Synchronizer', { |
| 106 | icon: '/static/cord-logo.png', |
| 107 | body: `The ${d.name} synchronizer has not performed actions in the last 15 minutes.` |
Matteo Scandolo | 0c989e0 | 2016-05-31 16:57:50 -0700 | [diff] [blame] | 108 | }); |
| 109 | } |
Matteo Scandolo | f0958b1 | 2016-06-01 10:58:25 -0700 | [diff] [blame] | 110 | XosUserPrefs.setSynchronizerNotificationStatus(d.name, true); |
Matteo Scandolo | 0c989e0 | 2016-05-31 16:57:50 -0700 | [diff] [blame] | 111 | } |
| 112 | else { |
Matteo Scandolo | f0958b1 | 2016-06-01 10:58:25 -0700 | [diff] [blame] | 113 | XosUserPrefs.setSynchronizerNotificationStatus(d.name, false); |
Matteo Scandolo | 0c989e0 | 2016-05-31 16:57:50 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | // hide list if empty |
Matteo Scandolo | 2906911 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 117 | this.showNoSync = false; |
| 118 | if(Object.keys(this.synchronizers).length === 0){ |
| 119 | this.showNoSync = true; |
| 120 | } |
| 121 | }); |
| 122 | |
| 123 | } |
| 124 | } |
| 125 | }); |
| 126 | |
| 127 | angular.element(document).ready(function() { |
| 128 | angular.bootstrap('#xosSynchronizerNotifier', ['xos.synchronizerNotifier']); |
| 129 | }); |