Commit 2c3d3d0f by Torkel Ödegaard

Refactoring out common panel metric and query code to a panelHelper service

parent d0d995da
......@@ -2,5 +2,6 @@ define([
'./panelMenu',
'./panelDirective',
'./panelSrv',
'./panelHelper',
'./soloPanelCtrl',
], function () {});
define([
'angular',
'lodash',
'kbn',
'jquery',
],
function (angular, _, kbn, $) {
'use strict';
var module = angular.module('grafana.services');
module.service('panelHelper', function(timeSrv) {
this.updateTimeRange = function(scope) {
scope.range = timeSrv.timeRange();
scope.rangeUnparsed = timeSrv.timeRange(false);
this.applyPanelTimeOverrides(scope);
if (scope.panel.maxDataPoints) {
scope.resolution = scope.panel.maxDataPoints;
}
else {
scope.resolution = Math.ceil($(window).width() * (scope.panel.span / 12));
}
scope.interval = kbn.calculateInterval(scope.range, scope.resolution, scope.panel.interval);
};
this.applyPanelTimeOverrides = function(scope) {
scope.panelMeta.timeInfo = '';
// check panel time overrrides
if (scope.panel.timeFrom) {
if (!kbn.isValidTimeSpan(scope.panel.timeFrom)) {
scope.panelMeta.timeInfo = 'invalid time override';
return;
}
if (_.isString(scope.rangeUnparsed.from)) {
scope.panelMeta.timeInfo = "last " + scope.panel.timeFrom;
scope.rangeUnparsed.from = 'now-' + scope.panel.timeFrom;
scope.range.from = kbn.parseDate(scope.rangeUnparsed.from);
}
}
if (scope.panel.timeShift) {
if (!kbn.isValidTimeSpan(scope.panel.timeFrom)) {
scope.panelMeta.timeInfo = 'invalid timeshift';
return;
}
var timeShift = '-' + scope.panel.timeShift;
scope.panelMeta.timeInfo += ' timeshift ' + timeShift;
scope.range.from = kbn.parseDateMath(timeShift, scope.range.from);
scope.range.to = kbn.parseDateMath(timeShift, scope.range.to);
scope.rangeUnparsed = scope.range;
}
};
this.issueMetricQuery = function(scope, datasource) {
var metricsQuery = {
range: scope.rangeUnparsed,
interval: scope.interval,
targets: scope.panel.targets,
format: scope.panel.renderer === 'png' ? 'png' : 'json',
maxDataPoints: scope.resolution,
cacheTimeout: scope.panel.cacheTimeout
};
return datasource.query(metricsQuery);
};
});
});
......@@ -7,6 +7,7 @@ function (angular, _, config) {
'use strict';
var module = angular.module('grafana.services');
module.service('panelSrv', function($rootScope, $timeout, datasourceSrv) {
this.init = function($scope) {
......@@ -99,10 +100,14 @@ function (angular, _, config) {
datasourceSrv.get($scope.panel.datasource).then(function(datasource) {
$scope.datasource = datasource;
return $scope.refreshData($scope.datasource);
return $scope.refreshData($scope.datasource).then(function() {
$scope.panelMeta.loading = false;
});
}, function(err) {
console.log('Panel data error:', err);
$scope.panelMeta.loading = false;
$scope.panelMeta.error = err.message;
$scope.panelMeta.error = err.message || "Timeseries data request error";
$scope.inspector.error = err;
});
};
......
......@@ -23,7 +23,7 @@ function (angular, app, $, _, kbn, moment, TimeSeries, PanelMeta) {
};
});
module.controller('GraphCtrl', function($scope, $rootScope, panelSrv, annotationsSrv, timeSrv) {
module.controller('GraphCtrl', function($scope, $rootScope, panelSrv, annotationsSrv, panelHelper) {
$scope.panelMeta = new PanelMeta({
panelName: 'Graph',
......@@ -123,72 +123,14 @@ function (angular, app, $, _, kbn, moment, TimeSeries, PanelMeta) {
$scope.render();
};
$scope.applyPanelTimeOverrides = function() {
$scope.panelMeta.timeInfo = '';
// check panel time overrrides
if ($scope.panel.timeFrom) {
if (!kbn.isValidTimeSpan($scope.panel.timeFrom)) {
$scope.panelMeta.timeInfo = 'invalid time override';
return;
}
if (_.isString($scope.rangeUnparsed.from)) {
$scope.panelMeta.timeInfo = "last " + $scope.panel.timeFrom;
$scope.rangeUnparsed.from = 'now-' + $scope.panel.timeFrom;
$scope.range.from = kbn.parseDate($scope.rangeUnparsed.from);
}
}
if ($scope.panel.timeShift) {
if (!kbn.isValidTimeSpan($scope.panel.timeFrom)) {
$scope.panelMeta.timeInfo = 'invalid timeshift';
return;
}
var timeShift = '-' + $scope.panel.timeShift;
$scope.panelMeta.timeInfo += ' timeshift ' + timeShift;
$scope.range.from = kbn.parseDateMath(timeShift, $scope.range.from);
$scope.range.to = kbn.parseDateMath(timeShift, $scope.range.to);
$scope.rangeUnparsed = $scope.range;
}
};
$scope.updateTimeRange = function () {
$scope.range = timeSrv.timeRange();
$scope.rangeUnparsed = timeSrv.timeRange(false);
$scope.applyPanelTimeOverrides();
if ($scope.panel.maxDataPoints) {
$scope.resolution = $scope.panel.maxDataPoints;
}
else {
$scope.resolution = Math.ceil($(window).width() * ($scope.panel.span / 12));
}
$scope.interval = kbn.calculateInterval($scope.range, $scope.resolution, $scope.panel.interval);
};
$scope.refreshData = function(datasource) {
$scope.updateTimeRange();
var metricsQuery = {
range: $scope.rangeUnparsed,
interval: $scope.interval,
targets: $scope.panel.targets,
format: $scope.panel.renderer === 'png' ? 'png' : 'json',
maxDataPoints: $scope.resolution,
cacheTimeout: $scope.panel.cacheTimeout
};
panelHelper.updateTimeRange($scope);
$scope.annotationsPromise = annotationsSrv.getAnnotations($scope.rangeUnparsed, $scope.dashboard);
return datasource.query(metricsQuery)
return panelHelper.issueMetricQuery($scope, datasource)
.then($scope.dataHandler)
.then(null, function(err) {
$scope.panelMeta.loading = false;
$scope.panelMeta.error = err.message || "Timeseries data request error";
$scope.inspector.error = err;
.then(null, function() {
$scope.seriesList = [];
$scope.render([]);
});
......@@ -197,7 +139,6 @@ function (angular, app, $, _, kbn, moment, TimeSeries, PanelMeta) {
$scope.dataHandler = function(results) {
// png renderer returns just a url
if (_.isString(results)) {
$scope.panelMeta.loading = false;
$scope.render(results);
return;
}
......
......@@ -20,7 +20,7 @@ function (angular, app, _, TimeSeries, kbn, PanelMeta) {
};
});
module.controller('SingleStatCtrl', function($scope, panelSrv, timeSrv) {
module.controller('SingleStatCtrl', function($scope, panelSrv, panelHelper) {
$scope.panelMeta = new PanelMeta({
panelName: 'Singlestat',
......@@ -32,6 +32,7 @@ function (angular, app, _, TimeSeries, kbn, PanelMeta) {
$scope.fontSizes = ['20%', '30%','50%','70%','80%','100%', '110%', '120%', '150%', '170%', '200%'];
$scope.panelMeta.addEditorTab('Options', 'app/panels/singlestat/editor.html');
$scope.panelMeta.addEditorTab('Time range', 'app/features/panel/partials/panelTime.html');
// Set and populate defaults
var _d = {
......@@ -76,31 +77,12 @@ function (angular, app, _, TimeSeries, kbn, PanelMeta) {
panelSrv.init($scope);
};
$scope.updateTimeRange = function () {
$scope.range = timeSrv.timeRange();
$scope.rangeUnparsed = timeSrv.timeRange(false);
$scope.resolution = $scope.panel.maxDataPoints;
$scope.interval = kbn.calculateInterval($scope.range, $scope.resolution, $scope.panel.interval);
};
$scope.refreshData = function(datasource) {
$scope.updateTimeRange();
var metricsQuery = {
range: $scope.rangeUnparsed,
interval: $scope.interval,
targets: $scope.panel.targets,
maxDataPoints: $scope.resolution,
cacheTimeout: $scope.panel.cacheTimeout
};
panelHelper.updateTimeRange($scope);
return datasource.query(metricsQuery)
return panelHelper.issueMetricQuery($scope, datasource)
.then($scope.dataHandler)
.then(null, function(err) {
console.log("err");
$scope.panelMeta.loading = false;
$scope.panelMeta.error = err.message || "Timeseries data request error";
$scope.inspector.error = err;
.then(null, function() {
$scope.render();
});
};
......
define([
'helpers',
'features/panel/panelSrv',
'features/panel/panelHelper',
'panels/graph/module'
], function(helpers) {
'use strict';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment