mysql-connector: deduplicate requests

This commit is contained in:
Alexander Zobnin
2017-07-23 09:50:37 +03:00
parent fb145575ec
commit 3f13aab9b8
8 changed files with 79 additions and 26 deletions

View File

@@ -45,17 +45,26 @@ System.register(['angular', 'lodash', './utils', './zabbixAPI.service.js', './za
var ZabbixAPI = zabbixAPIService;
this.zabbixAPI = new ZabbixAPI(url, username, password, basicAuth, withCredentials);
if (enableDirectDBConnection) {
this.dbConnector = new ZabbixDBConnector(sqlDatasourceId);
}
// Initialize caching proxy for requests
var cacheOptions = {
enabled: true,
ttl: cacheTTL
};
this.cachingProxy = new ZabbixCachingProxy(this.zabbixAPI, cacheOptions);
this.cachingProxy = new ZabbixCachingProxy(this.zabbixAPI, this.dbConnector, cacheOptions);
// Proxy methods
this.getHistory = this.cachingProxy.getHistory.bind(this.cachingProxy);
this.getMacros = this.cachingProxy.getMacros.bind(this.cachingProxy);
if (enableDirectDBConnection) {
this.getHistoryDB = this.cachingProxy.getHistoryDB.bind(this.cachingProxy);
this.getTrendsDB = this.cachingProxy.getTrendsDB.bind(this.cachingProxy);
}
this.getTrend = this.zabbixAPI.getTrend.bind(this.zabbixAPI);
this.getEvents = this.zabbixAPI.getEvents.bind(this.zabbixAPI);
this.getAlerts = this.zabbixAPI.getAlerts.bind(this.zabbixAPI);
@@ -64,12 +73,6 @@ System.register(['angular', 'lodash', './utils', './zabbixAPI.service.js', './za
this.getSLA = this.zabbixAPI.getSLA.bind(this.zabbixAPI);
this.getVersion = this.zabbixAPI.getVersion.bind(this.zabbixAPI);
this.login = this.zabbixAPI.login.bind(this.zabbixAPI);
if (enableDirectDBConnection) {
this.dbConnector = new ZabbixDBConnector(sqlDatasourceId);
this.getHistoryDB = this.dbConnector.getHistory.bind(this.dbConnector);
this.getTrendsDB = this.dbConnector.getTrends.bind(this.dbConnector);
}
}
_createClass(Zabbix, [{

File diff suppressed because one or more lines are too long

View File

@@ -29,10 +29,11 @@ System.register(['angular', 'lodash'], function (_export, _context) {
/** @ngInject */
function ZabbixCachingProxyFactory() {
var ZabbixCachingProxy = function () {
function ZabbixCachingProxy(zabbixAPI, cacheOptions) {
function ZabbixCachingProxy(zabbixAPI, zabbixDBConnector, cacheOptions) {
_classCallCheck(this, ZabbixCachingProxy);
this.zabbixAPI = zabbixAPI;
this.dbConnector = zabbixDBConnector;
this.cacheEnabled = cacheOptions.enabled;
this.ttl = cacheOptions.ttl || 600000; // 10 minutes by default
@@ -53,6 +54,11 @@ System.register(['angular', 'lodash'], function (_export, _context) {
// Don't run duplicated history requests
this.getHistory = callAPIRequestOnce(_.bind(this.zabbixAPI.getHistory, this.zabbixAPI), this.historyPromises, getHistoryRequestHash);
if (this.dbConnector) {
this.getHistoryDB = callAPIRequestOnce(_.bind(this.dbConnector.getHistory, this.dbConnector), this.historyPromises, getDBQueryHash);
this.getTrendsDB = callAPIRequestOnce(_.bind(this.dbConnector.getTrends, this.dbConnector), this.historyPromises, getDBQueryHash);
}
// Don't run duplicated requests
this.groupPromises = {};
this.getGroupsOnce = callAPIRequestOnce(_.bind(this.zabbixAPI.getGroups, this.zabbixAPI), this.groupPromises, getRequestHash);
@@ -209,6 +215,14 @@ System.register(['angular', 'lodash'], function (_export, _context) {
return stamp.getHash();
}
function getDBQueryHash(args) {
var itemids = _.map(args[0], 'itemid');
var consolidateBy = args[3].consolidateBy;
var intervalMs = args[3].intervalMs;
var stamp = itemids.join() + args[1] + args[2] + consolidateBy + intervalMs;
return stamp.getHash();
}
return {
setters: [function (_angular) {
angular = _angular.default;

File diff suppressed because one or more lines are too long

View File

@@ -50,17 +50,26 @@ function ZabbixFactory(zabbixAPIService, ZabbixCachingProxy, ZabbixDBConnector)
var ZabbixAPI = zabbixAPIService;
this.zabbixAPI = new ZabbixAPI(url, username, password, basicAuth, withCredentials);
if (enableDirectDBConnection) {
this.dbConnector = new ZabbixDBConnector(sqlDatasourceId);
}
// Initialize caching proxy for requests
var cacheOptions = {
enabled: true,
ttl: cacheTTL
};
this.cachingProxy = new ZabbixCachingProxy(this.zabbixAPI, cacheOptions);
this.cachingProxy = new ZabbixCachingProxy(this.zabbixAPI, this.dbConnector, cacheOptions);
// Proxy methods
this.getHistory = this.cachingProxy.getHistory.bind(this.cachingProxy);
this.getMacros = this.cachingProxy.getMacros.bind(this.cachingProxy);
if (enableDirectDBConnection) {
this.getHistoryDB = this.cachingProxy.getHistoryDB.bind(this.cachingProxy);
this.getTrendsDB = this.cachingProxy.getTrendsDB.bind(this.cachingProxy);
}
this.getTrend = this.zabbixAPI.getTrend.bind(this.zabbixAPI);
this.getEvents = this.zabbixAPI.getEvents.bind(this.zabbixAPI);
this.getAlerts = this.zabbixAPI.getAlerts.bind(this.zabbixAPI);
@@ -69,12 +78,6 @@ function ZabbixFactory(zabbixAPIService, ZabbixCachingProxy, ZabbixDBConnector)
this.getSLA = this.zabbixAPI.getSLA.bind(this.zabbixAPI);
this.getVersion = this.zabbixAPI.getVersion.bind(this.zabbixAPI);
this.login = this.zabbixAPI.login.bind(this.zabbixAPI);
if (enableDirectDBConnection) {
this.dbConnector = new ZabbixDBConnector(sqlDatasourceId);
this.getHistoryDB = this.dbConnector.getHistory.bind(this.dbConnector);
this.getTrendsDB = this.dbConnector.getTrends.bind(this.dbConnector);
}
}
_createClass(Zabbix, [{

View File

@@ -22,10 +22,11 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
/** @ngInject */
function ZabbixCachingProxyFactory() {
var ZabbixCachingProxy = function () {
function ZabbixCachingProxy(zabbixAPI, cacheOptions) {
function ZabbixCachingProxy(zabbixAPI, zabbixDBConnector, cacheOptions) {
_classCallCheck(this, ZabbixCachingProxy);
this.zabbixAPI = zabbixAPI;
this.dbConnector = zabbixDBConnector;
this.cacheEnabled = cacheOptions.enabled;
this.ttl = cacheOptions.ttl || 600000; // 10 minutes by default
@@ -46,6 +47,11 @@ function ZabbixCachingProxyFactory() {
// Don't run duplicated history requests
this.getHistory = callAPIRequestOnce(_lodash2.default.bind(this.zabbixAPI.getHistory, this.zabbixAPI), this.historyPromises, getHistoryRequestHash);
if (this.dbConnector) {
this.getHistoryDB = callAPIRequestOnce(_lodash2.default.bind(this.dbConnector.getHistory, this.dbConnector), this.historyPromises, getDBQueryHash);
this.getTrendsDB = callAPIRequestOnce(_lodash2.default.bind(this.dbConnector.getTrends, this.dbConnector), this.historyPromises, getDBQueryHash);
}
// Don't run duplicated requests
this.groupPromises = {};
this.getGroupsOnce = callAPIRequestOnce(_lodash2.default.bind(this.zabbixAPI.getGroups, this.zabbixAPI), this.groupPromises, getRequestHash);
@@ -210,6 +216,14 @@ function getHistoryRequestHash(args) {
return stamp.getHash();
}
function getDBQueryHash(args) {
var itemids = _lodash2.default.map(args[0], 'itemid');
var consolidateBy = args[3].consolidateBy;
var intervalMs = args[3].intervalMs;
var stamp = itemids.join() + args[1] + args[2] + consolidateBy + intervalMs;
return stamp.getHash();
}
String.prototype.getHash = function () {
var hash = 0,
i,