blob: 3b31a1f0163a1e59ef53a6466e07b8a7ac9681f3 [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
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070010window.angular && (function(angular) {
11 'use strict';
Iftekharul Islam99d199f2017-03-24 15:28:25 -050012
Andrew Geisslerd27bb132018-05-24 11:07:27 -070013 angular.module('app.common.services').service('dataService', [
14 'Constants',
15 function(Constants) {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070016 this.server_health = Constants.SERVER_HEALTH.unknown;
17 this.server_state = 'Unreachable';
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070018 this.LED_state = Constants.LED_STATE_TEXT.off;
19 this.last_updated = new Date();
Iftekharul Islam99d199f2017-03-24 15:28:25 -050020
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070021 this.loading = false;
22 this.server_unreachable = false;
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070023 this.showNavigation = false;
24 this.bodyStyle = {};
25 this.path = '';
Iftekharul Islam99d199f2017-03-24 15:28:25 -050026
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070027 this.hostname = '';
28 this.mac_address = '';
Gunnar Mills659651e2018-05-30 15:21:07 -050029 this.defaultgateway = '';
Iftekharul Islama1d238f2018-02-26 12:29:45 -060030
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070031 this.ignoreHttpError = false;
AppaRao Pulib1289ec2018-11-14 20:33:30 +053032 this.systemName = '';
AppaRao Pulif04960f2018-12-06 21:51:27 +053033
34 this.configJson = require('../../../config.json');
35
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070036 this.getServerId = function() {
37 return this.host.replace(/^https?\:\/\//ig, '');
38 };
Iftekharul Islam1acb4122017-11-02 13:20:32 -050039
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070040 this.reloadServerId = function() {
41 this.server_id = this.getServerId();
42 };
Iftekharul Islam1acb4122017-11-02 13:20:32 -050043
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070044 this.getHost = function() {
Andrew Geisslerd27bb132018-05-24 11:07:27 -070045 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 Geisslerba5e3f32018-05-24 10:58:00 -070050 return Constants.API_CREDENTIALS.default_protocol + '://' +
Andrew Geisslerd27bb132018-05-24 11:07:27 -070051 window.location.hostname +
52 (window.location.port ? ':' + window.location.port : '');
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070053 }
54 };
Iftekharul Islam1acb4122017-11-02 13:20:32 -050055
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070056 this.setHost = function(hostWithPort) {
57 hostWithPort = hostWithPort.replace(/^https?\:\/\//ig, '');
Andrew Geisslerd27bb132018-05-24 11:07:27 -070058 var hostURL =
59 Constants.API_CREDENTIALS.default_protocol + '://' + hostWithPort;
60 sessionStorage.setItem(
61 Constants.API_CREDENTIALS.host_storage_key, hostURL);
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070062 this.host = hostURL;
63 this.reloadServerId();
64 };
Iftekharul Islam1acb4122017-11-02 13:20:32 -050065
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070066 this.getUser = function() {
67 return sessionStorage.getItem('LOGIN_ID');
68 };
Gunnar Mills1a60f6e2018-03-14 13:42:08 -050069
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070070 this.host = this.getHost();
71 this.server_id = this.getServerId();
Iftekharul Islam1acb4122017-11-02 13:20:32 -050072
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070073 this.setNetworkInfo = function(data) {
Igor Kononenkod10511f2020-09-12 16:36:35 +030074 var formatted = data.formatted_data || {};
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070075 this.hostname = data.hostname;
Gunnar Mills659651e2018-05-30 15:21:07 -050076 this.defaultgateway = data.defaultgateway;
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070077 this.mac_address = data.mac_address;
Igor Kononenkod10511f2020-09-12 16:36:35 +030078 this.network_interfaces = formatted.interfaces || [];
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070079 };
Iftekharul Islamba556c32017-08-11 08:37:12 -050080
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070081 this.setPowerOnState = function() {
82 this.server_state = Constants.HOST_STATE_TEXT.on;
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070083 };
Iftekharul Islam99d199f2017-03-24 15:28:25 -050084
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070085 this.setPowerOffState = function() {
86 this.server_state = Constants.HOST_STATE_TEXT.off;
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070087 };
Iftekharul Islam99d199f2017-03-24 15:28:25 -050088
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070089 this.setErrorState = function() {
90 this.server_state = Constants.HOST_STATE_TEXT.error;
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070091 };
Iftekharul Islam99d199f2017-03-24 15:28:25 -050092
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070093 this.setUnreachableState = function() {
94 this.server_state = Constants.HOST_STATE_TEXT.unreachable;
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070095 };
Iftekharul Islam34714092017-09-06 10:45:27 -050096
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070097 this.updateServerHealth = function(logs) {
Gunnar Mills5760e532018-11-12 09:59:50 -060098 // 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 Geisslerba5e3f32018-05-24 10:58:00 -0700101 this.server_health = Constants.SERVER_HEALTH.good;
Gunnar Mills500ad782018-09-06 15:18:57 -0500102 for (var log of logs) {
Gunnar Mills5760e532018-11-12 09:59:50 -0600103 if (log.priority == 'High' && !log.Resolved) {
Gunnar Mills500ad782018-09-06 15:18:57 -0500104 this.server_health = Constants.SERVER_HEALTH.critical;
105 return;
Gunnar Mills5760e532018-11-12 09:59:50 -0600106 } else if (log.priority == 'Medium' && !log.Resolved) {
Gunnar Mills500ad782018-09-06 15:18:57 -0500107 this.server_health = Constants.SERVER_HEALTH.warning;
108 }
109 }
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700110 };
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600111
AppaRao Pulib1289ec2018-11-14 20:33:30 +0530112 this.setSystemName = function(sysName) {
113 this.systemName = sysName;
114 };
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700115 }
116 ]);
Sivas SRRbb9092a2018-01-27 08:40:58 -0600117})(window.angular);