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; var ZabbixAPI = zabbixAPIService;
this.zabbixAPI = new ZabbixAPI(url, username, password, basicAuth, withCredentials); this.zabbixAPI = new ZabbixAPI(url, username, password, basicAuth, withCredentials);
if (enableDirectDBConnection) {
this.dbConnector = new ZabbixDBConnector(sqlDatasourceId);
}
// Initialize caching proxy for requests // Initialize caching proxy for requests
var cacheOptions = { var cacheOptions = {
enabled: true, enabled: true,
ttl: cacheTTL ttl: cacheTTL
}; };
this.cachingProxy = new ZabbixCachingProxy(this.zabbixAPI, cacheOptions); this.cachingProxy = new ZabbixCachingProxy(this.zabbixAPI, this.dbConnector, cacheOptions);
// Proxy methods // Proxy methods
this.getHistory = this.cachingProxy.getHistory.bind(this.cachingProxy); this.getHistory = this.cachingProxy.getHistory.bind(this.cachingProxy);
this.getMacros = this.cachingProxy.getMacros.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.getTrend = this.zabbixAPI.getTrend.bind(this.zabbixAPI);
this.getEvents = this.zabbixAPI.getEvents.bind(this.zabbixAPI); this.getEvents = this.zabbixAPI.getEvents.bind(this.zabbixAPI);
this.getAlerts = this.zabbixAPI.getAlerts.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.getSLA = this.zabbixAPI.getSLA.bind(this.zabbixAPI);
this.getVersion = this.zabbixAPI.getVersion.bind(this.zabbixAPI); this.getVersion = this.zabbixAPI.getVersion.bind(this.zabbixAPI);
this.login = this.zabbixAPI.login.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, [{ _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 */ /** @ngInject */
function ZabbixCachingProxyFactory() { function ZabbixCachingProxyFactory() {
var ZabbixCachingProxy = function () { var ZabbixCachingProxy = function () {
function ZabbixCachingProxy(zabbixAPI, cacheOptions) { function ZabbixCachingProxy(zabbixAPI, zabbixDBConnector, cacheOptions) {
_classCallCheck(this, ZabbixCachingProxy); _classCallCheck(this, ZabbixCachingProxy);
this.zabbixAPI = zabbixAPI; this.zabbixAPI = zabbixAPI;
this.dbConnector = zabbixDBConnector;
this.cacheEnabled = cacheOptions.enabled; this.cacheEnabled = cacheOptions.enabled;
this.ttl = cacheOptions.ttl || 600000; // 10 minutes by default 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 // Don't run duplicated history requests
this.getHistory = callAPIRequestOnce(_.bind(this.zabbixAPI.getHistory, this.zabbixAPI), this.historyPromises, getHistoryRequestHash); 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 // Don't run duplicated requests
this.groupPromises = {}; this.groupPromises = {};
this.getGroupsOnce = callAPIRequestOnce(_.bind(this.zabbixAPI.getGroups, this.zabbixAPI), this.groupPromises, getRequestHash); 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(); 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 { return {
setters: [function (_angular) { setters: [function (_angular) {
angular = _angular.default; 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; var ZabbixAPI = zabbixAPIService;
this.zabbixAPI = new ZabbixAPI(url, username, password, basicAuth, withCredentials); this.zabbixAPI = new ZabbixAPI(url, username, password, basicAuth, withCredentials);
if (enableDirectDBConnection) {
this.dbConnector = new ZabbixDBConnector(sqlDatasourceId);
}
// Initialize caching proxy for requests // Initialize caching proxy for requests
var cacheOptions = { var cacheOptions = {
enabled: true, enabled: true,
ttl: cacheTTL ttl: cacheTTL
}; };
this.cachingProxy = new ZabbixCachingProxy(this.zabbixAPI, cacheOptions); this.cachingProxy = new ZabbixCachingProxy(this.zabbixAPI, this.dbConnector, cacheOptions);
// Proxy methods // Proxy methods
this.getHistory = this.cachingProxy.getHistory.bind(this.cachingProxy); this.getHistory = this.cachingProxy.getHistory.bind(this.cachingProxy);
this.getMacros = this.cachingProxy.getMacros.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.getTrend = this.zabbixAPI.getTrend.bind(this.zabbixAPI);
this.getEvents = this.zabbixAPI.getEvents.bind(this.zabbixAPI); this.getEvents = this.zabbixAPI.getEvents.bind(this.zabbixAPI);
this.getAlerts = this.zabbixAPI.getAlerts.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.getSLA = this.zabbixAPI.getSLA.bind(this.zabbixAPI);
this.getVersion = this.zabbixAPI.getVersion.bind(this.zabbixAPI); this.getVersion = this.zabbixAPI.getVersion.bind(this.zabbixAPI);
this.login = this.zabbixAPI.login.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, [{ _createClass(Zabbix, [{

View File

@@ -22,10 +22,11 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
/** @ngInject */ /** @ngInject */
function ZabbixCachingProxyFactory() { function ZabbixCachingProxyFactory() {
var ZabbixCachingProxy = function () { var ZabbixCachingProxy = function () {
function ZabbixCachingProxy(zabbixAPI, cacheOptions) { function ZabbixCachingProxy(zabbixAPI, zabbixDBConnector, cacheOptions) {
_classCallCheck(this, ZabbixCachingProxy); _classCallCheck(this, ZabbixCachingProxy);
this.zabbixAPI = zabbixAPI; this.zabbixAPI = zabbixAPI;
this.dbConnector = zabbixDBConnector;
this.cacheEnabled = cacheOptions.enabled; this.cacheEnabled = cacheOptions.enabled;
this.ttl = cacheOptions.ttl || 600000; // 10 minutes by default this.ttl = cacheOptions.ttl || 600000; // 10 minutes by default
@@ -46,6 +47,11 @@ function ZabbixCachingProxyFactory() {
// Don't run duplicated history requests // Don't run duplicated history requests
this.getHistory = callAPIRequestOnce(_lodash2.default.bind(this.zabbixAPI.getHistory, this.zabbixAPI), this.historyPromises, getHistoryRequestHash); 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 // Don't run duplicated requests
this.groupPromises = {}; this.groupPromises = {};
this.getGroupsOnce = callAPIRequestOnce(_lodash2.default.bind(this.zabbixAPI.getGroups, this.zabbixAPI), this.groupPromises, getRequestHash); this.getGroupsOnce = callAPIRequestOnce(_lodash2.default.bind(this.zabbixAPI.getGroups, this.zabbixAPI), this.groupPromises, getRequestHash);
@@ -210,6 +216,14 @@ function getHistoryRequestHash(args) {
return stamp.getHash(); 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 () { String.prototype.getHash = function () {
var hash = 0, var hash = 0,
i, i,

View File

@@ -22,17 +22,26 @@ function ZabbixFactory(zabbixAPIService, ZabbixCachingProxy, ZabbixDBConnector)
var ZabbixAPI = zabbixAPIService; var ZabbixAPI = zabbixAPIService;
this.zabbixAPI = new ZabbixAPI(url, username, password, basicAuth, withCredentials); this.zabbixAPI = new ZabbixAPI(url, username, password, basicAuth, withCredentials);
if (enableDirectDBConnection) {
this.dbConnector = new ZabbixDBConnector(sqlDatasourceId);
}
// Initialize caching proxy for requests // Initialize caching proxy for requests
let cacheOptions = { let cacheOptions = {
enabled: true, enabled: true,
ttl: cacheTTL ttl: cacheTTL
}; };
this.cachingProxy = new ZabbixCachingProxy(this.zabbixAPI, cacheOptions); this.cachingProxy = new ZabbixCachingProxy(this.zabbixAPI, this.dbConnector, cacheOptions);
// Proxy methods // Proxy methods
this.getHistory = this.cachingProxy.getHistory.bind(this.cachingProxy); this.getHistory = this.cachingProxy.getHistory.bind(this.cachingProxy);
this.getMacros = this.cachingProxy.getMacros.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.getTrend = this.zabbixAPI.getTrend.bind(this.zabbixAPI);
this.getEvents = this.zabbixAPI.getEvents.bind(this.zabbixAPI); this.getEvents = this.zabbixAPI.getEvents.bind(this.zabbixAPI);
this.getAlerts = this.zabbixAPI.getAlerts.bind(this.zabbixAPI); this.getAlerts = this.zabbixAPI.getAlerts.bind(this.zabbixAPI);
@@ -41,12 +50,6 @@ function ZabbixFactory(zabbixAPIService, ZabbixCachingProxy, ZabbixDBConnector)
this.getSLA = this.zabbixAPI.getSLA.bind(this.zabbixAPI); this.getSLA = this.zabbixAPI.getSLA.bind(this.zabbixAPI);
this.getVersion = this.zabbixAPI.getVersion.bind(this.zabbixAPI); this.getVersion = this.zabbixAPI.getVersion.bind(this.zabbixAPI);
this.login = this.zabbixAPI.login.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);
}
} }
getItemsFromTarget(target, options) { getItemsFromTarget(target, options) {

View File

@@ -8,8 +8,9 @@ import _ from 'lodash';
function ZabbixCachingProxyFactory() { function ZabbixCachingProxyFactory() {
class ZabbixCachingProxy { class ZabbixCachingProxy {
constructor(zabbixAPI, cacheOptions) { constructor(zabbixAPI, zabbixDBConnector, cacheOptions) {
this.zabbixAPI = zabbixAPI; this.zabbixAPI = zabbixAPI;
this.dbConnector = zabbixDBConnector;
this.cacheEnabled = cacheOptions.enabled; this.cacheEnabled = cacheOptions.enabled;
this.ttl = cacheOptions.ttl || 600000; // 10 minutes by default this.ttl = cacheOptions.ttl || 600000; // 10 minutes by default
@@ -31,6 +32,13 @@ function ZabbixCachingProxyFactory() {
this.getHistory = callAPIRequestOnce(_.bind(this.zabbixAPI.getHistory, this.zabbixAPI), this.getHistory = callAPIRequestOnce(_.bind(this.zabbixAPI.getHistory, this.zabbixAPI),
this.historyPromises, getHistoryRequestHash); 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 // Don't run duplicated requests
this.groupPromises = {}; this.groupPromises = {};
this.getGroupsOnce = callAPIRequestOnce(_.bind(this.zabbixAPI.getGroups, this.zabbixAPI), this.getGroupsOnce = callAPIRequestOnce(_.bind(this.zabbixAPI.getGroups, this.zabbixAPI),
@@ -195,6 +203,14 @@ function getHistoryRequestHash(args) {
return stamp.getHash(); return stamp.getHash();
} }
function getDBQueryHash(args) {
let itemids = _.map(args[0], 'itemid');
let consolidateBy = args[3].consolidateBy;
let intervalMs = args[3].intervalMs;
let stamp = itemids.join() + args[1] + args[2] + consolidateBy + intervalMs;
return stamp.getHash();
}
String.prototype.getHash = function() { String.prototype.getHash = function() {
var hash = 0, i, chr, len; var hash = 0, i, chr, len;
if (this.length !== 0) { if (this.length !== 0) {