Build query true QueryBuilder.
This commit is contained in:
@@ -2,19 +2,19 @@ define([
|
|||||||
'angular',
|
'angular',
|
||||||
'lodash',
|
'lodash',
|
||||||
'app/core/utils/datemath',
|
'app/core/utils/datemath',
|
||||||
|
'./queryBuilder',
|
||||||
'./directives',
|
'./directives',
|
||||||
'./zabbixAPI',
|
'./zabbixAPI',
|
||||||
'./utils',
|
|
||||||
'./helperFunctions',
|
'./helperFunctions',
|
||||||
'./zabbixCacheSrv',
|
'./zabbixCache',
|
||||||
'./queryCtrl'
|
'./queryCtrl'
|
||||||
],
|
],
|
||||||
function (angular, _, dateMath) {
|
function (angular, _, dateMath, QueryBuilder) {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
/** @ngInject */
|
/** @ngInject */
|
||||||
function ZabbixAPIDatasource(instanceSettings, $q, backendSrv, templateSrv, alertSrv,
|
function ZabbixAPIDatasource(instanceSettings, $q, templateSrv, alertSrv,
|
||||||
ZabbixAPI, Utils, zabbixHelperSrv, ZabbixCache) {
|
ZabbixAPI, zabbixHelperSrv, ZabbixCache) {
|
||||||
|
|
||||||
// General data source settings
|
// General data source settings
|
||||||
this.name = instanceSettings.name;
|
this.name = instanceSettings.name;
|
||||||
@@ -35,7 +35,13 @@ function (angular, _, dateMath) {
|
|||||||
|
|
||||||
// Initialize cache service
|
// Initialize cache service
|
||||||
this.zabbixCache = new ZabbixCache(this.zabbixAPI);
|
this.zabbixCache = new ZabbixCache(this.zabbixAPI);
|
||||||
console.log(this.zabbixCache);
|
|
||||||
|
// Initialize query builder
|
||||||
|
this.queryBuilder = new QueryBuilder(this.zabbixCache);
|
||||||
|
|
||||||
|
////////////////////////
|
||||||
|
// Datasource methods //
|
||||||
|
////////////////////////
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test connection to Zabbix API
|
* Test connection to Zabbix API
|
||||||
@@ -79,7 +85,7 @@ function (angular, _, dateMath) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calls for each panel in dashboard.
|
* Query panel data. Calls for each panel in dashboard.
|
||||||
*
|
*
|
||||||
* @param {Object} options Query options. Contains time range, targets
|
* @param {Object} options Query options. Contains time range, targets
|
||||||
* and other info.
|
* and other info.
|
||||||
@@ -112,112 +118,9 @@ function (angular, _, dateMath) {
|
|||||||
var itemFilter = templateSrv.replace(target.item.filter, options.scopedVars);
|
var itemFilter = templateSrv.replace(target.item.filter, options.scopedVars);
|
||||||
|
|
||||||
// Query numeric data
|
// Query numeric data
|
||||||
if (!target.mode) {
|
if (!target.mode || target.mode === 0) {
|
||||||
|
|
||||||
// Find items by item names and perform queries
|
var items = self.queryBuilder.build(groupFilter, hostFilter, appFilter, itemFilter);
|
||||||
var groups = [];
|
|
||||||
var hosts = [];
|
|
||||||
var apps = [];
|
|
||||||
var items = [];
|
|
||||||
|
|
||||||
if (target.host.isRegex) {
|
|
||||||
|
|
||||||
// Filter groups
|
|
||||||
if (target.group.isRegex) {
|
|
||||||
var groupPattern = Utils.buildRegex(groupFilter);
|
|
||||||
groups = _.filter(self.zabbixCache.getGroups(), function (groupObj) {
|
|
||||||
return groupPattern.test(groupObj.name);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
var findedGroup = _.find(self.zabbixCache.getGroups(), {'name': groupFilter});
|
|
||||||
if (findedGroup) {
|
|
||||||
groups.push(findedGroup);
|
|
||||||
} else {
|
|
||||||
groups = undefined;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (groups) {
|
|
||||||
var groupids = _.map(groups, 'groupid');
|
|
||||||
hosts = _.filter(self.zabbixCache.getHosts(), function (hostObj) {
|
|
||||||
return _.intersection(groupids, hostObj.groups).length;
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
// No groups finded
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Filter hosts
|
|
||||||
var hostPattern = Utils.buildRegex(hostFilter);
|
|
||||||
hosts = _.filter(hosts, function (hostObj) {
|
|
||||||
return hostPattern.test(hostObj.name);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
var findedHost = _.find(self.zabbixCache.getHosts(), {'name': hostFilter});
|
|
||||||
if (findedHost) {
|
|
||||||
hosts.push(findedHost);
|
|
||||||
} else {
|
|
||||||
// No hosts finded
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find items belongs to selected hosts
|
|
||||||
items = _.filter(self.zabbixCache.getItems(), function (itemObj) {
|
|
||||||
return _.contains(_.map(hosts, 'hostid'), itemObj.hostid);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (target.item.isRegex) {
|
|
||||||
|
|
||||||
// Filter applications
|
|
||||||
if (target.application.isRegex) {
|
|
||||||
var appPattern = Utils.buildRegex(appFilter);
|
|
||||||
apps = _.filter(self.zabbixCache.getApplications(), function (appObj) {
|
|
||||||
return appPattern.test(appObj.name);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
// Don't use application filter if it empty
|
|
||||||
else if (appFilter === "") {
|
|
||||||
apps = undefined;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
var findedApp = _.find(self.zabbixCache.getApplications(), {'name': appFilter});
|
|
||||||
if (findedApp) {
|
|
||||||
apps.push(findedApp);
|
|
||||||
} else {
|
|
||||||
// No applications finded
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find items belongs to selected applications
|
|
||||||
if (apps) {
|
|
||||||
var appids = _.flatten(_.map(apps, 'applicationids'));
|
|
||||||
items = _.filter(items, function (itemObj) {
|
|
||||||
return _.intersection(appids, itemObj.applications).length;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (items) {
|
|
||||||
var itemPattern = Utils.buildRegex(itemFilter);
|
|
||||||
items = _.filter(items, function (itemObj) {
|
|
||||||
return itemPattern.test(itemObj.name);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
// No items finded
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
items = _.filter(items, {'name': hostFilter});
|
|
||||||
if (!items.length) {
|
|
||||||
// No items finded
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set host as host name for each item
|
|
||||||
items = _.each(items, function (itemObj) {
|
|
||||||
itemObj.host = _.find(hosts, {'hostid': itemObj.hostid}).name;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Use alias only for single metric, otherwise use item names
|
// Use alias only for single metric, otherwise use item names
|
||||||
var alias;
|
var alias;
|
||||||
|
|||||||
@@ -1,15 +1,14 @@
|
|||||||
define([
|
define([
|
||||||
'angular',
|
'angular',
|
||||||
'lodash',
|
'lodash',
|
||||||
'./zabbixCacheSrv',
|
|
||||||
'./utils'
|
'./utils'
|
||||||
],
|
],
|
||||||
function (angular, _) {
|
function (angular, _, utils) {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var module = angular.module('grafana.services');
|
function QueryBuilder(zabbixCacheInstance) {
|
||||||
|
|
||||||
module.service('QueryBuilderSrv', function(ZabbixCache, Utils) {
|
this.cache = zabbixCacheInstance;
|
||||||
|
|
||||||
this.build = function (groupFilter, hostFilter, appFilter, itemFilter) {
|
this.build = function (groupFilter, hostFilter, appFilter, itemFilter) {
|
||||||
|
|
||||||
@@ -19,16 +18,16 @@ function (angular, _) {
|
|||||||
var apps = [];
|
var apps = [];
|
||||||
var items = [];
|
var items = [];
|
||||||
|
|
||||||
if (Utils.isRegex(hostFilter)) {
|
if (utils.isRegex(hostFilter)) {
|
||||||
|
|
||||||
// Filter groups
|
// Filter groups
|
||||||
if (Utils.isRegex(groupFilter)) {
|
if (utils.isRegex(groupFilter)) {
|
||||||
var groupPattern = Utils.buildRegex(groupFilter);
|
var groupPattern = utils.buildRegex(groupFilter);
|
||||||
groups = _.filter(ZabbixCache.getGroups(), function (groupObj) {
|
groups = _.filter(this.cache.getGroups(), function (groupObj) {
|
||||||
return groupPattern.test(groupObj.name);
|
return groupPattern.test(groupObj.name);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
var findedGroup = _.find(ZabbixCache.getGroups(), {'name': groupFilter});
|
var findedGroup = _.find(this.cache.getGroups(), {'name': groupFilter});
|
||||||
if (findedGroup) {
|
if (findedGroup) {
|
||||||
groups.push(findedGroup);
|
groups.push(findedGroup);
|
||||||
} else {
|
} else {
|
||||||
@@ -37,7 +36,7 @@ function (angular, _) {
|
|||||||
}
|
}
|
||||||
if (groups) {
|
if (groups) {
|
||||||
var groupids = _.map(groups, 'groupid');
|
var groupids = _.map(groups, 'groupid');
|
||||||
hosts = _.filter(ZabbixCache.getHosts(), function (hostObj) {
|
hosts = _.filter(this.cache.getHosts(), function (hostObj) {
|
||||||
return _.intersection(groupids, hostObj.groups).length;
|
return _.intersection(groupids, hostObj.groups).length;
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
@@ -46,12 +45,12 @@ function (angular, _) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Filter hosts
|
// Filter hosts
|
||||||
var hostPattern = Utils.buildRegex(hostFilter);
|
var hostPattern = utils.buildRegex(hostFilter);
|
||||||
hosts = _.filter(hosts, function (hostObj) {
|
hosts = _.filter(hosts, function (hostObj) {
|
||||||
return hostPattern.test(hostObj.name);
|
return hostPattern.test(hostObj.name);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
var findedHost = _.find(ZabbixCache.getHosts(), {'name': hostFilter});
|
var findedHost = _.find(this.cache.getHosts(), {'name': hostFilter});
|
||||||
if (findedHost) {
|
if (findedHost) {
|
||||||
hosts.push(findedHost);
|
hosts.push(findedHost);
|
||||||
} else {
|
} else {
|
||||||
@@ -61,16 +60,16 @@ function (angular, _) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Find items belongs to selected hosts
|
// Find items belongs to selected hosts
|
||||||
items = _.filter(ZabbixCache.getItems(), function (itemObj) {
|
items = _.filter(this.cache.getItems(), function (itemObj) {
|
||||||
return _.contains(_.map(hosts, 'hostid'), itemObj.hostid);
|
return _.contains(_.map(hosts, 'hostid'), itemObj.hostid);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (Utils.isRegex(itemFilter)) {
|
if (utils.isRegex(itemFilter)) {
|
||||||
|
|
||||||
// Filter applications
|
// Filter applications
|
||||||
if (Utils.isRegex(appFilter)) {
|
if (utils.isRegex(appFilter)) {
|
||||||
var appPattern = Utils.buildRegex(appFilter);
|
var appPattern = utils.buildRegex(appFilter);
|
||||||
apps = _.filter(ZabbixCache.getApplications(), function (appObj) {
|
apps = _.filter(this.cache.getApplications(), function (appObj) {
|
||||||
return appPattern.test(appObj.name);
|
return appPattern.test(appObj.name);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -79,7 +78,7 @@ function (angular, _) {
|
|||||||
apps = undefined;
|
apps = undefined;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
var findedApp = _.find(ZabbixCache.getApplications(), {'name': appFilter});
|
var findedApp = _.find(this.cache.getApplications(), {'name': appFilter});
|
||||||
if (findedApp) {
|
if (findedApp) {
|
||||||
apps.push(findedApp);
|
apps.push(findedApp);
|
||||||
} else {
|
} else {
|
||||||
@@ -97,7 +96,7 @@ function (angular, _) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (items) {
|
if (items) {
|
||||||
var itemPattern = Utils.buildRegex(itemFilter);
|
var itemPattern = utils.buildRegex(itemFilter);
|
||||||
items = _.filter(items, function (itemObj) {
|
items = _.filter(items, function (itemObj) {
|
||||||
return itemPattern.test(itemObj.name);
|
return itemPattern.test(itemObj.name);
|
||||||
});
|
});
|
||||||
@@ -118,40 +117,11 @@ function (angular, _) {
|
|||||||
itemObj.host = _.find(hosts, {'hostid': itemObj.hostid}).name;
|
itemObj.host = _.find(hosts, {'hostid': itemObj.hostid}).name;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Use alias only for single metric, otherwise use item names
|
return items;
|
||||||
var alias;
|
|
||||||
if (items.length === 1) {
|
|
||||||
alias = templateSrv.replace(alias, options.scopedVars);
|
|
||||||
}
|
|
||||||
|
|
||||||
var history;
|
|
||||||
if ((from < useTrendsFrom) && self.trends) {
|
|
||||||
// Use trends
|
|
||||||
var points = downsampleFunction ? downsampleFunction.value : "avg";
|
|
||||||
history = self.zabbixAPI.getTrends(items, from, to)
|
|
||||||
.then(_.bind(zabbixHelperSrv.handleTrendResponse, zabbixHelperSrv, items, alias, scale, points));
|
|
||||||
} else {
|
|
||||||
// Use history
|
|
||||||
history = self.zabbixAPI.getHistory(items, from, to)
|
|
||||||
.then(_.bind(zabbixHelperSrv.handleHistoryResponse, zabbixHelperSrv, items, alias, scale));
|
|
||||||
}
|
|
||||||
|
|
||||||
return history.then(function (timeseries) {
|
|
||||||
var timeseries_data = _.flatten(timeseries);
|
|
||||||
return _.map(timeseries_data, function (timeseries) {
|
|
||||||
|
|
||||||
// Series downsampling
|
|
||||||
if (timeseries.datapoints.length > options.maxDataPoints) {
|
|
||||||
var ms_interval = Math.floor((to - from) / options.maxDataPoints) * 1000;
|
|
||||||
var downsampleFunc = downsampleFunction ? downsampleFunction.value : "avg";
|
|
||||||
timeseries.datapoints = zabbixHelperSrv.downsampleSeries(timeseries.datapoints, to, ms_interval, downsampleFunc);
|
|
||||||
}
|
|
||||||
return timeseries;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
return itemFilter;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
});
|
}
|
||||||
|
|
||||||
|
return QueryBuilder;
|
||||||
|
|
||||||
});
|
});
|
||||||
@@ -3,13 +3,13 @@ define([
|
|||||||
'lodash',
|
'lodash',
|
||||||
'./utils'
|
'./utils'
|
||||||
],
|
],
|
||||||
function (angular, _) {
|
function (angular, _, Utils) {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var module = angular.module('grafana.controllers');
|
var module = angular.module('grafana.controllers');
|
||||||
var targetLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
var targetLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||||
|
|
||||||
module.controller('ZabbixAPIQueryCtrl', function ($scope, $sce, templateSrv, Utils) {
|
module.controller('ZabbixAPIQueryCtrl', function ($scope, $sce, templateSrv) {
|
||||||
|
|
||||||
var zabbixCache = $scope.datasource.zabbixCache;
|
var zabbixCache = $scope.datasource.zabbixCache;
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,10 @@
|
|||||||
define([
|
define([
|
||||||
'angular',
|
|
||||||
'lodash'
|
'lodash'
|
||||||
],
|
],
|
||||||
function (angular, _) {
|
function () {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var module = angular.module('grafana.services');
|
function utils() {
|
||||||
|
|
||||||
module.service('Utils', function() {
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Expand Zabbix item name
|
* Expand Zabbix item name
|
||||||
@@ -43,5 +40,7 @@ function (angular, _) {
|
|||||||
return new RegExp(pattern, flags);
|
return new RegExp(pattern, flags);
|
||||||
};
|
};
|
||||||
|
|
||||||
});
|
}
|
||||||
|
|
||||||
|
return new utils();
|
||||||
});
|
});
|
||||||
@@ -3,14 +3,14 @@ define([
|
|||||||
'lodash',
|
'lodash',
|
||||||
'./utils'
|
'./utils'
|
||||||
],
|
],
|
||||||
function (angular, _) {
|
function (angular, _, utils) {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var module = angular.module('grafana.services');
|
var module = angular.module('grafana.services');
|
||||||
|
|
||||||
// Use factory() instead service() for multiple datasources support.
|
// Use factory() instead service() for multiple datasources support.
|
||||||
// Each datasource instance must initialize its own cache.
|
// Each datasource instance must initialize its own cache.
|
||||||
module.factory('ZabbixCache', function($q, Utils) {
|
module.factory('ZabbixCache', function($q) {
|
||||||
|
|
||||||
function ZabbixCache(zabbixAPI, ttl) {
|
function ZabbixCache(zabbixAPI, ttl) {
|
||||||
var self = this;
|
var self = this;
|
||||||
@@ -105,7 +105,7 @@ function (angular, _) {
|
|||||||
return _.forEach(items, function(item) {
|
return _.forEach(items, function(item) {
|
||||||
item.applications = _.map(item.applications, 'applicationid');
|
item.applications = _.map(item.applications, 'applicationid');
|
||||||
item.item = item.name;
|
item.item = item.name;
|
||||||
item.name = Utils.expandItemName(item.item, item.key_);
|
item.name = utils.expandItemName(item.item, item.key_);
|
||||||
return item;
|
return item;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user