Merge branch 'cache-service' into grafana-3.0
This commit is contained in:
@@ -19,7 +19,7 @@ function (angular, _, dateMath, utils, metricFunctions) {
|
||||
|
||||
/** @ngInject */
|
||||
function ZabbixAPIDatasource(instanceSettings, $q, templateSrv, alertSrv, zabbixHelperSrv,
|
||||
ZabbixAPI, ZabbixCache, QueryProcessor, DataProcessingService) {
|
||||
ZabbixAPI, ZabbixCachingProxy, QueryProcessor, DataProcessingService) {
|
||||
|
||||
// General data source settings
|
||||
this.name = instanceSettings.name;
|
||||
@@ -39,7 +39,7 @@ function (angular, _, dateMath, utils, metricFunctions) {
|
||||
this.zabbixAPI = new ZabbixAPI(this.url, this.username, this.password, this.basicAuth, this.withCredentials);
|
||||
|
||||
// Initialize cache service
|
||||
this.zabbixCache = new ZabbixCache(this.zabbixAPI);
|
||||
this.zabbixCache = new ZabbixCachingProxy(this.zabbixAPI);
|
||||
|
||||
// Initialize query builder
|
||||
this.queryProcessor = new QueryProcessor(this.zabbixCache);
|
||||
@@ -140,7 +140,7 @@ function (angular, _, dateMath, utils, metricFunctions) {
|
||||
} else {
|
||||
|
||||
// Use history
|
||||
getHistory = self.zabbixAPI.getHistory(items, from, to).then(function(history) {
|
||||
getHistory = self.zabbixCache.getHistory(items, from, to).then(function(history) {
|
||||
return self.queryProcessor.handleHistory(history, addHostName);
|
||||
});
|
||||
}
|
||||
@@ -277,8 +277,6 @@ function (angular, _, dateMath, utils, metricFunctions) {
|
||||
* of metrics in "{metric1,metcic2,...,metricN}" format.
|
||||
*/
|
||||
this.metricFindQuery = function (query) {
|
||||
var metrics;
|
||||
|
||||
// Split query. Query structure:
|
||||
// group.host.app.item
|
||||
var parts = [];
|
||||
@@ -296,31 +294,36 @@ function (angular, _, dateMath, utils, metricFunctions) {
|
||||
|
||||
// Get items
|
||||
if (parts.length === 4) {
|
||||
var items = this.queryProcessor.filterItems(template.host, template.app, true);
|
||||
metrics = _.map(items, formatMetric);
|
||||
//var items = this.queryProcessor.filterItems(template.host, template.app, true);
|
||||
return this.queryProcessor.filterItems(template.host, template.app, true)
|
||||
.then(function(items) {
|
||||
return _.map(items, formatMetric);
|
||||
});
|
||||
}
|
||||
// Get applications
|
||||
else if (parts.length === 3) {
|
||||
var apps = this.queryProcessor.filterApplications(template.host);
|
||||
metrics = _.map(apps, formatMetric);
|
||||
return this.queryProcessor.filterApplications(template.host)
|
||||
.then(function(apps) {
|
||||
return _.map(apps, formatMetric);
|
||||
});
|
||||
}
|
||||
// Get hosts
|
||||
else if (parts.length === 2) {
|
||||
var hosts = this.queryProcessor.filterHosts(template.group);
|
||||
metrics = _.map(hosts, formatMetric);
|
||||
return this.queryProcessor.filterHosts(template.group)
|
||||
.then(function(hosts) {
|
||||
return _.map(hosts, formatMetric);
|
||||
});
|
||||
}
|
||||
// Get groups
|
||||
else if (parts.length === 1) {
|
||||
metrics = _.map(this.zabbixCache.getGroups(template.group), formatMetric);
|
||||
return this.zabbixCache.getGroups(template.group).then(function(groups) {
|
||||
return _.map(groups, formatMetric);
|
||||
});
|
||||
}
|
||||
// Return empty object for invalid request
|
||||
else {
|
||||
var d = $q.defer();
|
||||
d.resolve([]);
|
||||
return d.promise;
|
||||
return $q.when([]);
|
||||
}
|
||||
|
||||
return $q.when(metrics);
|
||||
};
|
||||
|
||||
function formatMetric(metricObj) {
|
||||
|
||||
@@ -10,13 +10,16 @@ define([
|
||||
var module = angular.module('grafana.controllers');
|
||||
var targetLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
|
||||
module.controller('ZabbixAPIQueryCtrl', function ($scope, $sce, templateSrv) {
|
||||
module.controller('ZabbixAPIQueryCtrl', function ($scope, $sce, $q, templateSrv) {
|
||||
|
||||
var zabbixCache = $scope.datasource.zabbixCache;
|
||||
|
||||
$scope.init = function () {
|
||||
$scope.targetLetters = targetLetters;
|
||||
|
||||
if (!$scope.metric) {
|
||||
$scope.metric = {};
|
||||
}
|
||||
|
||||
// Load default values
|
||||
var targetDefaults = {
|
||||
@@ -49,18 +52,10 @@ define([
|
||||
}
|
||||
|
||||
// Load metrics from cache
|
||||
if (zabbixCache._initialized) {
|
||||
$scope.getMetricsFromCache();
|
||||
$scope.getMetricsFromCache().then(function() {
|
||||
$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) {
|
||||
$scope.slaPropertyList = [
|
||||
{name: "Status", property: "status"},
|
||||
@@ -75,19 +70,27 @@ define([
|
||||
};
|
||||
|
||||
$scope.initFilters = function () {
|
||||
$scope.metric.filteredHosts = $scope.filterHosts();
|
||||
$scope.metric.filteredApplications = $scope.filterApplications();
|
||||
$scope.metric.filteredItems = $scope.filterItems();
|
||||
$scope.filterHosts();
|
||||
$scope.filterApplications();
|
||||
$scope.filterItems();
|
||||
};
|
||||
|
||||
$scope.getMetricsFromCache = function () {
|
||||
$scope.getMetricsFromCache = function() {
|
||||
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 = {
|
||||
groupList: zabbixCache.getGroups(),
|
||||
hostList: zabbixCache.getHosts(),
|
||||
applicationList: zabbixCache.getApplications(),
|
||||
itemList: zabbixCache.getItems(item_type)
|
||||
groupList: results[0],
|
||||
hostList: results[1],
|
||||
applicationList: results[2],
|
||||
itemList: results[3]
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
// Get list of metric names for bs-typeahead directive
|
||||
@@ -102,130 +105,26 @@ define([
|
||||
$scope.getItemNames = _.partial(getMetricNames, $scope, 'filteredItems');
|
||||
|
||||
$scope.filterHosts = function () {
|
||||
var group = $scope.target.group;
|
||||
var groups = [];
|
||||
var hosts = [];
|
||||
|
||||
// Filter groups by regex
|
||||
if (group.isRegex) {
|
||||
var filterPattern = Utils.buildRegex(group.filter);
|
||||
groups = _.filter($scope.metric.groupList, function (groupObj) {
|
||||
return filterPattern.test(groupObj.name);
|
||||
var groupFilter = templateSrv.replace($scope.target.group.filter);
|
||||
$scope.datasource.queryProcessor.filterHosts(groupFilter).then(function(hosts) {
|
||||
$scope.metric.filteredHosts = hosts;
|
||||
});
|
||||
}
|
||||
// Find hosts in selected group
|
||||
else {
|
||||
var finded = _.find($scope.metric.groupList, {'name': group.filter});
|
||||
if (finded) {
|
||||
groups.push(finded);
|
||||
} else {
|
||||
groups = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
if (groups) {
|
||||
var groupids = _.map(groups, 'groupid');
|
||||
hosts = _.filter($scope.metric.hostList, function (hostObj) {
|
||||
return _.intersection(groupids, hostObj.groups).length;
|
||||
});
|
||||
}
|
||||
return hosts;
|
||||
};
|
||||
|
||||
$scope.filterApplications = function () {
|
||||
var host = $scope.target.host;
|
||||
var hosts = [];
|
||||
var apps = [];
|
||||
|
||||
// Filter hosts by regex
|
||||
if (host.isRegex) {
|
||||
var filterPattern = Utils.buildRegex(host.filter);
|
||||
hosts = _.filter($scope.metric.hostList, function (hostObj) {
|
||||
return filterPattern.test(hostObj.name);
|
||||
var hostFilter = templateSrv.replace($scope.target.host.filter);
|
||||
$scope.datasource.queryProcessor.filterApplications(hostFilter).then(function(apps) {
|
||||
$scope.metric.filteredApplications = apps;
|
||||
});
|
||||
}
|
||||
// Find applications in selected host
|
||||
else {
|
||||
var finded = _.find($scope.metric.hostList, {'name': host.filter});
|
||||
if (finded) {
|
||||
hosts.push(finded);
|
||||
} else {
|
||||
hosts = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
if (hosts) {
|
||||
var hostsids = _.map(hosts, 'hostid');
|
||||
apps = _.filter($scope.metric.applicationList, function (appObj) {
|
||||
return _.intersection(hostsids, appObj.hosts).length;
|
||||
});
|
||||
}
|
||||
|
||||
return apps;
|
||||
};
|
||||
|
||||
$scope.filterItems = function () {
|
||||
var app = $scope.target.application;
|
||||
var host = $scope.target.host;
|
||||
var hosts = [];
|
||||
var apps = [];
|
||||
var items = [];
|
||||
|
||||
// Filter hosts by regex
|
||||
if (host.isRegex) {
|
||||
var hostFilterPattern = Utils.buildRegex(host.filter);
|
||||
hosts = _.filter($scope.metric.hostList, function (hostObj) {
|
||||
return hostFilterPattern.test(hostObj.name);
|
||||
var hostFilter = templateSrv.replace($scope.target.host.filter);
|
||||
var appFilter = templateSrv.replace($scope.target.application.filter);
|
||||
$scope.datasource.queryProcessor.filterItems(hostFilter, appFilter, $scope.target.showDisabledItems)
|
||||
.then(function(items) {
|
||||
$scope.metric.filteredItems = items;
|
||||
});
|
||||
}
|
||||
else {
|
||||
var findedHosts = _.find($scope.metric.hostList, {'name': host.filter});
|
||||
if (findedHosts) {
|
||||
hosts.push(findedHosts);
|
||||
} else {
|
||||
hosts = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
// Filter applications by regex
|
||||
if (app.isRegex) {
|
||||
var filterPattern = Utils.buildRegex(app.filter);
|
||||
apps = _.filter($scope.metric.applicationList, function (appObj) {
|
||||
return filterPattern.test(appObj.name);
|
||||
});
|
||||
}
|
||||
// Find items in selected application
|
||||
else if (app.filter) {
|
||||
var finded = _.find($scope.metric.applicationList, {'name': app.filter});
|
||||
if (finded) {
|
||||
apps.push(finded);
|
||||
} else {
|
||||
apps = undefined;
|
||||
}
|
||||
} else {
|
||||
apps = undefined;
|
||||
if (hosts) {
|
||||
items = _.filter($scope.metric.itemList, function (itemObj) {
|
||||
return _.find(hosts, {'hostid': itemObj.hostid });
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (apps) {
|
||||
var appids = _.flatten(_.map(apps, 'applicationids'));
|
||||
items = _.filter($scope.metric.itemList, function (itemObj) {
|
||||
return _.intersection(appids, itemObj.applications).length;
|
||||
});
|
||||
items = _.filter(items, function (itemObj) {
|
||||
return _.find(hosts, {'hostid': itemObj.hostid });
|
||||
});
|
||||
}
|
||||
|
||||
if (!$scope.target.showDisabledItems) {
|
||||
items = _.filter(items, {'status': '0'});
|
||||
}
|
||||
|
||||
return items;
|
||||
};
|
||||
|
||||
$scope.onTargetPartChange = function (targetPart) {
|
||||
@@ -236,21 +135,21 @@ define([
|
||||
|
||||
// Handle group blur and filter hosts
|
||||
$scope.onGroupBlur = function() {
|
||||
$scope.metric.filteredHosts = $scope.filterHosts();
|
||||
$scope.filterHosts();
|
||||
$scope.parseTarget();
|
||||
$scope.get_data();
|
||||
};
|
||||
|
||||
// Handle host blur and filter applications
|
||||
$scope.onHostBlur = function() {
|
||||
$scope.metric.filteredApplications = $scope.filterApplications();
|
||||
$scope.filterApplications();
|
||||
$scope.parseTarget();
|
||||
$scope.get_data();
|
||||
};
|
||||
|
||||
// Handle application blur and filter items
|
||||
$scope.onApplicationBlur = function() {
|
||||
$scope.metric.filteredItems = $scope.filterItems();
|
||||
$scope.filterItems();
|
||||
$scope.parseTarget();
|
||||
$scope.get_data();
|
||||
};
|
||||
|
||||
@@ -31,8 +31,8 @@ function (angular, _, utils) {
|
||||
this.filterHosts = function(groupFilter) {
|
||||
var groups = [];
|
||||
var hosts = [];
|
||||
var groupList = self.cache.getGroups();
|
||||
|
||||
return self.cache.getGroups().then(function(groupList) {
|
||||
// Filter groups by regex
|
||||
if (utils.isRegex(groupFilter)) {
|
||||
var filterPattern = utils.buildRegex(groupFilter);
|
||||
@@ -52,17 +52,29 @@ function (angular, _, utils) {
|
||||
|
||||
if (groups) {
|
||||
var groupids = _.map(groups, 'groupid');
|
||||
hosts = _.filter(self.cache.getHosts(), function (hostObj) {
|
||||
return self.cache.getHosts().then(function(hosts) {
|
||||
return _.filter(hosts, function (hostObj) {
|
||||
return _.intersection(groupids, hostObj.groups).length;
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
return hosts;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
this.filterApplications = function(hostFilter) {
|
||||
var hosts = [];
|
||||
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
|
||||
if (utils.isRegex(hostFilter)) {
|
||||
@@ -83,20 +95,29 @@ function (angular, _, utils) {
|
||||
|
||||
if (hosts) {
|
||||
var hostsids = _.map(hosts, 'hostid');
|
||||
apps = _.filter(this.cache.getApplications(), function (appObj) {
|
||||
apps = _.filter(applicationList, function (appObj) {
|
||||
return _.intersection(hostsids, appObj.hosts).length;
|
||||
});
|
||||
}
|
||||
|
||||
return apps;
|
||||
});
|
||||
};
|
||||
|
||||
this.filterItems = function (hostFilter, appFilter, showDisabledItems) {
|
||||
var hosts = [];
|
||||
var apps = [];
|
||||
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
|
||||
if (utils.isRegex(hostFilter)) {
|
||||
@@ -132,7 +153,7 @@ function (angular, _, utils) {
|
||||
} else {
|
||||
apps = undefined;
|
||||
if (hosts) {
|
||||
items = _.filter(this.cache.getItems(), function (itemObj) {
|
||||
items = _.filter(cachedItems, function (itemObj) {
|
||||
return _.find(hosts, {'hostid': itemObj.hostid });
|
||||
});
|
||||
}
|
||||
@@ -140,7 +161,7 @@ function (angular, _, utils) {
|
||||
|
||||
if (apps) {
|
||||
var appids = _.flatten(_.map(apps, 'applicationids'));
|
||||
items = _.filter(this.cache.getItems(), function (itemObj) {
|
||||
items = _.filter(cachedItems, function (itemObj) {
|
||||
return _.intersection(appids, itemObj.applications).length;
|
||||
});
|
||||
items = _.filter(items, function (itemObj) {
|
||||
@@ -153,6 +174,7 @@ function (angular, _, utils) {
|
||||
}
|
||||
|
||||
return items;
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -165,17 +187,29 @@ function (angular, _, utils) {
|
||||
var hosts = [];
|
||||
var apps = [];
|
||||
var items = [];
|
||||
var promises = [
|
||||
this.cache.getGroups(),
|
||||
this.cache.getHosts(),
|
||||
this.cache.getApplications(),
|
||||
this.cache.getItems()
|
||||
];
|
||||
|
||||
return $q.all(promises).then(function(results) {
|
||||
var cachedGroups = results[0];
|
||||
var cachedHosts = results[1];
|
||||
var cachedApps = results[2];
|
||||
var cachedItems = results[3];
|
||||
|
||||
if (utils.isRegex(hostFilter)) {
|
||||
|
||||
// Filter groups
|
||||
if (utils.isRegex(groupFilter)) {
|
||||
var groupPattern = utils.buildRegex(groupFilter);
|
||||
groups = _.filter(this.cache.getGroups(), function (groupObj) {
|
||||
groups = _.filter(cachedGroups, function (groupObj) {
|
||||
return groupPattern.test(groupObj.name);
|
||||
});
|
||||
} else {
|
||||
var findedGroup = _.find(this.cache.getGroups(), {'name': groupFilter});
|
||||
var findedGroup = _.find(cachedGroups, {'name': groupFilter});
|
||||
if (findedGroup) {
|
||||
groups.push(findedGroup);
|
||||
} else {
|
||||
@@ -184,7 +218,7 @@ function (angular, _, utils) {
|
||||
}
|
||||
if (groups) {
|
||||
var groupids = _.map(groups, 'groupid');
|
||||
hosts = _.filter(this.cache.getHosts(), function (hostObj) {
|
||||
hosts = _.filter(cachedHosts, function (hostObj) {
|
||||
return _.intersection(groupids, hostObj.groups).length;
|
||||
});
|
||||
} else {
|
||||
@@ -198,7 +232,7 @@ function (angular, _, utils) {
|
||||
return hostPattern.test(hostObj.name);
|
||||
});
|
||||
} else {
|
||||
var findedHost = _.find(this.cache.getHosts(), {'name': hostFilter});
|
||||
var findedHost = _.find(cachedHosts, {'name': hostFilter});
|
||||
if (findedHost) {
|
||||
hosts.push(findedHost);
|
||||
} else {
|
||||
@@ -208,7 +242,7 @@ function (angular, _, utils) {
|
||||
}
|
||||
|
||||
// Find items belongs to selected hosts
|
||||
items = _.filter(this.cache.getItems(), function (itemObj) {
|
||||
items = _.filter(cachedItems, function (itemObj) {
|
||||
return _.contains(_.map(hosts, 'hostid'), itemObj.hostid);
|
||||
});
|
||||
|
||||
@@ -217,7 +251,7 @@ function (angular, _, utils) {
|
||||
// Filter applications
|
||||
if (utils.isRegex(appFilter)) {
|
||||
var appPattern = utils.buildRegex(appFilter);
|
||||
apps = _.filter(this.cache.getApplications(), function (appObj) {
|
||||
apps = _.filter(cachedApps, function (appObj) {
|
||||
return appPattern.test(appObj.name);
|
||||
});
|
||||
}
|
||||
@@ -226,7 +260,7 @@ function (angular, _, utils) {
|
||||
apps = undefined;
|
||||
}
|
||||
else {
|
||||
var findedApp = _.find(this.cache.getApplications(), {'name': appFilter});
|
||||
var findedApp = _.find(cachedApps, {'name': appFilter});
|
||||
if (findedApp) {
|
||||
apps.push(findedApp);
|
||||
} else {
|
||||
@@ -266,6 +300,7 @@ function (angular, _, utils) {
|
||||
});
|
||||
|
||||
return items;
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -8,6 +8,11 @@ function (angular, _) {
|
||||
|
||||
var module = angular.module('grafana.services');
|
||||
|
||||
/**
|
||||
* Zabbix API Wrapper.
|
||||
* Creates Zabbix API instance with given parameters (url, credentials and other).
|
||||
* Wraps API calls and provides high-level methods.
|
||||
*/
|
||||
module.factory('ZabbixAPI', function($q, backendSrv, ZabbixAPIService) {
|
||||
|
||||
// Initialize Zabbix API.
|
||||
@@ -15,12 +20,14 @@ function (angular, _) {
|
||||
this.url = api_url;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.auth = null;
|
||||
this.auth = "";
|
||||
|
||||
this.requestOptions = {
|
||||
basicAuth: basicAuth,
|
||||
withCredentials: withCredentials
|
||||
};
|
||||
|
||||
this.loginPromise = null;
|
||||
}
|
||||
|
||||
var p = ZabbixAPI.prototype;
|
||||
@@ -31,31 +38,49 @@ function (angular, _) {
|
||||
|
||||
p.request = function(method, params) {
|
||||
var self = this;
|
||||
if (this.auth) {
|
||||
return ZabbixAPIService._request(this.url, method, params, this.requestOptions, this.auth)
|
||||
return ZabbixAPIService.request(this.url, method, params, this.requestOptions, this.auth)
|
||||
.then(function(result) {
|
||||
return result;
|
||||
},
|
||||
|
||||
// Handle errors
|
||||
function(error) {
|
||||
if (error.message === "Session terminated, re-login, please.") {
|
||||
return ZabbixAPIService.login(self.url, self.username, self.password, self.requestOptions)
|
||||
.then(function(auth) {
|
||||
self.auth = auth;
|
||||
return ZabbixAPIService._request(self.url, method, params, self.requestOptions, self.auth);
|
||||
if (isAuthError(error.data)) {
|
||||
return self.loginOnce().then(function() {
|
||||
return self.request(method, params);
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
function isAuthError(message) {
|
||||
return (
|
||||
message === "Session terminated, re-login, please." ||
|
||||
message === "Not authorised." ||
|
||||
message === "Not authorized."
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* When API unauthenticated or auth token expired each request produce login()
|
||||
* call. But auth token is common to all requests. This function wraps login() method
|
||||
* and call it once. If login() already called just wait for it (return its promise).
|
||||
* @return login promise
|
||||
*/
|
||||
p.loginOnce = function() {
|
||||
var self = this;
|
||||
var deferred = $q.defer();
|
||||
if (!self.loginPromise) {
|
||||
self.loginPromise = deferred.promise;
|
||||
self.login().then(function(auth) {
|
||||
self.loginPromise = null;
|
||||
self.auth = auth;
|
||||
deferred.resolve(auth);
|
||||
});
|
||||
} else {
|
||||
|
||||
// Login first
|
||||
return ZabbixAPIService.login(this.url, this.username, this.password, this.requestOptions)
|
||||
.then(function(auth) {
|
||||
self.auth = auth;
|
||||
return ZabbixAPIService._request(self.url, method, params, self.requestOptions, self.auth);
|
||||
});
|
||||
return self.loginPromise;
|
||||
}
|
||||
return deferred.promise;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -16,7 +16,8 @@ function (angular) {
|
||||
* Request data from Zabbix API
|
||||
* @return {object} response.result
|
||||
*/
|
||||
this._request = function(api_url, method, params, options, auth) {
|
||||
this.request = function(api_url, method, params, options, auth) {
|
||||
var deferred = $q.defer();
|
||||
var requestData = {
|
||||
jsonrpc: '2.0',
|
||||
method: method,
|
||||
@@ -24,8 +25,12 @@ function (angular) {
|
||||
id: 1
|
||||
};
|
||||
|
||||
if (auth === "") {
|
||||
// Reject immediately if not authenticated
|
||||
deferred.reject({data: "Not authorised."});
|
||||
return deferred.promise;
|
||||
} else if (auth) {
|
||||
// Set auth parameter only if it needed
|
||||
if (auth) {
|
||||
requestData.auth = auth;
|
||||
}
|
||||
|
||||
@@ -46,19 +51,20 @@ function (angular) {
|
||||
requestOptions.headers.Authorization = options.basicAuth;
|
||||
}
|
||||
|
||||
return backendSrv.datasourceRequest(requestOptions).then(function (response) {
|
||||
backendSrv.datasourceRequest(requestOptions).then(function (response) {
|
||||
// General connection issues
|
||||
if (!response.data) {
|
||||
return [];
|
||||
deferred.reject(response);
|
||||
}
|
||||
|
||||
// Handle Zabbix API errors
|
||||
else if (response.data.error) {
|
||||
throw new ZabbixException(response.data.error);
|
||||
deferred.reject(response.data.error);
|
||||
}
|
||||
|
||||
return response.data.result;
|
||||
deferred.resolve(response.data.result);
|
||||
});
|
||||
return deferred.promise;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -70,7 +76,7 @@ function (angular) {
|
||||
user: username,
|
||||
password: password
|
||||
};
|
||||
return this._request(api_url, 'user.login', params, options, null);
|
||||
return this.request(api_url, 'user.login', params, options, null);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -78,7 +84,7 @@ function (angular) {
|
||||
* Matches the version of Zabbix starting from Zabbix 2.0.4
|
||||
*/
|
||||
this.getVersion = function(api_url, options) {
|
||||
return this._request(api_url, 'apiinfo.version', [], options);
|
||||
return this.request(api_url, 'apiinfo.version', [], options);
|
||||
};
|
||||
|
||||
});
|
||||
|
||||
@@ -10,9 +10,9 @@ function (angular, _, utils) {
|
||||
|
||||
// Use factory() instead service() for multiple datasources support.
|
||||
// Each datasource instance must initialize its own cache.
|
||||
module.factory('ZabbixCache', function($q) {
|
||||
module.factory('ZabbixCachingProxy', function($q) {
|
||||
|
||||
function ZabbixCache(zabbixAPI, ttl) {
|
||||
function ZabbixCachingProxy(zabbixAPI, ttl) {
|
||||
this.zabbixAPI = zabbixAPI;
|
||||
this.ttl = ttl;
|
||||
|
||||
@@ -21,14 +21,23 @@ function (angular, _, utils) {
|
||||
this._hosts = undefined;
|
||||
this._applications = undefined;
|
||||
this._items = undefined;
|
||||
this.storage = {
|
||||
history: {},
|
||||
trends: {}
|
||||
};
|
||||
|
||||
// Check is a service initialized or not
|
||||
this._initialized = undefined;
|
||||
|
||||
this.refreshPromise = false;
|
||||
|
||||
// Wrap _refresh() method to call it once.
|
||||
this.refresh = callOnce(p._refresh, this.refreshPromise);
|
||||
}
|
||||
|
||||
var p = ZabbixCache.prototype;
|
||||
var p = ZabbixCachingProxy.prototype;
|
||||
|
||||
p.refresh = function () {
|
||||
p._refresh = function() {
|
||||
var self = this;
|
||||
var promises = [
|
||||
this.zabbixAPI.getGroups(),
|
||||
@@ -37,7 +46,7 @@ function (angular, _, utils) {
|
||||
this.zabbixAPI.getItems()
|
||||
];
|
||||
|
||||
return $q.all(promises).then(function (results) {
|
||||
return $q.all(promises).then(function(results) {
|
||||
if (results.length) {
|
||||
self._groups = results[0];
|
||||
self._hosts = convertHosts(results[1]);
|
||||
@@ -49,33 +58,107 @@ function (angular, _, utils) {
|
||||
};
|
||||
|
||||
p.getGroups = function() {
|
||||
return this._groups;
|
||||
var self = this;
|
||||
if (this._groups) {
|
||||
return $q.when(self._groups);
|
||||
} else {
|
||||
return this.refresh().then(function() {
|
||||
return self._groups;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
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() {
|
||||
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) {
|
||||
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) {
|
||||
case 'num':
|
||||
return _.filter(this._items, function(item) {
|
||||
return _.filter(items, function(item) {
|
||||
return (item.value_type === '0' ||
|
||||
item.value_type === '3');
|
||||
});
|
||||
case 'text':
|
||||
return _.filter(this._items, function(item) {
|
||||
return _.filter(items, function(item) {
|
||||
return (item.value_type === '1' ||
|
||||
item.value_type === '2' ||
|
||||
item.value_type === '4');
|
||||
});
|
||||
default:
|
||||
return this._items;
|
||||
return items;
|
||||
}
|
||||
}
|
||||
|
||||
p.getHistory = function(items, time_from, time_till) {
|
||||
var itemids = _.map(arguments[0], 'itemid');
|
||||
var stamp = itemids.join() + arguments[1] + arguments[2];
|
||||
//console.log(arguments, stamp);
|
||||
return this.zabbixAPI.getHistory(items, time_from, time_till);
|
||||
};
|
||||
|
||||
p.getHistory_ = function(items, time_from, time_till) {
|
||||
var deferred = $q.defer();
|
||||
var historyStorage = this.storage.history;
|
||||
var full_history;
|
||||
var expired = _.filter(_.indexBy(items, 'itemid'), function(item, itemid) {
|
||||
return !historyStorage[itemid];
|
||||
});
|
||||
if (expired.length) {
|
||||
this.zabbixAPI.getHistory(expired, time_from, time_till).then(function(history) {
|
||||
var grouped_history = _.groupBy(history, 'itemid');
|
||||
_.forEach(expired, function(item) {
|
||||
var itemid = item.itemid;
|
||||
historyStorage[itemid] = item;
|
||||
historyStorage[itemid].time_from = time_from;
|
||||
historyStorage[itemid].time_till = time_till;
|
||||
historyStorage[itemid].history = grouped_history[itemid];
|
||||
});
|
||||
full_history = _.map(items, function(item) {
|
||||
return historyStorage[item.itemid].history;
|
||||
});
|
||||
deferred.resolve(_.flatten(full_history, true));
|
||||
});
|
||||
} else {
|
||||
full_history = _.map(items, function(item) {
|
||||
return historyStorage[item.itemid].history;
|
||||
});
|
||||
deferred.resolve(_.flatten(full_history, true));
|
||||
}
|
||||
return deferred.promise;
|
||||
};
|
||||
|
||||
p.getHistoryFromAPI = function(items, time_from, time_till) {
|
||||
return this.zabbixAPI.getHistory(items, time_from, time_till);
|
||||
};
|
||||
|
||||
p.getHost = function(hostid) {
|
||||
@@ -102,7 +185,7 @@ function (angular, _, utils) {
|
||||
* host.hosts - array of host ids
|
||||
*/
|
||||
function convertApplications(applications) {
|
||||
return _.map(_.groupBy(applications, 'name'), function (value, key) {
|
||||
return _.map(_.groupBy(applications, 'name'), function(value, key) {
|
||||
return {
|
||||
name: key,
|
||||
applicationids: _.map(value, 'applicationid'),
|
||||
@@ -126,7 +209,23 @@ function (angular, _, utils) {
|
||||
});
|
||||
}
|
||||
|
||||
return ZabbixCache;
|
||||
function callOnce(func, promiseKeeper) {
|
||||
return function() {
|
||||
var deferred = $q.defer();
|
||||
if (!promiseKeeper) {
|
||||
promiseKeeper = deferred.promise;
|
||||
func.apply(this, arguments).then(function(result) {
|
||||
deferred.resolve(result);
|
||||
promiseKeeper = null;
|
||||
});
|
||||
} else {
|
||||
return promiseKeeper;
|
||||
}
|
||||
return deferred.promise;
|
||||
};
|
||||
}
|
||||
|
||||
return ZabbixCachingProxy;
|
||||
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user