blob: ffe15a22f260bf366e92b1e1a0406e455ce7f76f [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;
25 this.loading_message = '';
26 this.showNavigation = false;
27 this.bodyStyle = {};
28 this.path = '';
29 this.sensorData = [];
Iftekharul Islam99d199f2017-03-24 15:28:25 -050030
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070031 this.hostname = '';
32 this.mac_address = '';
33 this.remote_window_active = false;
Iftekharul Islama1d238f2018-02-26 12:29:45 -060034
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070035 this.displayErrorModal = false;
36 this.errorModalDetails = {};
Iftekharul Islama1d238f2018-02-26 12:29:45 -060037
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070038 this.ignoreHttpError = false;
39 this.getServerId = function() {
40 return this.host.replace(/^https?\:\/\//ig, '');
41 };
Iftekharul Islam1acb4122017-11-02 13:20:32 -050042
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070043 this.reloadServerId = function() {
44 this.server_id = this.getServerId();
45 };
Iftekharul Islam1acb4122017-11-02 13:20:32 -050046
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070047 this.getHost = function() {
Andrew Geisslerd27bb132018-05-24 11:07:27 -070048 if (sessionStorage.getItem(
49 Constants.API_CREDENTIALS.host_storage_key) !== null) {
50 return sessionStorage.getItem(
51 Constants.API_CREDENTIALS.host_storage_key);
52 } else {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070053 return Constants.API_CREDENTIALS.default_protocol + '://' +
Andrew Geisslerd27bb132018-05-24 11:07:27 -070054 window.location.hostname +
55 (window.location.port ? ':' + window.location.port : '');
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070056 }
57 };
Iftekharul Islam1acb4122017-11-02 13:20:32 -050058
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070059 this.setHost = function(hostWithPort) {
60 hostWithPort = hostWithPort.replace(/^https?\:\/\//ig, '');
Andrew Geisslerd27bb132018-05-24 11:07:27 -070061 var hostURL =
62 Constants.API_CREDENTIALS.default_protocol + '://' + hostWithPort;
63 sessionStorage.setItem(
64 Constants.API_CREDENTIALS.host_storage_key, hostURL);
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070065 this.host = hostURL;
66 this.reloadServerId();
67 };
Iftekharul Islam1acb4122017-11-02 13:20:32 -050068
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070069 this.getUser = function() {
70 return sessionStorage.getItem('LOGIN_ID');
71 };
Gunnar Mills1a60f6e2018-03-14 13:42:08 -050072
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070073 this.host = this.getHost();
74 this.server_id = this.getServerId();
Iftekharul Islam1acb4122017-11-02 13:20:32 -050075
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070076 this.setNetworkInfo = function(data) {
77 this.hostname = data.hostname;
78 this.mac_address = data.mac_address;
79 };
Iftekharul Islamba556c32017-08-11 08:37:12 -050080
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070081 this.setPowerOnState = function() {
82 this.server_state = Constants.HOST_STATE_TEXT.on;
83 this.server_status = Constants.HOST_STATE.on;
84 };
Iftekharul Islam99d199f2017-03-24 15:28:25 -050085
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070086 this.setPowerOffState = function() {
87 this.server_state = Constants.HOST_STATE_TEXT.off;
88 this.server_status = Constants.HOST_STATE.off;
89 };
Iftekharul Islam99d199f2017-03-24 15:28:25 -050090
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070091 this.setErrorState = function() {
92 this.server_state = Constants.HOST_STATE_TEXT.error;
93 this.server_status = Constants.HOST_STATE.error;
94 };
Iftekharul Islam99d199f2017-03-24 15:28:25 -050095
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070096 this.setUnreachableState = function() {
97 this.server_state = Constants.HOST_STATE_TEXT.unreachable;
98 this.server_status = Constants.HOST_STATE.unreachable;
99 };
Iftekharul Islam34714092017-09-06 10:45:27 -0500100
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700101 this.setRemoteWindowActive = function() {
102 this.remote_window_active = true;
103 };
Iftekharul Islam34714092017-09-06 10:45:27 -0500104
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700105 this.setRemoteWindowInactive = function() {
106 this.remote_window_active = false;
107 };
Iftekharul Islam34714092017-09-06 10:45:27 -0500108
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700109 this.updateServerHealth = function(logs) {
110 var criticals = logs.filter(function(item) {
111 return item.health_flags.critical == true;
112 });
Iftekharul Islam34714092017-09-06 10:45:27 -0500113
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700114 if (criticals.length) {
115 this.server_health = Constants.SERVER_HEALTH.critical;
116 return;
117 }
Iftekharul Islam34714092017-09-06 10:45:27 -0500118
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700119 var warnings = logs.filter(function(item) {
120 return item.health_flags.warning == true;
121 });
Iftekharul Islam34714092017-09-06 10:45:27 -0500122
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700123 if (warnings.length) {
124 this.server_health = Constants.SERVER_HEALTH.warning;
125 return;
126 }
Iftekharul Islam34714092017-09-06 10:45:27 -0500127
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700128 this.server_health = Constants.SERVER_HEALTH.good;
129 };
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600130
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700131 this.activateErrorModal = function(data) {
132 if (data && data.hasOwnProperty('title')) {
133 this.errorModalDetails.title = data.title;
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700134 } else {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700135 this.errorModalDetails.title = Constants.MESSAGES.ERROR_MODAL.TITLE;
136 }
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600137
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700138 if (data && data.hasOwnProperty('description')) {
139 this.errorModalDetails.description = data.description;
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700140 } else {
141 this.errorModalDetails.description =
142 Constants.MESSAGES.ERROR_MODAL.DESCRIPTION;
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700143 }
144 this.displayErrorModal = true;
145 };
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600146
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700147 this.deactivateErrorModal = function() {
148 this.displayErrorModal = false;
149 };
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700150 }
151 ]);
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500152
Sivas SRRbb9092a2018-01-27 08:40:58 -0600153})(window.angular);