Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 1 | /** |
| 2 | * data service |
| 3 | * |
| 4 | * @module app/common/services/dataService |
| 5 | * @exports dataService |
| 6 | * @name dataService |
| 7 | |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 8 | */ |
| 9 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 10 | window.angular && (function(angular) { |
| 11 | 'use strict'; |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 12 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 13 | angular.module('app.common.services').service('dataService', [ |
| 14 | 'Constants', |
| 15 | function(Constants) { |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 16 | 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 Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 22 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 23 | 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 Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 30 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 31 | this.hostname = ''; |
| 32 | this.mac_address = ''; |
Gunnar Mills | 659651e | 2018-05-30 15:21:07 -0500 | [diff] [blame^] | 33 | this.defaultgateway = ''; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 34 | this.remote_window_active = false; |
Iftekharul Islam | a1d238f | 2018-02-26 12:29:45 -0600 | [diff] [blame] | 35 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 36 | this.displayErrorModal = false; |
| 37 | this.errorModalDetails = {}; |
Iftekharul Islam | a1d238f | 2018-02-26 12:29:45 -0600 | [diff] [blame] | 38 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 39 | this.ignoreHttpError = false; |
| 40 | this.getServerId = function() { |
| 41 | return this.host.replace(/^https?\:\/\//ig, ''); |
| 42 | }; |
Iftekharul Islam | 1acb412 | 2017-11-02 13:20:32 -0500 | [diff] [blame] | 43 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 44 | this.reloadServerId = function() { |
| 45 | this.server_id = this.getServerId(); |
| 46 | }; |
Iftekharul Islam | 1acb412 | 2017-11-02 13:20:32 -0500 | [diff] [blame] | 47 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 48 | this.getHost = function() { |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 49 | if (sessionStorage.getItem( |
| 50 | Constants.API_CREDENTIALS.host_storage_key) !== null) { |
| 51 | return sessionStorage.getItem( |
| 52 | Constants.API_CREDENTIALS.host_storage_key); |
| 53 | } else { |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 54 | return Constants.API_CREDENTIALS.default_protocol + '://' + |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 55 | window.location.hostname + |
| 56 | (window.location.port ? ':' + window.location.port : ''); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 57 | } |
| 58 | }; |
Iftekharul Islam | 1acb412 | 2017-11-02 13:20:32 -0500 | [diff] [blame] | 59 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 60 | this.setHost = function(hostWithPort) { |
| 61 | hostWithPort = hostWithPort.replace(/^https?\:\/\//ig, ''); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 62 | var hostURL = |
| 63 | Constants.API_CREDENTIALS.default_protocol + '://' + hostWithPort; |
| 64 | sessionStorage.setItem( |
| 65 | Constants.API_CREDENTIALS.host_storage_key, hostURL); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 66 | this.host = hostURL; |
| 67 | this.reloadServerId(); |
| 68 | }; |
Iftekharul Islam | 1acb412 | 2017-11-02 13:20:32 -0500 | [diff] [blame] | 69 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 70 | this.getUser = function() { |
| 71 | return sessionStorage.getItem('LOGIN_ID'); |
| 72 | }; |
Gunnar Mills | 1a60f6e | 2018-03-14 13:42:08 -0500 | [diff] [blame] | 73 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 74 | this.host = this.getHost(); |
| 75 | this.server_id = this.getServerId(); |
Iftekharul Islam | 1acb412 | 2017-11-02 13:20:32 -0500 | [diff] [blame] | 76 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 77 | this.setNetworkInfo = function(data) { |
| 78 | this.hostname = data.hostname; |
Gunnar Mills | 659651e | 2018-05-30 15:21:07 -0500 | [diff] [blame^] | 79 | this.defaultgateway = data.defaultgateway; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 80 | this.mac_address = data.mac_address; |
| 81 | }; |
Iftekharul Islam | ba556c3 | 2017-08-11 08:37:12 -0500 | [diff] [blame] | 82 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 83 | this.setPowerOnState = function() { |
| 84 | this.server_state = Constants.HOST_STATE_TEXT.on; |
| 85 | this.server_status = Constants.HOST_STATE.on; |
| 86 | }; |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 87 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 88 | this.setPowerOffState = function() { |
| 89 | this.server_state = Constants.HOST_STATE_TEXT.off; |
| 90 | this.server_status = Constants.HOST_STATE.off; |
| 91 | }; |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 92 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 93 | this.setErrorState = function() { |
| 94 | this.server_state = Constants.HOST_STATE_TEXT.error; |
| 95 | this.server_status = Constants.HOST_STATE.error; |
| 96 | }; |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 97 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 98 | this.setUnreachableState = function() { |
| 99 | this.server_state = Constants.HOST_STATE_TEXT.unreachable; |
| 100 | this.server_status = Constants.HOST_STATE.unreachable; |
| 101 | }; |
Iftekharul Islam | 3471409 | 2017-09-06 10:45:27 -0500 | [diff] [blame] | 102 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 103 | this.setRemoteWindowActive = function() { |
| 104 | this.remote_window_active = true; |
| 105 | }; |
Iftekharul Islam | 3471409 | 2017-09-06 10:45:27 -0500 | [diff] [blame] | 106 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 107 | this.setRemoteWindowInactive = function() { |
| 108 | this.remote_window_active = false; |
| 109 | }; |
Iftekharul Islam | 3471409 | 2017-09-06 10:45:27 -0500 | [diff] [blame] | 110 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 111 | this.updateServerHealth = function(logs) { |
| 112 | var criticals = logs.filter(function(item) { |
| 113 | return item.health_flags.critical == true; |
| 114 | }); |
Iftekharul Islam | 3471409 | 2017-09-06 10:45:27 -0500 | [diff] [blame] | 115 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 116 | if (criticals.length) { |
| 117 | this.server_health = Constants.SERVER_HEALTH.critical; |
| 118 | return; |
| 119 | } |
Iftekharul Islam | 3471409 | 2017-09-06 10:45:27 -0500 | [diff] [blame] | 120 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 121 | var warnings = logs.filter(function(item) { |
| 122 | return item.health_flags.warning == true; |
| 123 | }); |
Iftekharul Islam | 3471409 | 2017-09-06 10:45:27 -0500 | [diff] [blame] | 124 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 125 | if (warnings.length) { |
| 126 | this.server_health = Constants.SERVER_HEALTH.warning; |
| 127 | return; |
| 128 | } |
Iftekharul Islam | 3471409 | 2017-09-06 10:45:27 -0500 | [diff] [blame] | 129 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 130 | this.server_health = Constants.SERVER_HEALTH.good; |
| 131 | }; |
Iftekharul Islam | a1d238f | 2018-02-26 12:29:45 -0600 | [diff] [blame] | 132 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 133 | this.activateErrorModal = function(data) { |
| 134 | if (data && data.hasOwnProperty('title')) { |
| 135 | this.errorModalDetails.title = data.title; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 136 | } else { |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 137 | this.errorModalDetails.title = Constants.MESSAGES.ERROR_MODAL.TITLE; |
| 138 | } |
Iftekharul Islam | a1d238f | 2018-02-26 12:29:45 -0600 | [diff] [blame] | 139 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 140 | if (data && data.hasOwnProperty('description')) { |
| 141 | this.errorModalDetails.description = data.description; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 142 | } else { |
| 143 | this.errorModalDetails.description = |
| 144 | Constants.MESSAGES.ERROR_MODAL.DESCRIPTION; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 145 | } |
| 146 | this.displayErrorModal = true; |
| 147 | }; |
Iftekharul Islam | a1d238f | 2018-02-26 12:29:45 -0600 | [diff] [blame] | 148 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 149 | this.deactivateErrorModal = function() { |
| 150 | this.displayErrorModal = false; |
| 151 | }; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 152 | } |
| 153 | ]); |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 154 | |
Sivas SRR | bb9092a | 2018-01-27 08:40:58 -0600 | [diff] [blame] | 155 | })(window.angular); |