anomalyChart.spec.tsx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import {getAnomalyMarkerSeries} from 'sentry/views/alerts/rules/metric/utils/anomalyChart';
  2. import {type Anomaly, AnomalyType} from 'sentry/views/alerts/types';
  3. const anomaly: Anomaly['anomaly'] = {anomaly_type: AnomalyType.NONE, anomaly_score: 0};
  4. const anomaly_high: Anomaly['anomaly'] = {
  5. anomaly_type: AnomalyType.HIGH_CONFIDENCE,
  6. anomaly_score: 2,
  7. };
  8. const anomaly_low: Anomaly['anomaly'] = {
  9. anomaly_type: AnomalyType.LOW_CONFIDENCE,
  10. anomaly_score: 1,
  11. };
  12. describe('anomalyChart', () => {
  13. it('should return an empty array for empty anomalies', () => {
  14. const input: Anomaly[] = [];
  15. const output = [];
  16. expect(getAnomalyMarkerSeries(input)).toEqual(output);
  17. });
  18. it('should not create anomaly values', () => {
  19. const input: Anomaly[] = [
  20. {
  21. anomaly,
  22. timestamp: d(-3),
  23. value: 1,
  24. },
  25. {
  26. anomaly,
  27. timestamp: d(-2),
  28. value: 1,
  29. },
  30. ];
  31. expect(getAnomalyMarkerSeries(input)).toHaveLength(1);
  32. });
  33. it('should create two anomaly areas', () => {
  34. const input: Anomaly[] = [
  35. {
  36. anomaly: anomaly_high,
  37. timestamp: d(-3),
  38. value: 1,
  39. },
  40. {
  41. anomaly: anomaly_high,
  42. timestamp: d(-2),
  43. value: 1,
  44. },
  45. {
  46. anomaly,
  47. timestamp: d(-1),
  48. value: 0,
  49. },
  50. {
  51. anomaly,
  52. timestamp: d(-1),
  53. value: 0,
  54. },
  55. ];
  56. expect(getAnomalyMarkerSeries(input)).toHaveLength(2);
  57. });
  58. it('should create three anomaly areas', () => {
  59. const input: Anomaly[] = [
  60. {
  61. anomaly: anomaly_high,
  62. timestamp: d(-3),
  63. value: 1,
  64. },
  65. {
  66. anomaly: anomaly_high,
  67. timestamp: d(-2),
  68. value: 1,
  69. },
  70. {
  71. anomaly,
  72. timestamp: d(-1),
  73. value: 0,
  74. },
  75. {
  76. anomaly,
  77. timestamp: d(-1),
  78. value: 0,
  79. },
  80. {
  81. anomaly: anomaly_low,
  82. timestamp: d(1),
  83. value: 2,
  84. },
  85. {
  86. anomaly: anomaly_low,
  87. timestamp: d(2),
  88. value: 2,
  89. },
  90. ];
  91. expect(getAnomalyMarkerSeries(input)).toHaveLength(3);
  92. });
  93. });
  94. function d(offset: number) {
  95. const value = new Date();
  96. value.setHours(12);
  97. value.setDate(value.getDate() + offset);
  98. return value.valueOf();
  99. }