56 lines
1.8 KiB
JavaScript
56 lines
1.8 KiB
JavaScript
/*global jQuery */
|
|
|
|
'use strict';
|
|
|
|
angular.module('bansho.surveil')
|
|
.service('surveilConfig', ['$http', '$q','surveilQuery', 'componentsConfig', 'surveilApiConfig',
|
|
function ($http, $q, surveilQuery, componentsConfig, surveilApiConfig) {
|
|
var executeQuery = function (url, method, query) {
|
|
return $http({
|
|
url: url,
|
|
method: method,
|
|
data: query
|
|
}).error(function () {
|
|
throw new Error('executeQuery : ' + method + ' Request failed');
|
|
});
|
|
};
|
|
|
|
var getData = function (fields, filters, endpoint) {
|
|
var promise = $q.defer();
|
|
|
|
if (!queryEndpoint[endpoint]) {
|
|
throw new Error('getData in surveilConfig : Invalid endpoint ' + endpoint);
|
|
}
|
|
|
|
queryEndpoint[endpoint](fields, filters, function (data) {
|
|
promise.resolve(data);
|
|
});
|
|
|
|
return promise.promise;
|
|
};
|
|
|
|
var queryEndpoint = {
|
|
"hosts": function (fields, filters, callback) {
|
|
var hostQuery = surveilQuery(fields, filters.hosts),
|
|
method = 'POST',
|
|
hostUrl = surveilApiConfig.endpoint('config') + '/hosts/';
|
|
|
|
executeQuery(hostUrl, method, hostQuery)
|
|
.success(function (hosts) {
|
|
var response = [];
|
|
|
|
angular.forEach(hosts, function (host) {
|
|
response.push(host);
|
|
});
|
|
|
|
callback(response);
|
|
});
|
|
}
|
|
};
|
|
|
|
|
|
return {
|
|
getData: getData
|
|
};
|
|
}]);
|