Cache service refactoring.
This commit is contained in:
@@ -80,13 +80,7 @@ define([
|
||||
$scope.getGroupNames = _.partial(getMetricNames, $scope, 'groupList');
|
||||
$scope.getHostNames = _.partial(getMetricNames, $scope, 'hostList');
|
||||
$scope.getApplicationNames = _.partial(getMetricNames, $scope, 'applicationList');
|
||||
|
||||
$scope.getItemNames = function () {
|
||||
var expandedItems = _.map($scope.metric.itemList, function (item) {
|
||||
return zabbixHelperSrv.expandItemName(item);
|
||||
});
|
||||
return _.uniq(expandedItems);
|
||||
};
|
||||
$scope.getItemNames = _.partial(getMetricNames, $scope, 'itemList');
|
||||
|
||||
/**
|
||||
* Switch query editor to specified mode.
|
||||
|
||||
32
plugins/datasource-zabbix/utils.js
Normal file
32
plugins/datasource-zabbix/utils.js
Normal file
@@ -0,0 +1,32 @@
|
||||
define([
|
||||
'angular',
|
||||
'lodash'
|
||||
],
|
||||
function (angular, _) {
|
||||
'use strict';
|
||||
|
||||
var module = angular.module('grafana.services');
|
||||
|
||||
module.service('Utils', function() {
|
||||
|
||||
/**
|
||||
* Expand Zabbix item name
|
||||
*
|
||||
* @param {string} name item name, ie "CPU $2 time"
|
||||
* @param {string} key item key, ie system.cpu.util[,system,avg1]
|
||||
* @return {string} expanded name, ie "CPU system time"
|
||||
*/
|
||||
this.expandItemName = function(name, key) {
|
||||
|
||||
// extract params from key:
|
||||
// "system.cpu.util[,system,avg1]" --> ["", "system", "avg1"]
|
||||
var key_params = key.substring(key.indexOf('[') + 1, key.lastIndexOf(']')).split(',');
|
||||
|
||||
// replace item parameters
|
||||
for (var i = key_params.length; i >= 1; i--) {
|
||||
name = name.replace('$' + i, key_params[i - 1]);
|
||||
}
|
||||
return name;
|
||||
};
|
||||
});
|
||||
});
|
||||
@@ -1,27 +1,33 @@
|
||||
define([
|
||||
'angular',
|
||||
'lodash'
|
||||
'lodash',
|
||||
'./utils'
|
||||
],
|
||||
function (angular, _) {
|
||||
'use strict';
|
||||
|
||||
var module = angular.module('grafana.services');
|
||||
|
||||
module.factory('ZabbixCache', function($q) {
|
||||
// Use factory() instead service() for multiple datasources support.
|
||||
// Each datasource instance must initialize its own cache.
|
||||
module.factory('ZabbixCache', function($q, Utils) {
|
||||
|
||||
function ZabbixCache(zabbixAPI, lifetime) {
|
||||
function ZabbixCache(zabbixAPI, ttl) {
|
||||
var self = this;
|
||||
|
||||
this.zabbixAPI = zabbixAPI;
|
||||
this.lifetime = lifetime;
|
||||
this.ttl = ttl;
|
||||
|
||||
// Internal objects for data storing
|
||||
this._groups = undefined;
|
||||
this._hosts = undefined;
|
||||
this._applications = undefined;
|
||||
this._items = undefined;
|
||||
|
||||
// Check is a service initialized or not
|
||||
this._initialized = undefined;
|
||||
|
||||
// Set _initialized when data has loaded
|
||||
this.refresh().then(function () {
|
||||
self._initialized = true;
|
||||
});
|
||||
@@ -40,19 +46,10 @@ function (angular, _) {
|
||||
|
||||
return $q.all(promises).then(function (results) {
|
||||
if (results.length) {
|
||||
self._groups = results[0];
|
||||
|
||||
self._hosts = _.forEach(results[1], function(host) {
|
||||
host.groups = _.map(host.groups, 'groupid');
|
||||
return host;
|
||||
});
|
||||
|
||||
self._applications = groupApplications(results[2]);
|
||||
|
||||
self._items = _.forEach(results[3], function(item) {
|
||||
item.applications = _.map(item.applications, 'applicationid');
|
||||
return item;
|
||||
});
|
||||
self._groups = results[0];
|
||||
self._hosts = convertHosts(results[1]);
|
||||
self._applications = convertApplications(results[2]);
|
||||
self._items = convertItems(results[3]);
|
||||
}
|
||||
});
|
||||
};
|
||||
@@ -74,18 +71,45 @@ function (angular, _) {
|
||||
};
|
||||
|
||||
/**
|
||||
* Group Zabbix applications by name
|
||||
* Convert host.get response to cache format
|
||||
* host.groups - array of group ids
|
||||
*/
|
||||
function groupApplications(applications) {
|
||||
function convertHosts(hosts) {
|
||||
return _.forEach(hosts, function(host) {
|
||||
host.groups = _.map(host.groups, 'groupid');
|
||||
return host;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Group Zabbix applications by name
|
||||
* host.hosts - array of host ids
|
||||
*/
|
||||
function convertApplications(applications) {
|
||||
return _.map(_.groupBy(applications, 'name'), function (value, key) {
|
||||
return {
|
||||
name: key,
|
||||
applicationids: _.map(value, 'applicationid'),
|
||||
hostids: _.uniq(_.map(_.flatten(value, 'hosts'), 'hostid'))
|
||||
hosts: _.uniq(_.map(_.flatten(value, 'hosts'), 'hostid'))
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert item.get response to cache format
|
||||
* item.applications - array of application ids
|
||||
* item.item - original item name returned by api (ie "CPU $2 time")
|
||||
* item.name - expanded name (ie "CPU system time")
|
||||
*/
|
||||
function convertItems(items) {
|
||||
return _.forEach(items, function(item) {
|
||||
item.applications = _.map(item.applications, 'applicationid');
|
||||
item.item = item.name;
|
||||
item.name = Utils.expandItemName(item.item, item.key_);
|
||||
return item;
|
||||
});
|
||||
}
|
||||
|
||||
return ZabbixCache;
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user