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