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.LED_state = Constants.LED_STATE_TEXT.off;
19      this.last_updated = new Date();
20
21      this.loading = false;
22      this.server_unreachable = false;
23      this.showNavigation = false;
24      this.bodyStyle = {};
25      this.path = '';
26
27      this.hostname = '';
28      this.mac_address = '';
29      this.defaultgateway = '';
30
31      this.ignoreHttpError = false;
32      this.systemName = '';
33
34      this.configJson = require('../../../config.json');
35
36      this.getServerId = function() {
37        return this.host.replace(/^https?\:\/\//ig, '');
38      };
39
40      this.reloadServerId = function() {
41        this.server_id = this.getServerId();
42      };
43
44      this.getHost = function() {
45        if (sessionStorage.getItem(
46                Constants.API_CREDENTIALS.host_storage_key) !== null) {
47          return sessionStorage.getItem(
48              Constants.API_CREDENTIALS.host_storage_key);
49        } else {
50          return Constants.API_CREDENTIALS.default_protocol + '://' +
51              window.location.hostname +
52              (window.location.port ? ':' + window.location.port : '');
53        }
54      };
55
56      this.setHost = function(hostWithPort) {
57        hostWithPort = hostWithPort.replace(/^https?\:\/\//ig, '');
58        var hostURL =
59            Constants.API_CREDENTIALS.default_protocol + '://' + hostWithPort;
60        sessionStorage.setItem(
61            Constants.API_CREDENTIALS.host_storage_key, hostURL);
62        this.host = hostURL;
63        this.reloadServerId();
64      };
65
66      this.getUser = function() {
67        return sessionStorage.getItem('LOGIN_ID');
68      };
69
70      this.host = this.getHost();
71      this.server_id = this.getServerId();
72
73      this.setNetworkInfo = function(data) {
74        var formatted = data.formatted_data || {};
75        this.hostname = data.hostname;
76        this.defaultgateway = data.defaultgateway;
77        this.mac_address = data.mac_address;
78        this.network_interfaces = formatted.interfaces || [];
79      };
80
81      this.setPowerOnState = function() {
82        this.server_state = Constants.HOST_STATE_TEXT.on;
83      };
84
85      this.setPowerOffState = function() {
86        this.server_state = Constants.HOST_STATE_TEXT.off;
87      };
88
89      this.setErrorState = function() {
90        this.server_state = Constants.HOST_STATE_TEXT.error;
91      };
92
93      this.setUnreachableState = function() {
94        this.server_state = Constants.HOST_STATE_TEXT.unreachable;
95      };
96
97      this.updateServerHealth = function(logs) {
98        // If any unresolved severity high logs are present, set server health
99        // to critical. Else if any unresolved severity medium logs are present
100        // set server health to warning.
101        this.server_health = Constants.SERVER_HEALTH.good;
102        for (var log of logs) {
103          if (log.priority == 'High' && !log.Resolved) {
104            this.server_health = Constants.SERVER_HEALTH.critical;
105            return;
106          } else if (log.priority == 'Medium' && !log.Resolved) {
107            this.server_health = Constants.SERVER_HEALTH.warning;
108          }
109        }
110      };
111
112      this.setSystemName = function(sysName) {
113        this.systemName = sysName;
114      };
115    }
116  ]);
117})(window.angular);
118