Major refactoring of surveil backend

This commit is contained in:
Vincent Fournier 2015-04-30 14:55:32 -04:00
parent f44133ec78
commit 67c8f3051b
11 changed files with 223 additions and 189 deletions

View File

@ -9,7 +9,7 @@ angular.module('bansho.host', ['bansho.live',
.value('hostConfig', {})
.controller('HostCtrl', ['$scope', 'hostConfig', 'getHost', function ($scope, hostConfig, getHost) {
.controller('HostCtrl', ['$scope', 'hostConfig', 'backendClient', function ($scope, hostConfig, backendClient) {
var objectType = 'host',
objectIdentifier = {};
@ -17,7 +17,7 @@ angular.module('bansho.host', ['bansho.live',
$scope.hostName = hostConfig.hostName;
$scope.data = {};
getHost(objectType, objectIdentifier).then(function (data) {
backendClient.getHost(objectType, objectIdentifier).then(function (data) {
$scope.data = data;
});
}])

View File

@ -2,7 +2,7 @@
angular.module('bansho.host.cpu', ['bansho.live'])
.controller('HostCpuCtrl', ['$scope', 'getObjects', function ($scope, getObjects) {
.controller('HostCpuCtrl', ['$scope', 'backendClient', function ($scope, backendClient) {
var hostName = $scope.hostName,
service = 'cpu',
fields = ['state', 'description', 'plugin_output'],
@ -10,7 +10,7 @@ angular.module('bansho.host.cpu', ['bansho.live'])
apiName = 'services',
additionnalFields = {'host_name': hostName, 'description': service};
getObjects(fields, filters, apiName, additionnalFields)
backendClient.getObjects(fields, filters, apiName, additionnalFields)
.success(function (data) {
$scope.cpuData = data[0];
});

View File

@ -2,7 +2,7 @@
angular.module('bansho.host.load', [])
.controller('HostLoadCtrl', ['$scope', 'getObjects', function ($scope, getObjects) {
.controller('HostLoadCtrl', ['$scope', 'backendClient', function ($scope, backendClient) {
var hostName = $scope.hostName,
service = 'load',
fields = ['state', 'description', 'plugin_output'],
@ -10,7 +10,7 @@ angular.module('bansho.host.load', [])
apiName = 'services',
additionnalFields = {'host_name': hostName, 'description': service};
getObjects(fields, filters, apiName, additionnalFields)
backendClient.getObjects(fields, filters, apiName, additionnalFields)
.success(function (data) {
$scope.loadData = data[0];
});

View File

@ -12,6 +12,64 @@ angular.module('bansho.live', [])
regex: '__regex'
})
.service('backendClient', ['$http', 'filterSuffixes', 'hostMiddleware', function ($http, filterSuffixes, hostMiddleware) {
this.getObjects = function (fields, filters, apiName, additionnalFields) {
var filtersQuery = '',
additionnalQuery = '';
function createFiltersQuery(filters) {
var builtQuery = '';
angular.forEach(filters, function (value, key) {
var filterType = filterSuffixes[key];
angular.forEach(value, function (fieldValues, fieldName) {
var filter = fieldName + filterType;
angular.forEach(fieldValues, function (_value) {
var filterQuery = '&' + filter + '=' + _value;
builtQuery += filterQuery;
});
});
});
return builtQuery;
}
function createAdditionnalQuery(additionnalFields) {
var query = '';
angular.forEach(additionnalFields, function (value, key) {
query += '&' + key + '=' + value;
});
return query;
}
filtersQuery = createFiltersQuery(filters);
additionnalQuery = createAdditionnalQuery(additionnalFields);
function appendTransform(defaults, transform) {
// We can't guarantee that the default transformation is an array
defaults = angular.isArray(defaults) ? defaults : [defaults];
return defaults.concat(transform);
};
function transformations(data) {
if (apiName === 'hosts') {
hostMiddleware(data);
}
return data;
}
return $http({
url: '/adagios/rest/status/json/' + apiName + '/?fields=' + fields + filtersQuery + additionnalQuery,
method: 'GET',
transformResponse: appendTransform($http.defaults.transformResponse, transformations)
}).error(function () {
throw new Error('getObjects : GET Request failed');
});
};
}])
.service('getObjects', ['$http', 'filterSuffixes', 'hostMiddleware',
function ($http, filterSuffixes, hostMiddleware) {
return function (fields, filters, apiName, additionnalFields) {

View File

@ -1,13 +1,12 @@
'use strict';
angular.module('bansho.live', [])
.service('getObjects', ['$http', 'hostQueryTransform', 'hostMiddleware', 'serviceMiddleware',
function ($http, hostQueryTransform, hostMiddleware, serviceMiddleware) {
return function (fields, filters, apiName, additionnalFields) {
.service('backendClient', ['$http', '$q',
function ($http, $q) {
var getObjects = function (fields, filters, apiName, additionnalFields) {
var query = {},
transformations;
// Merges additionnalFields into filters as 'is' filter
angular.forEach(additionnalFields, function (value, key) {
if (!('is' in filters)) {
@ -19,7 +18,7 @@ angular.module('bansho.live', [])
}
filters.is[key].push(value);
})
});
function appendTransform(defaults, transform) {
// We can't guarantee that the default transformation is an array
@ -55,26 +54,20 @@ angular.module('bansho.live', [])
throw new Error('getObjects : POST Request failed');
});
};
}])
.service('getService', ['$http', 'getObjects',
function ($http, getObjects) {
return function (hostName, description) {
var getService = function (hostName, description) {
var fields = [],
filters = {},
additionnalFields = { 'host_name': hostName, 'description': description };
additionnalFields = {'host_name': hostName, 'description': description};
return getObjects(fields, filters, 'services', additionnalFields)
return this.getObjects(fields, filters, 'services', additionnalFields)
.error(function () {
throw new Error('getService : POST Request failed');
});
};
}])
// This service is used to count the number of host open problems
.service('getHostOpenProblems', ['$http', 'getObjects',
function ($http, getObjects) {
return function () {
var getHostOpenProblems = function () {
var fields = ['state'],
filters = {},
apiName = 'hosts',
@ -85,17 +78,13 @@ angular.module('bansho.live', [])
throw new Error('getHostOpenProblems : POST Request failed');
});
};
}])
// This service is used to count the number of service open problems
.service('getServiceOpenProblems', ['$http', '$q', 'getObjects',
function ($http, $q, getObjects) {
return function () {
var getServiceOpenProblems = function () {
var serviceFields = ['host_name', 'state'],
serviceFilters = { 'isnot': { 'state': [0] } },
serviceAdditionnalFields = { 'acknowledged': 0 },
serviceFilters = {'isnot': {'state': [0]}},
serviceAdditionnalFields = {'acknowledged': 0},
hostFields = ['host_name', 'state'],
hostFilters = { 'isnot': { 'state': [2] } },
hostFilters = {'isnot': {'state': [2]}},
hostAdditionnalFields = {},
responsePromise = $q.defer();
@ -113,7 +102,7 @@ angular.module('bansho.live', [])
.success(function (serviceData) {
var result = [];
for (i = 0; i < serviceData.length; i += 1) {
if (serviceData[i].host_name in hostsResult) {
if (serviceData[i].host_name in hostsResult) {
result.push(serviceData[i]);
}
}
@ -122,15 +111,11 @@ angular.module('bansho.live', [])
});
return responsePromise.promise;
};
}])
}
// This service is used to count the number of host problems
.service('getHostProblems', ['$http', 'getObjects',
function ($http, getObjects) {
return function () {
var getHostProblems = function () {
var fields = ['state'],
filters = { 'isnot': {'state': [0]} },
filters = {'isnot': {'state': [0]}},
apiName = 'hosts',
additionnalFields = {};
@ -139,14 +124,11 @@ angular.module('bansho.live', [])
throw new Error('getHostProblems : POST Request failed');
});
};
}])
// This service is used to count the number of service problems
.service('getServiceProblems', ['$http', 'getObjects',
function ($http, getObjects) {
return function () {
// This service is used to count the number of service problems
var getServiceProblems = function () {
var fields = ['state'],
filters = { 'isnot': {'state': [0]} },
filters = {'isnot': {'state': [0]}},
apiName = 'services',
additionnalFields = {};
@ -154,13 +136,10 @@ angular.module('bansho.live', [])
.error(function () {
throw new Error('getServiceOpenProblems : POST Request failed');
});
};
}])
}
// This service is used to count the number of hosts
.service('getTotalHosts', ['$http', 'getObjects',
function ($http, getObjects) {
return function () {
// This service is used to count the number of hosts
var getTotalHosts = function () {
var fields = ['host_name'],
filters = {},
apiName = 'hosts',
@ -170,13 +149,10 @@ angular.module('bansho.live', [])
.error(function () {
throw new Error('getTotalHosts : POST Request failed');
});
};
}])
}
// This service is used to count the number of services
.service('getTotalServices', ['$http', 'getObjects',
function ($http, getObjects) {
return function () {
// This service is used to count the number of services
var getTotalServices = function () {
var fields = ['host_name'],
filters = {},
apiName = 'services',
@ -186,95 +162,83 @@ angular.module('bansho.live', [])
.error(function () {
throw new Error('getTotalServices : POST Request failed');
});
};
}])
}
.service('getHost', ['$http', '$q', function ($http, $q) {
return function (objectType, objectIdentifier) {
var objectData = {},
endpoints = {
"host" : "hosts",
"service" : "services"
},
liveUrl = '/surveil/v2/status/' + endpoints[objectType] + '/' + objectIdentifier.host_name + '/',
configUrl = '/surveil/v2/config/'+ endpoints[objectType] + '/' + objectIdentifier.host_name + '/',
responsePromise = $q.defer();
var getHost = function (objectType, objectIdentifier) {
var objectData = {},
endpoints = {
"host": "hosts",
"service": "services"
},
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(liveUrl).success(function (liveData) {
$http.get(configUrl).success(function (configData) {
objectData.live = liveData;
objectData.config = configData;
responsePromise.resolve(objectData);
})
});
return responsePromise.promise;
};
}])
return responsePromise.promise;
}
.service('hostQueryTransform', function () {
return function (fields, filters) {
var i,
transformations = {
'host_state': 'state',
};
var hostQueryTransform = function (fields, filters) {
var i,
transformations = {
'host_state': 'state',
};
for (i = 0; i < fields.length; i += 1) {
if (fields[i] in transformations) {
fields[i] = transformations[fields[i]];
for (i = 0; i < fields.length; i += 1) {
if (fields[i] in transformations) {
fields[i] = transformations[fields[i]];
}
}
}
}
})
// Modify response object to conform to web ui
.service('hostMiddleware', function() {
return function (data) {
var i = 0,
conversions = {
'state': 'host_state'
};
// Modify response object to conform to web ui
var hostMiddleware = function (data) {
var i = 0,
conversions = {
'state': 'host_state'
};
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];
}
});
}
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;
};
})
// 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];
}
});
}
// Modify response object to conform to web ui
var serviceMiddleware = function (data) {
var i = 0,
conversions = {};
return data;
};
})
if (jQuery.isEmptyObject(conversions)) {
return data;
}
.service('getTableData', ['$q', 'getObjects',
function ($q, getObjects) {
return function (fields, filters, apiName, additionnalFields) {
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;
};
var getTableData = function (fields, filters, apiName, additionnalFields) {
var hostFields = [],
serviceFields = [],
hostFilters = {},
@ -291,13 +255,13 @@ angular.module('bansho.live', [])
found = false;
if (apiName === 'hosts') {
getObjects(fields, filters, 'hosts', additionnalFields)
this.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]);
@ -355,7 +319,7 @@ angular.module('bansho.live', [])
hostDict[host_name] = {};
}
hostDict[host_name][field] = value;
hostDict[host_name][field] = value;
});
}
@ -371,30 +335,45 @@ angular.module('bansho.live', [])
});
return responsePromise.promise;
};
}])
.service('acknowledge', ['$http', function($http) {
return function (host_name, service_description, attrs) {
var data = {};
data.host_name = host_name;
data.author = attrs.author;
data.comment = attrs.comment;
data.sticky = parseInt(attrs.sticky, 10);
data.notify = parseInt(attrs.notify, 10);
data.persistent = parseInt(attrs.persistent, 10);
if (service_description !== undefined) {
data.service_description = service_description;
}
return $http({
url: '/surveil/v2/actions/acknowledge/',
method: 'POST',
data: data,
}).error(function () {
throw new Error('acknowledge : POST Request failed');
});
};
}])
var acknowledge = function (host_name, service_description, attrs) {
var data = {};
data.host_name = host_name;
data.author = attrs.author;
data.comment = attrs.comment;
data.sticky = parseInt(attrs.sticky, 10);
data.notify = parseInt(attrs.notify, 10);
data.persistent = parseInt(attrs.persistent, 10);
if (service_description !== undefined) {
data.service_description = service_description;
}
return $http({
url: '/surveil/v2/actions/acknowledge/',
method: 'POST',
data: data
}).error(function () {
throw new Error('acknowledge : POST Request failed');
});
}
return {
getHost: getHost,
getObjects : getObjects,
getService : getService,
hostQueryTransform: hostQueryTransform,
acknowledge: acknowledge,
getHostOpenProblems: getHostOpenProblems,
hostMiddleware: hostMiddleware,
getServiceProblems: getServiceProblems,
getServiceOpenProblems: getServiceOpenProblems,
getHostProblems: getHostProblems,
getTableData: getTableData,
getTotalHosts: getTotalHosts,
getTotalServices: getTotalServices
}
}]);

View File

@ -7,12 +7,12 @@ angular.module('bansho.service', ['bansho.live',
.value('serviceConfig', {})
.controller('ServiceCtrl', ['$scope', 'serviceConfig', 'getService',
function ($scope, serviceConfig, getService) {
.controller('ServiceCtrl', ['$scope', 'serviceConfig', 'backendClient',
function ($scope, serviceConfig, backendClient) {
var hostName = serviceConfig.hostName,
description = serviceConfig.description;
getService(hostName, description).success(function (data) {
backendClient.getService(hostName, description).success(function (data) {
$scope.data = data;
});
}])

View File

@ -28,8 +28,8 @@ angular.module('bansho.table.actionbar', ['bansho.table',
return actionbarFilters;
})
.controller('TableActionbarCtrl', ['$scope', '$filter', 'acknowledge', 'actionbarFilters', 'tablesConfig',
function ($scope, $filter, acknowledge, actionbarFilters, tablesConfig, actionbarSelectFilter) {
.controller('TableActionbarCtrl', ['$scope', '$filter', 'backendClient', 'actionbarFilters', 'tablesConfig',
function ($scope, $filter, backendClient, actionbarFilters, tablesConfig, actionbarSelectFilter) {
$scope.actionbarFilters = actionbarFilters;
$scope.actionbarFilters.activeFilter = $scope.actionbarFilters.possibleFilters[0];
$scope.ackFormIsOpen = false;
@ -54,7 +54,7 @@ angular.module('bansho.table.actionbar', ['bansho.table',
service_description = entry.description;
}
acknowledge(entry.host_name, service_description, $scope.acknowledgeData)
backendClient.acknowledge(entry.host_name, service_description, $scope.acknowledgeData)
.error(function (data) {
throw new Error('Acknowledge request failed');
});

View File

@ -16,9 +16,9 @@ angular.module('bansho.table', ['bansho.live',
.value('tablesConfig', [])
.controller('TableCtrl', ['$scope', '$interval', 'getTableData', 'tablesConfig',
.controller('TableCtrl', ['$scope', '$interval', 'backendClient', 'tablesConfig',
'actionbarFilters', 'promisesManager', 'tableGlobalConfig',
function ($scope, $interval, getTableData, tablesConfig, actionbarFilters, promisesManager, tableGlobalConfig) {
function ($scope, $interval, backendClient, tablesConfig, actionbarFilters, promisesManager, tableGlobalConfig) {
var requestFields = [],
conf = tablesConfig[tableGlobalConfig.nextTableIndex],
getData,
@ -39,7 +39,7 @@ angular.module('bansho.table', ['bansho.live',
});
getData = function (requestFields, filters, apiName, additionnalFields) {
var promise = getTableData(requestFields, filters, apiName, additionnalFields);
var promise = backendClient.getTableData(requestFields, filters, apiName, additionnalFields);
promise.then(function (data) {
$scope.entries = data;
conf.entries = data;

View File

@ -16,10 +16,8 @@ angular.module('bansho.tactical', ['bansho.live',
this.topAlertProducers = config.components.topAlertProducers;
})
.controller('TacticalCtrl', ['$scope', '$interval', 'tacticalConfig', 'getHostProblems', 'getServiceProblems',
'getTotalHosts', 'getTotalServices', 'promisesManager',
function ($scope, $interval, tacticalConfig, getHostProblems, getServiceProblems, getTotalHosts,
getTotalServices, promisesManager) {
.controller('TacticalCtrl', ['$scope', '$interval', 'tacticalConfig', 'backendClient', 'promisesManager',
function ($scope, $interval, tacticalConfig, backendClient, promisesManager) {
var getData;
@ -35,17 +33,17 @@ angular.module('bansho.tactical', ['bansho.live',
$scope.totalServices = undefined;
getData = function () {
getHostProblems().success(function (hostProblems) {
backendClient.getHostProblems().success(function (hostProblems) {
$scope.hostProblems = hostProblems.length;
getTotalHosts().success(function (allHosts) {
backendClient.getTotalHosts().success(function (allHosts) {
$scope.totalHosts = allHosts.length;
$scope.hostsRatio = ($scope.totalHosts - $scope.hostProblems) / $scope.totalHosts * 100;
});
});
getServiceProblems().success(function (serviceProblems) {
backendClient.getServiceProblems().success(function (serviceProblems) {
$scope.serviceProblems = serviceProblems.length;
getTotalServices().success(function (allServices) {
backendClient.getTotalServices().success(function (allServices) {
$scope.totalServices = allServices.length;
$scope.servicesRatio = ($scope.totalServices - $scope.serviceProblems) / $scope.totalServices * 100;
});

View File

@ -2,16 +2,16 @@
angular.module('bansho.topbar', ['bansho.live'])
.controller('TopBarCtrl', ['$scope', '$interval', 'getServiceProblems', 'getHostProblems', 'promisesManager',
function ($scope, $interval, getServiceProblems, getHostProblems, promisesManager) {
.controller('TopBarCtrl', ['$scope', '$interval', 'backendClient', 'promisesManager',
function ($scope, $interval, backendClient, promisesManager) {
var getData,
hostProblems,
serviceProblems;
getData = function () {
getServiceProblems().success(function (data) {
backendClient.getServiceProblems().success(function (data) {
serviceProblems = data.length;
getHostProblems().success(function (data) {
backendClient.getHostProblems().success(function (data) {
hostProblems = data.length;
$scope.allProblems = serviceProblems + hostProblems;
});

View File

@ -9,11 +9,10 @@ angular.module('bansho.view.dashboard', ['ngRoute',
.value('dashboardConfig', {})
.controller('DashboardCtrl', ['$scope', '$routeParams', '$interval', 'dashboardConfig', 'getObjects',
'TableConfigObj', 'TacticalConfigObj', 'getHostOpenProblems', 'getServiceOpenProblems', 'getHostProblems',
'getServiceProblems', 'promisesManager',
function ($scope, $routeParams, $interval, dashboardConfig, getObjects, TableConfigObj, TacticalConfigObj, getHostOpenProblems,
getServiceOpenProblems, getHostProblems, getServiceProblems, promisesManager) {
.controller('DashboardCtrl', ['$scope', '$routeParams', '$interval', 'dashboardConfig',
'TableConfigObj', 'TacticalConfigObj', 'backendClient', 'promisesManager',
function ($scope, $routeParams, $interval, dashboardConfig, TableConfigObj, TacticalConfigObj,
backendClient, promisesManager) {
var components = [],
component,
config,
@ -42,17 +41,17 @@ angular.module('bansho.view.dashboard', ['ngRoute',
}
getData = function () {
getHostOpenProblems().success(function (data) {
backendClient.getHostOpenProblems().success(function (data) {
$scope.nbHostOpenProblems = data.length;
getServiceOpenProblems().then(function (openProblems) {
backendClient.getServiceOpenProblems().then(function (openProblems) {
$scope.nbServiceOpenProblems = openProblems.length;
$scope.totalOpenProblems = $scope.nbServiceOpenProblems + $scope.nbHostOpenProblems;
});
});
getHostProblems().success(function (data) {
backendClient.getHostProblems().success(function (data) {
$scope.nbHostProblems = data.length;
getServiceProblems().success(function (data) {
backendClient.getServiceProblems().success(function (data) {
$scope.nbServiceProblems = data.length;
$scope.totalProblems = $scope.nbHostProblems + $scope.nbServiceProblems;
});