Refactor: queryProcessor.service.
This commit is contained in:
@@ -146,12 +146,10 @@ class ZabbixAPIDatasource {
|
||||
}
|
||||
|
||||
queryNumericData(target, timeFrom, timeTo, useTrends) {
|
||||
// Build query in asynchronous manner
|
||||
return this.queryProcessor.build(target.group.filter,
|
||||
target.host.filter,
|
||||
target.application.filter,
|
||||
target.item.filter,
|
||||
'num')
|
||||
let options = {
|
||||
itemtype: 'num'
|
||||
};
|
||||
return this.queryProcessor.build(target, options)
|
||||
.then(items => {
|
||||
// Add hostname for items from multiple hosts
|
||||
var addHostName = utils.isRegex(target.host.filter);
|
||||
@@ -227,11 +225,10 @@ class ZabbixAPIDatasource {
|
||||
}
|
||||
|
||||
queryTextData(target, timeFrom, timeTo) {
|
||||
return this.queryProcessor.build(target.group.filter,
|
||||
target.host.filter,
|
||||
target.application.filter,
|
||||
target.item.filter,
|
||||
'text')
|
||||
let options = {
|
||||
itemtype: 'text'
|
||||
};
|
||||
return this.queryProcessor.build(target, options)
|
||||
.then(items => {
|
||||
if (items.length) {
|
||||
return this.zabbixAPI.getHistory(items, timeFrom, timeTo)
|
||||
|
||||
@@ -20,6 +20,7 @@ export class ZabbixQueryController extends QueryCtrl {
|
||||
|
||||
this.zabbix = this.datasource.zabbixAPI;
|
||||
this.cache = this.datasource.zabbixCache;
|
||||
this.queryProcessor = this.datasource.queryProcessor;
|
||||
this.$q = $q;
|
||||
|
||||
// Use custom format for template variables
|
||||
@@ -115,76 +116,47 @@ export class ZabbixQueryController extends QueryCtrl {
|
||||
}
|
||||
|
||||
suggestGroups() {
|
||||
var self = this;
|
||||
return this.cache.getGroups().then(groups => {
|
||||
self.metric.groupList = groups;
|
||||
return this.queryProcessor.getAllGroups()
|
||||
.then(groups => {
|
||||
this.metric.groupList = groups;
|
||||
return groups;
|
||||
});
|
||||
}
|
||||
|
||||
suggestHosts() {
|
||||
var self = this;
|
||||
var groupFilter = this.replaceTemplateVars(this.target.group.filter);
|
||||
return this.datasource.queryProcessor
|
||||
.filterGroups(self.metric.groupList, groupFilter)
|
||||
.then(groups => {
|
||||
var groupids = _.map(groups, 'groupid');
|
||||
return self.zabbix
|
||||
.getHosts(groupids)
|
||||
.then(hosts => {
|
||||
self.metric.hostList = hosts;
|
||||
return hosts;
|
||||
});
|
||||
});
|
||||
let groupFilter = this.replaceTemplateVars(this.target.group.filter);
|
||||
return this.queryProcessor.getAllHosts(groupFilter)
|
||||
.then(hosts => {
|
||||
this.metric.hostList = hosts;
|
||||
return hosts;
|
||||
});
|
||||
}
|
||||
|
||||
suggestApps() {
|
||||
var self = this;
|
||||
var hostFilter = this.replaceTemplateVars(this.target.host.filter);
|
||||
return this.datasource.queryProcessor
|
||||
.filterHosts(self.metric.hostList, hostFilter)
|
||||
.then(hosts => {
|
||||
var hostids = _.map(hosts, 'hostid');
|
||||
return self.zabbix
|
||||
.getApps(hostids)
|
||||
.then(apps => {
|
||||
return self.metric.appList = apps;
|
||||
});
|
||||
});
|
||||
let groupFilter = this.replaceTemplateVars(this.target.group.filter);
|
||||
let hostFilter = this.replaceTemplateVars(this.target.host.filter);
|
||||
return this.queryProcessor.getAllApps(groupFilter, hostFilter)
|
||||
.then(apps => {
|
||||
this.metric.appList = apps;
|
||||
return apps;
|
||||
});
|
||||
}
|
||||
|
||||
suggestItems(itemtype='num') {
|
||||
var self = this;
|
||||
var appFilter = this.replaceTemplateVars(this.target.application.filter);
|
||||
if (appFilter) {
|
||||
// Filter by applications
|
||||
return this.datasource.queryProcessor
|
||||
.filterApps(self.metric.appList, appFilter)
|
||||
.then(apps => {
|
||||
var appids = _.map(apps, 'applicationid');
|
||||
return self.zabbix
|
||||
.getItems(undefined, appids, itemtype)
|
||||
.then(items => {
|
||||
if (!self.target.options.showDisabledItems) {
|
||||
items = _.filter(items, {'status': '0'});
|
||||
}
|
||||
self.metric.itemList = items;
|
||||
return items;
|
||||
});
|
||||
});
|
||||
} else {
|
||||
// Return all items belonged to selected hosts
|
||||
var hostids = _.map(self.metric.hostList, 'hostid');
|
||||
return self.zabbix
|
||||
.getItems(hostids, undefined, itemtype)
|
||||
.then(items => {
|
||||
if (!self.target.options.showDisabledItems) {
|
||||
items = _.filter(items, {'status': '0'});
|
||||
}
|
||||
self.metric.itemList = items;
|
||||
return items;
|
||||
});
|
||||
}
|
||||
suggestItems(itemtype = 'num') {
|
||||
let groupFilter = this.replaceTemplateVars(this.target.group.filter);
|
||||
let hostFilter = this.replaceTemplateVars(this.target.host.filter);
|
||||
let appFilter = this.replaceTemplateVars(this.target.application.filter);
|
||||
let options = {
|
||||
itemtype: itemtype,
|
||||
showDisabledItems: this.target.options.showDisabledItems
|
||||
};
|
||||
|
||||
return this.queryProcessor
|
||||
.getAllItems(groupFilter, hostFilter, appFilter, options)
|
||||
.then(items => {
|
||||
this.metric.itemList = items;
|
||||
return items;
|
||||
});
|
||||
}
|
||||
|
||||
isRegex(str) {
|
||||
|
||||
@@ -2,197 +2,137 @@ import angular from 'angular';
|
||||
import _ from 'lodash';
|
||||
import * as utils from './utils';
|
||||
|
||||
/** @ngInject */
|
||||
angular.module('grafana.services').factory('QueryProcessor', function($q) {
|
||||
function QueryProcessorFactory() {
|
||||
|
||||
class QueryProcessor {
|
||||
constructor(zabbixCacheInstance) {
|
||||
this.cache = zabbixCacheInstance;
|
||||
this.$q = $q;
|
||||
}
|
||||
|
||||
initializeCache() {
|
||||
if (this.cache._initialized) {
|
||||
return Promise.resolve();
|
||||
} else {
|
||||
return this.cache.refresh();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Build query in asynchronous manner
|
||||
* Build query - convert target filters to array of Zabbix items
|
||||
*/
|
||||
build(groupFilter, hostFilter, appFilter, itemFilter, itemtype) {
|
||||
var self = this;
|
||||
if (this.cache._initialized) {
|
||||
return this.$q.when(self.buildFromCache(groupFilter, hostFilter, appFilter, itemFilter, itemtype));
|
||||
} else {
|
||||
return this.cache.refresh().then(function() {
|
||||
return self.buildFromCache(groupFilter, hostFilter, appFilter, itemFilter, itemtype);
|
||||
});
|
||||
build(target, options) {
|
||||
function getFiltersFromTarget(target) {
|
||||
let parts = ['group', 'host', 'application', 'item'];
|
||||
return _.map(parts, p => target[p].filter);
|
||||
}
|
||||
|
||||
return this.initializeCache()
|
||||
.then(() => {
|
||||
return this.getItems(...getFiltersFromTarget(target), options);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Build trigger query in asynchronous manner
|
||||
*/
|
||||
buildTriggerQuery(groupFilter, hostFilter, appFilter) {
|
||||
var self = this;
|
||||
if (this.cache._initialized) {
|
||||
return this.$q.when(self.buildTriggerQueryFromCache(groupFilter, hostFilter, appFilter));
|
||||
} else {
|
||||
return this.cache.refresh().then(function() {
|
||||
return self.buildTriggerQueryFromCache(groupFilter, hostFilter, appFilter);
|
||||
});
|
||||
}
|
||||
return this.initializeCache()
|
||||
.then(() => {
|
||||
return this.buildTriggerQueryFromCache(groupFilter, hostFilter, appFilter);
|
||||
});
|
||||
}
|
||||
|
||||
filterGroups(groups, groupFilter) {
|
||||
return this.$q.when(
|
||||
findByFilter(groups, groupFilter)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get list of host belonging to given groups.
|
||||
* @return list of hosts
|
||||
*/
|
||||
filterHosts(hosts, hostFilter) {
|
||||
return this.$q.when(
|
||||
findByFilter(hosts, hostFilter)
|
||||
);
|
||||
}
|
||||
|
||||
filterApps(apps, appFilter) {
|
||||
return this.$q.when(
|
||||
findByFilter(apps, appFilter)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build query - convert target filters to array of Zabbix items
|
||||
*/
|
||||
buildFromCache(groupFilter, hostFilter, appFilter, itemFilter, itemtype, showDisabledItems) {
|
||||
return this.getItems(groupFilter, hostFilter, appFilter, itemtype, showDisabledItems)
|
||||
.then(items => {
|
||||
return getByFilter(items, itemFilter);
|
||||
});
|
||||
}
|
||||
|
||||
getGroups() {
|
||||
getAllGroups() {
|
||||
return this.cache.getGroups();
|
||||
}
|
||||
|
||||
getGroups(groupFilter) {
|
||||
return this.getAllGroups()
|
||||
.then(groups => findByFilter(groups, groupFilter));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get list of host belonging to given groups.
|
||||
* @return list of hosts
|
||||
*/
|
||||
getHosts(groupFilter) {
|
||||
var self = this;
|
||||
return this.cache
|
||||
.getGroups()
|
||||
.then(groups => {
|
||||
return findByFilter(groups, groupFilter);
|
||||
})
|
||||
.then(groups => {
|
||||
var groupids = _.map(groups, 'groupid');
|
||||
return self.cache.getHosts(groupids);
|
||||
});
|
||||
getAllHosts(groupFilter) {
|
||||
return this.getGroups(groupFilter)
|
||||
.then(groups => {
|
||||
let groupids = _.map(groups, 'groupid');
|
||||
return this.cache.getHosts(groupids);
|
||||
});
|
||||
}
|
||||
|
||||
getHosts(groupFilter, hostFilter) {
|
||||
return this.getAllHosts(groupFilter)
|
||||
.then(hosts => findByFilter(hosts, hostFilter));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get list of applications belonging to given groups and hosts.
|
||||
* @return list of applications belonging to given hosts
|
||||
*/
|
||||
getApps(groupFilter, hostFilter) {
|
||||
var self = this;
|
||||
return this.getHosts(groupFilter)
|
||||
.then(hosts => {
|
||||
return findByFilter(hosts, hostFilter);
|
||||
})
|
||||
.then(hosts => {
|
||||
var hostids = _.map(hosts, 'hostid');
|
||||
return self.cache.getApps(hostids);
|
||||
});
|
||||
getAllApps(groupFilter, hostFilter) {
|
||||
return this.getHosts(groupFilter, hostFilter)
|
||||
.then(hosts => {
|
||||
let hostids = _.map(hosts, 'hostid');
|
||||
return this.cache.getApps(hostids);
|
||||
});
|
||||
}
|
||||
|
||||
getItems(groupFilter, hostFilter, appFilter, itemtype, showDisabledItems) {
|
||||
var self = this;
|
||||
return this.getHosts(groupFilter)
|
||||
.then(hosts => {
|
||||
return findByFilter(hosts, hostFilter);
|
||||
})
|
||||
.then(hosts => {
|
||||
var hostids = _.map(hosts, 'hostid');
|
||||
if (appFilter) {
|
||||
return self.cache
|
||||
.getApps(hostids)
|
||||
.then(apps => {
|
||||
// Use getByFilter for proper item filtering
|
||||
return getByFilter(apps, appFilter);
|
||||
});
|
||||
} else {
|
||||
return {
|
||||
appFilterEmpty: true,
|
||||
hostids: hostids
|
||||
};
|
||||
}
|
||||
})
|
||||
.then(apps => {
|
||||
if (apps.appFilterEmpty) {
|
||||
return self.cache
|
||||
.getItems(apps.hostids, undefined, itemtype)
|
||||
.then(items => {
|
||||
if (showDisabledItems) {
|
||||
items = _.filter(items, {'status': '0'});
|
||||
}
|
||||
return items;
|
||||
});
|
||||
} else {
|
||||
var appids = _.map(apps, 'applicationid');
|
||||
return self.cache
|
||||
.getItems(undefined, appids, itemtype)
|
||||
.then(items => {
|
||||
if (showDisabledItems) {
|
||||
items = _.filter(items, {'status': '0'});
|
||||
}
|
||||
return items;
|
||||
});
|
||||
}
|
||||
});
|
||||
getApps(groupFilter, hostFilter, appFilter) {
|
||||
return this.getHosts(groupFilter, hostFilter)
|
||||
.then(hosts => {
|
||||
let hostids = _.map(hosts, 'hostid');
|
||||
if (appFilter) {
|
||||
return this.cache.getApps(hostids)
|
||||
.then(apps => filterByQuery(apps, appFilter));
|
||||
} else {
|
||||
return {
|
||||
appFilterEmpty: true,
|
||||
hostids: hostids
|
||||
};
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
getAllItems(groupFilter, hostFilter, appFilter, options = {}) {
|
||||
return this.getApps(groupFilter, hostFilter, appFilter)
|
||||
.then(apps => {
|
||||
if (apps.appFilterEmpty) {
|
||||
return this.cache.getItems(apps.hostids, undefined, options.itemtype);
|
||||
} else {
|
||||
let appids = _.map(apps, 'applicationid');
|
||||
return this.cache.getItems(undefined, appids, options.itemtype);
|
||||
}
|
||||
})
|
||||
.then(items => {
|
||||
if (!options.showDisabledItems) {
|
||||
items = _.filter(items, {'status': '0'});
|
||||
}
|
||||
return items;
|
||||
});
|
||||
}
|
||||
|
||||
getItems(groupFilter, hostFilter, appFilter, itemFilter, options = {}) {
|
||||
return this.getAllItems(groupFilter, hostFilter, appFilter, options)
|
||||
.then(items => filterByQuery(items, itemFilter));
|
||||
}
|
||||
|
||||
/**
|
||||
* Build query - convert target filters to array of Zabbix items
|
||||
*/
|
||||
buildTriggerQueryFromCache(groupFilter, hostFilter, appFilter) {
|
||||
var promises = [
|
||||
this.cache.getGroups().then(function(groups) {
|
||||
return _.filter(groups, function(group) {
|
||||
if (utils.isRegex(groupFilter)) {
|
||||
return utils.buildRegex(groupFilter).test(group.name);
|
||||
} else {
|
||||
return group.name === groupFilter;
|
||||
}
|
||||
});
|
||||
}),
|
||||
this.getHosts(groupFilter).then(function(hosts) {
|
||||
return _.filter(hosts, function(host) {
|
||||
if (utils.isRegex(hostFilter)) {
|
||||
return utils.buildRegex(hostFilter).test(host.name);
|
||||
} else {
|
||||
return host.name === hostFilter;
|
||||
}
|
||||
});
|
||||
}),
|
||||
this.getApps(groupFilter, hostFilter).then(function(apps) {
|
||||
return _.filter(apps, function(app) {
|
||||
if (utils.isRegex(appFilter)) {
|
||||
return utils.buildRegex(appFilter).test(app.name);
|
||||
} else {
|
||||
return app.name === appFilter;
|
||||
}
|
||||
});
|
||||
})
|
||||
let promises = [
|
||||
this.getGroups(groupFilter),
|
||||
this.getHosts(groupFilter, hostFilter),
|
||||
this.getApps(groupFilter, hostFilter, appFilter)
|
||||
];
|
||||
|
||||
return this.$q.all(promises).then(function(results) {
|
||||
var filteredGroups = results[0];
|
||||
var filteredHosts = results[1];
|
||||
var filteredApps = results[2];
|
||||
var query = {};
|
||||
return Promise.all(promises)
|
||||
.then(results => {
|
||||
let filteredGroups = results[0];
|
||||
let filteredHosts = results[1];
|
||||
let filteredApps = results[2];
|
||||
let query = {};
|
||||
|
||||
if (appFilter) {
|
||||
query.applicationids = _.flatten(_.map(filteredApps, 'applicationid'));
|
||||
@@ -278,7 +218,11 @@ angular.module('grafana.services').factory('QueryProcessor', function($q) {
|
||||
}
|
||||
|
||||
return QueryProcessor;
|
||||
});
|
||||
}
|
||||
|
||||
angular
|
||||
.module('grafana.services')
|
||||
.factory('QueryProcessor', QueryProcessorFactory);
|
||||
|
||||
/**
|
||||
* Find group, host, app or item by given name.
|
||||
@@ -312,7 +256,7 @@ function filterByName(list, name) {
|
||||
}
|
||||
}
|
||||
|
||||
function findByRegex(list, regex) {
|
||||
function filterByRegex(list, regex) {
|
||||
var filterPattern = utils.buildRegex(regex);
|
||||
return _.filter(list, function (zbx_obj) {
|
||||
return filterPattern.test(zbx_obj.name);
|
||||
@@ -321,15 +265,15 @@ function findByRegex(list, regex) {
|
||||
|
||||
function findByFilter(list, filter) {
|
||||
if (utils.isRegex(filter)) {
|
||||
return findByRegex(list, filter);
|
||||
return filterByRegex(list, filter);
|
||||
} else {
|
||||
return findByName(list, filter);
|
||||
}
|
||||
}
|
||||
|
||||
function getByFilter(list, filter) {
|
||||
function filterByQuery(list, filter) {
|
||||
if (utils.isRegex(filter)) {
|
||||
return findByRegex(list, filter);
|
||||
return filterByRegex(list, filter);
|
||||
} else {
|
||||
return filterByName(list, filter);
|
||||
}
|
||||
|
||||
@@ -62,8 +62,6 @@ class TriggerPanelEditorCtrl {
|
||||
};
|
||||
_.defaults(this, scopeDefaults);
|
||||
|
||||
var self = this;
|
||||
|
||||
// Get zabbix data sources
|
||||
var datasources = _.filter(this.datasourceSrv.getMetricSources(), datasource => {
|
||||
return datasource.meta.id === 'alexanderzobnin-zabbix-datasource';
|
||||
@@ -75,10 +73,12 @@ class TriggerPanelEditorCtrl {
|
||||
this.panel.datasource = this.datasources[0];
|
||||
}
|
||||
// Load datasource
|
||||
this.datasourceSrv.get(this.panel.datasource).then(function (datasource) {
|
||||
self.datasource = datasource;
|
||||
self.initFilters();
|
||||
self.panelCtrl.refresh();
|
||||
this.datasourceSrv.get(this.panel.datasource)
|
||||
.then(datasource => {
|
||||
this.datasource = datasource;
|
||||
this.queryProcessor = datasource.queryProcessor;
|
||||
this.initFilters();
|
||||
this.panelCtrl.refresh();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -91,44 +91,30 @@ class TriggerPanelEditorCtrl {
|
||||
}
|
||||
|
||||
suggestGroups() {
|
||||
var self = this;
|
||||
return this.datasource.zabbixCache
|
||||
.getGroups()
|
||||
.then(groups => {
|
||||
self.metric.groupList = groups;
|
||||
return groups;
|
||||
});
|
||||
return this.queryProcessor.getAllGroups()
|
||||
.then(groups => {
|
||||
this.metric.groupList = groups;
|
||||
return groups;
|
||||
});
|
||||
}
|
||||
|
||||
suggestHosts() {
|
||||
var self = this;
|
||||
var groupFilter = this.datasource.replaceTemplateVars(this.panel.triggers.group.filter);
|
||||
return this.datasource.queryProcessor
|
||||
.filterGroups(self.metric.groupList, groupFilter)
|
||||
.then(groups => {
|
||||
var groupids = _.map(groups, 'groupid');
|
||||
return self.datasource.zabbixAPI
|
||||
.getHosts(groupids)
|
||||
.then(hosts => {
|
||||
self.metric.hostList = hosts;
|
||||
return hosts;
|
||||
});
|
||||
});
|
||||
let groupFilter = this.datasource.replaceTemplateVars(this.panel.triggers.group.filter);
|
||||
return this.queryProcessor.getAllHosts(groupFilter)
|
||||
.then(hosts => {
|
||||
this.metric.hostList = hosts;
|
||||
return hosts;
|
||||
});
|
||||
}
|
||||
|
||||
suggestApps() {
|
||||
var self = this;
|
||||
var hostFilter = this.datasource.replaceTemplateVars(this.panel.triggers.host.filter);
|
||||
return this.datasource.queryProcessor
|
||||
.filterHosts(self.metric.hostList, hostFilter)
|
||||
.then(hosts => {
|
||||
var hostids = _.map(hosts, 'hostid');
|
||||
return self.datasource.zabbixAPI
|
||||
.getApps(hostids)
|
||||
.then(apps => {
|
||||
return self.metric.appList = apps;
|
||||
});
|
||||
});
|
||||
let groupFilter = this.datasource.replaceTemplateVars(this.panel.triggers.group.filter);
|
||||
let hostFilter = this.datasource.replaceTemplateVars(this.panel.triggers.host.filter);
|
||||
return this.queryProcessor.getAllApps(groupFilter, hostFilter)
|
||||
.then(apps => {
|
||||
this.metric.appList = apps;
|
||||
return apps;
|
||||
});
|
||||
}
|
||||
|
||||
onVariableChange() {
|
||||
|
||||
Reference in New Issue
Block a user