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.showNavigation = false;
26      this.bodyStyle = {};
27      this.path = '';
28
29      this.hostname = '';
30      this.mac_address = '';
31      this.defaultgateway = '';
32
33      this.displayErrorModal = false;
34      this.errorModalDetails = {};
35
36      this.ignoreHttpError = false;
37      this.getServerId = function() {
38        return this.host.replace(/^https?\:\/\//ig, '');
39      };
40
41      this.reloadServerId = function() {
42        this.server_id = this.getServerId();
43      };
44
45      this.getHost = function() {
46        if (sessionStorage.getItem(
47                Constants.API_CREDENTIALS.host_storage_key) !== null) {
48          return sessionStorage.getItem(
49              Constants.API_CREDENTIALS.host_storage_key);
50        } else {
51          return Constants.API_CREDENTIALS.default_protocol + '://' +
52              window.location.hostname +
53              (window.location.port ? ':' + window.location.port : '');
54        }
55      };
56
57      this.setHost = function(hostWithPort) {
58        hostWithPort = hostWithPort.replace(/^https?\:\/\//ig, '');
59        var hostURL =
60            Constants.API_CREDENTIALS.default_protocol + '://' + hostWithPort;
61        sessionStorage.setItem(
62            Constants.API_CREDENTIALS.host_storage_key, hostURL);
63        this.host = hostURL;
64        this.reloadServerId();
65      };
66
67      this.getUser = function() {
68        return sessionStorage.getItem('LOGIN_ID');
69      };
70
71      this.host = this.getHost();
72      this.server_id = this.getServerId();
73
74      this.setNetworkInfo = function(data) {
75        this.hostname = data.hostname;
76        this.defaultgateway = data.defaultgateway;
77        this.mac_address = data.mac_address;
78      };
79
80      this.setPowerOnState = function() {
81        this.server_state = Constants.HOST_STATE_TEXT.on;
82        this.server_status = Constants.HOST_STATE.on;
83      };
84
85      this.setPowerOffState = function() {
86        this.server_state = Constants.HOST_STATE_TEXT.off;
87        this.server_status = Constants.HOST_STATE.off;
88      };
89
90      this.setErrorState = function() {
91        this.server_state = Constants.HOST_STATE_TEXT.error;
92        this.server_status = Constants.HOST_STATE.error;
93      };
94
95      this.setUnreachableState = function() {
96        this.server_state = Constants.HOST_STATE_TEXT.unreachable;
97        this.server_status = Constants.HOST_STATE.unreachable;
98      };
99
100      this.updateServerHealth = function(logs) {
101        // If any unresolved severity high logs are present, set server health
102        // to critical. Else if any unresolved severity medium logs are present
103        // set server health to warning.
104        this.server_health = Constants.SERVER_HEALTH.good;
105        for (var log of logs) {
106          if (log.priority == 'High' && !log.Resolved) {
107            this.server_health = Constants.SERVER_HEALTH.critical;
108            return;
109          } else if (log.priority == 'Medium' && !log.Resolved) {
110            this.server_health = Constants.SERVER_HEALTH.warning;
111          }
112        }
113      };
114
115      this.activateErrorModal = function(data) {
116        if (data && data.hasOwnProperty('title')) {
117          this.errorModalDetails.title = data.title;
118        } else {
119          this.errorModalDetails.title = Constants.MESSAGES.ERROR_MODAL.TITLE;
120        }
121
122        if (data && data.hasOwnProperty('description')) {
123          this.errorModalDetails.description = data.description;
124        } else {
125          this.errorModalDetails.description =
126              Constants.MESSAGES.ERROR_MODAL.DESCRIPTION;
127        }
128        this.displayErrorModal = true;
129      };
130
131      this.deactivateErrorModal = function() {
132        this.displayErrorModal = false;
133      };
134    }
135  ]);
136})(window.angular);
137