Refactoring: change performZabbixAPIRequest()? deleted code duplication.

This commit is contained in:
Alexander Zobnin
2015-05-18 12:36:43 +03:00
parent a62d0e60fc
commit c3b64fc2b5

View File

@@ -85,14 +85,20 @@ function (angular, _, kbn) {
// Request data from Zabbix API // Request data from Zabbix API
ZabbixAPIDatasource.prototype.performZabbixAPIRequest = function(request_data) { ZabbixAPIDatasource.prototype.performZabbixAPIRequest = function(method, params) {
var options = { var options = {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },
url: this.url, url: this.url,
data: request_data data: {
jsonrpc: '2.0',
method: method,
params: params,
auth: this.auth,
id: 1
}
}; };
var performedQuery; var performedQuery;
@@ -125,28 +131,22 @@ function (angular, _, kbn) {
* @param items: array of zabbix api item objects * @param items: array of zabbix api item objects
*/ */
ZabbixAPIDatasource.prototype.performTimeSeriesQuery = function(items, start, end) { ZabbixAPIDatasource.prototype.performTimeSeriesQuery = function(items, start, end) {
var data = { var params = {
jsonrpc: '2.0', output: 'extend',
method: 'history.get', history: items.value_type,
params: { itemids: items.itemid,
output: 'extend', sortfield: 'clock',
history: items.value_type, sortorder: 'ASC',
itemids: items.itemid, limit: this.limitmetrics,
sortfield: 'clock', time_from: start,
sortorder: 'ASC',
limit: this.limitmetrics,
time_from: start,
},
auth: this.auth,
id: 1
}; };
// Relative queries (e.g. last hour) don't include an end time // Relative queries (e.g. last hour) don't include an end time
if (end) { if (end) {
data.params.time_till = end; params.time_till = end;
} }
return this.performZabbixAPIRequest(data); return this.performZabbixAPIRequest('history.get', params);
}; };
@@ -178,83 +178,62 @@ function (angular, _, kbn) {
// Get the list of host groups // Get the list of host groups
ZabbixAPIDatasource.prototype.performHostGroupSuggestQuery = function() { ZabbixAPIDatasource.prototype.performHostGroupSuggestQuery = function() {
var data = { var params = {
jsonrpc: '2.0', output: ['name'],
method: 'hostgroup.get', real_hosts: true, //Return only host groups that contain hosts
params: { sortfield: 'name'
output: ['name'],
real_hosts: true, //Return only host groups that contain hosts
sortfield: 'name'
},
auth: this.auth,
id: 1
}; };
return this.performZabbixAPIRequest(data); return this.performZabbixAPIRequest('hostgroup.get', params);
}; };
// Get the list of hosts // Get the list of hosts
ZabbixAPIDatasource.prototype.performHostSuggestQuery = function(groupid) { ZabbixAPIDatasource.prototype.performHostSuggestQuery = function(groupid) {
var data = { var params = {
jsonrpc: '2.0', output: ['name'],
method: 'host.get', sortfield: 'name'
params: {
output: ['name'],
sortfield: 'name'
},
auth: this.auth,
id: 1
}; };
// Return only hosts in given group
if (groupid) { if (groupid) {
data.params.groupids = groupid; params.groupids = groupid;
} }
return this.performZabbixAPIRequest('host.get', params);
return this.performZabbixAPIRequest(data);
}; };
// Get the list of applications // Get the list of applications
ZabbixAPIDatasource.prototype.performAppSuggestQuery = function(hostid) { ZabbixAPIDatasource.prototype.performAppSuggestQuery = function(hostid) {
var data = { var params = {
jsonrpc: '2.0', output: ['name'],
method: 'application.get', sortfield: 'name',
params: { hostids: hostid
output: ['name'],
sortfield: 'name',
hostids: hostid
},
auth: this.auth,
id: 1
}; };
return this.performZabbixAPIRequest(data); return this.performZabbixAPIRequest('application.get', params);
}; };
// Get the list of host items // Get the list of host items
ZabbixAPIDatasource.prototype.performItemSuggestQuery = function(hostid, applicationid) { ZabbixAPIDatasource.prototype.performItemSuggestQuery = function(hostid, applicationid) {
var data = { var params = {
jsonrpc: '2.0', output: ['name', 'key_', 'value_type', 'delay'],
method: 'item.get', sortfield: 'name',
params: { hostids: hostid,
output: ['name', 'key_', 'value_type', 'delay'],
sortfield: 'name', //Include web items in the result
hostids: hostid, webitems: true,
webitems: true, //Include web items in the result // Return only numeric items
filter: { filter: {
value_type: [0,3] value_type: [0,3]
} }
},
auth: this.auth,
id: 1
}; };
// If application selected return only relative items // If application selected return only relative items
if (applicationid) { if (applicationid) {
data.params.applicationids = applicationid; params.applicationids = applicationid;
} }
return this.performZabbixAPIRequest(data); return this.performZabbixAPIRequest('item.get', params);
}; };