1/**
2 * data service
3 *
4 * @module app/common/services/dataService
5 * @exports dataService
6 * @name dataService
7
8 */
9
10window.angular && (function(angular) {
11  'use strict';
12
13  angular.module('app.common.services').service('dataService', [
14    'Constants',
15    function(Constants) {
16      this.server_health = Constants.SERVER_HEALTH.unknown;
17      this.server_state = 'Unreachable';
18      this.server_status = -2;
19      this.chassis_state = 'On';
20      this.LED_state = Constants.LED_STATE_TEXT.off;
21      this.last_updated = new Date();
22
23      this.loading = false;
24      this.server_unreachable = false;
25      this.loading_message = '';
26      this.showNavigation = false;
27      this.bodyStyle = {};
28      this.path = '';
29      this.sensorData = [];
30
31      this.hostname = '';
32      this.mac_address = '';
33      this.remote_window_active = false;
34
35      this.displayErrorModal = false;
36      this.errorModalDetails = {};
37
38      this.ignoreHttpError = false;
39      this.getServerId = function() {
40        return this.host.replace(/^https?\:\/\//ig, '');
41      };
42
43      this.reloadServerId = function() {
44        this.server_id = this.getServerId();
45      };
46
47      this.getHost = function() {
48        if (sessionStorage.getItem(
49                Constants.API_CREDENTIALS.host_storage_key) !== null) {
50          return sessionStorage.getItem(
51              Constants.API_CREDENTIALS.host_storage_key);
52        } else {
53          return Constants.API_CREDENTIALS.default_protocol + '://' +
54              window.location.hostname +
55              (window.location.port ? ':' + window.location.port : '');
56        }
57      };
58
59      this.setHost = function(hostWithPort) {
60        hostWithPort = hostWithPort.replace(/^https?\:\/\//ig, '');
61        var hostURL =
62            Constants.API_CREDENTIALS.default_protocol + '://' + hostWithPort;
63        sessionStorage.setItem(
64            Constants.API_CREDENTIALS.host_storage_key, hostURL);
65        this.host = hostURL;
66        this.reloadServerId();
67      };
68
69      this.getUser = function() {
70        return sessionStorage.getItem('LOGIN_ID');
71      };
72
73      this.host = this.getHost();
74      this.server_id = this.getServerId();
75
76      this.setNetworkInfo = function(data) {
77        this.hostname = data.hostname;
78        this.mac_address = data.mac_address;
79      };
80
81      this.setPowerOnState = function() {
82        this.server_state = Constants.HOST_STATE_TEXT.on;
83        this.server_status = Constants.HOST_STATE.on;
84      };
85
86      this.setPowerOffState = function() {
87        this.server_state = Constants.HOST_STATE_TEXT.off;
88        this.server_status = Constants.HOST_STATE.off;
89      };
90
91      this.setErrorState = function() {
92        this.server_state = Constants.HOST_STATE_TEXT.error;
93        this.server_status = Constants.HOST_STATE.error;
94      };
95
96      this.setUnreachableState = function() {
97        this.server_state = Constants.HOST_STATE_TEXT.unreachable;
98        this.server_status = Constants.HOST_STATE.unreachable;
99      };
100
101      this.setRemoteWindowActive = function() {
102        this.remote_window_active = true;
103      };
104
105      this.setRemoteWindowInactive = function() {
106        this.remote_window_active = false;
107      };
108
109      this.updateServerHealth = function(logs) {
110        var criticals = logs.filter(function(item) {
111          return item.health_flags.critical == true;
112        });
113
114        if (criticals.length) {
115          this.server_health = Constants.SERVER_HEALTH.critical;
116          return;
117        }
118
119        var warnings = logs.filter(function(item) {
120          return item.health_flags.warning == true;
121        });
122
123        if (warnings.length) {
124          this.server_health = Constants.SERVER_HEALTH.warning;
125          return;
126        }
127
128        this.server_health = Constants.SERVER_HEALTH.good;
129      };
130
131      this.activateErrorModal = function(data) {
132        if (data && data.hasOwnProperty('title')) {
133          this.errorModalDetails.title = data.title;
134        } else {
135          this.errorModalDetails.title = Constants.MESSAGES.ERROR_MODAL.TITLE;
136        }
137
138        if (data && data.hasOwnProperty('description')) {
139          this.errorModalDetails.description = data.description;
140        } else {
141          this.errorModalDetails.description =
142              Constants.MESSAGES.ERROR_MODAL.DESCRIPTION;
143        }
144        this.displayErrorModal = true;
145      };
146
147      this.deactivateErrorModal = function() {
148        this.displayErrorModal = false;
149      };
150    }
151  ]);
152
153})(window.angular);
154