module.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. define([
  2. 'angular',
  3. 'app',
  4. 'lodash',
  5. 'components/timeSeries',
  6. 'kbn',
  7. 'components/panelmeta',
  8. './singleStatPanel',
  9. ],
  10. function (angular, app, _, TimeSeries, kbn, PanelMeta) {
  11. 'use strict';
  12. var module = angular.module('grafana.panels.singlestat');
  13. app.useModule(module);
  14. module.directive('grafanaPanelSinglestat', function() {
  15. return {
  16. controller: 'SingleStatCtrl',
  17. templateUrl: 'app/panels/singlestat/module.html',
  18. };
  19. });
  20. module.controller('SingleStatCtrl', function($scope, panelSrv, panelHelper) {
  21. $scope.panelMeta = new PanelMeta({
  22. panelName: 'Singlestat',
  23. editIcon: "fa fa-dashboard",
  24. fullscreen: true,
  25. metricsEditor: true
  26. });
  27. $scope.fontSizes = ['20%', '30%','50%','70%','80%','100%', '110%', '120%', '150%', '170%', '200%'];
  28. $scope.panelMeta.addEditorTab('Options', 'app/panels/singlestat/editor.html');
  29. $scope.panelMeta.addEditorTab('Time range', 'app/features/panel/partials/panelTime.html');
  30. // Set and populate defaults
  31. var _d = {
  32. links: [],
  33. maxDataPoints: 100,
  34. interval: null,
  35. targets: [{}],
  36. cacheTimeout: null,
  37. format: 'none',
  38. prefix: '',
  39. postfix: '',
  40. nullText: null,
  41. valueMaps: [
  42. { value: 'null', op: '=', text: 'N/A' }
  43. ],
  44. nullPointMode: 'connected',
  45. valueName: 'avg',
  46. prefixFontSize: '50%',
  47. valueFontSize: '80%',
  48. postfixFontSize: '50%',
  49. thresholds: '',
  50. colorBackground: false,
  51. colorValue: false,
  52. colors: ["rgba(245, 54, 54, 0.9)", "rgba(237, 129, 40, 0.89)", "rgba(50, 172, 45, 0.97)"],
  53. sparkline: {
  54. show: false,
  55. full: false,
  56. lineColor: 'rgb(31, 120, 193)',
  57. fillColor: 'rgba(31, 118, 189, 0.18)',
  58. }
  59. };
  60. _.defaults($scope.panel, _d);
  61. $scope.unitFormats = kbn.getUnitFormats();
  62. $scope.setUnitFormat = function(subItem) {
  63. $scope.panel.format = subItem.value;
  64. $scope.render();
  65. };
  66. $scope.init = function() {
  67. panelSrv.init($scope);
  68. };
  69. $scope.refreshData = function(datasource) {
  70. panelHelper.updateTimeRange($scope);
  71. return panelHelper.issueMetricQuery($scope, datasource)
  72. .then($scope.dataHandler, function(err) {
  73. $scope.series = [];
  74. $scope.render();
  75. throw err;
  76. });
  77. };
  78. $scope.loadSnapshot = function(snapshotData) {
  79. panelHelper.updateTimeRange($scope);
  80. $scope.dataHandler(snapshotData);
  81. };
  82. $scope.dataHandler = function(results) {
  83. $scope.series = _.map(results.data, $scope.seriesHandler);
  84. $scope.render();
  85. };
  86. $scope.seriesHandler = function(seriesData) {
  87. var series = new TimeSeries({
  88. datapoints: seriesData.datapoints,
  89. alias: seriesData.target,
  90. });
  91. series.flotpairs = series.getFlotPairs($scope.panel.nullPointMode);
  92. return series;
  93. };
  94. $scope.setColoring = function(options) {
  95. if (options.background) {
  96. $scope.panel.colorValue = false;
  97. $scope.panel.colors = ['rgba(71, 212, 59, 0.4)', 'rgba(245, 150, 40, 0.73)', 'rgba(225, 40, 40, 0.59)'];
  98. }
  99. else {
  100. $scope.panel.colorBackground = false;
  101. $scope.panel.colors = ['rgba(50, 172, 45, 0.97)', 'rgba(237, 129, 40, 0.89)', 'rgba(245, 54, 54, 0.9)'];
  102. }
  103. $scope.render();
  104. };
  105. $scope.invertColorOrder = function() {
  106. var tmp = $scope.panel.colors[0];
  107. $scope.panel.colors[0] = $scope.panel.colors[2];
  108. $scope.panel.colors[2] = tmp;
  109. $scope.render();
  110. };
  111. $scope.getDecimalsForValue = function(value) {
  112. if (_.isNumber($scope.panel.decimals)) {
  113. return { decimals: $scope.panel.decimals, scaledDecimals: null };
  114. }
  115. var delta = value / 2;
  116. var dec = -Math.floor(Math.log(delta) / Math.LN10);
  117. var magn = Math.pow(10, -dec),
  118. norm = delta / magn, // norm is between 1.0 and 10.0
  119. size;
  120. if (norm < 1.5) {
  121. size = 1;
  122. } else if (norm < 3) {
  123. size = 2;
  124. // special case for 2.5, requires an extra decimal
  125. if (norm > 2.25) {
  126. size = 2.5;
  127. ++dec;
  128. }
  129. } else if (norm < 7.5) {
  130. size = 5;
  131. } else {
  132. size = 10;
  133. }
  134. size *= magn;
  135. // reduce starting decimals if not needed
  136. if (Math.floor(value) === value) { dec = 0; }
  137. var result = {};
  138. result.decimals = Math.max(0, dec);
  139. result.scaledDecimals = result.decimals - Math.floor(Math.log(size) / Math.LN10) + 2;
  140. return result;
  141. };
  142. $scope.render = function() {
  143. var data = {};
  144. $scope.setValues(data);
  145. data.thresholds = $scope.panel.thresholds.split(',').map(function(strVale) {
  146. return Number(strVale.trim());
  147. });
  148. data.colorMap = $scope.panel.colors;
  149. $scope.data = data;
  150. $scope.$broadcast('render');
  151. };
  152. $scope.setValues = function(data) {
  153. data.flotpairs = [];
  154. if ($scope.series && $scope.series.length > 0) {
  155. var lastPoint = _.last($scope.series[0].datapoints);
  156. var lastValue = _.isArray(lastPoint) ? lastPoint[0] : null;
  157. if (_.isString(lastValue)) {
  158. data.value = 0;
  159. data.valueFormated = lastValue;
  160. data.valueRounded = 0;
  161. } else {
  162. data.value = $scope.series[0].stats[$scope.panel.valueName];
  163. data.flotpairs = $scope.series[0].flotpairs;
  164. var decimalInfo = $scope.getDecimalsForValue(data.value);
  165. var formatFunc = kbn.valueFormats[$scope.panel.format];
  166. data.valueFormated = formatFunc(data.value, decimalInfo.decimals, decimalInfo.scaledDecimals);
  167. data.valueRounded = kbn.roundValue(data.value, decimalInfo.decimals);
  168. }
  169. }
  170. // check value to text mappings
  171. for(var i = 0; i < $scope.panel.valueMaps.length; i++) {
  172. var map = $scope.panel.valueMaps[i];
  173. // special null case
  174. if (map.value === 'null') {
  175. if (data.value === null || data.value === void 0) {
  176. data.valueFormated = map.text;
  177. return;
  178. }
  179. continue;
  180. }
  181. // value/number to text mapping
  182. var value = parseFloat(map.value);
  183. if (value === data.value) {
  184. data.valueFormated = map.text;
  185. return;
  186. }
  187. }
  188. if (data.value === null || data.value === void 0) {
  189. data.valueFormated = "no value";
  190. }
  191. };
  192. $scope.removeValueMap = function(map) {
  193. var index = _.indexOf($scope.panel.valueMaps, map);
  194. $scope.panel.valueMaps.splice(index, 1);
  195. $scope.render();
  196. };
  197. $scope.addValueMap = function() {
  198. $scope.panel.valueMaps.push({value: '', op: '=', text: '' });
  199. };
  200. $scope.init();
  201. });
  202. });