Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 19 | 'use strict'; |
| 20 | |
| 21 | angular.module('xos.synchronizerNotifier', [ |
| 22 | 'ngResource', |
| 23 | 'ngCookies', |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 24 | 'xos.helpers' |
| 25 | ]) |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 26 | .service('Diag', function($rootScope, $http, $q, $interval){ |
| 27 | |
| 28 | let isRunning = false; |
| 29 | |
| 30 | this.getDiags = () => { |
| 31 | let d = $q.defer(); |
| 32 | $http.get('/api/core/diags') |
| 33 | .then(res => { |
| 34 | d.resolve(res.data); |
| 35 | }) |
| 36 | .catch(err => { |
| 37 | d.reject(err); |
| 38 | }); |
| 39 | |
| 40 | return d.promise; |
| 41 | }; |
| 42 | |
| 43 | this.sendEvents = (diags) => { |
| 44 | diags.forEach(d => { |
| 45 | let status = JSON.parse(d.backend_register); |
| 46 | status.last_run = new Date(status.last_run * 1000); |
Matteo Scandolo | 56773f3 | 2016-05-31 16:57:50 -0700 | [diff] [blame] | 47 | status.last_duration = status.last_duration * 1000; |
Matteo Scandolo | 1a5bf20 | 2016-05-31 14:26:26 -0700 | [diff] [blame] | 48 | status.last_synchronizer_start = new Date(status.last_synchronizer_start * 1000); |
| 49 | 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] | 50 | $rootScope.$broadcast(`diag`, { |
| 51 | name: d.name, |
| 52 | updated: d.updated, |
| 53 | info: status, |
| 54 | status: this.getSyncStatus(status) |
| 55 | }); |
| 56 | }); |
| 57 | }; |
| 58 | |
| 59 | this.start = () => { |
| 60 | isRunning = true; |
| 61 | this.getDiags() |
| 62 | .then(diags => { |
| 63 | this.sendEvents(diags); |
| 64 | }); |
| 65 | return isRunning; |
| 66 | }; |
| 67 | |
| 68 | this.stop = () => { |
| 69 | isRunning = false; |
| 70 | return isRunning; |
| 71 | }; |
| 72 | |
| 73 | this.getSyncStatus = (status) => { |
| 74 | |
Matteo Scandolo | 99ac4ae | 2016-06-01 08:35:15 -0700 | [diff] [blame] | 75 | const now = new Date(); |
Matteo Scandolo | 7c4ffa4 | 2016-06-01 12:15:42 -0700 | [diff] [blame] | 76 | const gap = 15 * 60 * 1000; /* ms */ |
Matteo Scandolo | 8fabb0a | 2016-06-01 11:47:22 -0700 | [diff] [blame] | 77 | // const gap = 1 * 60 * 1000; // for demo use 1 minute |
Matteo Scandolo | 99ac4ae | 2016-06-01 08:35:15 -0700 | [diff] [blame] | 78 | // if all of this values are older than 15 min, |
| 79 | // probably something is wrong |
| 80 | if ( |
| 81 | (now - status.last_synchronizer_start) > gap && |
| 82 | (now - status.last_syncrecord_start) > gap && |
| 83 | (now - status.last_run) > gap |
| 84 | ){ |
| 85 | return false; |
| 86 | } |
| 87 | else{ |
| 88 | return true; |
| 89 | } |
Matteo Scandolo | 2919495 | 2016-06-17 11:57:05 -0700 | [diff] [blame] | 90 | }; |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 91 | |
| 92 | $interval(() => { |
| 93 | if(isRunning){ |
| 94 | this.getDiags() |
| 95 | .then(diags => { |
| 96 | this.sendEvents(diags); |
| 97 | }); |
| 98 | } |
Matteo Scandolo | 7c4ffa4 | 2016-06-01 12:15:42 -0700 | [diff] [blame] | 99 | }, 5 * 60 * 1000); |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 100 | }) |
| 101 | .directive('syncStatus', function() { |
| 102 | return { |
| 103 | restrict: 'E', |
| 104 | scope: {}, |
| 105 | bindToController: true, |
| 106 | controllerAs: 'vm', |
| 107 | templateUrl: 'templates/sync-status.tpl.html', |
Matteo Scandolo | e0afc4e | 2016-06-01 10:58:25 -0700 | [diff] [blame] | 108 | controller: function($log, $rootScope, Diag, xosNotification, XosUserPrefs){ |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 109 | Diag.start(); |
Matteo Scandolo | 7c4ffa4 | 2016-06-01 12:15:42 -0700 | [diff] [blame] | 110 | // to debug set this to true, |
| 111 | // the panel will be opened by default |
| 112 | // this.showNotificationPanel = true; |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 113 | this.synchronizers = {}; |
| 114 | |
| 115 | this.showNoSync = true; |
| 116 | |
| 117 | $rootScope.$on('diag', (e, d) => { |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 118 | this.synchronizers[d.name] = d; |
Matteo Scandolo | 56773f3 | 2016-05-31 16:57:50 -0700 | [diff] [blame] | 119 | |
Matteo Scandolo | 99ac4ae | 2016-06-01 08:35:15 -0700 | [diff] [blame] | 120 | // if errored |
Matteo Scandolo | 56773f3 | 2016-05-31 16:57:50 -0700 | [diff] [blame] | 121 | if(!d.status){ |
Matteo Scandolo | 99ac4ae | 2016-06-01 08:35:15 -0700 | [diff] [blame] | 122 | // and not already notified |
Matteo Scandolo | e0afc4e | 2016-06-01 10:58:25 -0700 | [diff] [blame] | 123 | if(!XosUserPrefs.getSynchronizerNotificationStatus(d.name)){ |
| 124 | xosNotification.notify('CORD Synchronizer', { |
| 125 | icon: '/static/cord-logo.png', |
| 126 | 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] | 127 | }); |
| 128 | } |
Matteo Scandolo | e0afc4e | 2016-06-01 10:58:25 -0700 | [diff] [blame] | 129 | XosUserPrefs.setSynchronizerNotificationStatus(d.name, true); |
Matteo Scandolo | 56773f3 | 2016-05-31 16:57:50 -0700 | [diff] [blame] | 130 | } |
| 131 | else { |
Matteo Scandolo | e0afc4e | 2016-06-01 10:58:25 -0700 | [diff] [blame] | 132 | XosUserPrefs.setSynchronizerNotificationStatus(d.name, false); |
Matteo Scandolo | 56773f3 | 2016-05-31 16:57:50 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | // hide list if empty |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 136 | this.showNoSync = false; |
| 137 | if(Object.keys(this.synchronizers).length === 0){ |
| 138 | this.showNoSync = true; |
| 139 | } |
| 140 | }); |
| 141 | |
| 142 | } |
| 143 | } |
| 144 | }); |
| 145 | |
| 146 | angular.element(document).ready(function() { |
| 147 | angular.bootstrap('#xosSynchronizerNotifier', ['xos.synchronizerNotifier']); |
| 148 | }); |