controller.js 6.9 KB

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