From 7a73449828d307a4c40995a83d75f3e6b610424d Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Sun, 17 Jan 2016 13:45:55 +0300 Subject: [PATCH] Cache service refactoring. --- plugins/datasource-zabbix/queryCtrl.js | 8 +-- plugins/datasource-zabbix/utils.js | 32 +++++++++++ plugins/datasource-zabbix/zabbixCacheSrv.js | 64 ++++++++++++++------- 3 files changed, 77 insertions(+), 27 deletions(-) create mode 100644 plugins/datasource-zabbix/utils.js diff --git a/plugins/datasource-zabbix/queryCtrl.js b/plugins/datasource-zabbix/queryCtrl.js index adb00b3..f9cc3dd 100644 --- a/plugins/datasource-zabbix/queryCtrl.js +++ b/plugins/datasource-zabbix/queryCtrl.js @@ -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. diff --git a/plugins/datasource-zabbix/utils.js b/plugins/datasource-zabbix/utils.js new file mode 100644 index 0000000..8a73482 --- /dev/null +++ b/plugins/datasource-zabbix/utils.js @@ -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; + }; + }); +}); \ No newline at end of file diff --git a/plugins/datasource-zabbix/zabbixCacheSrv.js b/plugins/datasource-zabbix/zabbixCacheSrv.js index 3585e27..96bd424 100644 --- a/plugins/datasource-zabbix/zabbixCacheSrv.js +++ b/plugins/datasource-zabbix/zabbixCacheSrv.js @@ -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; });