History and trends response handling moved to helperFunctions.js.

This commit is contained in:
Alexander Zobnin
2015-07-11 10:55:17 +03:00
parent 11cf488d71
commit 082d56e1a6
2 changed files with 100 additions and 88 deletions

View File

@@ -131,10 +131,10 @@ function (angular, _, kbn) {
if ((from < useTrendsFrom) && self.trends) { if ((from < useTrendsFrom) && self.trends) {
return self.zabbixAPI.getTrends(items, from, to) return self.zabbixAPI.getTrends(items, from, to)
.then(_.bind(self.handleTrendResponse, self, items, alias, target.scale)); .then(_.bind(zabbixHelperSrv.handleTrendResponse, self, items, alias, target.scale));
} else { } else {
return self.zabbixAPI.getHistory(items, from, to) return self.zabbixAPI.getHistory(items, from, to)
.then(_.bind(self.handleHistoryResponse, self, items, alias, target.scale)); .then(_.bind(zabbixHelperSrv.handleHistoryResponse, self, items, alias, target.scale));
} }
} }
}); });
@@ -155,91 +155,6 @@ function (angular, _, kbn) {
}); });
}; };
ZabbixAPIDatasource.prototype.handleTrendResponse = function (items, alias, scale, trends) {
// Group items and trends by itemid
var indexed_items = _.indexBy(items, 'itemid');
var grouped_history = _.groupBy(trends, 'itemid');
var self = this;
return $q.when(_.map(grouped_history, function (trends, itemid) {
var item = indexed_items[itemid];
var series = {
target: (item.hosts ? item.hosts[0].name+': ' : '')
+ (alias ? alias : self.zabbixAPI.expandItemName(item)),
datapoints: _.map(trends, function (p) {
// Value must be a number for properly work
var value = Number(p.value_avg);
// Apply scale
if (scale) {
value *= scale;
}
return [value, p.clock * 1000];
})
};
return series;
})).then(function (result) {
return _.sortBy(result, 'target');
});
};
/**
* Convert Zabbix API data to Grafana format
*
* @param {Array} items Array of Zabbix Items
* @param {Array} history Array of Zabbix History
*
* @return {Array} Array of timeseries in Grafana format
* {
* target: "Metric name",
* datapoints: [[<value>, <unixtime>], ...]
* }
*/
ZabbixAPIDatasource.prototype.handleHistoryResponse = function(items, alias, scale, history) {
/**
* Response should be in the format:
* data: [
* {
* target: "Metric name",
* datapoints: [[<value>, <unixtime>], ...]
* },
* {
* target: "Metric name",
* datapoints: [[<value>, <unixtime>], ...]
* },
* ]
*/
// Group items and history by itemid
var indexed_items = _.indexBy(items, 'itemid');
var grouped_history = _.groupBy(history, 'itemid');
var self = this;
return $q.when(_.map(grouped_history, function (history, itemid) {
var item = indexed_items[itemid];
var series = {
target: (item.hosts ? item.hosts[0].name+': ' : '')
+ (alias ? alias : self.zabbixAPI.expandItemName(item)),
datapoints: _.map(history, function (p) {
// Value must be a number for properly work
var value = Number(p.value);
// Apply scale
if (scale) {
value *= scale;
}
return [value, p.clock * 1000];
})
};
return series;
})).then(function (result) {
return _.sortBy(result, 'target');
});
};
//////////////// ////////////////
// Templating // // Templating //
//////////////// ////////////////

View File

@@ -7,9 +7,106 @@ function (angular, _) {
var module = angular.module('grafana.services'); var module = angular.module('grafana.services');
module.service('zabbixHelperSrv', function() { module.service('zabbixHelperSrv', function($q) {
var self = this; var self = this;
/**
* Convert Zabbix API history.get response to Grafana format
*
* @param {Array} items Array of Zabbix Items
* @param {Array} history Array of Zabbix History
*
* @return {Array} Array of timeseries in Grafana format
* {
* target: "Metric name",
* datapoints: [[<value>, <unixtime>], ...]
* }
*/
this.handleHistoryResponse = function(items, alias, scale, history) {
/**
* Response should be in the format:
* data: [
* {
* target: "Metric name",
* datapoints: [[<value>, <unixtime>], ...]
* },
* {
* target: "Metric name",
* datapoints: [[<value>, <unixtime>], ...]
* },
* ]
*/
// Group items and history by itemid
var indexed_items = _.indexBy(items, 'itemid');
var grouped_history = _.groupBy(history, 'itemid');
var self = this;
return $q.when(_.map(grouped_history, function (history, itemid) {
var item = indexed_items[itemid];
var series = {
target: (item.hosts ? item.hosts[0].name+': ' : '')
+ (alias ? alias : self.zabbixAPI.expandItemName(item)),
datapoints: _.map(history, function (p) {
// Value must be a number for properly work
var value = Number(p.value);
// Apply scale
if (scale) {
value *= scale;
}
return [value, p.clock * 1000];
})
};
return series;
})).then(function (result) {
return _.sortBy(result, 'target');
});
};
/**
* Convert Zabbix API trends.get response to Grafana format
*
* @param {Array} items Array of Zabbix Items
* @param {Array} trends Array of Zabbix Trends
*
* @return {Array} Array of timeseries in Grafana format
* {
* target: "Metric name",
* datapoints: [[<value>, <unixtime>], ...]
* }
*/
this.handleTrendResponse = function (items, alias, scale, trends) {
// Group items and trends by itemid
var indexed_items = _.indexBy(items, 'itemid');
var grouped_trends = _.groupBy(trends, 'itemid');
var self = this;
return $q.when(_.map(grouped_trends, function (trends, itemid) {
var item = indexed_items[itemid];
var series = {
target: (item.hosts ? item.hosts[0].name+': ' : '')
+ (alias ? alias : self.zabbixAPI.expandItemName(item)),
datapoints: _.map(trends, function (p) {
// Value must be a number for properly work
var value = Number(p.value_avg);
// Apply scale
if (scale) {
value *= scale;
}
return [value, p.clock * 1000];
})
};
return series;
})).then(function (result) {
return _.sortBy(result, 'target');
});
};
/** /**
* Convert multiple mettrics to array * Convert multiple mettrics to array
* "{metric1,metcic2,...,metricN}" --> [metric1, metcic2,..., metricN] * "{metric1,metcic2,...,metricN}" --> [metric1, metcic2,..., metricN]