module.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. define([
  2. 'angular',
  3. 'app/app',
  4. 'lodash',
  5. 'app/core/utils/kbn',
  6. 'app/core/time_series',
  7. 'app/features/panel/panel_meta',
  8. './singleStatPanel',
  9. ],
  10. function (angular, app, _, kbn, TimeSeries, 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/plugins/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/plugins/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. datasource: null,
  34. maxDataPoints: 100,
  35. interval: null,
  36. targets: [{}],
  37. cacheTimeout: null,
  38. format: 'none',
  39. prefix: '',
  40. postfix: '',
  41. nullText: null,
  42. valueMaps: [
  43. { value: 'null', op: '=', text: 'N/A' }
  44. ],
  45. nullPointMode: 'connected',
  46. valueName: 'avg',
  47. prefixFontSize: '50%',
  48. valueFontSize: '80%',
  49. postfixFontSize: '50%',
  50. thresholds: '',
  51. colorBackground: false,
  52. colorValue: false,
  53. colors: ["rgba(245, 54, 54, 0.9)", "rgba(237, 129, 40, 0.89)", "rgba(50, 172, 45, 0.97)"],
  54. sparkline: {
  55. show: false,
  56. full: false,
  57. lineColor: 'rgb(31, 120, 193)',
  58. fillColor: 'rgba(31, 118, 189, 0.18)',
  59. }
  60. };
  61. _.defaults($scope.panel, _d);
  62. $scope.unitFormats = kbn.getUnitFormats();
  63. $scope.setUnitFormat = function(subItem) {
  64. $scope.panel.format = subItem.value;
  65. $scope.render();
  66. };
  67. $scope.init = function() {
  68. panelSrv.init($scope);
  69. };
  70. $scope.refreshData = function(datasource) {
  71. panelHelper.updateTimeRange($scope);
  72. return panelHelper.issueMetricQuery($scope, datasource)
  73. .then($scope.dataHandler, function(err) {
  74. $scope.series = [];
  75. $scope.render();
  76. throw err;
  77. });
  78. };
  79. $scope.loadSnapshot = function(snapshotData) {
  80. panelHelper.updateTimeRange($scope);
  81. $scope.dataHandler(snapshotData);
  82. };
  83. $scope.dataHandler = function(results) {
  84. $scope.series = _.map(results.data, $scope.seriesHandler);
  85. $scope.render();
  86. };
  87. $scope.seriesHandler = function(seriesData) {
  88. var series = new TimeSeries({
  89. datapoints: seriesData.datapoints,
  90. alias: seriesData.target,
  91. });
  92. series.flotpairs = series.getFlotPairs($scope.panel.nullPointMode);
  93. return series;
  94. };
  95. $scope.setColoring = function(options) {
  96. if (options.background) {
  97. $scope.panel.colorValue = false;
  98. $scope.panel.colors = ['rgba(71, 212, 59, 0.4)', 'rgba(245, 150, 40, 0.73)', 'rgba(225, 40, 40, 0.59)'];
  99. }
  100. else {
  101. $scope.panel.colorBackground = false;
  102. $scope.panel.colors = ['rgba(50, 172, 45, 0.97)', 'rgba(237, 129, 40, 0.89)', 'rgba(245, 54, 54, 0.9)'];
  103. }
  104. $scope.render();
  105. };
  106. $scope.invertColorOrder = function() {
  107. var tmp = $scope.panel.colors[0];
  108. $scope.panel.colors[0] = $scope.panel.colors[2];
  109. $scope.panel.colors[2] = tmp;
  110. $scope.render();
  111. };
  112. $scope.getDecimalsForValue = function(value) {
  113. if (_.isNumber($scope.panel.decimals)) {
  114. return { decimals: $scope.panel.decimals, scaledDecimals: null };
  115. }
  116. var delta = value / 2;
  117. var dec = -Math.floor(Math.log(delta) / Math.LN10);
  118. var magn = Math.pow(10, -dec),
  119. norm = delta / magn, // norm is between 1.0 and 10.0
  120. size;
  121. if (norm < 1.5) {
  122. size = 1;
  123. } else if (norm < 3) {
  124. size = 2;
  125. // special case for 2.5, requires an extra decimal
  126. if (norm > 2.25) {
  127. size = 2.5;
  128. ++dec;
  129. }
  130. } else if (norm < 7.5) {
  131. size = 5;
  132. } else {
  133. size = 10;
  134. }
  135. size *= magn;
  136. // reduce starting decimals if not needed
  137. if (Math.floor(value) === value) { dec = 0; }
  138. var result = {};
  139. result.decimals = Math.max(0, dec);
  140. result.scaledDecimals = result.decimals - Math.floor(Math.log(size) / Math.LN10) + 2;
  141. return result;
  142. };
  143. $scope.render = function() {
  144. var data = {};
  145. $scope.setValues(data);
  146. data.thresholds = $scope.panel.thresholds.split(',').map(function(strVale) {
  147. return Number(strVale.trim());
  148. });
  149. data.colorMap = $scope.panel.colors;
  150. $scope.data = data;
  151. $scope.$broadcast('render');
  152. };
  153. $scope.setValues = function(data) {
  154. data.flotpairs = [];
  155. if($scope.series.length > 1) {
  156. $scope.inspector.error = new Error();
  157. $scope.inspector.error.message = 'Multiple Series Error';
  158. $scope.inspector.error.data = 'Metric query returns ' + $scope.series.length +
  159. ' series. Single Stat Panel expects a single series.\n\nResponse:\n'+JSON.stringify($scope.series);
  160. throw $scope.inspector.error;
  161. }
  162. if ($scope.series && $scope.series.length > 0) {
  163. var lastPoint = _.last($scope.series[0].datapoints);
  164. var lastValue = _.isArray(lastPoint) ? lastPoint[0] : null;
  165. if (_.isString(lastValue)) {
  166. data.value = 0;
  167. data.valueFormated = lastValue;
  168. data.valueRounded = 0;
  169. } else {
  170. data.value = $scope.series[0].stats[$scope.panel.valueName];
  171. data.flotpairs = $scope.series[0].flotpairs;
  172. var decimalInfo = $scope.getDecimalsForValue(data.value);
  173. var formatFunc = kbn.valueFormats[$scope.panel.format];
  174. data.valueFormated = formatFunc(data.value, decimalInfo.decimals, decimalInfo.scaledDecimals);
  175. data.valueRounded = kbn.roundValue(data.value, decimalInfo.decimals);
  176. }
  177. }
  178. // check value to text mappings
  179. for(var i = 0; i < $scope.panel.valueMaps.length; i++) {
  180. var map = $scope.panel.valueMaps[i];
  181. // special null case
  182. if (map.value === 'null') {
  183. if (data.value === null || data.value === void 0) {
  184. data.valueFormated = map.text;
  185. return;
  186. }
  187. continue;
  188. }
  189. // value/number to text mapping
  190. var value = parseFloat(map.value);
  191. if (value === data.value) {
  192. data.valueFormated = map.text;
  193. return;
  194. }
  195. }
  196. if (data.value === null || data.value === void 0) {
  197. data.valueFormated = "no value";
  198. }
  199. };
  200. $scope.removeValueMap = function(map) {
  201. var index = _.indexOf($scope.panel.valueMaps, map);
  202. $scope.panel.valueMaps.splice(index, 1);
  203. $scope.render();
  204. };
  205. $scope.addValueMap = function() {
  206. $scope.panel.valueMaps.push({value: '', op: '=', text: '' });
  207. };
  208. $scope.init();
  209. });
  210. });