threshold_manager_specs.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { describe, it, expect } from '../../../../../test/lib/common';
  2. import angular from 'angular';
  3. import TimeSeries from 'app/core/time_series2';
  4. import { ThresholdManager } from '../threshold_manager';
  5. describe('ThresholdManager', function() {
  6. function plotOptionsScenario(desc, func) {
  7. describe(desc, function() {
  8. var ctx: any = {
  9. panel: {
  10. thresholds: [],
  11. },
  12. options: {
  13. grid: { markings: [] },
  14. },
  15. panelCtrl: {},
  16. };
  17. ctx.setup = function(thresholds, data) {
  18. ctx.panel.thresholds = thresholds;
  19. var manager = new ThresholdManager(ctx.panelCtrl);
  20. if (data !== undefined) {
  21. var element = angular.element('<div grafana-graph><div>');
  22. manager.prepare(element, data);
  23. }
  24. manager.addFlotOptions(ctx.options, ctx.panel);
  25. };
  26. func(ctx);
  27. });
  28. }
  29. describe('When creating plot markings', () => {
  30. plotOptionsScenario('for simple gt threshold', ctx => {
  31. ctx.setup([{ op: 'gt', value: 300, fill: true, line: true, colorMode: 'critical' }]);
  32. it('should add fill for threshold with fill: true', function() {
  33. var markings = ctx.options.grid.markings;
  34. expect(markings[0].yaxis.from).to.be(300);
  35. expect(markings[0].yaxis.to).to.be(Infinity);
  36. expect(markings[0].color).to.be('rgba(234, 112, 112, 0.12)');
  37. });
  38. it('should add line', function() {
  39. var markings = ctx.options.grid.markings;
  40. expect(markings[1].yaxis.from).to.be(300);
  41. expect(markings[1].yaxis.to).to.be(300);
  42. expect(markings[1].color).to.be('rgba(237, 46, 24, 0.60)');
  43. });
  44. });
  45. plotOptionsScenario('for two gt thresholds', ctx => {
  46. ctx.setup([
  47. { op: 'gt', value: 200, fill: true, colorMode: 'warning' },
  48. { op: 'gt', value: 300, fill: true, colorMode: 'critical' },
  49. ]);
  50. it('should add fill for first thresholds to next threshold', function() {
  51. var markings = ctx.options.grid.markings;
  52. expect(markings[0].yaxis.from).to.be(200);
  53. expect(markings[0].yaxis.to).to.be(300);
  54. });
  55. it('should add fill for last thresholds to infinity', function() {
  56. var markings = ctx.options.grid.markings;
  57. expect(markings[1].yaxis.from).to.be(300);
  58. expect(markings[1].yaxis.to).to.be(Infinity);
  59. });
  60. });
  61. plotOptionsScenario('for lt then gt threshold (inside)', ctx => {
  62. ctx.setup([
  63. { op: 'lt', value: 300, fill: true, colorMode: 'critical' },
  64. { op: 'gt', value: 200, fill: true, colorMode: 'critical' },
  65. ]);
  66. it('should add fill for first thresholds to next threshold', function() {
  67. var markings = ctx.options.grid.markings;
  68. expect(markings[0].yaxis.from).to.be(300);
  69. expect(markings[0].yaxis.to).to.be(200);
  70. });
  71. it('should add fill for last thresholds to itself', function() {
  72. var markings = ctx.options.grid.markings;
  73. expect(markings[1].yaxis.from).to.be(200);
  74. expect(markings[1].yaxis.to).to.be(200);
  75. });
  76. });
  77. plotOptionsScenario('for gt then lt threshold (outside)', ctx => {
  78. ctx.setup([
  79. { op: 'gt', value: 300, fill: true, colorMode: 'critical' },
  80. { op: 'lt', value: 200, fill: true, colorMode: 'critical' },
  81. ]);
  82. it('should add fill for first thresholds to next threshold', function() {
  83. var markings = ctx.options.grid.markings;
  84. expect(markings[0].yaxis.from).to.be(300);
  85. expect(markings[0].yaxis.to).to.be(Infinity);
  86. });
  87. it('should add fill for last thresholds to itself', function() {
  88. var markings = ctx.options.grid.markings;
  89. expect(markings[1].yaxis.from).to.be(200);
  90. expect(markings[1].yaxis.to).to.be(-Infinity);
  91. });
  92. });
  93. plotOptionsScenario('for threshold on two Y axes', ctx => {
  94. var data = new Array(2);
  95. data[0] = new TimeSeries({
  96. datapoints: [[0, 1], [300, 2]],
  97. alias: 'left',
  98. });
  99. data[0].yaxis = 1;
  100. data[1] = new TimeSeries({
  101. datapoints: [[0, 1], [300, 2]],
  102. alias: 'right',
  103. });
  104. data[1].yaxis = 2;
  105. ctx.setup(
  106. [
  107. { op: 'gt', value: 100, line: true, colorMode: 'critical' },
  108. { op: 'gt', value: 200, line: true, colorMode: 'critical', yaxis: 'right' },
  109. ],
  110. data
  111. );
  112. it('should add first threshold for left axis', function() {
  113. var markings = ctx.options.grid.markings;
  114. expect(markings[0].yaxis.from).to.be(100);
  115. });
  116. it('should add second threshold for right axis', function() {
  117. var markings = ctx.options.grid.markings;
  118. expect(markings[1].y2axis.from).to.be(200);
  119. });
  120. });
  121. });
  122. });