Build query in asynchronous manner (fixed getting data at first load).
This commit is contained in:
@@ -122,72 +122,74 @@ function (angular, _, dateMath, utils, metricFunctions) {
|
|||||||
// Query numeric data
|
// Query numeric data
|
||||||
if (!target.mode || target.mode === 0) {
|
if (!target.mode || target.mode === 0) {
|
||||||
|
|
||||||
var items = self.queryProcessor.build(groupFilter, hostFilter, appFilter, itemFilter);
|
// Build query in asynchronous manner
|
||||||
|
return self.queryProcessor.build(groupFilter, hostFilter, appFilter, itemFilter)
|
||||||
|
.then(function(items) {
|
||||||
|
// Add hostname for items from multiple hosts
|
||||||
|
var addHostName = target.host.isRegex;
|
||||||
|
|
||||||
// Add hostname for items from multiple hosts
|
var getHistory;
|
||||||
var addHostName = target.host.isRegex;
|
if ((from < useTrendsFrom) && self.trends) {
|
||||||
|
|
||||||
var getHistory;
|
// Use trends
|
||||||
if ((from < useTrendsFrom) && self.trends) {
|
var valueType = target.downsampleFunction ? target.downsampleFunction.value : "avg";
|
||||||
|
getHistory = self.zabbixAPI.getTrends(items, from, to).then(function(history) {
|
||||||
|
return self.queryProcessor.handleTrends(history, addHostName, valueType);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
|
||||||
// Use trends
|
// Use history
|
||||||
var valueType = target.downsampleFunction ? target.downsampleFunction.value : "avg";
|
getHistory = self.zabbixAPI.getHistory(items, from, to).then(function(history) {
|
||||||
getHistory = self.zabbixAPI.getTrends(items, from, to).then(function(history) {
|
return self.queryProcessor.handleHistory(history, addHostName);
|
||||||
return self.queryProcessor.handleTrends(history, addHostName, valueType);
|
});
|
||||||
});
|
|
||||||
} else {
|
|
||||||
|
|
||||||
// Use history
|
|
||||||
getHistory = self.zabbixAPI.getHistory(items, from, to).then(function(history) {
|
|
||||||
return self.queryProcessor.handleHistory(history, addHostName);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return getHistory.then(function (timeseries_data) {
|
|
||||||
timeseries_data = _.map(timeseries_data, function (timeseries) {
|
|
||||||
|
|
||||||
// Filter only transform functions
|
|
||||||
var transformFunctions = _.map(metricFunctions.getCategories()['Transform'], 'name');
|
|
||||||
var transFuncDefs = _.filter(target.functions, function(func) {
|
|
||||||
return _.contains(transformFunctions, func.def.name);
|
|
||||||
});
|
|
||||||
var functions = _.map(transFuncDefs, function(func) {
|
|
||||||
var funcInstance = metricFunctions.createFuncInstance(func.def, func.params);
|
|
||||||
return funcInstance.bindFunction(DataProcessingService.metricFunctions);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Metric data processing
|
|
||||||
var dp = timeseries.datapoints;
|
|
||||||
for (var i = 0; i < functions.length; i++) {
|
|
||||||
dp = functions[i](dp);
|
|
||||||
}
|
}
|
||||||
timeseries.datapoints = dp;
|
|
||||||
|
|
||||||
return timeseries;
|
return getHistory.then(function (timeseries_data) {
|
||||||
});
|
timeseries_data = _.map(timeseries_data, function (timeseries) {
|
||||||
|
|
||||||
// Aggregations
|
// Filter only transform functions
|
||||||
var aggregationFunctions = _.map(metricFunctions.getCategories()['Aggregate'], 'name');
|
var transformFunctions = _.map(metricFunctions.getCategories()['Transform'], 'name');
|
||||||
var aggFuncDefs = _.filter(target.functions, function(func) {
|
var transFuncDefs = _.filter(target.functions, function(func) {
|
||||||
return _.contains(aggregationFunctions, func.def.name);
|
return _.contains(transformFunctions, func.def.name);
|
||||||
});
|
});
|
||||||
var functions = _.map(aggFuncDefs, function(func) {
|
var functions = _.map(transFuncDefs, function(func) {
|
||||||
var funcInstance = metricFunctions.createFuncInstance(func.def, func.params);
|
var funcInstance = metricFunctions.createFuncInstance(func.def, func.params);
|
||||||
return funcInstance.bindFunction(DataProcessingService.metricFunctions);
|
return funcInstance.bindFunction(DataProcessingService.metricFunctions);
|
||||||
});
|
});
|
||||||
var dp = _.map(timeseries_data, 'datapoints');
|
|
||||||
|
|
||||||
if (functions.length) {
|
// Metric data processing
|
||||||
for (var i = 0; i < functions.length; i++) {
|
var dp = timeseries.datapoints;
|
||||||
dp = functions[i](dp);
|
for (var i = 0; i < functions.length; i++) {
|
||||||
}
|
dp = functions[i](dp);
|
||||||
timeseries_data = {
|
}
|
||||||
target: 'agg',
|
timeseries.datapoints = dp;
|
||||||
datapoints: dp
|
|
||||||
};
|
return timeseries;
|
||||||
}
|
});
|
||||||
return timeseries_data;
|
|
||||||
});
|
// Aggregations
|
||||||
|
var aggregationFunctions = _.map(metricFunctions.getCategories()['Aggregate'], 'name');
|
||||||
|
var aggFuncDefs = _.filter(target.functions, function(func) {
|
||||||
|
return _.contains(aggregationFunctions, func.def.name);
|
||||||
|
});
|
||||||
|
var functions = _.map(aggFuncDefs, function(func) {
|
||||||
|
var funcInstance = metricFunctions.createFuncInstance(func.def, func.params);
|
||||||
|
return funcInstance.bindFunction(DataProcessingService.metricFunctions);
|
||||||
|
});
|
||||||
|
var dp = _.map(timeseries_data, 'datapoints');
|
||||||
|
|
||||||
|
if (functions.length) {
|
||||||
|
for (var i = 0; i < functions.length; i++) {
|
||||||
|
dp = functions[i](dp);
|
||||||
|
}
|
||||||
|
timeseries_data = {
|
||||||
|
target: 'agg',
|
||||||
|
datapoints: dp
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return timeseries_data;
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Query text data
|
// Query text data
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ define([
|
|||||||
zabbixCache.refresh().then(function () {
|
zabbixCache.refresh().then(function () {
|
||||||
$scope.getMetricsFromCache();
|
$scope.getMetricsFromCache();
|
||||||
$scope.initFilters();
|
$scope.initFilters();
|
||||||
console.log("From server", $scope.metric);
|
//console.log("From server", $scope.metric);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ function (angular, _, utils) {
|
|||||||
|
|
||||||
var module = angular.module('grafana.services');
|
var module = angular.module('grafana.services');
|
||||||
|
|
||||||
module.factory('QueryProcessor', function() {
|
module.factory('QueryProcessor', function($q) {
|
||||||
|
|
||||||
function QueryProcessor(zabbixCacheInstance) {
|
function QueryProcessor(zabbixCacheInstance) {
|
||||||
var self = this;
|
var self = this;
|
||||||
@@ -16,9 +16,22 @@ function (angular, _, utils) {
|
|||||||
this.cache = zabbixCacheInstance;
|
this.cache = zabbixCacheInstance;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Build query - convert target filters to array of Zabbix items
|
* Build query in asynchronous manner
|
||||||
*/
|
*/
|
||||||
this.build = function (groupFilter, hostFilter, appFilter, itemFilter) {
|
this.build = function (groupFilter, hostFilter, appFilter, itemFilter) {
|
||||||
|
if (this.cache._initialized) {
|
||||||
|
return $q.when(self.buildFromCache(groupFilter, hostFilter, appFilter, itemFilter));
|
||||||
|
} else {
|
||||||
|
return this.cache.refresh().then(function() {
|
||||||
|
return self.buildFromCache(groupFilter, hostFilter, appFilter, itemFilter);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build query - convert target filters to array of Zabbix items
|
||||||
|
*/
|
||||||
|
this.buildFromCache = function (groupFilter, hostFilter, appFilter, itemFilter) {
|
||||||
|
|
||||||
// Find items by item names and perform queries
|
// Find items by item names and perform queries
|
||||||
var groups = [];
|
var groups = [];
|
||||||
|
|||||||
Reference in New Issue
Block a user