Refactor: datasource.js

This commit is contained in:
Alexander Zobnin
2016-11-13 18:41:10 +03:00
parent 834b953e41
commit 6e0f59cd69

View File

@@ -58,12 +58,12 @@ class ZabbixAPIDatasource {
// Create request for each target // Create request for each target
var promises = _.map(options.targets, target => { var promises = _.map(options.targets, target => {
// Prevent changes of original object // Prevent changes of original object
target = _.cloneDeep(target); target = _.cloneDeep(target);
this.replaceTargetVariables(target, options);
// Metrics or Text query mode
if (target.mode !== 1) { if (target.mode !== 1) {
// Migrate old targets // Migrate old targets
target = migrations.migrate(target); target = migrations.migrate(target);
@@ -72,30 +72,9 @@ class ZabbixAPIDatasource {
return []; return [];
} }
// Replace templated variables
target.group.filter = this.replaceTemplateVars(target.group.filter, options.scopedVars);
target.host.filter = this.replaceTemplateVars(target.host.filter, options.scopedVars);
target.application.filter = this.replaceTemplateVars(target.application.filter, options.scopedVars);
target.item.filter = this.replaceTemplateVars(target.item.filter, options.scopedVars);
target.textFilter = this.replaceTemplateVars(target.textFilter, options.scopedVars);
_.forEach(target.functions, func => {
func.params = _.map(func.params, param => {
if (typeof param === 'number') {
return +this.templateSrv.replace(param.toString(), options.scopedVars);
} else {
return this.templateSrv.replace(param, options.scopedVars);
}
});
});
// Query numeric data
if (!target.mode || target.mode === 0) { if (!target.mode || target.mode === 0) {
return this.queryNumericData(target, timeFrom, timeTo, useTrends); return this.queryNumericData(target, timeFrom, timeTo, useTrends);
} } else if (target.mode === 2) {
// Query text data
else if (target.mode === 2) {
return this.queryTextData(target, timeFrom, timeTo); return this.queryTextData(target, timeFrom, timeTo);
} }
} }
@@ -107,8 +86,7 @@ class ZabbixAPIDatasource {
return []; return [];
} }
return this.zabbix return this.zabbix.getSLA(target.itservice.serviceid, timeFrom, timeTo)
.getSLA(target.itservice.serviceid, timeFrom, timeTo)
.then(slaObject => { .then(slaObject => {
return responseHandler.handleSLAResponse(target.itservice, target.slaProperty, slaObject); return responseHandler.handleSLAResponse(target.itservice, target.slaProperty, slaObject);
}); });
@@ -119,15 +97,9 @@ class ZabbixAPIDatasource {
return Promise.all(_.flatten(promises)) return Promise.all(_.flatten(promises))
.then(_.flatten) .then(_.flatten)
.then(timeseries_data => { .then(timeseries_data => {
return downsampleSeries(timeseries_data, options);
// Series downsampling })
var data = _.map(timeseries_data, timeseries => { .then(data => {
if (timeseries.datapoints.length > options.maxDataPoints) {
timeseries.datapoints = DataProcessor
.groupBy(options.interval, DataProcessor.AVERAGE, timeseries.datapoints);
}
return timeseries;
});
return { data: data }; return { data: data };
}); });
} }
@@ -137,78 +109,75 @@ class ZabbixAPIDatasource {
itemtype: 'num' itemtype: 'num'
}; };
return this.zabbix.getItemsFromTarget(target, options) return this.zabbix.getItemsFromTarget(target, options)
.then(items => { .then(items => {
// Add hostname for items from multiple hosts let getHistoryPromise;
var addHostName = utils.isRegex(target.host.filter);
var getHistory;
// Use trends if (useTrends) {
if (useTrends) { let valueType = this.getTrendValueType(target);
getHistoryPromise = this.zabbix.getTrend(items, timeFrom, timeTo)
// Find trendValue() function and get specified trend value .then(history => {
var trendFunctions = _.map(metricFunctions.getCategories()['Trends'], 'name'); return responseHandler.handleTrends(history, items, valueType);
var trendValueFunc = _.find(target.functions, func => {
return _.includes(trendFunctions, func.def.name);
}); });
var valueType = trendValueFunc ? trendValueFunc.params[0] : "avg"; } else {
getHistory = this.zabbix
.getTrend(items, timeFrom, timeTo)
.then(history => {
return responseHandler.handleTrends(history, items, addHostName, valueType);
});
}
// Use history // Use history
else { getHistoryPromise = this.zabbix.getHistory(items, timeFrom, timeTo)
getHistory = this.zabbix .then(history => {
.getHistory(items, timeFrom, timeTo) return responseHandler.handleHistory(history, items);
.then(history => {
return responseHandler.handleHistory(history, items, addHostName);
});
}
return getHistory.then(timeseries_data => {
let transformFunctions = bindFunctionDefs(target.functions, 'Transform');
let aggregationFunctions = bindFunctionDefs(target.functions, 'Aggregate');
let filterFunctions = bindFunctionDefs(target.functions, 'Filter');
let aliasFunctions = bindFunctionDefs(target.functions, 'Alias');
// Apply transformation functions
timeseries_data = _.map(timeseries_data, timeseries => {
timeseries.datapoints = sequence(transformFunctions)(timeseries.datapoints);
return timeseries;
}); });
}
// Apply filter functions return getHistoryPromise.then(timeseries_data => {
if (filterFunctions.length) { return this.applyDataProcessingFunctions(timeseries_data, target);
timeseries_data = sequence(filterFunctions)(timeseries_data);
}
// Apply aggregations
if (aggregationFunctions.length) {
let dp = _.map(timeseries_data, 'datapoints');
dp = sequence(aggregationFunctions)(dp);
let aggFuncNames = _.map(metricFunctions.getCategories()['Aggregate'], 'name');
let lastAgg = _.findLast(target.functions, func => {
return _.includes(aggFuncNames, func.def.name);
});
timeseries_data = [
{
target: lastAgg.text,
datapoints: dp
}
];
}
// Apply alias functions
_.each(timeseries_data, sequence(aliasFunctions));
return timeseries_data;
});
}); });
});
}
getTrendValueType(target) {
// Find trendValue() function and get specified trend value
var trendFunctions = _.map(metricFunctions.getCategories()['Trends'], 'name');
var trendValueFunc = _.find(target.functions, func => {
return _.includes(trendFunctions, func.def.name);
});
return trendValueFunc ? trendValueFunc.params[0] : "avg";
}
applyDataProcessingFunctions(timeseries_data, target) {
let transformFunctions = bindFunctionDefs(target.functions, 'Transform');
let aggregationFunctions = bindFunctionDefs(target.functions, 'Aggregate');
let filterFunctions = bindFunctionDefs(target.functions, 'Filter');
let aliasFunctions = bindFunctionDefs(target.functions, 'Alias');
// Apply transformation functions
timeseries_data = _.map(timeseries_data, timeseries => {
timeseries.datapoints = sequence(transformFunctions)(timeseries.datapoints);
return timeseries;
});
// Apply filter functions
if (filterFunctions.length) {
timeseries_data = sequence(filterFunctions)(timeseries_data);
}
// Apply aggregations
if (aggregationFunctions.length) {
let dp = _.map(timeseries_data, 'datapoints');
dp = sequence(aggregationFunctions)(dp);
let aggFuncNames = _.map(metricFunctions.getCategories()['Aggregate'], 'name');
let lastAgg = _.findLast(target.functions, func => {
return _.includes(aggFuncNames, func.def.name);
});
timeseries_data = [{
target: lastAgg.text,
datapoints: dp
}];
}
// Apply alias functions
_.each(timeseries_data, sequence(aliasFunctions));
return timeseries_data;
} }
queryTextData(target, timeFrom, timeTo) { queryTextData(target, timeFrom, timeTo) {
@@ -320,7 +289,7 @@ class ZabbixAPIDatasource {
} }
return result.then(metrics => { return result.then(metrics => {
return _.map(metrics, formatMetric); return metrics.map(formatMetric);
}); });
} }
@@ -396,6 +365,25 @@ class ZabbixAPIDatasource {
}); });
} }
// Replace template variables
replaceTargetVariables(target, options) {
let parts = ['group', 'host', 'application', 'item'];
parts.forEach(p => {
target[p].filter = this.replaceTemplateVars(target[p].filter, options.scopedVars);
});
target.textFilter = this.replaceTemplateVars(target.textFilter, options.scopedVars);
target.functions.forEach(func => {
func.params = func.params.map(param => {
if (typeof param === 'number') {
return +this.templateSrv.replace(param.toString(), options.scopedVars);
} else {
return this.templateSrv.replace(param, options.scopedVars);
}
});
});
}
} }
function bindFunctionDefs(functionDefs, category) { function bindFunctionDefs(functionDefs, category) {
@@ -410,6 +398,16 @@ function bindFunctionDefs(functionDefs, category) {
}); });
} }
function downsampleSeries(timeseries_data, options) {
return _.map(timeseries_data, timeseries => {
if (timeseries.datapoints.length > options.maxDataPoints) {
timeseries.datapoints = DataProcessor
.groupBy(options.interval, DataProcessor.AVERAGE, timeseries.datapoints);
}
return timeseries;
});
}
function formatMetric(metricObj) { function formatMetric(metricObj) {
return { return {
text: metricObj.name, text: metricObj.name,