controller.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. ],
  9. function (angular, app, _, kbn, TimeSeries, PanelMeta) {
  10. 'use strict';
  11. /** @ngInject */
  12. function SingleStatCtrl($scope, panelSrv, panelHelper) {
  13. $scope.panelMeta = new PanelMeta({
  14. panelName: 'Singlestat',
  15. editIcon: "fa fa-dashboard",
  16. fullscreen: true,
  17. metricsEditor: true
  18. });
  19. $scope.fontSizes = ['20%', '30%','50%','70%','80%','100%', '110%', '120%', '150%', '170%', '200%'];
  20. $scope.panelMeta.addEditorTab('Options', 'app/plugins/panel/singlestat/editor.html');
  21. $scope.panelMeta.addEditorTab('Time range', 'app/features/panel/partials/panelTime.html');
  22. // Set and populate defaults
  23. var _d = {
  24. links: [],
  25. datasource: null,
  26. maxDataPoints: 100,
  27. interval: null,
  28. targets: [{}],
  29. cacheTimeout: null,
  30. format: 'none',
  31. prefix: '',
  32. postfix: '',
  33. nullText: null,
  34. valueMaps: [
  35. { value: 'null', op: '=', text: 'N/A' }
  36. ],
  37. nullPointMode: 'connected',
  38. valueName: 'avg',
  39. prefixFontSize: '50%',
  40. valueFontSize: '80%',
  41. postfixFontSize: '50%',
  42. thresholds: '',
  43. colorBackground: false,
  44. colorValue: false,
  45. colors: ["rgba(245, 54, 54, 0.9)", "rgba(237, 129, 40, 0.89)", "rgba(50, 172, 45, 0.97)"],
  46. sparkline: {
  47. show: false,
  48. full: false,
  49. lineColor: 'rgb(31, 120, 193)',
  50. fillColor: 'rgba(31, 118, 189, 0.18)',
  51. }
  52. };
  53. _.defaults($scope.panel, _d);
  54. $scope.unitFormats = kbn.getUnitFormats();
  55. $scope.setUnitFormat = function(subItem) {
  56. $scope.panel.format = subItem.value;
  57. $scope.render();
  58. };
  59. $scope.init = function() {
  60. panelSrv.init($scope);
  61. };
  62. $scope.refreshData = function(datasource) {
  63. panelHelper.updateTimeRange($scope);
  64. return panelHelper.issueMetricQuery($scope, datasource)
  65. .then($scope.dataHandler, function(err) {
  66. $scope.series = [];
  67. $scope.render();
  68. throw err;
  69. });
  70. };
  71. $scope.loadSnapshot = function(snapshotData) {
  72. panelHelper.updateTimeRange($scope);
  73. $scope.dataHandler(snapshotData);
  74. };
  75. $scope.dataHandler = function(results) {
  76. $scope.series = _.map(results.data, $scope.seriesHandler);
  77. $scope.render();
  78. };
  79. $scope.seriesHandler = function(seriesData) {
  80. var series = new TimeSeries({
  81. datapoints: seriesData.datapoints,
  82. alias: seriesData.target,
  83. });
  84. series.flotpairs = series.getFlotPairs($scope.panel.nullPointMode);
  85. return series;
  86. };
  87. $scope.setColoring = function(options) {
  88. if (options.background) {
  89. $scope.panel.colorValue = false;
  90. $scope.panel.colors = ['rgba(71, 212, 59, 0.4)', 'rgba(245, 150, 40, 0.73)', 'rgba(225, 40, 40, 0.59)'];
  91. }
  92. else {
  93. $scope.panel.colorBackground = false;
  94. $scope.panel.colors = ['rgba(50, 172, 45, 0.97)', 'rgba(237, 129, 40, 0.89)', 'rgba(245, 54, 54, 0.9)'];
  95. }
  96. $scope.render();
  97. };
  98. $scope.invertColorOrder = function() {
  99. var tmp = $scope.panel.colors[0];
  100. $scope.panel.colors[0] = $scope.panel.colors[2];
  101. $scope.panel.colors[2] = tmp;
  102. $scope.render();
  103. };
  104. $scope.getDecimalsForValue = function(value) {
  105. if (_.isNumber($scope.panel.decimals)) {
  106. return { decimals: $scope.panel.decimals, scaledDecimals: null };
  107. }
  108. var delta = value / 2;
  109. var dec = -Math.floor(Math.log(delta) / Math.LN10);
  110. var magn = Math.pow(10, -dec),
  111. norm = delta / magn, // norm is between 1.0 and 10.0
  112. size;
  113. if (norm < 1.5) {
  114. size = 1;
  115. } else if (norm < 3) {
  116. size = 2;
  117. // special case for 2.5, requires an extra decimal
  118. if (norm > 2.25) {
  119. size = 2.5;
  120. ++dec;
  121. }
  122. } else if (norm < 7.5) {
  123. size = 5;
  124. } else {
  125. size = 10;
  126. }
  127. size *= magn;
  128. // reduce starting decimals if not needed
  129. if (Math.floor(value) === value) { dec = 0; }
  130. var result = {};
  131. result.decimals = Math.max(0, dec);
  132. result.scaledDecimals = result.decimals - Math.floor(Math.log(size) / Math.LN10) + 2;
  133. return result;
  134. };
  135. $scope.render = function() {
  136. var data = {};
  137. $scope.setValues(data);
  138. data.thresholds = $scope.panel.thresholds.split(',').map(function(strVale) {
  139. return Number(strVale.trim());
  140. });
  141. data.colorMap = $scope.panel.colors;
  142. $scope.data = data;
  143. $scope.$broadcast('render');
  144. };
  145. $scope.setValues = function(data) {
  146. data.flotpairs = [];
  147. if($scope.series.length > 1) {
  148. $scope.inspector.error = new Error();
  149. $scope.inspector.error.message = 'Multiple Series Error';
  150. $scope.inspector.error.data = 'Metric query returns ' + $scope.series.length +
  151. ' series. Single Stat Panel expects a single series.\n\nResponse:\n'+JSON.stringify($scope.series);
  152. throw $scope.inspector.error;
  153. }
  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. return SingleStatCtrl;
  203. });