refactor: move history calls to zabbix module
This commit is contained in:
@@ -176,35 +176,14 @@ class ZabbixAPIDatasource {
|
||||
* Query history for numeric items
|
||||
*/
|
||||
queryNumericDataForItems(items, target, timeRange, useTrends, options) {
|
||||
let [timeFrom, timeTo] = timeRange;
|
||||
let getHistoryPromise;
|
||||
options.consolidateBy = getConsolidateBy(target);
|
||||
options.valueType = this.getTrendValueType(target);
|
||||
options.consolidateBy = getConsolidateBy(target) || options.valueType;
|
||||
|
||||
if (useTrends) {
|
||||
if (this.enableDirectDBConnection) {
|
||||
getHistoryPromise = this.zabbix.getTrendsDB(items, timeFrom, timeTo, options)
|
||||
.then(history => this.zabbix.dbConnector.handleGrafanaTSResponse(history, items));
|
||||
} else {
|
||||
let valueType = this.getTrendValueType(target);
|
||||
getHistoryPromise = this.zabbix.getTrend(items, timeFrom, timeTo)
|
||||
.then(history => responseHandler.handleTrends(history, items, valueType))
|
||||
.then(timeseries => {
|
||||
// Sort trend data, issue #202
|
||||
_.forEach(timeseries, series => {
|
||||
series.datapoints = _.sortBy(series.datapoints, point => point[c.DATAPOINT_TS]);
|
||||
});
|
||||
return timeseries;
|
||||
});
|
||||
}
|
||||
getHistoryPromise = this.zabbix.getTrends(items, timeRange, options);
|
||||
} else {
|
||||
// Use history
|
||||
if (this.enableDirectDBConnection) {
|
||||
getHistoryPromise = this.zabbix.getHistoryDB(items, timeFrom, timeTo, options)
|
||||
.then(history => this.zabbix.dbConnector.handleGrafanaTSResponse(history, items));
|
||||
} else {
|
||||
getHistoryPromise = this.zabbix.getHistory(items, timeFrom, timeTo)
|
||||
.then(history => responseHandler.handleHistory(history, items));
|
||||
}
|
||||
getHistoryPromise = this.zabbix.getHistoryTS(items, timeRange, options);
|
||||
}
|
||||
|
||||
return getHistoryPromise
|
||||
@@ -281,25 +260,13 @@ class ZabbixAPIDatasource {
|
||||
* Query target data for Text mode
|
||||
*/
|
||||
queryTextData(target, timeRange) {
|
||||
let [timeFrom, timeTo] = timeRange;
|
||||
let options = {
|
||||
itemtype: 'text'
|
||||
};
|
||||
return this.zabbix.getItemsFromTarget(target, options)
|
||||
.then(items => {
|
||||
if (items.length) {
|
||||
return this.zabbix.getHistory(items, timeFrom, timeTo)
|
||||
.then(history => {
|
||||
if (target.resultFormat === 'table') {
|
||||
return responseHandler.handleHistoryAsTable(history, items, target);
|
||||
} else {
|
||||
return responseHandler.handleText(history, items, target);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
return Promise.resolve([]);
|
||||
}
|
||||
});
|
||||
.then(items => {
|
||||
return this.zabbix.getHistoryText(items, timeRange, target);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -329,12 +296,10 @@ class ZabbixAPIDatasource {
|
||||
return [];
|
||||
}
|
||||
|
||||
let itServiceIds = [];
|
||||
let itServices = [];
|
||||
let itServiceFilter;
|
||||
let isOldVersion = target.itservice && !target.itServiceFilter;
|
||||
options.isOldVersion = target.itservice && !target.itServiceFilter;
|
||||
|
||||
if (isOldVersion) {
|
||||
if (options.isOldVersion) {
|
||||
// Backward compatibility
|
||||
itServiceFilter = '/.*/';
|
||||
} else {
|
||||
@@ -343,22 +308,7 @@ class ZabbixAPIDatasource {
|
||||
|
||||
return this.zabbix.getITServices(itServiceFilter)
|
||||
.then(itservices => {
|
||||
itServices = itservices;
|
||||
if (isOldVersion) {
|
||||
itServices = _.filter(itServices, {'serviceid': target.itservice.serviceid});
|
||||
}
|
||||
|
||||
itServiceIds = _.map(itServices, 'serviceid');
|
||||
return itServiceIds;
|
||||
})
|
||||
.then(serviceids => {
|
||||
return this.zabbix.getSLA(serviceids, timeRange);
|
||||
})
|
||||
.then(slaResponse => {
|
||||
return _.map(itServiceIds, serviceid => {
|
||||
let itservice = _.find(itServices, {'serviceid': serviceid});
|
||||
return responseHandler.handleSLAResponse(itservice, target.slaProperty, slaResponse);
|
||||
});
|
||||
return this.zabbix.getSLA(itservices, timeRange, target, options);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -659,7 +609,7 @@ function bindFunctionDefs(functionDefs, category) {
|
||||
}
|
||||
|
||||
function getConsolidateBy(target) {
|
||||
let consolidateBy = 'avg';
|
||||
let consolidateBy;
|
||||
let funcDef = _.find(target.functions, func => {
|
||||
return func.def.name === 'consolidateBy';
|
||||
});
|
||||
|
||||
@@ -40,6 +40,14 @@ function convertHistory(history, items, addHostName, convertPointCallback) {
|
||||
});
|
||||
}
|
||||
|
||||
function sortTimeseries(timeseries) {
|
||||
// Sort trend data, issue #202
|
||||
_.forEach(timeseries, series => {
|
||||
series.datapoints = _.sortBy(series.datapoints, point => point[c.DATAPOINT_TS]);
|
||||
});
|
||||
return timeseries;
|
||||
}
|
||||
|
||||
function handleHistory(history, items, addHostName = true) {
|
||||
return convertHistory(history, items, addHostName, convertHistoryPoint);
|
||||
}
|
||||
@@ -211,13 +219,14 @@ function convertTrendPoint(valueType, point) {
|
||||
}
|
||||
|
||||
export default {
|
||||
handleHistory: handleHistory,
|
||||
convertHistory: convertHistory,
|
||||
handleTrends: handleTrends,
|
||||
handleText: handleText,
|
||||
handleHistoryAsTable: handleHistoryAsTable,
|
||||
handleSLAResponse: handleSLAResponse,
|
||||
handleTriggersResponse: handleTriggersResponse
|
||||
handleHistory,
|
||||
convertHistory,
|
||||
handleTrends,
|
||||
handleText,
|
||||
handleHistoryAsTable,
|
||||
handleSLAResponse,
|
||||
handleTriggersResponse,
|
||||
sortTimeseries
|
||||
};
|
||||
|
||||
// Fix for backward compatibility with lodash 2.4
|
||||
|
||||
@@ -95,7 +95,7 @@ export class ZabbixDBConnector {
|
||||
}
|
||||
|
||||
getTrends(items, timeFrom, timeTill, options) {
|
||||
let {intervalMs, consolidateBy} = options;
|
||||
let { intervalMs, consolidateBy } = options;
|
||||
let intervalSec = Math.ceil(intervalMs / 1000);
|
||||
|
||||
consolidateBy = consolidateBy || 'avg';
|
||||
|
||||
@@ -8,8 +8,6 @@ import { ZabbixAPICore } from './zabbixAPICore';
|
||||
* Wraps API calls and provides high-level methods.
|
||||
*/
|
||||
export class ZabbixAPIConnector {
|
||||
|
||||
/** @ngInject */
|
||||
constructor(api_url, username, password, basicAuth, withCredentials, backendSrv) {
|
||||
this.url = api_url;
|
||||
this.username = username;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import _ from 'lodash';
|
||||
import * as utils from '../utils';
|
||||
import responseHandler from '../responseHandler';
|
||||
import { ZabbixAPIConnector } from './connectors/zabbix_api/zabbixAPIConnector';
|
||||
import { ZabbixDBConnector } from './connectors/sql/zabbixDBConnector';
|
||||
import { CachingProxy } from './proxy/cachingProxy';
|
||||
@@ -15,7 +16,7 @@ const REQUESTS_TO_CACHE = [
|
||||
|
||||
const REQUESTS_TO_BIND = [
|
||||
'getHistory', 'getTrend', 'getMacros', 'getItemsByIDs', 'getEvents', 'getAlerts', 'getHostAlerts',
|
||||
'getAcknowledges', 'getITService', 'getSLA', 'getVersion', 'login'
|
||||
'getAcknowledges', 'getITService', 'getVersion', 'login'
|
||||
];
|
||||
|
||||
export class Zabbix {
|
||||
@@ -33,6 +34,8 @@ export class Zabbix {
|
||||
sqlDatasourceId
|
||||
} = options;
|
||||
|
||||
this.enableDirectDBConnection = enableDirectDBConnection;
|
||||
|
||||
// Initialize caching proxy for requests
|
||||
let cacheOptions = {
|
||||
enabled: true,
|
||||
@@ -219,6 +222,61 @@ export class Zabbix {
|
||||
return this.zabbixAPI.getTriggers(query.groupids, query.hostids, query.applicationids, options);
|
||||
});
|
||||
}
|
||||
|
||||
getHistoryTS(items, timeRange, options) {
|
||||
let [timeFrom, timeTo] = timeRange;
|
||||
if (this.enableDirectDBConnection) {
|
||||
return this.getHistoryDB(items, timeFrom, timeTo, options)
|
||||
.then(history => this.dbConnector.handleGrafanaTSResponse(history, items));
|
||||
} else {
|
||||
return this.zabbixAPI.getHistory(items, timeFrom, timeTo)
|
||||
.then(history => responseHandler.handleHistory(history, items));
|
||||
}
|
||||
}
|
||||
|
||||
getTrends(items, timeRange, options) {
|
||||
let [timeFrom, timeTo] = timeRange;
|
||||
if (this.enableDirectDBConnection) {
|
||||
return this.getTrendsDB(items, timeFrom, timeTo, options)
|
||||
.then(history => this.dbConnector.handleGrafanaTSResponse(history, items));
|
||||
} else {
|
||||
let valueType = options.consolidateBy || options.valueType;
|
||||
return this.zabbixAPI.getTrend(items, timeFrom, timeTo)
|
||||
.then(history => responseHandler.handleTrends(history, items, valueType))
|
||||
.then(responseHandler.sortTimeseries); // Sort trend data, issue #202
|
||||
}
|
||||
}
|
||||
|
||||
getHistoryText(items, timeRange, target) {
|
||||
let [timeFrom, timeTo] = timeRange;
|
||||
if (items.length) {
|
||||
return this.zabbixAPI.getHistory(items, timeFrom, timeTo)
|
||||
.then(history => {
|
||||
if (target.resultFormat === 'table') {
|
||||
return responseHandler.handleHistoryAsTable(history, items, target);
|
||||
} else {
|
||||
return responseHandler.handleText(history, items, target);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
return Promise.resolve([]);
|
||||
}
|
||||
}
|
||||
|
||||
getSLA(itservices, timeRange, target, options) {
|
||||
let itServices = itservices;
|
||||
if (options.isOldVersion) {
|
||||
itServices = _.filter(itServices, {'serviceid': target.itservice.serviceid});
|
||||
}
|
||||
let itServiceIds = _.map(itServices, 'serviceid');
|
||||
return this.zabbixAPI.getSLA(itServiceIds, timeRange)
|
||||
.then(slaResponse => {
|
||||
return _.map(itServiceIds, serviceid => {
|
||||
let itservice = _.find(itServices, {'serviceid': serviceid});
|
||||
return responseHandler.handleSLAResponse(itservice, target.slaProperty, slaResponse);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Reference in New Issue
Block a user