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'; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 18 | this.LED_state = Constants.LED_STATE_TEXT.off; |
| 19 | this.last_updated = new Date(); |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 20 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 21 | this.loading = false; |
| 22 | this.server_unreachable = false; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 23 | this.showNavigation = false; |
| 24 | this.bodyStyle = {}; |
| 25 | this.path = ''; |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 26 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 27 | this.hostname = ''; |
| 28 | this.mac_address = ''; |
Gunnar Mills | 659651e | 2018-05-30 15:21:07 -0500 | [diff] [blame] | 29 | this.defaultgateway = ''; |
Iftekharul Islam | a1d238f | 2018-02-26 12:29:45 -0600 | [diff] [blame] | 30 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 31 | this.ignoreHttpError = false; |
AppaRao Puli | b1289ec | 2018-11-14 20:33:30 +0530 | [diff] [blame] | 32 | this.systemName = ''; |
AppaRao Puli | f04960f | 2018-12-06 21:51:27 +0530 | [diff] [blame] | 33 | |
| 34 | this.configJson = require('../../../config.json'); |
| 35 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 36 | this.getServerId = function() { |
| 37 | return this.host.replace(/^https?\:\/\//ig, ''); |
| 38 | }; |
Iftekharul Islam | 1acb412 | 2017-11-02 13:20:32 -0500 | [diff] [blame] | 39 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 40 | this.reloadServerId = function() { |
| 41 | this.server_id = this.getServerId(); |
| 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.getHost = function() { |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 45 | if (sessionStorage.getItem( |
| 46 | Constants.API_CREDENTIALS.host_storage_key) !== null) { |
| 47 | return sessionStorage.getItem( |
| 48 | Constants.API_CREDENTIALS.host_storage_key); |
| 49 | } else { |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 50 | return Constants.API_CREDENTIALS.default_protocol + '://' + |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 51 | window.location.hostname + |
| 52 | (window.location.port ? ':' + window.location.port : ''); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 53 | } |
| 54 | }; |
Iftekharul Islam | 1acb412 | 2017-11-02 13:20:32 -0500 | [diff] [blame] | 55 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 56 | this.setHost = function(hostWithPort) { |
| 57 | hostWithPort = hostWithPort.replace(/^https?\:\/\//ig, ''); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 58 | var hostURL = |
| 59 | Constants.API_CREDENTIALS.default_protocol + '://' + hostWithPort; |
| 60 | sessionStorage.setItem( |
| 61 | Constants.API_CREDENTIALS.host_storage_key, hostURL); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 62 | this.host = hostURL; |
| 63 | this.reloadServerId(); |
| 64 | }; |
Iftekharul Islam | 1acb412 | 2017-11-02 13:20:32 -0500 | [diff] [blame] | 65 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 66 | this.getUser = function() { |
| 67 | return sessionStorage.getItem('LOGIN_ID'); |
| 68 | }; |
Gunnar Mills | 1a60f6e | 2018-03-14 13:42:08 -0500 | [diff] [blame] | 69 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 70 | this.host = this.getHost(); |
| 71 | this.server_id = this.getServerId(); |
Iftekharul Islam | 1acb412 | 2017-11-02 13:20:32 -0500 | [diff] [blame] | 72 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 73 | this.setNetworkInfo = function(data) { |
Igor Kononenko | d10511f | 2020-09-12 16:36:35 +0300 | [diff] [blame] | 74 | var formatted = data.formatted_data || {}; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 75 | this.hostname = data.hostname; |
Gunnar Mills | 659651e | 2018-05-30 15:21:07 -0500 | [diff] [blame] | 76 | this.defaultgateway = data.defaultgateway; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 77 | this.mac_address = data.mac_address; |
Igor Kononenko | d10511f | 2020-09-12 16:36:35 +0300 | [diff] [blame] | 78 | this.network_interfaces = formatted.interfaces || []; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 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; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 83 | }; |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 84 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 85 | this.setPowerOffState = function() { |
| 86 | this.server_state = Constants.HOST_STATE_TEXT.off; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 87 | }; |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 88 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 89 | this.setErrorState = function() { |
| 90 | this.server_state = Constants.HOST_STATE_TEXT.error; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 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.setUnreachableState = function() { |
| 94 | this.server_state = Constants.HOST_STATE_TEXT.unreachable; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 95 | }; |
Iftekharul Islam | 3471409 | 2017-09-06 10:45:27 -0500 | [diff] [blame] | 96 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 97 | this.updateServerHealth = function(logs) { |
Gunnar Mills | 5760e53 | 2018-11-12 09:59:50 -0600 | [diff] [blame] | 98 | // If any unresolved severity high logs are present, set server health |
| 99 | // to critical. Else if any unresolved severity medium logs are present |
| 100 | // set server health to warning. |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 101 | this.server_health = Constants.SERVER_HEALTH.good; |
Gunnar Mills | 500ad78 | 2018-09-06 15:18:57 -0500 | [diff] [blame] | 102 | for (var log of logs) { |
Gunnar Mills | 5760e53 | 2018-11-12 09:59:50 -0600 | [diff] [blame] | 103 | if (log.priority == 'High' && !log.Resolved) { |
Gunnar Mills | 500ad78 | 2018-09-06 15:18:57 -0500 | [diff] [blame] | 104 | this.server_health = Constants.SERVER_HEALTH.critical; |
| 105 | return; |
Gunnar Mills | 5760e53 | 2018-11-12 09:59:50 -0600 | [diff] [blame] | 106 | } else if (log.priority == 'Medium' && !log.Resolved) { |
Gunnar Mills | 500ad78 | 2018-09-06 15:18:57 -0500 | [diff] [blame] | 107 | this.server_health = Constants.SERVER_HEALTH.warning; |
| 108 | } |
| 109 | } |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 110 | }; |
Iftekharul Islam | a1d238f | 2018-02-26 12:29:45 -0600 | [diff] [blame] | 111 | |
AppaRao Puli | b1289ec | 2018-11-14 20:33:30 +0530 | [diff] [blame] | 112 | this.setSystemName = function(sysName) { |
| 113 | this.systemName = sysName; |
| 114 | }; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 115 | } |
| 116 | ]); |
Sivas SRR | bb9092a | 2018-01-27 08:40:58 -0600 | [diff] [blame] | 117 | })(window.angular); |