blob: d84821a2ce2d1dd2733db0d864a85484c02b41ea [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
8 * @version 0.0.1
9 */
10
11window.angular && (function (angular) {
12 'use strict';
13
14 angular
15 .module('app.common.services')
16 .service('dataService', ['Constants', function (Constants) {
Michael Davis7f89fad2017-07-31 18:36:45 -050017 this.app_version = "V.0.0.1";
Iftekharul Islam34714092017-09-06 10:45:27 -050018 this.server_health = Constants.SERVER_HEALTH.unknown;
Iftekharul Islam99d199f2017-03-24 15:28:25 -050019 this.server_state = 'Unreachable';
20 this.server_status = -2;
21 this.chassis_state = 'On';
Iftekharul Islamcd789502017-04-19 14:37:55 -050022 this.LED_state = Constants.LED_STATE_TEXT.off;
Iftekharul Islam99d199f2017-03-24 15:28:25 -050023 this.last_updated = new Date();
24
25 this.loading = false;
26 this.server_unreachable = false;
27 this.loading_message = "";
28 this.showNavigation = false;
29 this.bodyStyle = {};
30 this.path = '';
Iftekharul Islamd2269e22017-05-02 09:32:45 -050031 this.sensorData = [];
Iftekharul Islam99d199f2017-03-24 15:28:25 -050032
Iftekharul Islamba556c32017-08-11 08:37:12 -050033 this.hostname = "";
34 this.mac_address = "";
Iftekharul Islam34714092017-09-06 10:45:27 -050035 this.remote_window_active = false;
Iftekharul Islama1d238f2018-02-26 12:29:45 -060036
37 this.displayErrorModal = false;
38 this.errorModalDetails = {};
39
Gunnar Mills32581cf2018-03-16 15:52:54 -050040 this.ignoreHttpError = false;
Iftekharul Islam1acb4122017-11-02 13:20:32 -050041 this.getServerId = function(){
Sivas SRRbb9092a2018-01-27 08:40:58 -060042 return this.host.replace(/^https?\:\/\//ig,"");
Iftekharul Islam1acb4122017-11-02 13:20:32 -050043 }
44
45 this.reloadServerId = function(){
46 this.server_id = this.getServerId();
47 }
48
49 this.getHost = function(){
50 if(sessionStorage.getItem(Constants.API_CREDENTIALS.host_storage_key) !== null){
51 return sessionStorage.getItem(Constants.API_CREDENTIALS.host_storage_key);
52 }else{
53 return Constants.API_CREDENTIALS.default_protocol + "://" +
Gunnar Mills0c61caa2018-03-13 15:49:11 -050054 window.location.hostname +
55 (window.location.port ? ":" + window.location.port : "");
Iftekharul Islam1acb4122017-11-02 13:20:32 -050056 }
57 }
58
59 this.setHost = function(hostWithPort){
60 hostWithPort = hostWithPort.replace(/^https?\:\/\//ig, '');
61 var hostURL = Constants.API_CREDENTIALS.default_protocol + "://" + hostWithPort;
62 sessionStorage.setItem(Constants.API_CREDENTIALS.host_storage_key, hostURL);
63 this.host = hostURL;
64 this.reloadServerId();
65 }
66
Gunnar Mills1a60f6e2018-03-14 13:42:08 -050067 this.getUser = function(){
68 return sessionStorage.getItem('LOGIN_ID');
69 }
70
Iftekharul Islam1acb4122017-11-02 13:20:32 -050071 this.host = this.getHost();
72 this.server_id = this.getServerId();
73
Iftekharul Islamba556c32017-08-11 08:37:12 -050074 this.setNetworkInfo = function(data){
75 this.hostname = data.hostname;
76 this.mac_address = data.mac_address;
77 }
78
Iftekharul Islam99d199f2017-03-24 15:28:25 -050079 this.setPowerOnState = function(){
80 this.server_state = Constants.HOST_STATE_TEXT.on;
81 this.server_status = Constants.HOST_STATE.on;
Iftekharul Islamba556c32017-08-11 08:37:12 -050082 }
Iftekharul Islam99d199f2017-03-24 15:28:25 -050083
84 this.setPowerOffState = function(){
85 this.server_state = Constants.HOST_STATE_TEXT.off;
86 this.server_status = Constants.HOST_STATE.off;
Iftekharul Islamba556c32017-08-11 08:37:12 -050087 }
Iftekharul Islam99d199f2017-03-24 15:28:25 -050088
89 this.setBootingState = function(){
90 this.server_state = Constants.HOST_STATE_TEXT.booting;
91 this.server_status = Constants.HOST_STATE.booting;
Iftekharul Islamba556c32017-08-11 08:37:12 -050092 }
Iftekharul Islam99d199f2017-03-24 15:28:25 -050093
94 this.setUnreachableState = function(){
95 this.server_state = Constants.HOST_STATE_TEXT.unreachable;
96 this.server_status = Constants.HOST_STATE.unreachable;
97 }
Iftekharul Islam34714092017-09-06 10:45:27 -050098
99 this.setRemoteWindowActive = function(){
100 this.remote_window_active = true;
101 }
102
103 this.setRemoteWindowInactive = function(){
104 this.remote_window_active = false;
105 }
106
107 this.updateServerHealth = function(logs){
108 var criticals = logs.filter(function(item){
109 return item.health_flags.critical == true;
110 });
111
112 if(criticals.length){
113 this.server_health = Constants.SERVER_HEALTH.critical;
114 return;
115 }
116
117 var warnings = logs.filter(function(item){
118 return item.health_flags.warning == true;
119 });
120
121 if(warnings.length){
122 this.server_health = Constants.SERVER_HEALTH.warning;
123 return;
124 }
125
126 this.server_health = Constants.SERVER_HEALTH.good;
127 }
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600128
129 this.activateErrorModal = function(data){
130 if(data && data.hasOwnProperty('title')){
131 this.errorModalDetails.title = data.title;
132 }else{
133 this.errorModalDetails.title = Constants.MESSAGES.ERROR_MODAL.TITLE;
134 }
135
136 if(data && data.hasOwnProperty('description')){
137 this.errorModalDetails.description = data.description;
138 }else{
139 this.errorModalDetails.description = Constants.MESSAGES.ERROR_MODAL.DESCRIPTION;
140 }
141 this.displayErrorModal = true;
142 }
143
144 this.deactivateErrorModal = function(){
145 this.displayErrorModal = false;
146 }
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500147 }]);
148
Sivas SRRbb9092a2018-01-27 08:40:58 -0600149})(window.angular);