2014-10-08 11:43:51 -04:00
|
|
|
define([
|
|
|
|
|
'angular',
|
|
|
|
|
'app',
|
|
|
|
|
'lodash',
|
|
|
|
|
'components/timeSeries',
|
|
|
|
|
'kbn',
|
2014-11-08 16:27:49 +01:00
|
|
|
'components/panelmeta',
|
2014-10-08 11:43:51 -04:00
|
|
|
'services/panelSrv',
|
2014-11-06 14:07:32 +01:00
|
|
|
'./singleStatPanel',
|
2014-10-08 11:43:51 -04:00
|
|
|
],
|
2014-11-08 16:27:49 +01:00
|
|
|
function (angular, app, _, TimeSeries, kbn, PanelMeta) {
|
2014-10-08 11:43:51 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
2014-11-06 14:07:32 +01:00
|
|
|
var module = angular.module('grafana.panels.singlestat');
|
2014-10-08 11:43:51 -04:00
|
|
|
app.useModule(module);
|
|
|
|
|
|
2014-11-06 14:07:32 +01:00
|
|
|
module.controller('SingleStatCtrl', function($scope, panelSrv, timeSrv) {
|
2014-10-08 11:43:51 -04:00
|
|
|
|
2014-11-08 16:27:49 +01:00
|
|
|
$scope.panelMeta = new PanelMeta({
|
|
|
|
|
description: 'Singlestat panel',
|
2014-10-08 11:43:51 -04:00
|
|
|
titlePos: 'left',
|
2014-11-08 16:27:49 +01:00
|
|
|
fullscreen: true,
|
|
|
|
|
metricsEditor: true
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$scope.panelMeta.addEditorTab('Options', 'app/panels/singlestat/editor.html');
|
2014-10-08 11:43:51 -04:00
|
|
|
|
|
|
|
|
// Set and populate defaults
|
|
|
|
|
var _d = {
|
2014-11-06 16:25:46 +01:00
|
|
|
maxDataPoints: 100,
|
|
|
|
|
interval: null,
|
2014-10-15 17:29:47 -04:00
|
|
|
targets: [{}],
|
|
|
|
|
cacheTimeout: null,
|
2014-10-15 18:16:04 -04:00
|
|
|
format: 'none',
|
2014-11-06 09:56:50 +01:00
|
|
|
prefix: '',
|
|
|
|
|
postfix: '',
|
|
|
|
|
valueName: 'avg',
|
2014-11-06 10:47:46 +01:00
|
|
|
prefixFontSize: '50%',
|
|
|
|
|
valueFontSize: '100%',
|
|
|
|
|
postfixFontSize: '50%',
|
2014-10-19 19:36:59 -04:00
|
|
|
thresholds: '',
|
|
|
|
|
colorBackground: false,
|
|
|
|
|
colorValue: false,
|
|
|
|
|
colors: ["rgba(245, 54, 54, 0.9)", "rgba(237, 129, 40, 0.89)", "rgba(50, 172, 45, 0.97)"],
|
|
|
|
|
sparkline: {
|
|
|
|
|
show: false,
|
|
|
|
|
full: false,
|
|
|
|
|
lineColor: 'rgb(31, 120, 193)',
|
2014-11-06 13:57:16 +01:00
|
|
|
fillColor: 'rgba(31, 118, 189, 0.18)',
|
2014-10-16 13:44:52 -04:00
|
|
|
}
|
2014-10-08 11:43:51 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_.defaults($scope.panel, _d);
|
|
|
|
|
|
|
|
|
|
$scope.init = function() {
|
|
|
|
|
panelSrv.init($scope);
|
2014-10-15 17:29:47 -04:00
|
|
|
$scope.$on('refresh', $scope.get_data);
|
2014-10-08 11:43:51 -04:00
|
|
|
};
|
|
|
|
|
|
2014-10-15 17:29:47 -04:00
|
|
|
$scope.updateTimeRange = function () {
|
|
|
|
|
$scope.range = timeSrv.timeRange();
|
2014-10-08 11:43:51 -04:00
|
|
|
$scope.rangeUnparsed = timeSrv.timeRange(false);
|
2014-11-06 16:25:46 +01:00
|
|
|
$scope.resolution = $scope.panel.maxDataPoints;
|
|
|
|
|
$scope.interval = kbn.calculateInterval($scope.range, $scope.resolution, $scope.panel.interval);
|
2014-10-15 17:29:47 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.get_data = function() {
|
|
|
|
|
$scope.updateTimeRange();
|
2014-10-08 11:43:51 -04:00
|
|
|
|
|
|
|
|
var metricsQuery = {
|
|
|
|
|
range: $scope.rangeUnparsed,
|
2014-11-06 16:25:46 +01:00
|
|
|
interval: $scope.interval,
|
2014-10-08 11:43:51 -04:00
|
|
|
targets: $scope.panel.targets,
|
2014-11-06 16:25:46 +01:00
|
|
|
maxDataPoints: $scope.resolution,
|
2014-10-15 17:29:47 -04:00
|
|
|
cacheTimeout: $scope.panel.cacheTimeout
|
2014-10-08 11:43:51 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return $scope.datasource.query(metricsQuery)
|
|
|
|
|
.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;
|
2014-10-15 17:29:47 -04:00
|
|
|
$scope.render();
|
2014-10-08 11:43:51 -04:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.dataHandler = function(results) {
|
|
|
|
|
$scope.panelMeta.loading = false;
|
2014-10-15 18:16:04 -04:00
|
|
|
$scope.series = _.map(results.data, $scope.seriesHandler);
|
2014-10-15 17:29:47 -04:00
|
|
|
$scope.render();
|
2014-10-08 11:43:51 -04:00
|
|
|
};
|
|
|
|
|
|
2014-10-19 19:36:59 -04:00
|
|
|
$scope.seriesHandler = function(seriesData) {
|
2014-10-08 11:43:51 -04:00
|
|
|
var series = new TimeSeries({
|
2014-10-19 19:36:59 -04:00
|
|
|
datapoints: seriesData.datapoints,
|
|
|
|
|
info: { alias: seriesData.target },
|
2014-10-08 11:43:51 -04:00
|
|
|
});
|
|
|
|
|
|
2014-11-06 09:56:50 +01:00
|
|
|
series.flotpairs = series.getFlotPairs('connected');
|
2014-10-08 11:43:51 -04:00
|
|
|
|
|
|
|
|
return series;
|
|
|
|
|
};
|
|
|
|
|
|
2014-10-17 10:36:04 -04:00
|
|
|
$scope.setColoring = function(options) {
|
|
|
|
|
if (options.background) {
|
2014-10-19 19:36:59 -04:00
|
|
|
$scope.panel.colorValue = false;
|
|
|
|
|
$scope.panel.colors = ['rgba(71, 212, 59, 0.4)', 'rgba(245, 150, 40, 0.73)', 'rgba(225, 40, 40, 0.59)'];
|
2014-10-17 10:36:04 -04:00
|
|
|
}
|
|
|
|
|
else {
|
2014-10-19 19:36:59 -04:00
|
|
|
$scope.panel.colorBackground = false;
|
|
|
|
|
$scope.panel.colors = ['rgba(50, 172, 45, 0.97)', 'rgba(237, 129, 40, 0.89)', 'rgba(245, 54, 54, 0.9)'];
|
2014-10-17 10:36:04 -04:00
|
|
|
}
|
|
|
|
|
$scope.render();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.invertColorOrder = function() {
|
2014-10-19 19:36:59 -04:00
|
|
|
var tmp = $scope.panel.colors[0];
|
|
|
|
|
$scope.panel.colors[0] = $scope.panel.colors[2];
|
|
|
|
|
$scope.panel.colors[2] = tmp;
|
2014-10-17 10:36:04 -04:00
|
|
|
$scope.render();
|
|
|
|
|
};
|
|
|
|
|
|
2014-11-06 12:17:46 +01:00
|
|
|
$scope.getDecimalsForValue = function(value) {
|
|
|
|
|
var opts = {};
|
|
|
|
|
|
|
|
|
|
var delta = value / 2;
|
|
|
|
|
var dec = -Math.floor(Math.log(delta) / Math.LN10);
|
|
|
|
|
|
|
|
|
|
var magn = Math.pow(10, -dec),
|
|
|
|
|
norm = delta / magn, // norm is between 1.0 and 10.0
|
|
|
|
|
size;
|
|
|
|
|
|
|
|
|
|
if (norm < 1.5) {
|
|
|
|
|
size = 1;
|
|
|
|
|
} else if (norm < 3) {
|
|
|
|
|
size = 2;
|
|
|
|
|
// special case for 2.5, requires an extra decimal
|
|
|
|
|
if (norm > 2.25) {
|
|
|
|
|
size = 2.5;
|
|
|
|
|
++dec;
|
|
|
|
|
}
|
|
|
|
|
} else if (norm < 7.5) {
|
|
|
|
|
size = 5;
|
|
|
|
|
} else {
|
|
|
|
|
size = 10;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size *= magn;
|
|
|
|
|
|
|
|
|
|
if (opts.minTickSize != null && size < opts.minTickSize) {
|
|
|
|
|
size = opts.minTickSize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = {};
|
|
|
|
|
result.decimals = Math.max(0, dec);
|
|
|
|
|
result.scaledDecimals = result.decimals - Math.floor(Math.log(size) / Math.LN10);
|
2014-11-07 13:54:19 +01:00
|
|
|
|
2014-11-06 12:17:46 +01:00
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
|
2014-10-08 11:43:51 -04:00
|
|
|
$scope.render = function() {
|
2014-11-06 09:56:50 +01:00
|
|
|
var data = {};
|
2014-10-15 18:16:04 -04:00
|
|
|
|
2014-11-06 09:56:50 +01:00
|
|
|
if (!$scope.series || $scope.series.length === 0) {
|
2014-11-06 12:30:42 +01:00
|
|
|
data.flotpairs = [];
|
|
|
|
|
data.mainValue = Number.NaN;
|
|
|
|
|
data.mainValueFormated = 'NaN';
|
2014-11-06 09:56:50 +01:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
var series = $scope.series[0];
|
|
|
|
|
data.mainValue = series.stats[$scope.panel.valueName];
|
2014-11-06 12:17:46 +01:00
|
|
|
var decimalInfo = $scope.getDecimalsForValue(data.mainValue);
|
|
|
|
|
var formatFunc = kbn.valueFormats[$scope.panel.format];
|
|
|
|
|
|
|
|
|
|
data.mainValueFormated = formatFunc(data.mainValue, decimalInfo.decimals, decimalInfo.scaledDecimals);
|
2014-11-06 09:56:50 +01:00
|
|
|
data.flotpairs = series.flotpairs;
|
2014-10-16 11:16:20 -04:00
|
|
|
}
|
|
|
|
|
|
2014-10-19 19:36:59 -04:00
|
|
|
data.thresholds = $scope.panel.thresholds.split(',').map(function(strVale) {
|
2014-10-17 10:36:04 -04:00
|
|
|
return Number(strVale.trim());
|
|
|
|
|
});
|
|
|
|
|
|
2014-10-19 19:36:59 -04:00
|
|
|
data.colorMap = $scope.panel.colors;
|
2014-10-17 10:36:04 -04:00
|
|
|
|
2014-10-15 18:16:04 -04:00
|
|
|
$scope.data = data;
|
2014-10-15 17:29:47 -04:00
|
|
|
$scope.$emit('render');
|
2014-10-08 11:43:51 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.init();
|
|
|
|
|
});
|
|
|
|
|
});
|