logs_model.test.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import { calculateLogsLabelStats, dedupLogRows, LogsDedupStrategy, LogsModel } from '../logs_model';
  2. describe('dedupLogRows()', () => {
  3. test('should return rows as is when dedup is set to none', () => {
  4. const logs = {
  5. rows: [
  6. {
  7. entry: 'WARN test 1.23 on [xxx]',
  8. },
  9. {
  10. entry: 'WARN test 1.23 on [xxx]',
  11. },
  12. ],
  13. };
  14. expect(dedupLogRows(logs as LogsModel, LogsDedupStrategy.none).rows).toMatchObject(logs.rows);
  15. });
  16. test('should dedup on exact matches', () => {
  17. const logs = {
  18. rows: [
  19. {
  20. entry: 'WARN test 1.23 on [xxx]',
  21. },
  22. {
  23. entry: 'WARN test 1.23 on [xxx]',
  24. },
  25. {
  26. entry: 'INFO test 2.44 on [xxx]',
  27. },
  28. {
  29. entry: 'WARN test 1.23 on [xxx]',
  30. },
  31. ],
  32. };
  33. expect(dedupLogRows(logs as LogsModel, LogsDedupStrategy.exact).rows).toEqual([
  34. {
  35. duplicates: 1,
  36. entry: 'WARN test 1.23 on [xxx]',
  37. },
  38. {
  39. duplicates: 0,
  40. entry: 'INFO test 2.44 on [xxx]',
  41. },
  42. {
  43. duplicates: 0,
  44. entry: 'WARN test 1.23 on [xxx]',
  45. },
  46. ]);
  47. });
  48. test('should dedup on number matches', () => {
  49. const logs = {
  50. rows: [
  51. {
  52. entry: 'WARN test 1.2323423 on [xxx]',
  53. },
  54. {
  55. entry: 'WARN test 1.23 on [xxx]',
  56. },
  57. {
  58. entry: 'INFO test 2.44 on [xxx]',
  59. },
  60. {
  61. entry: 'WARN test 1.23 on [xxx]',
  62. },
  63. ],
  64. };
  65. expect(dedupLogRows(logs as LogsModel, LogsDedupStrategy.numbers).rows).toEqual([
  66. {
  67. duplicates: 1,
  68. entry: 'WARN test 1.2323423 on [xxx]',
  69. },
  70. {
  71. duplicates: 0,
  72. entry: 'INFO test 2.44 on [xxx]',
  73. },
  74. {
  75. duplicates: 0,
  76. entry: 'WARN test 1.23 on [xxx]',
  77. },
  78. ]);
  79. });
  80. test('should dedup on signature matches', () => {
  81. const logs = {
  82. rows: [
  83. {
  84. entry: 'WARN test 1.2323423 on [xxx]',
  85. },
  86. {
  87. entry: 'WARN test 1.23 on [xxx]',
  88. },
  89. {
  90. entry: 'INFO test 2.44 on [xxx]',
  91. },
  92. {
  93. entry: 'WARN test 1.23 on [xxx]',
  94. },
  95. ],
  96. };
  97. expect(dedupLogRows(logs as LogsModel, LogsDedupStrategy.signature).rows).toEqual([
  98. {
  99. duplicates: 3,
  100. entry: 'WARN test 1.2323423 on [xxx]',
  101. },
  102. ]);
  103. });
  104. });
  105. describe('calculateLogsLabelStats()', () => {
  106. test('should return no stats for empty rows', () => {
  107. expect(calculateLogsLabelStats([], '')).toEqual([]);
  108. });
  109. test('should return no stats of label is not found', () => {
  110. const rows = [
  111. {
  112. entry: 'foo 1',
  113. labels: {
  114. foo: 'bar',
  115. },
  116. },
  117. ];
  118. expect(calculateLogsLabelStats(rows as any, 'baz')).toEqual([]);
  119. });
  120. test('should return stats for found labels', () => {
  121. const rows = [
  122. {
  123. entry: 'foo 1',
  124. labels: {
  125. foo: 'bar',
  126. },
  127. },
  128. {
  129. entry: 'foo 0',
  130. labels: {
  131. foo: 'xxx',
  132. },
  133. },
  134. {
  135. entry: 'foo 2',
  136. labels: {
  137. foo: 'bar',
  138. },
  139. },
  140. ];
  141. expect(calculateLogsLabelStats(rows as any, 'foo')).toMatchObject([
  142. {
  143. value: 'bar',
  144. count: 2,
  145. },
  146. {
  147. value: 'xxx',
  148. count: 1,
  149. },
  150. ]);
  151. });
  152. });