blob: f0077f2f15ebc729670f17290f3873b69d5713fc [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001
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 Scandolo42e3fe22016-05-27 14:52:37 -070019'use strict';
20
21angular.module('xos.synchronizerNotifier', [
22 'ngResource',
23 'ngCookies',
Matteo Scandolo42e3fe22016-05-27 14:52:37 -070024 'xos.helpers'
25])
Matteo Scandolo42e3fe22016-05-27 14:52:37 -070026.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 Scandolo56773f32016-05-31 16:57:50 -070047 status.last_duration = status.last_duration * 1000;
Matteo Scandolo1a5bf202016-05-31 14:26:26 -070048 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 Scandolo42e3fe22016-05-27 14:52:37 -070050 $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 Scandolo99ac4ae2016-06-01 08:35:15 -070075 const now = new Date();
Matteo Scandolo7c4ffa42016-06-01 12:15:42 -070076 const gap = 15 * 60 * 1000; /* ms */
Matteo Scandolo8fabb0a2016-06-01 11:47:22 -070077 // const gap = 1 * 60 * 1000; // for demo use 1 minute
Matteo Scandolo99ac4ae2016-06-01 08:35:15 -070078 // 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 Scandolo29194952016-06-17 11:57:05 -070090 };
Matteo Scandolo42e3fe22016-05-27 14:52:37 -070091
92 $interval(() => {
93 if(isRunning){
94 this.getDiags()
95 .then(diags => {
96 this.sendEvents(diags);
97 });
98 }
Matteo Scandolo7c4ffa42016-06-01 12:15:42 -070099 }, 5 * 60 * 1000);
Matteo Scandolo42e3fe22016-05-27 14:52:37 -0700100})
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 Scandoloe0afc4e2016-06-01 10:58:25 -0700108 controller: function($log, $rootScope, Diag, xosNotification, XosUserPrefs){
Matteo Scandolo42e3fe22016-05-27 14:52:37 -0700109 Diag.start();
Matteo Scandolo7c4ffa42016-06-01 12:15:42 -0700110 // to debug set this to true,
111 // the panel will be opened by default
112 // this.showNotificationPanel = true;
Matteo Scandolo42e3fe22016-05-27 14:52:37 -0700113 this.synchronizers = {};
114
115 this.showNoSync = true;
116
117 $rootScope.$on('diag', (e, d) => {
Matteo Scandolo42e3fe22016-05-27 14:52:37 -0700118 this.synchronizers[d.name] = d;
Matteo Scandolo56773f32016-05-31 16:57:50 -0700119
Matteo Scandolo99ac4ae2016-06-01 08:35:15 -0700120 // if errored
Matteo Scandolo56773f32016-05-31 16:57:50 -0700121 if(!d.status){
Matteo Scandolo99ac4ae2016-06-01 08:35:15 -0700122 // and not already notified
Matteo Scandoloe0afc4e2016-06-01 10:58:25 -0700123 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 Scandolo56773f32016-05-31 16:57:50 -0700127 });
128 }
Matteo Scandoloe0afc4e2016-06-01 10:58:25 -0700129 XosUserPrefs.setSynchronizerNotificationStatus(d.name, true);
Matteo Scandolo56773f32016-05-31 16:57:50 -0700130 }
131 else {
Matteo Scandoloe0afc4e2016-06-01 10:58:25 -0700132 XosUserPrefs.setSynchronizerNotificationStatus(d.name, false);
Matteo Scandolo56773f32016-05-31 16:57:50 -0700133 }
134
135 // hide list if empty
Matteo Scandolo42e3fe22016-05-27 14:52:37 -0700136 this.showNoSync = false;
137 if(Object.keys(this.synchronizers).length === 0){
138 this.showNoSync = true;
139 }
140 });
141
142 }
143 }
144});
145
146angular.element(document).ready(function() {
147 angular.bootstrap('#xosSynchronizerNotifier', ['xos.synchronizerNotifier']);
148});