blob: d39e757f9945ae21aacfd1b1089960a86dd4974b [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
10window.angular && (function (angular) {
11 'use strict';
12
13 angular
14 .module('app.common.services')
15 .service('dataService', ['Constants', function (Constants) {
Iftekharul Islam34714092017-09-06 10:45:27 -050016 this.server_health = Constants.SERVER_HEALTH.unknown;
Iftekharul Islam99d199f2017-03-24 15:28:25 -050017 this.server_state = 'Unreachable';
18 this.server_status = -2;
19 this.chassis_state = 'On';
Iftekharul Islamcd789502017-04-19 14:37:55 -050020 this.LED_state = Constants.LED_STATE_TEXT.off;
Iftekharul Islam99d199f2017-03-24 15:28:25 -050021 this.last_updated = new Date();
22
23 this.loading = false;
24 this.server_unreachable = false;
25 this.loading_message = "";
26 this.showNavigation = false;
27 this.bodyStyle = {};
28 this.path = '';
Iftekharul Islamd2269e22017-05-02 09:32:45 -050029 this.sensorData = [];
Iftekharul Islam99d199f2017-03-24 15:28:25 -050030
Iftekharul Islamba556c32017-08-11 08:37:12 -050031 this.hostname = "";
32 this.mac_address = "";
Iftekharul Islam34714092017-09-06 10:45:27 -050033 this.remote_window_active = false;
Iftekharul Islama1d238f2018-02-26 12:29:45 -060034
35 this.displayErrorModal = false;
36 this.errorModalDetails = {};
37
Gunnar Mills32581cf2018-03-16 15:52:54 -050038 this.ignoreHttpError = false;
Iftekharul Islam1acb4122017-11-02 13:20:32 -050039 this.getServerId = function(){
Sivas SRRbb9092a2018-01-27 08:40:58 -060040 return this.host.replace(/^https?\:\/\//ig,"");
Iftekharul Islam1acb4122017-11-02 13:20:32 -050041 }
42
43 this.reloadServerId = function(){
44 this.server_id = this.getServerId();
45 }
46
47 this.getHost = function(){
48 if(sessionStorage.getItem(Constants.API_CREDENTIALS.host_storage_key) !== null){
49 return sessionStorage.getItem(Constants.API_CREDENTIALS.host_storage_key);
50 }else{
51 return Constants.API_CREDENTIALS.default_protocol + "://" +
Gunnar Mills0c61caa2018-03-13 15:49:11 -050052 window.location.hostname +
53 (window.location.port ? ":" + window.location.port : "");
Iftekharul Islam1acb4122017-11-02 13:20:32 -050054 }
55 }
56
57 this.setHost = function(hostWithPort){
58 hostWithPort = hostWithPort.replace(/^https?\:\/\//ig, '');
59 var hostURL = Constants.API_CREDENTIALS.default_protocol + "://" + hostWithPort;
60 sessionStorage.setItem(Constants.API_CREDENTIALS.host_storage_key, hostURL);
61 this.host = hostURL;
62 this.reloadServerId();
63 }
64
Gunnar Mills1a60f6e2018-03-14 13:42:08 -050065 this.getUser = function(){
66 return sessionStorage.getItem('LOGIN_ID');
67 }
68
Iftekharul Islam1acb4122017-11-02 13:20:32 -050069 this.host = this.getHost();
70 this.server_id = this.getServerId();
71
Iftekharul Islamba556c32017-08-11 08:37:12 -050072 this.setNetworkInfo = function(data){
73 this.hostname = data.hostname;
74 this.mac_address = data.mac_address;
75 }
76
Iftekharul Islam99d199f2017-03-24 15:28:25 -050077 this.setPowerOnState = function(){
78 this.server_state = Constants.HOST_STATE_TEXT.on;
79 this.server_status = Constants.HOST_STATE.on;
Iftekharul Islamba556c32017-08-11 08:37:12 -050080 }
Iftekharul Islam99d199f2017-03-24 15:28:25 -050081
82 this.setPowerOffState = function(){
83 this.server_state = Constants.HOST_STATE_TEXT.off;
84 this.server_status = Constants.HOST_STATE.off;
Iftekharul Islamba556c32017-08-11 08:37:12 -050085 }
Iftekharul Islam99d199f2017-03-24 15:28:25 -050086
CamVan Nguyend80c2802018-04-17 19:25:16 -050087 this.setErrorState = function(){
88 this.server_state = Constants.HOST_STATE_TEXT.error;
89 this.server_status = Constants.HOST_STATE.error;
Iftekharul Islamba556c32017-08-11 08:37:12 -050090 }
Iftekharul Islam99d199f2017-03-24 15:28:25 -050091
92 this.setUnreachableState = function(){
93 this.server_state = Constants.HOST_STATE_TEXT.unreachable;
94 this.server_status = Constants.HOST_STATE.unreachable;
95 }
Iftekharul Islam34714092017-09-06 10:45:27 -050096
97 this.setRemoteWindowActive = function(){
98 this.remote_window_active = true;
99 }
100
101 this.setRemoteWindowInactive = function(){
102 this.remote_window_active = false;
103 }
104
105 this.updateServerHealth = function(logs){
106 var criticals = logs.filter(function(item){
107 return item.health_flags.critical == true;
108 });
109
110 if(criticals.length){
111 this.server_health = Constants.SERVER_HEALTH.critical;
112 return;
113 }
114
115 var warnings = logs.filter(function(item){
116 return item.health_flags.warning == true;
117 });
118
119 if(warnings.length){
120 this.server_health = Constants.SERVER_HEALTH.warning;
121 return;
122 }
123
124 this.server_health = Constants.SERVER_HEALTH.good;
125 }
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600126
127 this.activateErrorModal = function(data){
128 if(data && data.hasOwnProperty('title')){
129 this.errorModalDetails.title = data.title;
130 }else{
131 this.errorModalDetails.title = Constants.MESSAGES.ERROR_MODAL.TITLE;
132 }
133
134 if(data && data.hasOwnProperty('description')){
135 this.errorModalDetails.description = data.description;
136 }else{
137 this.errorModalDetails.description = Constants.MESSAGES.ERROR_MODAL.DESCRIPTION;
138 }
139 this.displayErrorModal = true;
140 }
141
142 this.deactivateErrorModal = function(){
143 this.displayErrorModal = false;
144 }
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500145 }]);
146
Sivas SRRbb9092a2018-01-27 08:40:58 -0600147})(window.angular);