Sync notification working, need to be tested
diff --git a/views/ngXosLib/xosHelpers/spec/services/helpers/user-prefs.test.js b/views/ngXosLib/xosHelpers/spec/services/helpers/user-prefs.test.js
new file mode 100644
index 0000000..4c76484
--- /dev/null
+++ b/views/ngXosLib/xosHelpers/spec/services/helpers/user-prefs.test.js
@@ -0,0 +1,113 @@
+/**
+ * © OpenCORD
+ *
+ * Created by teone on 5/25/16.
+ */
+
+(function () {
+ 'use strict';
+ let service;
+ let cookies = {
+ xosUserPrefs: JSON.stringify({test: true})
+ };
+
+ const cookieMock = {
+ get: (name) => {
+ return cookies[name]
+ },
+ put: (name, value) => {
+ cookies[name] = value
+ }
+ };
+
+ describe('The xos.helper module', function(){
+
+ describe('The XosUserPrefs service', () => {
+
+ // load the application module
+ beforeEach(module('xos.helpers', ($provide) => {
+ $provide.value('$cookies', cookieMock);
+ }));
+
+ // inject the cartService
+ beforeEach(inject(function (_XosUserPrefs_) {
+ // The injector unwraps the underscores (_) from around the parameter names when matching
+ service = _XosUserPrefs_;
+ spyOn(cookieMock, 'put').and.callThrough();
+ }));
+
+ it('should exists and have methods', () => {
+ expect(service).toBeDefined();
+ expect(service.getAll).toBeDefined();
+ expect(service.setAll).toBeDefined();
+ expect(service.getSynchronizerNotificationStatus).toBeDefined();
+ expect(service.setSynchronizerNotificationStatus).toBeDefined();
+ });
+
+ describe('the getAll method', () => {
+ it('should return all the stored prefs', () => {
+ let prefs = service.getAll();
+ expect(prefs).toEqual(JSON.parse(cookies.xosUserPrefs));
+ });
+ });
+
+ describe('the setAll method', () => {
+ it('should override all preferences', () => {
+ service.setAll({test: true, updated: true});
+ expect(JSON.parse(cookies.xosUserPrefs)).toEqual({test: true, updated: true});
+ });
+ });
+
+ describe('the synchronizers status', () => {
+ let syncNotification;
+ beforeEach(() => {
+ syncNotification = {
+ synchronizers: {
+ notification: {
+ first: true,
+ second: false
+ }
+ }
+ }
+ cookies.xosUserPrefs = JSON.stringify(syncNotification);
+ });
+
+ describe('the getSynchronizerNotificationStatus method', () => {
+ it('should return notification status for all synchronizers', () => {
+ expect(service.getSynchronizerNotificationStatus()).toEqual(syncNotification.synchronizers.notification);
+ });
+
+ it('should return notification status for a single synchronizers', () => {
+ expect(service.getSynchronizerNotificationStatus('first')).toEqual(syncNotification.synchronizers.notification.first);
+ expect(service.getSynchronizerNotificationStatus('second')).toEqual(syncNotification.synchronizers.notification.second);
+ });
+ });
+
+ describe('the setSynchronizerNotificationStatus', () => {
+
+ it('should throw an error if called without synchronizer name', () => {
+ function wrapper (){
+ service.setSynchronizerNotificationStatus();
+ }
+ expect(wrapper).toThrowError('[XosUserPrefs] When updating a synchronizer is mandatory to provide a name.');
+ });
+
+ it('should update a synchronizer notification status', () => {
+ service.setSynchronizerNotificationStatus('second', true);
+ expect(service.getSynchronizerNotificationStatus('second')).toEqual(true);
+ expect(service.getSynchronizerNotificationStatus('first')).toEqual(true);
+
+ // should persist the change
+ expect(cookieMock.put).toHaveBeenCalledWith('xosUserPrefs', '{"synchronizers":{"notification":{"first":true,"second":true}}}');
+ });
+
+ it('should handle empty cookies', () => {
+ cookies.xosUserPrefs = '';
+ service.setSynchronizerNotificationStatus('second', true);
+ expect(service.getSynchronizerNotificationStatus('second')).toEqual(true);
+ });
+ });
+ });
+ });
+ });
+})();
\ No newline at end of file
diff --git a/views/ngXosLib/xosHelpers/src/services/helpers/user-prefs.service.js b/views/ngXosLib/xosHelpers/src/services/helpers/user-prefs.service.js
new file mode 100644
index 0000000..27edf7f
--- /dev/null
+++ b/views/ngXosLib/xosHelpers/src/services/helpers/user-prefs.service.js
@@ -0,0 +1,95 @@
+(function () {
+
+ angular.module('xos.helpers')
+
+ /**
+ * @ngdoc service
+ * @name xos.helpers.XosUserPrefs
+ * @description
+ * This service is used to store the user preferences in cookies, so that they survive to page changes.
+ * The structure of the user preference is:
+ * ```
+ * {
+ * synchronizers: {
+ * notification: {
+ * 'volt': boolean,
+ * 'openstack': boolean,
+ * ...
+ * }
+ * }
+ * }
+ * ```
+ **/
+
+ .service('XosUserPrefs', function($cookies){
+
+ let userPrefs = $cookies.get('xosUserPrefs') ? JSON.parse($cookies.get('xosUserPrefs')) : {};
+
+ /**
+ * @ngdoc method
+ * @name xos.helpers.XosUserPrefs#getAll
+ * @methodOf xos.helpers.XosUserPrefs
+ * @description
+ * Return all the user preferences stored in cookies
+ * @returns {object} The user preferences
+ **/
+ this.getAll = () => {
+ userPrefs = $cookies.get('xosUserPrefs') ? JSON.parse($cookies.get('xosUserPrefs')) : {};
+ return userPrefs;
+ };
+
+ /**
+ * @ngdoc method
+ * @name xos.helpers.XosUserPrefs#setAll
+ * @methodOf xos.helpers.XosUserPrefs
+ * @description
+ * Override all user preferences
+ * @param {object} prefs The user preferences
+ **/
+ this.setAll = (prefs) => {
+ $cookies.put('xosUserPrefs', JSON.stringify(prefs));
+ };
+
+ /**
+ * @ngdoc method
+ * @name xos.helpers.XosUserPrefs#getSynchronizerNotificationStatus
+ * @methodOf xos.helpers.XosUserPrefs
+ * @description
+ * Return the synchronizer notification status, if name is not provided return the status for all synchronizers
+ * @param {string=} prefs The synchronizer name
+ * @returns {object | string} The synchronizer status
+ **/
+ this.getSynchronizerNotificationStatus = (name = false) => {
+ if(name){
+ return this.getAll().synchronizers.notification[name];
+ }
+ return this.getAll().synchronizers.notification;
+ };
+
+ /**
+ * @ngdoc method
+ * @name xos.helpers.XosUserPrefs#setSynchronizerNotificationStatus
+ * @methodOf xos.helpers.XosUserPrefs
+ * @description
+ * Update the notification status for a single synchronizer
+ * @param {string} name The synchronizer name
+ * @param {boolean} value The notification status (true means that it has been sent)
+ **/
+ this.setSynchronizerNotificationStatus = (name = false, value) => {
+ if(!name){
+ throw new Error('[XosUserPrefs] When updating a synchronizer is mandatory to provide a name.')
+ }
+
+ let cookies = this.getAll();
+
+ if(!cookies.synchronizers){
+ cookies.synchronizers = {
+ notification: {}
+ }
+ }
+
+ cookies.synchronizers.notification[name] = value;
+ this.setAll(cookies);
+ }
+ });
+})();
\ No newline at end of file
diff --git a/views/ngXosViews/synchronizerNotifier/bower.json b/views/ngXosViews/synchronizerNotifier/bower.json
index 342b8f3..8b0eb12 100644
--- a/views/ngXosViews/synchronizerNotifier/bower.json
+++ b/views/ngXosViews/synchronizerNotifier/bower.json
@@ -14,8 +14,7 @@
"test",
"tests"
],
- "dependencies": {
- },
+ "dependencies": {},
"devDependencies": {
"jquery": "2.1.4",
"angular-mocks": "1.4.7",
@@ -27,6 +26,7 @@
"lodash": "~4.11.1",
"bootstrap-css": "3.3.6",
"angular-chart.js": "~0.10.2",
- "d3": "~3.5.17"
+ "d3": "~3.5.17",
+ "angular-recursion": "~1.0.5"
}
}
diff --git a/views/ngXosViews/synchronizerNotifier/src/css/main.css b/views/ngXosViews/synchronizerNotifier/src/css/main.css
index b95bde2..88a08e7 100644
--- a/views/ngXosViews/synchronizerNotifier/src/css/main.css
+++ b/views/ngXosViews/synchronizerNotifier/src/css/main.css
@@ -3,7 +3,8 @@
#xosSynchronizerNotifier .alert {
margin-bottom: 0px !important; }
#xosSynchronizerNotifier .sync-status-container {
- position: relative; }
+ position: relative;
+ z-index: 200; }
#xosSynchronizerNotifier .notification-panel {
position: absolute;
width: 200px; }
diff --git a/views/ngXosViews/synchronizerNotifier/src/index.html b/views/ngXosViews/synchronizerNotifier/src/index.html
index d8bc502..cb9b7b5 100644
--- a/views/ngXosViews/synchronizerNotifier/src/index.html
+++ b/views/ngXosViews/synchronizerNotifier/src/index.html
@@ -26,6 +26,7 @@
<script src="vendor/Chart.js/Chart.js"></script>
<script src="vendor/angular-chart.js/dist/angular-chart.js"></script>
<script src="vendor/d3/d3.js"></script>
+<script src="vendor/angular-recursion/angular-recursion.js"></script>
<!-- endbower -->
<!-- endjs -->
<!-- inject:js -->
diff --git a/views/ngXosViews/synchronizerNotifier/src/js/main.js b/views/ngXosViews/synchronizerNotifier/src/js/main.js
index 301345c..03c10be 100644
--- a/views/ngXosViews/synchronizerNotifier/src/js/main.js
+++ b/views/ngXosViews/synchronizerNotifier/src/js/main.js
@@ -79,7 +79,7 @@
this.sendEvents(diags);
});
}
- }, 10000);
+ }, 6 * 1000);
})
.directive('syncStatus', function() {
return {
@@ -88,31 +88,31 @@
bindToController: true,
controllerAs: 'vm',
templateUrl: 'templates/sync-status.tpl.html',
- controller: function($log, $rootScope, Diag, xosNotification){
+ controller: function($log, $rootScope, Diag, xosNotification, XosUserPrefs){
Diag.start();
this.showNotificationPanel = true;
this.synchronizers = {};
- const notified = {};
-
this.showNoSync = true;
$rootScope.$on('diag', (e, d) => {
this.synchronizers[d.name] = d;
+
// if errored
if(!d.status){
+ console.log(d.name, XosUserPrefs.getSynchronizerNotificationStatus(d.name));
// and not already notified
- if(!notified[d.name]){
- xosNotification.notify('CORD Synchronizer Error', {
- icon: '/xos/core/static/cord-logo.png',
- body: `The ${d.name} synchronizer has stopped.`
+ if(!XosUserPrefs.getSynchronizerNotificationStatus(d.name)){
+ xosNotification.notify('CORD Synchronizer', {
+ icon: '/static/cord-logo.png',
+ body: `The ${d.name} synchronizer has not performed actions in the last 15 minutes.`
});
}
- notified[d.name] = true;
+ XosUserPrefs.setSynchronizerNotificationStatus(d.name, true);
}
else {
- notified[d.name] = false;
+ XosUserPrefs.setSynchronizerNotificationStatus(d.name, false);
}
// hide list if empty
diff --git a/views/ngXosViews/synchronizerNotifier/src/sass/main.scss b/views/ngXosViews/synchronizerNotifier/src/sass/main.scss
index 273131a..d101d76 100644
--- a/views/ngXosViews/synchronizerNotifier/src/sass/main.scss
+++ b/views/ngXosViews/synchronizerNotifier/src/sass/main.scss
@@ -10,6 +10,7 @@
.sync-status-container {
position: relative;
+ z-index: 200;
}
.notification-panel {