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