controller.ts 6.9 KB

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