blob: 76ab381a8155fcc4b3d2722942153a4efe48a0cd [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 Pulif04960f2018-12-06 21:51:27 +053037
38 this.configJson = require('../../../config.json');
39
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070040 this.getServerId = function() {
41 return this.host.replace(/^https?\:\/\//ig, '');
42 };
Iftekharul Islam1acb4122017-11-02 13:20:32 -050043
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070044 this.reloadServerId = function() {
45 this.server_id = this.getServerId();
46 };
Iftekharul Islam1acb4122017-11-02 13:20:32 -050047
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070048 this.getHost = function() {
Andrew Geisslerd27bb132018-05-24 11:07:27 -070049 if (sessionStorage.getItem(
50 Constants.API_CREDENTIALS.host_storage_key) !== null) {
51 return sessionStorage.getItem(
52 Constants.API_CREDENTIALS.host_storage_key);
53 } else {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070054 return Constants.API_CREDENTIALS.default_protocol + '://' +
Andrew Geisslerd27bb132018-05-24 11:07:27 -070055 window.location.hostname +
56 (window.location.port ? ':' + window.location.port : '');
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070057 }
58 };
Iftekharul Islam1acb4122017-11-02 13:20:32 -050059
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070060 this.setHost = function(hostWithPort) {
61 hostWithPort = hostWithPort.replace(/^https?\:\/\//ig, '');
Andrew Geisslerd27bb132018-05-24 11:07:27 -070062 var hostURL =
63 Constants.API_CREDENTIALS.default_protocol + '://' + hostWithPort;
64 sessionStorage.setItem(
65 Constants.API_CREDENTIALS.host_storage_key, hostURL);
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070066 this.host = hostURL;
67 this.reloadServerId();
68 };
Iftekharul Islam1acb4122017-11-02 13:20:32 -050069
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070070 this.getUser = function() {
71 return sessionStorage.getItem('LOGIN_ID');
72 };
Gunnar Mills1a60f6e2018-03-14 13:42:08 -050073
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070074 this.host = this.getHost();
75 this.server_id = this.getServerId();
Iftekharul Islam1acb4122017-11-02 13:20:32 -050076
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070077 this.setNetworkInfo = function(data) {
78 this.hostname = data.hostname;
Gunnar Mills659651e2018-05-30 15:21:07 -050079 this.defaultgateway = data.defaultgateway;
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070080 this.mac_address = data.mac_address;
81 };
Iftekharul Islamba556c32017-08-11 08:37:12 -050082
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070083 this.setPowerOnState = function() {
84 this.server_state = Constants.HOST_STATE_TEXT.on;
85 this.server_status = Constants.HOST_STATE.on;
86 };
Iftekharul Islam99d199f2017-03-24 15:28:25 -050087
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070088 this.setPowerOffState = function() {
89 this.server_state = Constants.HOST_STATE_TEXT.off;
90 this.server_status = Constants.HOST_STATE.off;
91 };
Iftekharul Islam99d199f2017-03-24 15:28:25 -050092
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070093 this.setErrorState = function() {
94 this.server_state = Constants.HOST_STATE_TEXT.error;
95 this.server_status = Constants.HOST_STATE.error;
96 };
Iftekharul Islam99d199f2017-03-24 15:28:25 -050097
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070098 this.setUnreachableState = function() {
99 this.server_state = Constants.HOST_STATE_TEXT.unreachable;
100 this.server_status = Constants.HOST_STATE.unreachable;
101 };
Iftekharul Islam34714092017-09-06 10:45:27 -0500102
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700103 this.updateServerHealth = function(logs) {
Gunnar Mills5760e532018-11-12 09:59:50 -0600104 // 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.
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700107 this.server_health = Constants.SERVER_HEALTH.good;
Gunnar Mills500ad782018-09-06 15:18:57 -0500108 for (var log of logs) {
Gunnar Mills5760e532018-11-12 09:59:50 -0600109 if (log.priority == 'High' && !log.Resolved) {
Gunnar Mills500ad782018-09-06 15:18:57 -0500110 this.server_health = Constants.SERVER_HEALTH.critical;
111 return;
Gunnar Mills5760e532018-11-12 09:59:50 -0600112 } else if (log.priority == 'Medium' && !log.Resolved) {
Gunnar Mills500ad782018-09-06 15:18:57 -0500113 this.server_health = Constants.SERVER_HEALTH.warning;
114 }
115 }
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700116 };
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600117
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700118 this.activateErrorModal = function(data) {
119 if (data && data.hasOwnProperty('title')) {
120 this.errorModalDetails.title = data.title;
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700121 } else {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700122 this.errorModalDetails.title = Constants.MESSAGES.ERROR_MODAL.TITLE;
123 }
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600124
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700125 if (data && data.hasOwnProperty('description')) {
126 this.errorModalDetails.description = data.description;
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700127 } else {
128 this.errorModalDetails.description =
129 Constants.MESSAGES.ERROR_MODAL.DESCRIPTION;
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700130 }
131 this.displayErrorModal = true;
132 };
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600133
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700134 this.deactivateErrorModal = function() {
135 this.displayErrorModal = false;
136 };
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700137 }
138 ]);
Sivas SRRbb9092a2018-01-27 08:40:58 -0600139})(window.angular);