Refactor: move zabbix api response handle to separate module.
This commit is contained in:
@@ -5,6 +5,7 @@ import * as utils from './utils';
|
||||
import * as migrations from './migrations';
|
||||
import * as metricFunctions from './metricFunctions';
|
||||
import DataProcessor from './DataProcessor';
|
||||
import responseHandler from './responseHandler';
|
||||
import './zabbixAPI.service.js';
|
||||
import './zabbixCache.service.js';
|
||||
import './queryProcessor.service.js';
|
||||
@@ -122,8 +123,7 @@ class ZabbixAPIDatasource {
|
||||
return this.zabbixAPI
|
||||
.getSLA(target.itservice.serviceid, timeFrom, timeTo)
|
||||
.then(slaObject => {
|
||||
return this.queryProcessor
|
||||
.handleSLAResponse(target.itservice, target.slaProperty, slaObject);
|
||||
return responseHandler.handleSLAResponse(target.itservice, target.slaProperty, slaObject);
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -168,7 +168,7 @@ class ZabbixAPIDatasource {
|
||||
getHistory = this.zabbixAPI
|
||||
.getTrend(items, timeFrom, timeTo)
|
||||
.then(history => {
|
||||
return this.queryProcessor.handleTrends(history, items, addHostName, valueType);
|
||||
return responseHandler.handleTrends(history, items, addHostName, valueType);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -177,7 +177,7 @@ class ZabbixAPIDatasource {
|
||||
getHistory = this.zabbixCache
|
||||
.getHistory(items, timeFrom, timeTo)
|
||||
.then(history => {
|
||||
return this.queryProcessor.handleHistory(history, items, addHostName);
|
||||
return responseHandler.handleHistory(history, items, addHostName);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -233,7 +233,7 @@ class ZabbixAPIDatasource {
|
||||
if (items.length) {
|
||||
return this.zabbixAPI.getHistory(items, timeFrom, timeTo)
|
||||
.then(history => {
|
||||
return this.queryProcessor.convertHistory(history, items, false, (point) => {
|
||||
return responseHandler.convertHistory(history, items, false, (point) => {
|
||||
let value = point.value;
|
||||
|
||||
// Regex-based extractor
|
||||
|
||||
@@ -147,74 +147,6 @@ function QueryProcessorFactory() {
|
||||
return query;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert Zabbix API history.get response to Grafana format
|
||||
*
|
||||
* @return {Array} Array of timeseries in Grafana format
|
||||
* {
|
||||
* target: "Metric name",
|
||||
* datapoints: [[<value>, <unixtime>], ...]
|
||||
* }
|
||||
*/
|
||||
convertHistory(history, items, addHostName, convertPointCallback) {
|
||||
/**
|
||||
* Response should be in the format:
|
||||
* data: [
|
||||
* {
|
||||
* target: "Metric name",
|
||||
* datapoints: [[<value>, <unixtime>], ...]
|
||||
* }, ...
|
||||
* ]
|
||||
*/
|
||||
|
||||
// Group history by itemid
|
||||
var grouped_history = _.groupBy(history, 'itemid');
|
||||
var hosts = _.uniq(_.flatten(_.map(items, 'hosts')),'hostid'); //uniq is needed to deduplicate
|
||||
|
||||
return _.map(grouped_history, function(hist, itemid) {
|
||||
var item = _.find(items, {'itemid': itemid});
|
||||
var alias = item.name;
|
||||
if (_.keys(hosts).length > 1 && addHostName) { //only when actual multi hosts selected
|
||||
var host = _.find(hosts, {'hostid': item.hostid});
|
||||
alias = host.name + ": " + alias;
|
||||
}
|
||||
return {
|
||||
target: alias,
|
||||
datapoints: _.map(hist, convertPointCallback)
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
handleHistory(history, items, addHostName) {
|
||||
return this.convertHistory(history, items, addHostName, convertHistoryPoint);
|
||||
}
|
||||
|
||||
handleTrends(history, items, addHostName, valueType) {
|
||||
var convertPointCallback = _.partial(convertTrendPoint, valueType);
|
||||
return this.convertHistory(history, items, addHostName, convertPointCallback);
|
||||
}
|
||||
|
||||
handleSLAResponse(itservice, slaProperty, slaObject) {
|
||||
var targetSLA = slaObject[itservice.serviceid].sla[0];
|
||||
if (slaProperty.property === 'status') {
|
||||
var targetStatus = parseInt(slaObject[itservice.serviceid].status);
|
||||
return {
|
||||
target: itservice.name + ' ' + slaProperty.name,
|
||||
datapoints: [
|
||||
[targetStatus, targetSLA.to * 1000]
|
||||
]
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
target: itservice.name + ' ' + slaProperty.name,
|
||||
datapoints: [
|
||||
[targetSLA[slaProperty.property], targetSLA.from * 1000],
|
||||
[targetSLA[slaProperty.property], targetSLA.to * 1000]
|
||||
]
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return QueryProcessor;
|
||||
@@ -278,33 +210,3 @@ function filterByQuery(list, filter) {
|
||||
return filterByName(list, filter);
|
||||
}
|
||||
}
|
||||
|
||||
function convertHistoryPoint(point) {
|
||||
// Value must be a number for properly work
|
||||
return [
|
||||
Number(point.value),
|
||||
point.clock * 1000
|
||||
];
|
||||
}
|
||||
|
||||
function convertTrendPoint(valueType, point) {
|
||||
var value;
|
||||
switch (valueType) {
|
||||
case "min":
|
||||
value = point.value_min;
|
||||
break;
|
||||
case "max":
|
||||
value = point.value_max;
|
||||
break;
|
||||
case "avg":
|
||||
value = point.value_avg;
|
||||
break;
|
||||
default:
|
||||
value = point.value_avg;
|
||||
}
|
||||
|
||||
return [
|
||||
Number(value),
|
||||
point.clock * 1000
|
||||
];
|
||||
}
|
||||
|
||||
106
src/datasource-zabbix/responseHandler.js
Normal file
106
src/datasource-zabbix/responseHandler.js
Normal file
@@ -0,0 +1,106 @@
|
||||
import _ from 'lodash';
|
||||
|
||||
/**
|
||||
* Convert Zabbix API history.get response to Grafana format
|
||||
*
|
||||
* @return {Array} Array of timeseries in Grafana format
|
||||
* {
|
||||
* target: "Metric name",
|
||||
* datapoints: [[<value>, <unixtime>], ...]
|
||||
* }
|
||||
*/
|
||||
function convertHistory(history, items, addHostName, convertPointCallback) {
|
||||
/**
|
||||
* Response should be in the format:
|
||||
* data: [
|
||||
* {
|
||||
* target: "Metric name",
|
||||
* datapoints: [[<value>, <unixtime>], ...]
|
||||
* }, ...
|
||||
* ]
|
||||
*/
|
||||
|
||||
// Group history by itemid
|
||||
var grouped_history = _.groupBy(history, 'itemid');
|
||||
var hosts = _.uniq(_.flatten(_.map(items, 'hosts')),'hostid'); //uniq is needed to deduplicate
|
||||
|
||||
return _.map(grouped_history, function(hist, itemid) {
|
||||
var item = _.find(items, {'itemid': itemid});
|
||||
var alias = item.name;
|
||||
if (_.keys(hosts).length > 1 && addHostName) { //only when actual multi hosts selected
|
||||
var host = _.find(hosts, {'hostid': item.hostid});
|
||||
alias = host.name + ": " + alias;
|
||||
}
|
||||
return {
|
||||
target: alias,
|
||||
datapoints: _.map(hist, convertPointCallback)
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
function handleHistory(history, items, addHostName) {
|
||||
return convertHistory(history, items, addHostName, convertHistoryPoint);
|
||||
}
|
||||
|
||||
function handleTrends(history, items, addHostName, valueType) {
|
||||
var convertPointCallback = _.partial(convertTrendPoint, valueType);
|
||||
return convertHistory(history, items, addHostName, convertPointCallback);
|
||||
}
|
||||
|
||||
function handleSLAResponse(itservice, slaProperty, slaObject) {
|
||||
var targetSLA = slaObject[itservice.serviceid].sla[0];
|
||||
if (slaProperty.property === 'status') {
|
||||
var targetStatus = parseInt(slaObject[itservice.serviceid].status);
|
||||
return {
|
||||
target: itservice.name + ' ' + slaProperty.name,
|
||||
datapoints: [
|
||||
[targetStatus, targetSLA.to * 1000]
|
||||
]
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
target: itservice.name + ' ' + slaProperty.name,
|
||||
datapoints: [
|
||||
[targetSLA[slaProperty.property], targetSLA.from * 1000],
|
||||
[targetSLA[slaProperty.property], targetSLA.to * 1000]
|
||||
]
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function convertHistoryPoint(point) {
|
||||
// Value must be a number for properly work
|
||||
return [
|
||||
Number(point.value),
|
||||
point.clock * 1000
|
||||
];
|
||||
}
|
||||
|
||||
function convertTrendPoint(valueType, point) {
|
||||
var value;
|
||||
switch (valueType) {
|
||||
case "min":
|
||||
value = point.value_min;
|
||||
break;
|
||||
case "max":
|
||||
value = point.value_max;
|
||||
break;
|
||||
case "avg":
|
||||
value = point.value_avg;
|
||||
break;
|
||||
default:
|
||||
value = point.value_avg;
|
||||
}
|
||||
|
||||
return [
|
||||
Number(value),
|
||||
point.clock * 1000
|
||||
];
|
||||
}
|
||||
|
||||
export default {
|
||||
handleHistory: handleHistory,
|
||||
convertHistory: convertHistory,
|
||||
handleTrends: handleTrends,
|
||||
handleSLAResponse: handleSLAResponse
|
||||
};
|
||||
Reference in New Issue
Block a user