blob: 7ca13a6ef147fb8a0f48b0765b4fe85834d8bc1f [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;
37 this.getServerId = function() {
38 return this.host.replace(/^https?\:\/\//ig, '');
39 };
Iftekharul Islam1acb4122017-11-02 13:20:32 -050040
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070041 this.reloadServerId = function() {
42 this.server_id = this.getServerId();
43 };
Iftekharul Islam1acb4122017-11-02 13:20:32 -050044
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070045 this.getHost = function() {
Andrew Geisslerd27bb132018-05-24 11:07:27 -070046 if (sessionStorage.getItem(
47 Constants.API_CREDENTIALS.host_storage_key) !== null) {
48 return sessionStorage.getItem(
49 Constants.API_CREDENTIALS.host_storage_key);
50 } else {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070051 return Constants.API_CREDENTIALS.default_protocol + '://' +
Andrew Geisslerd27bb132018-05-24 11:07:27 -070052 window.location.hostname +
53 (window.location.port ? ':' + window.location.port : '');
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070054 }
55 };
Iftekharul Islam1acb4122017-11-02 13:20:32 -050056
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070057 this.setHost = function(hostWithPort) {
58 hostWithPort = hostWithPort.replace(/^https?\:\/\//ig, '');
Andrew Geisslerd27bb132018-05-24 11:07:27 -070059 var hostURL =
60 Constants.API_CREDENTIALS.default_protocol + '://' + hostWithPort;
61 sessionStorage.setItem(
62 Constants.API_CREDENTIALS.host_storage_key, hostURL);
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070063 this.host = hostURL;
64 this.reloadServerId();
65 };
Iftekharul Islam1acb4122017-11-02 13:20:32 -050066
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070067 this.getUser = function() {
68 return sessionStorage.getItem('LOGIN_ID');
69 };
Gunnar Mills1a60f6e2018-03-14 13:42:08 -050070
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070071 this.host = this.getHost();
72 this.server_id = this.getServerId();
Iftekharul Islam1acb4122017-11-02 13:20:32 -050073
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070074 this.setNetworkInfo = function(data) {
75 this.hostname = data.hostname;
Gunnar Mills659651e2018-05-30 15:21:07 -050076 this.defaultgateway = data.defaultgateway;
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070077 this.mac_address = data.mac_address;
78 };
Iftekharul Islamba556c32017-08-11 08:37:12 -050079
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070080 this.setPowerOnState = function() {
81 this.server_state = Constants.HOST_STATE_TEXT.on;
82 this.server_status = Constants.HOST_STATE.on;
83 };
Iftekharul Islam99d199f2017-03-24 15:28:25 -050084
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070085 this.setPowerOffState = function() {
86 this.server_state = Constants.HOST_STATE_TEXT.off;
87 this.server_status = Constants.HOST_STATE.off;
88 };
Iftekharul Islam99d199f2017-03-24 15:28:25 -050089
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070090 this.setErrorState = function() {
91 this.server_state = Constants.HOST_STATE_TEXT.error;
92 this.server_status = Constants.HOST_STATE.error;
93 };
Iftekharul Islam99d199f2017-03-24 15:28:25 -050094
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070095 this.setUnreachableState = function() {
96 this.server_state = Constants.HOST_STATE_TEXT.unreachable;
97 this.server_status = Constants.HOST_STATE.unreachable;
98 };
Iftekharul Islam34714092017-09-06 10:45:27 -050099
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700100 this.updateServerHealth = function(logs) {
Gunnar Mills5760e532018-11-12 09:59:50 -0600101 // If any unresolved severity high logs are present, set server health
102 // to critical. Else if any unresolved severity medium logs are present
103 // set server health to warning.
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700104 this.server_health = Constants.SERVER_HEALTH.good;
Gunnar Mills500ad782018-09-06 15:18:57 -0500105 for (var log of logs) {
Gunnar Mills5760e532018-11-12 09:59:50 -0600106 if (log.priority == 'High' && !log.Resolved) {
Gunnar Mills500ad782018-09-06 15:18:57 -0500107 this.server_health = Constants.SERVER_HEALTH.critical;
108 return;
Gunnar Mills5760e532018-11-12 09:59:50 -0600109 } else if (log.priority == 'Medium' && !log.Resolved) {
Gunnar Mills500ad782018-09-06 15:18:57 -0500110 this.server_health = Constants.SERVER_HEALTH.warning;
111 }
112 }
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700113 };
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600114
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700115 this.activateErrorModal = function(data) {
116 if (data && data.hasOwnProperty('title')) {
117 this.errorModalDetails.title = data.title;
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700118 } else {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700119 this.errorModalDetails.title = Constants.MESSAGES.ERROR_MODAL.TITLE;
120 }
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600121
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700122 if (data && data.hasOwnProperty('description')) {
123 this.errorModalDetails.description = data.description;
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700124 } else {
125 this.errorModalDetails.description =
126 Constants.MESSAGES.ERROR_MODAL.DESCRIPTION;
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700127 }
128 this.displayErrorModal = true;
129 };
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600130
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700131 this.deactivateErrorModal = function() {
132 this.displayErrorModal = false;
133 };
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700134 }
135 ]);
Sivas SRRbb9092a2018-01-27 08:40:58 -0600136})(window.angular);