blob: a98f6725ae3b7d8836d7f1a318cdb0e416ae7c3c [file] [log] [blame]
Matteo Scandolo29069112016-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 Scandolo29069112016-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 Scandolo0c989e02016-05-31 16:57:50 -070030 status.last_duration = status.last_duration * 1000;
Matteo Scandolo24464a62016-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 Scandolo29069112016-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 Scandoloc8065742016-06-01 08:35:15 -070058 const now = new Date();
Matteo Scandolo9b6d11f2016-06-01 11:47:22 -070059 let gap = 15 * 60 * 1000; /* ms */
60 // const gap = 1 * 60 * 1000; // for demo use 1 minute
Matteo Scandoloc8065742016-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 Scandolo29069112016-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 Scandolof0958b12016-06-01 10:58:25 -070082 }, 6 * 1000);
Matteo Scandolo29069112016-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 Scandolof0958b12016-06-01 10:58:25 -070091 controller: function($log, $rootScope, Diag, xosNotification, XosUserPrefs){
Matteo Scandolo29069112016-05-27 14:52:37 -070092 Diag.start();
Matteo Scandolo24464a62016-05-31 14:26:26 -070093 this.showNotificationPanel = true;
Matteo Scandolo29069112016-05-27 14:52:37 -070094 this.synchronizers = {};
95
96 this.showNoSync = true;
97
98 $rootScope.$on('diag', (e, d) => {
Matteo Scandolo29069112016-05-27 14:52:37 -070099 this.synchronizers[d.name] = d;
Matteo Scandolo0c989e02016-05-31 16:57:50 -0700100
Matteo Scandoloc8065742016-06-01 08:35:15 -0700101 // if errored
Matteo Scandolo0c989e02016-05-31 16:57:50 -0700102 if(!d.status){
Matteo Scandoloc8065742016-06-01 08:35:15 -0700103 // and not already notified
Matteo Scandolof0958b12016-06-01 10:58:25 -0700104 if(!XosUserPrefs.getSynchronizerNotificationStatus(d.name)){
105 xosNotification.notify('CORD Synchronizer', {
106 icon: '/static/cord-logo.png',
107 body: `The ${d.name} synchronizer has not performed actions in the last 15 minutes.`
Matteo Scandolo0c989e02016-05-31 16:57:50 -0700108 });
109 }
Matteo Scandolof0958b12016-06-01 10:58:25 -0700110 XosUserPrefs.setSynchronizerNotificationStatus(d.name, true);
Matteo Scandolo0c989e02016-05-31 16:57:50 -0700111 }
112 else {
Matteo Scandolof0958b12016-06-01 10:58:25 -0700113 XosUserPrefs.setSynchronizerNotificationStatus(d.name, false);
Matteo Scandolo0c989e02016-05-31 16:57:50 -0700114 }
115
116 // hide list if empty
Matteo Scandolo29069112016-05-27 14:52:37 -0700117 this.showNoSync = false;
118 if(Object.keys(this.synchronizers).length === 0){
119 this.showNoSync = true;
120 }
121 });
122
123 }
124 }
125});
126
127angular.element(document).ready(function() {
128 angular.bootstrap('#xosSynchronizerNotifier', ['xos.synchronizerNotifier']);
129});