blob: 499d47498bdd000063958bdbedacaabf2308e472 [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 Scandolo42e3fe22016-05-27 14:52:37 -07008.service('Diag', function($rootScope, $http, $q, $interval){
9
10 let isRunning = false;
11
12 this.getDiags = () => {
13 let d = $q.defer();
14 $http.get('/api/core/diags')
15 .then(res => {
16 d.resolve(res.data);
17 })
18 .catch(err => {
19 d.reject(err);
20 });
21
22 return d.promise;
23 };
24
25 this.sendEvents = (diags) => {
26 diags.forEach(d => {
27 let status = JSON.parse(d.backend_register);
28 status.last_run = new Date(status.last_run * 1000);
Matteo Scandolo56773f32016-05-31 16:57:50 -070029 status.last_duration = status.last_duration * 1000;
Matteo Scandolo1a5bf202016-05-31 14:26:26 -070030 status.last_synchronizer_start = new Date(status.last_synchronizer_start * 1000);
31 status.last_syncrecord_start = status.last_syncrecord_start ? new Date(status.last_syncrecord_start * 1000) : null;
Matteo Scandolo42e3fe22016-05-27 14:52:37 -070032 $rootScope.$broadcast(`diag`, {
33 name: d.name,
34 updated: d.updated,
35 info: status,
36 status: this.getSyncStatus(status)
37 });
38 });
39 };
40
41 this.start = () => {
42 isRunning = true;
43 this.getDiags()
44 .then(diags => {
45 this.sendEvents(diags);
46 });
47 return isRunning;
48 };
49
50 this.stop = () => {
51 isRunning = false;
52 return isRunning;
53 };
54
55 this.getSyncStatus = (status) => {
56
Matteo Scandolo99ac4ae2016-06-01 08:35:15 -070057 const now = new Date();
Matteo Scandolo7c4ffa42016-06-01 12:15:42 -070058 const gap = 15 * 60 * 1000; /* ms */
Matteo Scandolo8fabb0a2016-06-01 11:47:22 -070059 // const gap = 1 * 60 * 1000; // for demo use 1 minute
Matteo Scandolo99ac4ae2016-06-01 08:35:15 -070060 // if all of this values are older than 15 min,
61 // probably something is wrong
62 if (
63 (now - status.last_synchronizer_start) > gap &&
64 (now - status.last_syncrecord_start) > gap &&
65 (now - status.last_run) > gap
66 ){
67 return false;
68 }
69 else{
70 return true;
71 }
Matteo Scandolo29194952016-06-17 11:57:05 -070072 };
Matteo Scandolo42e3fe22016-05-27 14:52:37 -070073
74 $interval(() => {
75 if(isRunning){
76 this.getDiags()
77 .then(diags => {
78 this.sendEvents(diags);
79 });
80 }
Matteo Scandolo7c4ffa42016-06-01 12:15:42 -070081 }, 5 * 60 * 1000);
Matteo Scandolo42e3fe22016-05-27 14:52:37 -070082})
83.directive('syncStatus', function() {
84 return {
85 restrict: 'E',
86 scope: {},
87 bindToController: true,
88 controllerAs: 'vm',
89 templateUrl: 'templates/sync-status.tpl.html',
Matteo Scandoloe0afc4e2016-06-01 10:58:25 -070090 controller: function($log, $rootScope, Diag, xosNotification, XosUserPrefs){
Matteo Scandolo42e3fe22016-05-27 14:52:37 -070091 Diag.start();
Matteo Scandolo7c4ffa42016-06-01 12:15:42 -070092 // to debug set this to true,
93 // the panel will be opened by default
94 // this.showNotificationPanel = true;
Matteo Scandolo42e3fe22016-05-27 14:52:37 -070095 this.synchronizers = {};
96
97 this.showNoSync = true;
98
99 $rootScope.$on('diag', (e, d) => {
Matteo Scandolo42e3fe22016-05-27 14:52:37 -0700100 this.synchronizers[d.name] = d;
Matteo Scandolo56773f32016-05-31 16:57:50 -0700101
Matteo Scandolo99ac4ae2016-06-01 08:35:15 -0700102 // if errored
Matteo Scandolo56773f32016-05-31 16:57:50 -0700103 if(!d.status){
Matteo Scandolo99ac4ae2016-06-01 08:35:15 -0700104 // and not already notified
Matteo Scandoloe0afc4e2016-06-01 10:58:25 -0700105 if(!XosUserPrefs.getSynchronizerNotificationStatus(d.name)){
106 xosNotification.notify('CORD Synchronizer', {
107 icon: '/static/cord-logo.png',
108 body: `The ${d.name} synchronizer has not performed actions in the last 15 minutes.`
Matteo Scandolo56773f32016-05-31 16:57:50 -0700109 });
110 }
Matteo Scandoloe0afc4e2016-06-01 10:58:25 -0700111 XosUserPrefs.setSynchronizerNotificationStatus(d.name, true);
Matteo Scandolo56773f32016-05-31 16:57:50 -0700112 }
113 else {
Matteo Scandoloe0afc4e2016-06-01 10:58:25 -0700114 XosUserPrefs.setSynchronizerNotificationStatus(d.name, false);
Matteo Scandolo56773f32016-05-31 16:57:50 -0700115 }
116
117 // hide list if empty
Matteo Scandolo42e3fe22016-05-27 14:52:37 -0700118 this.showNoSync = false;
119 if(Object.keys(this.synchronizers).length === 0){
120 this.showNoSync = true;
121 }
122 });
123
124 }
125 }
126});
127
128angular.element(document).ready(function() {
129 angular.bootstrap('#xosSynchronizerNotifier', ['xos.synchronizerNotifier']);
130});