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