statsDirective.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. define([
  2. 'angular',
  3. 'app',
  4. 'lodash',
  5. 'kbn',
  6. 'jquery',
  7. 'jquery.flot',
  8. 'jquery.flot.time',
  9. ],
  10. function (angular, app, _, kbn, $) {
  11. 'use strict';
  12. var module = angular.module('grafana.panels.stats', []);
  13. app.useModule(module);
  14. module.directive('statsPanel', function() {
  15. return {
  16. link: function(scope, elem) {
  17. var data;
  18. var valueRegex = /\{\{([a-zA-Z]+)\}\}/g;
  19. var smallValueTextRegex = /!(\S+)/g;
  20. var $panelContainer = elem.parents('.panel-container');
  21. scope.$on('render', function() {
  22. data = scope.data;
  23. data.mainValue = null;
  24. if (!data || data.series.length === 0) {
  25. elem.html('no data');
  26. return;
  27. }
  28. render();
  29. });
  30. function setElementHeight() {
  31. try {
  32. var height = scope.height || scope.panel.height || scope.row.height;
  33. if (_.isString(height)) {
  34. height = parseInt(height.replace('px', ''), 10);
  35. }
  36. height -= scope.panel.title ? 24 : 9; // subtract panel title bar
  37. elem.css('height', height + 'px');
  38. return true;
  39. } catch(e) { // IE throws errors sometimes
  40. return false;
  41. }
  42. }
  43. function applyColoringThresholds(value, valueString) {
  44. if (!scope.panel.colorValue) {
  45. return valueString;
  46. }
  47. var color = getColorForValue(value);
  48. if (color) {
  49. return '<span style="color:' + color + '">'+ valueString + '</span>';
  50. }
  51. return valueString;
  52. }
  53. function getColorForValue(value) {
  54. for (var i = data.thresholds.length - 1; i >= 0 ; i--) {
  55. if (value > data.thresholds[i]) {
  56. return data.colorMap[i];
  57. }
  58. }
  59. return null;
  60. }
  61. function valueTemplateReplaceFunc(match, statType) {
  62. var stats = data.series[0].stats;
  63. data.mainValue = stats[statType];
  64. var valueFormated = scope.formatValue(data.mainValue);
  65. return applyColoringThresholds(data.mainValue, valueFormated);
  66. }
  67. function smallValueTextReplaceFunc(match, text) {
  68. return '<span class="stats-panel-value-small">' + text + '</span>';
  69. }
  70. function addSparkline() {
  71. var panel = scope.panel;
  72. var width = elem.width() + 20;
  73. var height = elem.height() || 100;
  74. var plotCanvas = $('<div></div>');
  75. var plotCss = {};
  76. plotCss.position = 'absolute';
  77. if (panel.sparkline.full) {
  78. plotCss.bottom = '5px';
  79. plotCss.left = '-5px';
  80. plotCss.width = (width - 10) + 'px';
  81. plotCss.height = (height - 45) + 'px';
  82. }
  83. else {
  84. plotCss.bottom = "0px";
  85. plotCss.left = "-5px";
  86. plotCss.width = (width - 10) + 'px';
  87. plotCss.height = Math.floor(height * 0.3) + "px";
  88. }
  89. plotCanvas.css(plotCss);
  90. var options = {
  91. legend: { show: false },
  92. series: {
  93. lines: {
  94. show: true,
  95. fill: 1,
  96. lineWidth: 1,
  97. fillColor: panel.sparkline.fillColor,
  98. },
  99. },
  100. yaxes: { show: false },
  101. xaxis: {
  102. show: false,
  103. mode: "time",
  104. min: scope.range.from.getTime(),
  105. max: scope.range.to.getTime(),
  106. },
  107. grid: { hoverable: false, show: false },
  108. };
  109. elem.append(plotCanvas);
  110. data.series[0].color = panel.sparkline.lineColor;
  111. setTimeout(function() {
  112. $.plot(plotCanvas, [data.series[0]], options);
  113. }, 10);
  114. }
  115. function render() {
  116. setElementHeight();
  117. var panel = scope.panel;
  118. var body = '';
  119. body += '<div class="stats-panel-value-container">';
  120. body += '<span class="stats-panel-value">';
  121. var valueHtml = panel.template.replace(valueRegex, valueTemplateReplaceFunc);
  122. body += valueHtml.replace(smallValueTextRegex, smallValueTextReplaceFunc);
  123. body += '</div>';
  124. body += '</div>';
  125. if (panel.colorBackground && data.mainValue) {
  126. var color = getColorForValue(data.mainValue);
  127. if (color) {
  128. $panelContainer.css('background-color', color);
  129. if (scope.fullscreen) {
  130. elem.css('background-color', color);
  131. } else {
  132. elem.css('background-color', '');
  133. }
  134. }
  135. } else {
  136. $panelContainer.css('background-color', '');
  137. elem.css('background-color', '');
  138. }
  139. elem.html(body);
  140. if (panel.sparkline.show) {
  141. addSparkline();
  142. }
  143. }
  144. }
  145. };
  146. });
  147. });