From e0bf937e6a9f26ac94d071804800536644f2a029 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Vachon?= Date: Thu, 23 Apr 2015 15:38:55 -0400 Subject: [PATCH] Surveil backend support completed --- app/components/host/host.js | 6 +- app/components/live/adagios.js | 84 ++++---- app/components/live/surveil.js | 212 +++++++------------- app/components/table/cell_host/cell_host.js | 2 +- app/templates/host/host.js | 4 +- 5 files changed, 125 insertions(+), 183 deletions(-) diff --git a/app/components/host/host.js b/app/components/host/host.js index 7fecb8d..28824db 100644 --- a/app/components/host/host.js +++ b/app/components/host/host.js @@ -9,7 +9,7 @@ angular.module('adagios.host', ['adagios.live', .value('hostConfig', {}) - .controller('HostCtrl', ['$scope', 'hostConfig', 'addObjectToScope', function ($scope, hostConfig, addObjectToScope) { + .controller('HostCtrl', ['$scope', 'hostConfig', 'getHost', function ($scope, hostConfig, getHost) { var objectType = 'host', objectIdentifier = {}; @@ -17,7 +17,9 @@ angular.module('adagios.host', ['adagios.live', $scope.hostName = hostConfig.hostName; $scope.data = {}; - addObjectToScope(objectType, objectIdentifier, $scope); + getHost(objectType, objectIdentifier).then(function (data) { + $scope.data = data; + }); }]) .directive('adgHost', ['$http', '$compile', 'hostConfig', diff --git a/app/components/live/adagios.js b/app/components/live/adagios.js index 4baab96..5b66106 100644 --- a/app/components/live/adagios.js +++ b/app/components/live/adagios.js @@ -98,18 +98,24 @@ angular.module('adagios.live') }]) // This service is used to count the number of service open problems - .service('getServiceOpenProblems', ['$http', 'getObjects', - function ($http, getObjects) { + .service('getServiceOpenProblems', ['$http', '$q', 'getObjects', + function ($http, $q, getObjects) { return function () { var fields = ['state'], filters = { "isnot": { "state": [0], "host_state": [2] }}, apiName = 'services', - additionnalQueryFields = {'acknowledged': 0}; + additionnalQueryFields = {'acknowledged': 0}, + responsePromise = $q.defer(); - return getObjects(fields, filters, apiName, additionnalQueryFields) + getObjects(fields, filters, apiName, additionnalQueryFields) + .success(function (data) { + responsePromise.resolve(data); + }) .error(function () { throw new Error('getServiceOpenProblems : GET Request failed'); }); + + return responsePromise.promise; }; }]) @@ -231,43 +237,47 @@ angular.module('adagios.live') }]) // Add object of specified type to $scope.data - .service('addObjectToScope', ['$http', 'getObjectId', 'getObjectById', function ($http, getObjectId, getObjectById) { - return function (objectType, objectIdentifier, scope) { - var objectData = {}, - url = "/adagios/rest/status/json/", - firstParameter = true, - endpoints = { - "host" : "hosts", - "service" : "services" - }; + .service('getHost', ['$http', '$q', 'getObjectId', 'getObjectById', + function ($http, $q, getObjectId, getObjectById) { + return function (objectType, objectIdentifier) { + var objectData = {}, + url = "/adagios/rest/status/json/", + firstParameter = true, + endpoints = { + "host" : "hosts", + "service" : "services" + }, + response = {}, + responsePromise = $q.defer(); - url += endpoints[objectType]; - url += "/?"; + url += endpoints[objectType]; + url += "/?"; - angular.forEach(objectIdentifier, function (value, key) { - if (!firstParameter) { - url += "&"; - } - url += key + "=" + value; - firstParameter = false; + angular.forEach(objectIdentifier, function (value, key) { + if (!firstParameter) { + url += "&"; + } + url += key + "=" + value; + firstParameter = false; - }); - - $http.get(url) - .success(function (data) { - objectData.live = data[0]; - getObjectId(objectType, objectIdentifier) - .success(function (data) { - var objectId = data[0].id; - scope.data.id = objectId; - getObjectById(objectId) - .success(function (data) { - objectData.config = data; - scope.data = objectData; - }); - }); }); - }; + + $http.get(url) + .success(function (data) { + response.live = data[0]; + getObjectId(objectType, objectIdentifier) + .success(function (data) { + var objectId = data[0].id; + getObjectById(objectId) + .success(function (data) { + response.config = data; + responsePromise.resolve(response); + }); + }); + }); + + return responsePromise.promise; + }; }]) // Modify response object to conform to web ui diff --git a/app/components/live/surveil.js b/app/components/live/surveil.js index ad071a4..cd4a613 100644 --- a/app/components/live/surveil.js +++ b/app/components/live/surveil.js @@ -2,10 +2,11 @@ angular.module('adagios.live') - .service('getObjects', ['$http', 'hostQueryTransform', - function ($http, hostQueryTransform) { + .service('getObjects', ['$http', 'hostQueryTransform', 'hostMiddleware', 'serviceMiddleware', + function ($http, hostQueryTransform, hostMiddleware, serviceMiddleware) { return function (fields, filters, apiName, additionnalFields) { - var query = {}; + var query = {}, + transformations; // Merges additionnalFields into filters as 'is' filter angular.forEach(additionnalFields, function (value, key) { @@ -27,13 +28,14 @@ angular.module('adagios.live') return defaults.concat(transform); }; - - function transformations(data) { - // TODO: implement transformation - return data; + if (apiName === 'hosts') { + transformations = hostMiddleware; + } else if (apiName === 'services') { + transformations = serviceMiddleware; + } else { + throw new Error('getObjects : ' + apiName + ' API is not supported'); } - if (apiName === 'hosts') { hostQueryTransform(fields, filters); } @@ -62,7 +64,7 @@ angular.module('adagios.live') filters = {}, additionnalFields = { 'host_name': hostName, 'description': description }; - return getObjects(fields, filters, additionnalFields) + return getObjects(fields, filters, 'services', additionnalFields) .error(function () { throw new Error('getService : POST Request failed'); }); @@ -187,96 +189,26 @@ angular.module('adagios.live') }; }]) - .service('getObjectId', ['$http', function ($http) { + .service('getHost', ['$http', '$q', function ($http, $q) { return function (objectType, objectIdentifier) { - - var postString, req; - - postString = "with_fields=id&object_type=" + objectType; - angular.forEach(objectIdentifier, function (value, key) { - if (key === "description") { - key = "service_description"; - } - postString += "&" + key + "=" + value; - }); - - req = { - method: 'POST', - url: '/rest/pynag/json/get_objects', - headers: { - 'Content-Type': 'application/x-www-form-urlencoded' - }, - data: postString - }; - - return $http(req) - .error(function () { - throw new Error('getObjectId : POST Request failed'); - }); - }; - }]) - - .service('getObjectById', ['$http', function ($http) { - return function (objectId) { - - var postString, req; - - postString = "with_fields=&id=" + objectId; - - req = { - method: 'POST', - url: '/rest/pynag/json/get_object', - headers: { - 'Content-Type': 'application/x-www-form-urlencoded' - }, - data: postString - }; - - - return $http(req) - .error(function () { - throw new Error('getHostById : POST Request failed'); - }); - }; - }]) - - // Add object of specified type to $scope.data - .service('addObjectToScope', ['$http', 'getObjectId', 'getObjectById', function ($http, getObjectId, getObjectById) { - return function (objectType, objectIdentifier, scope) { var objectData = {}, - url = "/rest/status/json/", - firstParameter = true, endpoints = { "host" : "hosts", "service" : "services" - }; - - url += endpoints[objectType]; - url += "/?"; - - angular.forEach(objectIdentifier, function (value, key) { - if (!firstParameter) { - url += "&"; - } - url += key + "=" + value; - firstParameter = false; + }, + liveUrl = '/surveil/v2/status/' + endpoints[objectType] + '/' + objectIdentifier.host_name + '/', + configUrl = '/surveil/v2/config/'+ endpoints[objectType] + '/' + objectIdentifier.host_name + '/', + responsePromise = $q.defer(); + $http.get(liveUrl) .success(function (liveData) { + $http.get(configUrl).success(function (configData) { + objectData.live = liveData; + objectData.config = configData; + responsePromise.resolve(objectData); + }) }); - $http.get(url) - .success(function (data) { - objectData.live = data[0]; - getObjectId(objectType, objectIdentifier) - .success(function (data) { - var objectId = data[0].id; - scope.data.id = objectId; - getObjectById(objectId) - .success(function (data) { - objectData.config = data; - scope.data = objectData; - }); - }); - }); + return responsePromise.promise; }; }]) @@ -297,7 +229,7 @@ angular.module('adagios.live') // Modify response object to conform to web ui .service('hostMiddleware', function() { - return function(data) { + return function (data) { var i = 0, conversions = { 'state': 'host_state' @@ -311,6 +243,32 @@ angular.module('adagios.live') } }); } + + return data; + }; + }) + + // Modify response object to conform to web ui + .service('serviceMiddleware', function() { + return function (data) { + var i = 0, + conversions = { + }; + + if (jQuery.isEmptyObject(conversions)) { + return data; + } + + for (i = 0; i < data.length; i += 1) { + angular.forEach(data[i], function (value, field) { + if (field in conversions) { + data[i][conversions[field]] = value; + delete data[i][field]; + } + }); + } + + return data; }; }) @@ -331,37 +289,29 @@ angular.module('adagios.live') responsePromise = $q.defer(), i, found = false; - - for (i = 0; i < fields.length; i += 1) { - if (fields[i] in hostKeys) { - hostFields.push(hostKeys[fields[i]]); - } else { - serviceFields.push(fields[i]); - } + + if (apiName === 'hosts') { + getObjects(fields, filters, 'hosts', additionnalFields) + .success(function (data) { + responsePromise.resolve(data); + }); + return responsePromise.promise; } + + angular.forEach(fields, function (field) { + if (field in hostKeys) { + hostFields.push(hostKeys[field]); + } else { + serviceFields.push(field); + } + }); // Make sure that 'host_name' is in both queries as we // use this field to merge data - for (i = 0; i < hostFields.length; i += 1) { - if (hostFields[i] === 'host_name') { - found = true; - break; - } - } - - if (!found) { + if ($.inArray('host_name', hostFields) === -1) { hostFields.push('host_name'); } - - found = false; - for (i = 0; i < serviceFields.length; i += 1) { - if (serviceFields[i] === 'host_name') { - found = true; - break; - } - } - - if (!found) { + if ($.inArray('host_name', serviceFields) === -1) { serviceFields.push('host_name'); } @@ -373,7 +323,6 @@ angular.module('adagios.live') } }) - //{ 'isnot': {'state': [0]} }, angular.forEach(filters, function (filterData, filterName) { angular.forEach(filterData, function (values, field) { if (field in hostKeys) { @@ -390,7 +339,7 @@ angular.module('adagios.live') }); }); - // Query host and service APIs and merges responses + // Queries host and service APIs and merges responses getObjects(hostFields, hostFilters, 'hosts', hostAdditionnalFields) .success(function (hostData) { getObjects(serviceFields, serviceFilters, 'services', serviceAdditionnalFields) @@ -402,30 +351,11 @@ angular.module('adagios.live') var host_name = hostData[i].host_name; angular.forEach(hostData[i], function (value, field) { - var field_ = undefined, - skip = false; - - if (field === 'host_name') { - skip = true; + if (!(host_name in hostDict)) { + hostDict[host_name] = {}; } - if (!skip) { - angular.forEach(hostKeys, function (value, key) { - if (value === field) { - field_ = key; - } - }); - - if (field === undefined) { - field_ = field; - } - - if (!(host_name in hostDict)) { - hostDict[host_name] = {}; - } - - hostDict[host_name][field_] = value; - } + hostDict[host_name][field] = value; }); } diff --git a/app/components/table/cell_host/cell_host.js b/app/components/table/cell_host/cell_host.js index b960eb3..7934c88 100644 --- a/app/components/table/cell_host/cell_host.js +++ b/app/components/table/cell_host/cell_host.js @@ -9,7 +9,7 @@ angular.module('adagios.table.cell_host', ['adagios.table']) $scope.state = 'state--ok'; } else if ($scope.entry.host_state === 1) { $scope.state = 'state--warning'; - } else if ($scope.entry.host_state === "") { + } else if ($scope.entry.host_state === '') { $scope.state = ''; } else { $scope.state = 'state--error'; diff --git a/app/templates/host/host.js b/app/templates/host/host.js index b6c6224..3363108 100644 --- a/app/templates/host/host.js +++ b/app/templates/host/host.js @@ -2,8 +2,8 @@ angular.module('adagios.view.host', ['adagios.live']) - .controller('HostViewCtrl', ['$http', '$scope', '$routeParams', 'getObjectId', 'getObjectById', 'addObjectToScope', - function ($http, $scope, $routeParams, getObjectId, getObjectById, addObjectToScope) { + .controller('HostViewCtrl', ['$http', '$scope', '$routeParams', + function ($http, $scope, $routeParams) { if (!!$routeParams.host_name) { $scope.hostName = $routeParams.host_name; } else {