controller.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 TimeSeries from '../../../core/time_series2';
  6. import {MetricsPanelCtrl} from '../../../features/panel/panel';
  7. // Set and populate defaults
  8. var panelDefaults = {
  9. links: [],
  10. datasource: null,
  11. maxDataPoints: 100,
  12. interval: null,
  13. targets: [{}],
  14. cacheTimeout: null,
  15. format: 'none',
  16. prefix: '',
  17. postfix: '',
  18. nullText: null,
  19. valueMaps: [
  20. { value: 'null', op: '=', text: 'N/A' }
  21. ],
  22. nullPointMode: 'connected',
  23. valueName: 'avg',
  24. prefixFontSize: '50%',
  25. valueFontSize: '80%',
  26. postfixFontSize: '50%',
  27. thresholds: '',
  28. colorBackground: false,
  29. colorValue: false,
  30. colors: ["rgba(245, 54, 54, 0.9)", "rgba(237, 129, 40, 0.89)", "rgba(50, 172, 45, 0.97)"],
  31. sparkline: {
  32. show: false,
  33. full: false,
  34. lineColor: 'rgb(31, 120, 193)',
  35. fillColor: 'rgba(31, 118, 189, 0.18)',
  36. }
  37. };
  38. export class SingleStatCtrl extends MetricsPanelCtrl {
  39. series: any[];
  40. data: any[];
  41. fontSizes: any[];
  42. unitFormats: any[];
  43. /** @ngInject */
  44. constructor($scope, $injector) {
  45. super($scope, $injector);
  46. _.defaults(this.panel, panelDefaults);
  47. }
  48. initEditMode() {
  49. super.initEditMode();
  50. this.icon = "fa fa-dashboard";
  51. this.fontSizes = ['20%', '30%','50%','70%','80%','100%', '110%', '120%', '150%', '170%', '200%'];
  52. this.addEditorTab('Options', 'app/plugins/panel/singlestat/editor.html', 2);
  53. this.unitFormats = kbn.getUnitFormats();
  54. }
  55. setUnitFormat(subItem) {
  56. this.panel.format = subItem.value;
  57. this.render();
  58. }
  59. refreshData(datasource) {
  60. return this.issueQueries(datasource)
  61. .then(this.dataHandler.bind(this))
  62. .catch(err => {
  63. this.series = [];
  64. this.render();
  65. throw err;
  66. });
  67. }
  68. loadSnapshot(snapshotData) {
  69. this.updateTimeRange();
  70. this.dataHandler(snapshotData);
  71. }
  72. dataHandler(results) {
  73. this.series = _.map(results.data, this.seriesHandler.bind(this));
  74. this.render();
  75. }
  76. seriesHandler(seriesData) {
  77. var series = new TimeSeries({
  78. datapoints: seriesData.datapoints,
  79. alias: seriesData.target,
  80. });
  81. series.flotpairs = series.getFlotPairs(this.panel.nullPointMode);
  82. return series;
  83. }
  84. setColoring(options) {
  85. if (options.background) {
  86. this.panel.colorValue = false;
  87. this.panel.colors = ['rgba(71, 212, 59, 0.4)', 'rgba(245, 150, 40, 0.73)', 'rgba(225, 40, 40, 0.59)'];
  88. } else {
  89. this.panel.colorBackground = false;
  90. this.panel.colors = ['rgba(50, 172, 45, 0.97)', 'rgba(237, 129, 40, 0.89)', 'rgba(245, 54, 54, 0.9)'];
  91. }
  92. this.render();
  93. }
  94. invertColorOrder() {
  95. var tmp = this.panel.colors[0];
  96. this.panel.colors[0] = this.panel.colors[2];
  97. this.panel.colors[2] = tmp;
  98. this.render();
  99. }
  100. getDecimalsForValue(value) {
  101. if (_.isNumber(this.panel.decimals)) {
  102. return {decimals: this.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. render() {
  132. var data: any = {};
  133. this.setValues(data);
  134. data.thresholds = this.panel.thresholds.split(',').map(function(strVale) {
  135. return Number(strVale.trim());
  136. });
  137. data.colorMap = this.panel.colors;
  138. this.data = data;
  139. this.broadcastRender();
  140. }
  141. setValues(data) {
  142. data.flotpairs = [];
  143. if (this.series.length > 1) {
  144. this.inspector.error = new Error();
  145. this.inspector.error.message = 'Multiple Series Error';
  146. this.inspector.error.data = 'Metric query returns ' + this.series.length +
  147. ' series. Single Stat Panel expects a single series.\n\nResponse:\n'+JSON.stringify(this.series);
  148. throw this.inspector.error;
  149. }
  150. if (this.series && this.series.length > 0) {
  151. var lastPoint = _.last(this.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 = this.series[0].stats[this.panel.valueName];
  159. data.flotpairs = this.series[0].flotpairs;
  160. var decimalInfo = this.getDecimalsForValue(data.value);
  161. var formatFunc = kbn.valueFormats[this.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 < this.panel.valueMaps.length; i++) {
  168. var map = this.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. removeValueMap(map) {
  189. var index = _.indexOf(this.panel.valueMaps, map);
  190. this.panel.valueMaps.splice(index, 1);
  191. this.render();
  192. };
  193. addValueMap() {
  194. this.panel.valueMaps.push({value: '', op: '=', text: '' });
  195. }
  196. }