Refacrot ZabbixCachingProxy. Now returns all data in async manner.

This commit is contained in:
Alexander Zobnin
2016-01-31 17:20:16 +03:00
parent 1de4b4bc5c
commit 5b3fe1559c
3 changed files with 144 additions and 97 deletions

View File

@@ -10,13 +10,16 @@ define([
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) { module.controller('ZabbixAPIQueryCtrl', function ($scope, $sce, $q, templateSrv) {
var zabbixCache = $scope.datasource.zabbixCache; var zabbixCache = $scope.datasource.zabbixCache;
$scope.init = function () { $scope.init = function () {
$scope.targetLetters = targetLetters; $scope.targetLetters = targetLetters;
if (!$scope.metric) {
$scope.metric = {}; $scope.metric = {};
}
// Load default values // Load default values
var targetDefaults = { var targetDefaults = {
@@ -49,18 +52,10 @@ define([
} }
// Load metrics from cache // Load metrics from cache
if (zabbixCache._initialized) { $scope.getMetricsFromCache().then(function() {
$scope.getMetricsFromCache();
$scope.initFilters(); $scope.initFilters();
//console.log("Cached", $scope.metric);
} else {
zabbixCache.refresh().then(function () {
$scope.getMetricsFromCache();
$scope.initFilters();
//console.log("From server", $scope.metric);
}); });
} }
}
else if ($scope.target.mode === 1) { else if ($scope.target.mode === 1) {
$scope.slaPropertyList = [ $scope.slaPropertyList = [
{name: "Status", property: "status"}, {name: "Status", property: "status"},
@@ -82,12 +77,20 @@ define([
$scope.getMetricsFromCache = function() { $scope.getMetricsFromCache = function() {
var item_type = $scope.editorModes[$scope.target.mode]; var item_type = $scope.editorModes[$scope.target.mode];
var promises = [
zabbixCache.getGroups(),
zabbixCache.getHosts(),
zabbixCache.getApplications(),
zabbixCache.getItems(item_type)
];
return $q.all(promises).then(function(results) {
$scope.metric = { $scope.metric = {
groupList: zabbixCache.getGroups(), groupList: results[0],
hostList: zabbixCache.getHosts(), hostList: results[1],
applicationList: zabbixCache.getApplications(), applicationList: results[2],
itemList: zabbixCache.getItems(item_type) itemList: results[3]
}; };
});
}; };
// Get list of metric names for bs-typeahead directive // Get list of metric names for bs-typeahead directive

View File

@@ -66,7 +66,15 @@ function (angular, _, utils) {
this.filterApplications = function(hostFilter) { this.filterApplications = function(hostFilter) {
var hosts = []; var hosts = [];
var apps = []; var apps = [];
var hostList = this.cache.getHosts();
var promises = [
this.cache.getHosts(),
this.cache.getApplications()
];
return $q.all(promises).then(function(results) {
var hostList = results[0];
var applicationList = results[1];
// Filter hosts by regex // Filter hosts by regex
if (utils.isRegex(hostFilter)) { if (utils.isRegex(hostFilter)) {
@@ -87,20 +95,29 @@ function (angular, _, utils) {
if (hosts) { if (hosts) {
var hostsids = _.map(hosts, 'hostid'); var hostsids = _.map(hosts, 'hostid');
apps = _.filter(this.cache.getApplications(), function (appObj) { apps = _.filter(applicationList, function (appObj) {
return _.intersection(hostsids, appObj.hosts).length; return _.intersection(hostsids, appObj.hosts).length;
}); });
} }
return apps; return apps;
});
}; };
this.filterItems = function (hostFilter, appFilter, showDisabledItems) { this.filterItems = function (hostFilter, appFilter, showDisabledItems) {
var hosts = []; var hosts = [];
var apps = []; var apps = [];
var items = []; var items = [];
var hostList = this.cache.getHosts();
var applicationList = this.cache.getApplications(); var promises = [
this.cache.getHosts(),
this.cache.getApplications(),
this.cache.getItems()
];
return $q.all(promises).then(function(results) {
var hostList = results[0];
var applicationList = results[1];
var cachedItems = results[2];
// Filter hosts by regex // Filter hosts by regex
if (utils.isRegex(hostFilter)) { if (utils.isRegex(hostFilter)) {
@@ -144,7 +161,7 @@ function (angular, _, utils) {
if (apps) { if (apps) {
var appids = _.flatten(_.map(apps, 'applicationids')); var appids = _.flatten(_.map(apps, 'applicationids'));
items = _.filter(this.cache.getItems(), function (itemObj) { items = _.filter(cachedItems, function (itemObj) {
return _.intersection(appids, itemObj.applications).length; return _.intersection(appids, itemObj.applications).length;
}); });
items = _.filter(items, function (itemObj) { items = _.filter(items, function (itemObj) {
@@ -157,6 +174,7 @@ function (angular, _, utils) {
} }
return items; return items;
});
}; };
/** /**

View File

@@ -56,38 +56,64 @@ function (angular, _, utils) {
p.getGroups = function() { p.getGroups = function() {
var self = this; var self = this;
if (this._groups) { if (this._groups) {
return $q.when(self._groups);
} else {
return this.refresh().then(function() { return this.refresh().then(function() {
return self._groups; return self._groups;
}); });
} }
return $q.when(this._groups);
}; };
p.getHosts = function() { p.getHosts = function() {
return this._hosts; var self = this;
if (this._hosts) {
return $q.when(self._hosts);
} else {
return this.refresh().then(function() {
return self._hosts;
});
}
}; };
p.getApplications = function() { p.getApplications = function() {
return this._applications; var self = this;
if (this._applications) {
return $q.when(self._applications);
} else {
return this.refresh().then(function() {
return self._applications;
});
}
}; };
p.getItems = function(type) { p.getItems = function(type) {
var self = this;
if (this._items) {
return $q.when(filterItems(self._items, type));
} else {
return this.refresh().then(function() {
return filterItems(self._items, type);
});
}
};
function filterItems(items, type) {
switch (type) { switch (type) {
case 'num': case 'num':
return _.filter(this._items, function(item) { return _.filter(items, function(item) {
return (item.value_type === '0' || return (item.value_type === '0' ||
item.value_type === '3'); item.value_type === '3');
}); });
case 'text': case 'text':
return _.filter(this._items, function(item) { return _.filter(items, function(item) {
return (item.value_type === '1' || return (item.value_type === '1' ||
item.value_type === '2' || item.value_type === '2' ||
item.value_type === '4'); item.value_type === '4');
}); });
default: default:
return this._items; return items;
}
} }
};
p.getHost = function(hostid) { p.getHost = function(hostid) {
return _.find(this._hosts, {'hostid': hostid}); return _.find(this._hosts, {'hostid': hostid});