SmartIT_Extensions/BMC/smart-it-full-helix/scripts/app/admin/health-check/health-check-controller.js

120 lines
4.9 KiB
JavaScript

"use strict";
(function () {
'use strict';
angular
.module('adminModule')
.controller('MonitorController', MonitorController);
MonitorController.$inject = ['$http', '$q', '$window'];
function MonitorController($http, $q, $window) {
var vm = this;
vm.changeActiveTenant = changeActiveTenant;
vm.addNewHost = addNewHost;
vm.restartCheck = restartCheck;
vm.removeHostFromList = removeHostFromList;
vm.newHostName = '';
vm.newHostProtocol = 'http://';
vm.showCurrentHostCheck = true;
vm.currentHost = { hostName: $window.location.host, tenants: [] };
vm.otherHosts = [];
vm.$onInit = init;
function init(host) {
host = host || '';
var tenants = [], checks = [], errors = [], getTenantsPromise = $http.get(host + '/smartit/rest/test/getHostNames')
.then(function (result) {
for (var tenant in result.data) {
tenants.push({ name: tenant });
}
})
.catch(function () {
errors.push('Cannot get list of tenants');
}), getChecksPromise = $http.get(host + '/smartit/rest/test/checks')
.then(function (result) {
checks = result.data;
})
.catch(function () {
errors.push('Cannot get list of checks');
});
if (host) {
vm.newHostDataLoading = true;
}
else {
vm.currentHost.tenantsChecksLoading = true;
}
$q.all([getTenantsPromise, getChecksPromise])
.finally(function () {
if (host) {
vm.newHostDataLoading = false;
var hostIndex = vm.otherHosts.length;
vm.otherHosts.push({ tenants: tenants, checks: checks, hostName: host, errors: errors });
vm.otherHosts[hostIndex].tenants[0].active = true;
tenants.forEach(function (tenant, i) {
checksPerTenant(host, vm.otherHosts[hostIndex].tenants[i], angular.copy(checks));
});
}
else {
vm.currentHost.errors = errors;
vm.currentHost.tenants = tenants;
vm.currentHost.checks = checks;
vm.currentHost.tenants[0].active = true;
vm.currentHost.tenantsChecksLoading = false;
tenants.forEach(function (tenant, i) {
checksPerTenant(host, vm.currentHost.tenants[i], angular.copy(checks));
});
}
});
}
function checksPerTenant(host, tenant, checks) {
if (tenant && checks) {
tenant.checks = [];
var i, l = checks.length;
for (i = 0; i < l; i++) {
tenant.checks.push(checks[i]);
(function (index) {
$http.get(host + checks[i].endpoint)
.then(function (result) {
tenant.checks[index].status = result.data.status || 'UNKNOWN';
tenant.checks[index].enabled = result.data.enabled;
tenant.checks[index].additionalInfo = result.data.enabled ? result.data.additionalInfo : result.data.additionalInfo || 'DISABLED';
})
.catch(function (error) {
tenant.checks[index].status = 'ERROR';
tenant.checks[index].enabled = false;
tenant.checks[index].additionalInfo = 'ERROR ' + error.status;
});
})(i);
}
}
}
function changeActiveTenant(tenants, activeTenant) {
if (activeTenant.active) {
return;
}
tenants.forEach(function (tenant) {
if (tenant.active) {
tenant.active = false;
}
});
activeTenant.active = true;
}
function addNewHost() {
vm.newHostName = vm.newHostName.replace(/(http:|https:)\/\//g, '').split('/')[0];
init(vm.newHostProtocol + vm.newHostName);
vm.newHostName = '';
}
function restartCheck(tenant, host) {
host = host || '';
tenant.checks.forEach(function (check) {
check.status = null;
check.enabled = null;
check.additionalInfo = null;
});
var checkList = angular.copy(tenant.checks);
checksPerTenant(host, tenant, checkList);
}
function removeHostFromList(index) {
var i = vm.otherHosts.length - index - 1;
vm.otherHosts.splice(i, 1);
}
}
}());