blob: f65c4d150d1c87adcf7880710fac39e57e2aa54c [file] [log] [blame]
Matteo Scandolo42e3fe22016-05-27 14:52:37 -07001'use strict';
2
3angular.module('xos.synchronizerNotifier', [
4 'ngResource',
5 'ngCookies',
Matteo Scandolo42e3fe22016-05-27 14:52:37 -07006 'xos.helpers'
7])
Matteo Scandoloc0de7ed2016-06-06 13:09:51 -07008.run(function($rootScope){
9 $rootScope.$on('$locationChangeStart', function(event) {
10 event.preventDefault();
11 });
12})
Matteo Scandolo42e3fe22016-05-27 14:52:37 -070013.service('Diag', function($rootScope, $http, $q, $interval){
14
15 let isRunning = false;
16
17 this.getDiags = () => {
18 let d = $q.defer();
19 $http.get('/api/core/diags')
20 .then(res => {
21 d.resolve(res.data);
22 })
23 .catch(err => {
24 d.reject(err);
25 });
26
27 return d.promise;
28 };
29
30 this.sendEvents = (diags) => {
31 diags.forEach(d => {
32 let status = JSON.parse(d.backend_register);
33 status.last_run = new Date(status.last_run * 1000);
Matteo Scandolo56773f32016-05-31 16:57:50 -070034 status.last_duration = status.last_duration * 1000;
Matteo Scandolo1a5bf202016-05-31 14:26:26 -070035 status.last_synchronizer_start = new Date(status.last_synchronizer_start * 1000);
36 status.last_syncrecord_start = status.last_syncrecord_start ? new Date(status.last_syncrecord_start * 1000) : null;
Matteo Scandolo42e3fe22016-05-27 14:52:37 -070037 $rootScope.$broadcast(`diag`, {
38 name: d.name,
39 updated: d.updated,
40 info: status,
41 status: this.getSyncStatus(status)
42 });
43 });
44 };
45
46 this.start = () => {
47 isRunning = true;
48 this.getDiags()
49 .then(diags => {
50 this.sendEvents(diags);
51 });
52 return isRunning;
53 };
54
55 this.stop = () => {
56 isRunning = false;
57 return isRunning;
58 };
59
60 this.getSyncStatus = (status) => {
61
Matteo Scandolo99ac4ae2016-06-01 08:35:15 -070062 const now = new Date();
Matteo Scandolo7c4ffa42016-06-01 12:15:42 -070063 const gap = 15 * 60 * 1000; /* ms */
Matteo Scandolo8fabb0a2016-06-01 11:47:22 -070064 // const gap = 1 * 60 * 1000; // for demo use 1 minute
Matteo Scandolo99ac4ae2016-06-01 08:35:15 -070065 // if all of this values are older than 15 min,
66 // probably something is wrong
67 if (
68 (now - status.last_synchronizer_start) > gap &&
69 (now - status.last_syncrecord_start) > gap &&
70 (now - status.last_run) > gap
71 ){
72 return false;
73 }
74 else{
75 return true;
76 }
Matteo Scandolo42e3fe22016-05-27 14:52:37 -070077 }
78
79 $interval(() => {
80 if(isRunning){
81 this.getDiags()
82 .then(diags => {
83 this.sendEvents(diags);
84 });
85 }
Matteo Scandolo7c4ffa42016-06-01 12:15:42 -070086 }, 5 * 60 * 1000);
Matteo Scandolo42e3fe22016-05-27 14:52:37 -070087})
88.directive('syncStatus', function() {
89 return {
90 restrict: 'E',
91 scope: {},
92 bindToController: true,
93 controllerAs: 'vm',
94 templateUrl: 'templates/sync-status.tpl.html',
Matteo Scandoloe0afc4e2016-06-01 10:58:25 -070095 controller: function($log, $rootScope, Diag, xosNotification, XosUserPrefs){
Matteo Scandolo42e3fe22016-05-27 14:52:37 -070096 Diag.start();
Matteo Scandolo7c4ffa42016-06-01 12:15:42 -070097 // to debug set this to true,
98 // the panel will be opened by default
99 // this.showNotificationPanel = true;
Matteo Scandolo42e3fe22016-05-27 14:52:37 -0700100 this.synchronizers = {};
101
102 this.showNoSync = true;
103
104 $rootScope.$on('diag', (e, d) => {
Matteo Scandolo42e3fe22016-05-27 14:52:37 -0700105 this.synchronizers[d.name] = d;
Matteo Scandolo56773f32016-05-31 16:57:50 -0700106
Matteo Scandolo99ac4ae2016-06-01 08:35:15 -0700107 // if errored
Matteo Scandolo56773f32016-05-31 16:57:50 -0700108 if(!d.status){
Matteo Scandolo99ac4ae2016-06-01 08:35:15 -0700109 // and not already notified
Matteo Scandoloe0afc4e2016-06-01 10:58:25 -0700110 if(!XosUserPrefs.getSynchronizerNotificationStatus(d.name)){
111 xosNotification.notify('CORD Synchronizer', {
112 icon: '/static/cord-logo.png',
113 body: `The ${d.name} synchronizer has not performed actions in the last 15 minutes.`
Matteo Scandolo56773f32016-05-31 16:57:50 -0700114 });
115 }
Matteo Scandoloe0afc4e2016-06-01 10:58:25 -0700116 XosUserPrefs.setSynchronizerNotificationStatus(d.name, true);
Matteo Scandolo56773f32016-05-31 16:57:50 -0700117 }
118 else {
Matteo Scandoloe0afc4e2016-06-01 10:58:25 -0700119 XosUserPrefs.setSynchronizerNotificationStatus(d.name, false);
Matteo Scandolo56773f32016-05-31 16:57:50 -0700120 }
121
122 // hide list if empty
Matteo Scandolo42e3fe22016-05-27 14:52:37 -0700123 this.showNoSync = false;
124 if(Object.keys(this.synchronizers).length === 0){
125 this.showNoSync = true;
126 }
127 });
128
129 }
130 }
131});
132
133angular.element(document).ready(function() {
134 angular.bootstrap('#xosSynchronizerNotifier', ['xos.synchronizerNotifier']);
135});