blob: 9017227a7ca4c22f7f8a7ebf717cc57a6ca095ac [file] [log] [blame]
Iftekharul Islam99d199f2017-03-24 15:28:25 -05001/**
2 * data service
3 *
4 * @module app/common/services/dataService
5 * @exports dataService
6 * @name dataService
7
Iftekharul Islam99d199f2017-03-24 15:28:25 -05008 */
9
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070010window.angular && (function(angular) {
11 'use strict';
Iftekharul Islam99d199f2017-03-24 15:28:25 -050012
Andrew Geisslerd27bb132018-05-24 11:07:27 -070013 angular.module('app.common.services').service('dataService', [
14 'Constants',
15 function(Constants) {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070016 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();
Iftekharul Islam99d199f2017-03-24 15:28:25 -050022
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070023 this.loading = false;
24 this.server_unreachable = false;
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070025 this.showNavigation = false;
26 this.bodyStyle = {};
27 this.path = '';
Iftekharul Islam99d199f2017-03-24 15:28:25 -050028
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070029 this.hostname = '';
30 this.mac_address = '';
Gunnar Mills659651e2018-05-30 15:21:07 -050031 this.defaultgateway = '';
Iftekharul Islama1d238f2018-02-26 12:29:45 -060032
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070033 this.displayErrorModal = false;
34 this.errorModalDetails = {};
Iftekharul Islama1d238f2018-02-26 12:29:45 -060035
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070036 this.ignoreHttpError = false;
AppaRao Pulib1289ec2018-11-14 20:33:30 +053037 this.systemName = '';
AppaRao Pulif04960f2018-12-06 21:51:27 +053038
39 this.configJson = require('../../../config.json');
40
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070041 this.getServerId = function() {
42 return this.host.replace(/^https?\:\/\//ig, '');
43 };
Iftekharul Islam1acb4122017-11-02 13:20:32 -050044
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070045 this.reloadServerId = function() {
46 this.server_id = this.getServerId();
47 };
Iftekharul Islam1acb4122017-11-02 13:20:32 -050048
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070049 this.getHost = function() {
Andrew Geisslerd27bb132018-05-24 11:07:27 -070050 if (sessionStorage.getItem(
51 Constants.API_CREDENTIALS.host_storage_key) !== null) {
52 return sessionStorage.getItem(
53 Constants.API_CREDENTIALS.host_storage_key);
54 } else {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070055 return Constants.API_CREDENTIALS.default_protocol + '://' +
Andrew Geisslerd27bb132018-05-24 11:07:27 -070056 window.location.hostname +
57 (window.location.port ? ':' + window.location.port : '');
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070058 }
59 };
Iftekharul Islam1acb4122017-11-02 13:20:32 -050060
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070061 this.setHost = function(hostWithPort) {
62 hostWithPort = hostWithPort.replace(/^https?\:\/\//ig, '');
Andrew Geisslerd27bb132018-05-24 11:07:27 -070063 var hostURL =
64 Constants.API_CREDENTIALS.default_protocol + '://' + hostWithPort;
65 sessionStorage.setItem(
66 Constants.API_CREDENTIALS.host_storage_key, hostURL);
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070067 this.host = hostURL;
68 this.reloadServerId();
69 };
Iftekharul Islam1acb4122017-11-02 13:20:32 -050070
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070071 this.getUser = function() {
72 return sessionStorage.getItem('LOGIN_ID');
73 };
Gunnar Mills1a60f6e2018-03-14 13:42:08 -050074
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070075 this.host = this.getHost();
76 this.server_id = this.getServerId();
Iftekharul Islam1acb4122017-11-02 13:20:32 -050077
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070078 this.setNetworkInfo = function(data) {
79 this.hostname = data.hostname;
Gunnar Mills659651e2018-05-30 15:21:07 -050080 this.defaultgateway = data.defaultgateway;
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070081 this.mac_address = data.mac_address;
82 };
Iftekharul Islamba556c32017-08-11 08:37:12 -050083
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070084 this.setPowerOnState = function() {
85 this.server_state = Constants.HOST_STATE_TEXT.on;
86 this.server_status = Constants.HOST_STATE.on;
87 };
Iftekharul Islam99d199f2017-03-24 15:28:25 -050088
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070089 this.setPowerOffState = function() {
90 this.server_state = Constants.HOST_STATE_TEXT.off;
91 this.server_status = Constants.HOST_STATE.off;
92 };
Iftekharul Islam99d199f2017-03-24 15:28:25 -050093
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070094 this.setErrorState = function() {
95 this.server_state = Constants.HOST_STATE_TEXT.error;
96 this.server_status = Constants.HOST_STATE.error;
97 };
Iftekharul Islam99d199f2017-03-24 15:28:25 -050098
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070099 this.setUnreachableState = function() {
100 this.server_state = Constants.HOST_STATE_TEXT.unreachable;
101 this.server_status = Constants.HOST_STATE.unreachable;
102 };
Iftekharul Islam34714092017-09-06 10:45:27 -0500103
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700104 this.updateServerHealth = function(logs) {
Gunnar Mills5760e532018-11-12 09:59:50 -0600105 // 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.
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700108 this.server_health = Constants.SERVER_HEALTH.good;
Gunnar Mills500ad782018-09-06 15:18:57 -0500109 for (var log of logs) {
Gunnar Mills5760e532018-11-12 09:59:50 -0600110 if (log.priority == 'High' && !log.Resolved) {
Gunnar Mills500ad782018-09-06 15:18:57 -0500111 this.server_health = Constants.SERVER_HEALTH.critical;
112 return;
Gunnar Mills5760e532018-11-12 09:59:50 -0600113 } else if (log.priority == 'Medium' && !log.Resolved) {
Gunnar Mills500ad782018-09-06 15:18:57 -0500114 this.server_health = Constants.SERVER_HEALTH.warning;
115 }
116 }
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700117 };
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600118
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700119 this.activateErrorModal = function(data) {
120 if (data && data.hasOwnProperty('title')) {
121 this.errorModalDetails.title = data.title;
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700122 } else {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700123 this.errorModalDetails.title = Constants.MESSAGES.ERROR_MODAL.TITLE;
124 }
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600125
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700126 if (data && data.hasOwnProperty('description')) {
127 this.errorModalDetails.description = data.description;
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700128 } else {
129 this.errorModalDetails.description =
130 Constants.MESSAGES.ERROR_MODAL.DESCRIPTION;
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700131 }
132 this.displayErrorModal = true;
133 };
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600134
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700135 this.deactivateErrorModal = function() {
136 this.displayErrorModal = false;
137 };
AppaRao Pulib1289ec2018-11-14 20:33:30 +0530138
139 this.setSystemName = function(sysName) {
140 this.systemName = sysName;
141 };
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700142 }
143 ]);
Sivas SRRbb9092a2018-01-27 08:40:58 -0600144})(window.angular);