threshold_manager.test.ts 4.7 KB

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