blob: c2131fe2ead98644bf5dcec8830353cb56e099e2 [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) {
Michael Davis7f89fad2017-07-31 18:36:45 -050016 this.app_version = "V.0.0.1";
Iftekharul Islam34714092017-09-06 10:45:27 -050017 this.server_health = Constants.SERVER_HEALTH.unknown;
Iftekharul Islam99d199f2017-03-24 15:28:25 -050018 this.server_state = 'Unreachable';
19 this.server_status = -2;
20 this.chassis_state = 'On';
Iftekharul Islamcd789502017-04-19 14:37:55 -050021 this.LED_state = Constants.LED_STATE_TEXT.off;
Iftekharul Islam99d199f2017-03-24 15:28:25 -050022 this.last_updated = new Date();
23
24 this.loading = false;
25 this.server_unreachable = false;
26 this.loading_message = "";
27 this.showNavigation = false;
28 this.bodyStyle = {};
29 this.path = '';
Iftekharul Islamd2269e22017-05-02 09:32:45 -050030 this.sensorData = [];
Iftekharul Islam99d199f2017-03-24 15:28:25 -050031
Iftekharul Islamba556c32017-08-11 08:37:12 -050032 this.hostname = "";
33 this.mac_address = "";
Iftekharul Islam34714092017-09-06 10:45:27 -050034 this.remote_window_active = false;
Iftekharul Islama1d238f2018-02-26 12:29:45 -060035
36 this.displayErrorModal = false;
37 this.errorModalDetails = {};
38
Gunnar Mills32581cf2018-03-16 15:52:54 -050039 this.ignoreHttpError = false;
Iftekharul Islam1acb4122017-11-02 13:20:32 -050040 this.getServerId = function(){
Sivas SRRbb9092a2018-01-27 08:40:58 -060041 return this.host.replace(/^https?\:\/\//ig,"");
Iftekharul Islam1acb4122017-11-02 13:20:32 -050042 }
43
44 this.reloadServerId = function(){
45 this.server_id = this.getServerId();
46 }
47
48 this.getHost = function(){
49 if(sessionStorage.getItem(Constants.API_CREDENTIALS.host_storage_key) !== null){
50 return sessionStorage.getItem(Constants.API_CREDENTIALS.host_storage_key);
51 }else{
52 return Constants.API_CREDENTIALS.default_protocol + "://" +
Gunnar Mills0c61caa2018-03-13 15:49:11 -050053 window.location.hostname +
54 (window.location.port ? ":" + window.location.port : "");
Iftekharul Islam1acb4122017-11-02 13:20:32 -050055 }
56 }
57
58 this.setHost = function(hostWithPort){
59 hostWithPort = hostWithPort.replace(/^https?\:\/\//ig, '');
60 var hostURL = Constants.API_CREDENTIALS.default_protocol + "://" + hostWithPort;
61 sessionStorage.setItem(Constants.API_CREDENTIALS.host_storage_key, hostURL);
62 this.host = hostURL;
63 this.reloadServerId();
64 }
65
Gunnar Mills1a60f6e2018-03-14 13:42:08 -050066 this.getUser = function(){
67 return sessionStorage.getItem('LOGIN_ID');
68 }
69
Iftekharul Islam1acb4122017-11-02 13:20:32 -050070 this.host = this.getHost();
71 this.server_id = this.getServerId();
72
Iftekharul Islamba556c32017-08-11 08:37:12 -050073 this.setNetworkInfo = function(data){
74 this.hostname = data.hostname;
75 this.mac_address = data.mac_address;
76 }
77
Iftekharul Islam99d199f2017-03-24 15:28:25 -050078 this.setPowerOnState = function(){
79 this.server_state = Constants.HOST_STATE_TEXT.on;
80 this.server_status = Constants.HOST_STATE.on;
Iftekharul Islamba556c32017-08-11 08:37:12 -050081 }
Iftekharul Islam99d199f2017-03-24 15:28:25 -050082
83 this.setPowerOffState = function(){
84 this.server_state = Constants.HOST_STATE_TEXT.off;
85 this.server_status = Constants.HOST_STATE.off;
Iftekharul Islamba556c32017-08-11 08:37:12 -050086 }
Iftekharul Islam99d199f2017-03-24 15:28:25 -050087
CamVan Nguyend80c2802018-04-17 19:25:16 -050088 this.setErrorState = function(){
89 this.server_state = Constants.HOST_STATE_TEXT.error;
90 this.server_status = Constants.HOST_STATE.error;
Iftekharul Islamba556c32017-08-11 08:37:12 -050091 }
Iftekharul Islam99d199f2017-03-24 15:28:25 -050092
93 this.setUnreachableState = function(){
94 this.server_state = Constants.HOST_STATE_TEXT.unreachable;
95 this.server_status = Constants.HOST_STATE.unreachable;
96 }
Iftekharul Islam34714092017-09-06 10:45:27 -050097
98 this.setRemoteWindowActive = function(){
99 this.remote_window_active = true;
100 }
101
102 this.setRemoteWindowInactive = function(){
103 this.remote_window_active = false;
104 }
105
106 this.updateServerHealth = function(logs){
107 var criticals = logs.filter(function(item){
108 return item.health_flags.critical == true;
109 });
110
111 if(criticals.length){
112 this.server_health = Constants.SERVER_HEALTH.critical;
113 return;
114 }
115
116 var warnings = logs.filter(function(item){
117 return item.health_flags.warning == true;
118 });
119
120 if(warnings.length){
121 this.server_health = Constants.SERVER_HEALTH.warning;
122 return;
123 }
124
125 this.server_health = Constants.SERVER_HEALTH.good;
126 }
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600127
128 this.activateErrorModal = function(data){
129 if(data && data.hasOwnProperty('title')){
130 this.errorModalDetails.title = data.title;
131 }else{
132 this.errorModalDetails.title = Constants.MESSAGES.ERROR_MODAL.TITLE;
133 }
134
135 if(data && data.hasOwnProperty('description')){
136 this.errorModalDetails.description = data.description;
137 }else{
138 this.errorModalDetails.description = Constants.MESSAGES.ERROR_MODAL.DESCRIPTION;
139 }
140 this.displayErrorModal = true;
141 }
142
143 this.deactivateErrorModal = function(){
144 this.displayErrorModal = false;
145 }
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500146 }]);
147
Sivas SRRbb9092a2018-01-27 08:40:58 -0600148})(window.angular);