Merge branch 'develop'

This commit is contained in:
Alexander Zobnin
2016-04-12 16:25:21 +03:00
4 changed files with 48 additions and 42 deletions

View File

@@ -134,8 +134,9 @@ export class ZabbixAPIDatasource {
if (!target.mode || target.mode === 0) { if (!target.mode || target.mode === 0) {
// Build query in asynchronous manner // Build query in asynchronous manner
return self.queryProcessor.build(groupFilter, hostFilter, appFilter, itemFilter) return self.queryProcessor
.then(function(items) { .build(groupFilter, hostFilter, appFilter, itemFilter, 'num')
.then(items => {
// Add hostname for items from multiple hosts // Add hostname for items from multiple hosts
var addHostName = utils.isRegex(target.host.filter); var addHostName = utils.isRegex(target.host.filter);
var getHistory; var getHistory;
@@ -207,36 +208,40 @@ export class ZabbixAPIDatasource {
// Query text data // Query text data
else if (target.mode === 2) { else if (target.mode === 2) {
return self.queryProcessor.build(groupFilter, hostFilter, appFilter, itemFilter) return self.queryProcessor
.then(function(items) { .build(groupFilter, hostFilter, appFilter, itemFilter, 'text')
var deferred = self.q.defer(); .then(items => {
if (items.length) { if (items.length) {
self.zabbixAPI.getLastValue(items[0].itemid).then(function(lastvalue) { var textItemsPromises = _.map(items, item => {
return self.zabbixAPI.getLastValue(item.itemid);
});
return self.q.all(textItemsPromises)
.then(result => {
return _.map(result, (lastvalue, index) => {
var extractedValue;
if (target.textFilter) { if (target.textFilter) {
var text_extract_pattern = new RegExp(self.replaceTemplateVars(target.textFilter, options.scopedVars)); var text_extract_pattern = new RegExp(self.replaceTemplateVars(target.textFilter, options.scopedVars));
var result = text_extract_pattern.exec(lastvalue); extractedValue = text_extract_pattern.exec(lastvalue);
if (result) { if (extractedValue) {
if (target.useCaptureGroups) { if (target.useCaptureGroups) {
result = result[1]; extractedValue = extractedValue[1];
} else { } else {
result = result[0]; extractedValue = extractedValue[0];
} }
} }
deferred.resolve(result);
} else { } else {
deferred.resolve(lastvalue); extractedValue = lastvalue;
} }
});
} else {
deferred.resolve(null);
}
return deferred.promise.then(function(text) {
return { return {
target: target.item.name, target: items[index].name,
datapoints: [[text, to * 1000]] datapoints: [[extractedValue, to * 1000]]
}; };
}); });
}); });
} else {
return self.q.when([]);
}
});
} }
} }

View File

@@ -14,13 +14,13 @@ angular.module('grafana.services').factory('QueryProcessor', function($q) {
/** /**
* Build query in asynchronous manner * Build query in asynchronous manner
*/ */
build(groupFilter, hostFilter, appFilter, itemFilter) { build(groupFilter, hostFilter, appFilter, itemFilter, itemtype) {
var self = this; var self = this;
if (this.cache._initialized) { if (this.cache._initialized) {
return this.$q.when(self.buildFromCache(groupFilter, hostFilter, appFilter, itemFilter)); return this.$q.when(self.buildFromCache(groupFilter, hostFilter, appFilter, itemFilter, itemtype));
} else { } else {
return this.cache.refresh().then(function() { return this.cache.refresh().then(function() {
return self.buildFromCache(groupFilter, hostFilter, appFilter, itemFilter); return self.buildFromCache(groupFilter, hostFilter, appFilter, itemFilter, itemtype);
}); });
} }
} }
@@ -64,8 +64,8 @@ angular.module('grafana.services').factory('QueryProcessor', function($q) {
/** /**
* Build query - convert target filters to array of Zabbix items * Build query - convert target filters to array of Zabbix items
*/ */
buildFromCache(groupFilter, hostFilter, appFilter, itemFilter, showDisabledItems) { buildFromCache(groupFilter, hostFilter, appFilter, itemFilter, itemtype, showDisabledItems) {
return this.getItems(groupFilter, hostFilter, appFilter, showDisabledItems) return this.getItems(groupFilter, hostFilter, appFilter, itemtype, showDisabledItems)
.then(items => { .then(items => {
return getByFilter(items, itemFilter); return getByFilter(items, itemFilter);
}); });
@@ -108,7 +108,7 @@ angular.module('grafana.services').factory('QueryProcessor', function($q) {
}); });
} }
getItems(groupFilter, hostFilter, appFilter, showDisabledItems) { getItems(groupFilter, hostFilter, appFilter, itemtype, showDisabledItems) {
var self = this; var self = this;
return this.getHosts(groupFilter) return this.getHosts(groupFilter)
.then(hosts => { .then(hosts => {
@@ -133,7 +133,7 @@ angular.module('grafana.services').factory('QueryProcessor', function($q) {
.then(apps => { .then(apps => {
if (apps.appFilterEmpty) { if (apps.appFilterEmpty) {
return self.cache return self.cache
.getItems(apps.hostids, undefined) .getItems(apps.hostids, undefined, itemtype)
.then(items => { .then(items => {
if (showDisabledItems) { if (showDisabledItems) {
items = _.filter(items, {'status': '0'}); items = _.filter(items, {'status': '0'});
@@ -143,7 +143,7 @@ angular.module('grafana.services').factory('QueryProcessor', function($q) {
} else { } else {
var appids = _.map(apps, 'applicationid'); var appids = _.map(apps, 'applicationid');
return self.cache return self.cache
.getItems(undefined, appids) .getItems(undefined, appids, itemtype)
.then(items => { .then(items => {
if (showDisabledItems) { if (showDisabledItems) {
items = _.filter(items, {'status': '0'}); items = _.filter(items, {'status': '0'});

View File

@@ -152,7 +152,7 @@ function ZabbixAPIService($q, alertSrv, zabbixAPICoreService) {
* @param {String} itemtype 'num' or 'text' * @param {String} itemtype 'num' or 'text'
* @return {[type]} array of items * @return {[type]} array of items
*/ */
getItems(hostids, appids, itemtype='num') { getItems(hostids, appids, itemtype) {
var params = { var params = {
output: [ output: [
'name', 'key_', 'name', 'key_',
@@ -163,10 +163,7 @@ function ZabbixAPIService($q, alertSrv, zabbixAPICoreService) {
], ],
sortfield: 'name', sortfield: 'name',
webitems: true, webitems: true,
filter: { filter: {},
// Return only numeric items by default
value_type: [0, 3]
},
selectHosts: [ selectHosts: [
'hostid', 'hostid',
'name' 'name'
@@ -178,6 +175,10 @@ function ZabbixAPIService($q, alertSrv, zabbixAPICoreService) {
if (appids) { if (appids) {
params.applicationids = appids; params.applicationids = appids;
} }
if (itemtype === 'num') {
// Return only numeric metrics
params.filter.value_type = [0, 3];
}
if (itemtype === 'text') { if (itemtype === 'text') {
// Return only text metrics // Return only text metrics
params.filter.value_type = [1, 2, 4]; params.filter.value_type = [1, 2, 4];

View File

@@ -102,9 +102,9 @@ angular.module('grafana.services').factory('ZabbixCachingProxy', function($q, $i
}); });
} }
getItems(hostids, appids) { getItems(hostids, appids, itemtype) {
var self = this; var self = this;
return this.getItemsOnce(hostids, appids) return this.getItemsOnce(hostids, appids, itemtype)
.then(items => { .then(items => {
self._items = _.union(self._items, items); self._items = _.union(self._items, items);
return items; return items;