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