blob: 93b8191d17ac3edcafe7680e08b58ce0697e8e50 [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])
9.config(function($provide) {
10 $provide.decorator('$rootScope', function($delegate) {
11 var Scope = $delegate.constructor;
12 // var origBroadcast = Scope.prototype.$broadcast;
13 // var origEmit = Scope.prototype.$emit;
14 var origOn = Scope.prototype.$on;
15
16 // Scope.prototype.$broadcast = function() {
17 // // console.log("$broadcast was called on $scope " + $scope.$id + " with arguments:", arguments);
18 // return origBroadcast.apply(this, arguments);
19 // };
20 // Scope.prototype.$emit = function() {
21 // // console.log("$emit was called on $scope " + $scope.$id + " with arguments:", arguments);
22 // return origEmit.apply(this, arguments);
23 // };
24
25 Scope.prototype.$on = function(){
26 // console.log('$on', arguments, arguments[1].toString());
27 return origOn.apply(this, arguments);
28 }
29 return $delegate;
30 });
31})
32.service('Diag', function($rootScope, $http, $q, $interval){
33
34 let isRunning = false;
35
36 this.getDiags = () => {
37 let d = $q.defer();
38 $http.get('/api/core/diags')
39 .then(res => {
40 d.resolve(res.data);
41 })
42 .catch(err => {
43 d.reject(err);
44 });
45
46 return d.promise;
47 };
48
49 this.sendEvents = (diags) => {
50 diags.forEach(d => {
51 let status = JSON.parse(d.backend_register);
52 status.last_run = new Date(status.last_run * 1000);
53 $rootScope.$broadcast(`diag`, {
54 name: d.name,
55 updated: d.updated,
56 info: status,
57 status: this.getSyncStatus(status)
58 });
59 });
60 };
61
62 this.start = () => {
63 isRunning = true;
64 this.getDiags()
65 .then(diags => {
66 this.sendEvents(diags);
67 });
68 return isRunning;
69 };
70
71 this.stop = () => {
72 isRunning = false;
73 return isRunning;
74 };
75
76 this.getSyncStatus = (status) => {
77
78 let gap = 5 * 60 * 1000; /* ms */
79 // let gap = 2;
80
81 if (((new Date()) - status.last_run) > gap){
82 return false;
83 }
84 return true;
85 }
86
87 $interval(() => {
88 if(isRunning){
89 this.getDiags()
90 .then(diags => {
91 this.sendEvents(diags);
92 });
93 }
94 }, 25000);
95})
96.run(function($log){
97 $log.info('Listening for Syncronizers Events!');
98})
99.directive('syncStatus', function() {
100 return {
101 restrict: 'E',
102 scope: {},
103 bindToController: true,
104 controllerAs: 'vm',
105 templateUrl: 'templates/sync-status.tpl.html',
106 controller: function($log, $rootScope, Diag){
107 Diag.start();
108 // this.showNotificationPanel = true;
109 this.synchronizers = {};
110
111 this.showNoSync = true;
112
113 $rootScope.$on('diag', (e, d) => {
114 // $log.info('Received event: ', d);
115 this.synchronizers[d.name] = d;
116 this.showNoSync = false;
117 if(Object.keys(this.synchronizers).length === 0){
118 this.showNoSync = true;
119 }
120 });
121
122 }
123 }
124});
125
126angular.element(document).ready(function() {
127 angular.bootstrap('#xosSynchronizerNotifier', ['xos.synchronizerNotifier']);
128});