blob: e761fadd63c2498a88c3914c18f0b141e876ccc0 [file] [log] [blame]
Matteo Scandolo42e3fe22016-05-27 14:52:37 -07001'use strict';
2
3angular.module('xos.synchronizerNotifier', [
4 'ngResource',
5 'ngCookies',
6 'ui.router',
7 'xos.helpers'
8])
Matteo Scandolo42e3fe22016-05-27 14:52:37 -07009.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 Scandolo56773f32016-05-31 16:57:50 -070030 status.last_duration = status.last_duration * 1000;
Matteo Scandolo1a5bf202016-05-31 14:26:26 -070031 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 Scandolo42e3fe22016-05-27 14:52:37 -070033 $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 Scandolo99ac4ae2016-06-01 08:35:15 -070058 const now = new Date();
Matteo Scandolo7c4ffa42016-06-01 12:15:42 -070059 const gap = 15 * 60 * 1000; /* ms */
Matteo Scandolo8fabb0a2016-06-01 11:47:22 -070060 // const gap = 1 * 60 * 1000; // for demo use 1 minute
Matteo Scandolo99ac4ae2016-06-01 08:35:15 -070061 // 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 Scandolo42e3fe22016-05-27 14:52:37 -070073 }
74
75 $interval(() => {
76 if(isRunning){
77 this.getDiags()
78 .then(diags => {
79 this.sendEvents(diags);
80 });
81 }
Matteo Scandolo7c4ffa42016-06-01 12:15:42 -070082 }, 5 * 60 * 1000);
Matteo Scandolo42e3fe22016-05-27 14:52:37 -070083})
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 Scandoloe0afc4e2016-06-01 10:58:25 -070091 controller: function($log, $rootScope, Diag, xosNotification, XosUserPrefs){
Matteo Scandolo42e3fe22016-05-27 14:52:37 -070092 Diag.start();
Matteo Scandolo7c4ffa42016-06-01 12:15:42 -070093 // to debug set this to true,
94 // the panel will be opened by default
95 // this.showNotificationPanel = true;
Matteo Scandolo42e3fe22016-05-27 14:52:37 -070096 this.synchronizers = {};
97
98 this.showNoSync = true;
99
100 $rootScope.$on('diag', (e, d) => {
Matteo Scandolo42e3fe22016-05-27 14:52:37 -0700101 this.synchronizers[d.name] = d;
Matteo Scandolo56773f32016-05-31 16:57:50 -0700102
Matteo Scandolo99ac4ae2016-06-01 08:35:15 -0700103 // if errored
Matteo Scandolo56773f32016-05-31 16:57:50 -0700104 if(!d.status){
Matteo Scandolo99ac4ae2016-06-01 08:35:15 -0700105 // and not already notified
Matteo Scandoloe0afc4e2016-06-01 10:58:25 -0700106 if(!XosUserPrefs.getSynchronizerNotificationStatus(d.name)){
107 xosNotification.notify('CORD Synchronizer', {
108 icon: '/static/cord-logo.png',
109 body: `The ${d.name} synchronizer has not performed actions in the last 15 minutes.`
Matteo Scandolo56773f32016-05-31 16:57:50 -0700110 });
111 }
Matteo Scandoloe0afc4e2016-06-01 10:58:25 -0700112 XosUserPrefs.setSynchronizerNotificationStatus(d.name, true);
Matteo Scandolo56773f32016-05-31 16:57:50 -0700113 }
114 else {
Matteo Scandoloe0afc4e2016-06-01 10:58:25 -0700115 XosUserPrefs.setSynchronizerNotificationStatus(d.name, false);
Matteo Scandolo56773f32016-05-31 16:57:50 -0700116 }
117
118 // hide list if empty
Matteo Scandolo42e3fe22016-05-27 14:52:37 -0700119 this.showNoSync = false;
120 if(Object.keys(this.synchronizers).length === 0){
121 this.showNoSync = true;
122 }
123 });
124
125 }
126 }
127});
128
129angular.element(document).ready(function() {
130 angular.bootstrap('#xosSynchronizerNotifier', ['xos.synchronizerNotifier']);
131});