Add itemFindQuery for search templated items.

This commit is contained in:
Alexander Zobnin
2015-05-23 17:45:07 +03:00
parent 7bc24fc2a0
commit 0e0f5907c4

View File

@@ -282,6 +282,8 @@ function (angular, _, kbn) {
_.each(query.split('.'), function (part) { _.each(query.split('.'), function (part) {
part = templateSrv.replace(part); part = templateSrv.replace(part);
if (part[0] === '{') { if (part[0] === '{') {
// Convert multiple mettrics to array
// "{metric1,metcic2,...,metricN}" --> [metric1, metcic2,..., metricN]
parts.push(part.slice(1, -1).split(',')); parts.push(part.slice(1, -1).split(','));
} else { } else {
parts.push(part); parts.push(part);
@@ -300,28 +302,7 @@ function (angular, _, kbn) {
// Get items // Get items
if (parts.length === 4) { if (parts.length === 4) {
var params = { return this.itemFindQuery(template);
output: ['name', 'key_'],
sortfield: 'name',
};
if (template.group != '*' && template.group) {
params.group = template.group;
}
if (template.host != '*' && template.host) {
params.host = template.host;
}
if (template.application != '*' && template.application) {
params.application = template.app;
}
return this.performZabbixAPIRequest('item.get', params)
.then(function (result) {
return _.map(result, function (item) {
return {
text: item.key_,
expandable: false
};
});
});
} }
// Get applications // Get applications
else if (parts.length === 3) { else if (parts.length === 3) {
@@ -335,12 +316,11 @@ function (angular, _, kbn) {
} }
}; };
ZabbixAPIDatasource.prototype.appFindQuery = function(template) {
var params = { ZabbixAPIDatasource.prototype.itemFindQuery = function(template) {
output: ['name'],
sortfield: 'name'
};
var promises = []; var promises = [];
// Get groupids from names
if (template.group != '*' && template.group) { if (template.group != '*' && template.group) {
if (_.isArray(template.group)) { if (_.isArray(template.group)) {
_.each(template.group, function (group) { _.each(template.group, function (group) {
@@ -350,6 +330,79 @@ function (angular, _, kbn) {
promises.push(this.findZabbixGroup(template.group)); promises.push(this.findZabbixGroup(template.group));
} }
} }
// Get hostids from names
if (template.host != '*' && template.host) {
if (_.isArray(template.host)) {
_.each(template.host, function (host) {
promises.push(this.findZabbixHost(host));
}, this);
} else {
promises.push(this.findZabbixHost(template.host));
}
}
// Get applicationids from names
if (template.app != '*' && template.app) {
if (_.isArray(template.app)) {
_.each(template.app, function (app) {
promises.push(this.findZabbixHost(app));
}, this);
} else {
promises.push(this.findZabbixHost(template.app));
}
}
var self = this;
return $q.all(promises).then(function (results) {
results = _.flatten(results);
var groupids = _.map(_.filter(results, function (object) {
return object.groupid;
}), 'groupid');
var hostids = _.map(_.filter(results, function (object) {
return object.hostid;
}), 'hostid');
var applicationids = _.map(_.filter(results, function (object) {
return object.applicationid;
}), 'applicationid');
var params = {
output: ['name', 'key_'],
sortfield: 'name',
};
if (applicationids.length) {
params.applicationids = applicationids;
}
if (hostids.length) {
params.hostids = hostids;
} else if (groupids.length) {
params.groupids = groupids;
}
return self.performZabbixAPIRequest('item.get', params)
.then(function (result) {
return _.map(result, function (item) {
return {
text: item.key_,
expandable: false
};
});
});
});
};
ZabbixAPIDatasource.prototype.appFindQuery = function(template) {
var promises = [];
// Get groupids from names
if (template.group != '*' && template.group) {
if (_.isArray(template.group)) {
_.each(template.group, function (group) {
promises.push(this.findZabbixGroup(group));
}, this);
} else {
promises.push(this.findZabbixGroup(template.group));
}
}
// Get hostids from names
if (template.host != '*' && template.host) { if (template.host != '*' && template.host) {
if (_.isArray(template.host)) { if (_.isArray(template.host)) {
_.each(template.host, function (host) { _.each(template.host, function (host) {
@@ -373,9 +426,9 @@ function (angular, _, kbn) {
var params = { var params = {
output: ['name'] output: ['name']
} }
if (hostids) { if (hostids.length) {
params.hostids = hostids; params.hostids = hostids;
} else if (groupids) { } else if (groupids.length) {
params.groupids = groupids; params.groupids = groupids;
} }