result_transformer.test.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import { ResultTransformer } from '../result_transformer';
  2. describe('Prometheus Result Transformer', () => {
  3. const ctx: any = {};
  4. beforeEach(() => {
  5. ctx.templateSrv = {
  6. replace: str => str,
  7. };
  8. ctx.resultTransformer = new ResultTransformer(ctx.templateSrv);
  9. });
  10. describe('When resultFormat is table', () => {
  11. const response = {
  12. status: 'success',
  13. data: {
  14. resultType: 'matrix',
  15. result: [
  16. {
  17. metric: { __name__: 'test', job: 'testjob' },
  18. values: [[1443454528, '3846']],
  19. },
  20. {
  21. metric: {
  22. __name__: 'test',
  23. instance: 'localhost:8080',
  24. job: 'otherjob',
  25. },
  26. values: [[1443454529, '3847']],
  27. },
  28. ],
  29. },
  30. };
  31. it('should return table model', () => {
  32. const table = ctx.resultTransformer.transformMetricDataToTable(response.data.result);
  33. expect(table.type).toBe('table');
  34. expect(table.rows).toEqual([
  35. [1443454528000, 'test', '', 'testjob', 3846],
  36. [1443454529000, 'test', 'localhost:8080', 'otherjob', 3847],
  37. ]);
  38. expect(table.columns).toMatchObject([
  39. { text: 'Time', type: 'time' },
  40. { text: '__name__' },
  41. { text: 'instance' },
  42. { text: 'job' },
  43. { text: 'Value' },
  44. ]);
  45. });
  46. it('should column title include refId if response count is more than 2', () => {
  47. const table = ctx.resultTransformer.transformMetricDataToTable(response.data.result, 2, 'B');
  48. expect(table.type).toBe('table');
  49. expect(table.columns).toMatchObject([
  50. { text: 'Time', type: 'time' },
  51. { text: '__name__' },
  52. { text: 'instance' },
  53. { text: 'job' },
  54. { text: 'Value #B' },
  55. ]);
  56. });
  57. });
  58. describe('When resultFormat is table and instant = true', () => {
  59. const response = {
  60. status: 'success',
  61. data: {
  62. resultType: 'vector',
  63. result: [
  64. {
  65. metric: { __name__: 'test', job: 'testjob' },
  66. value: [1443454528, '3846'],
  67. },
  68. ],
  69. },
  70. };
  71. it('should return table model', () => {
  72. const table = ctx.resultTransformer.transformMetricDataToTable(response.data.result);
  73. expect(table.type).toBe('table');
  74. expect(table.rows).toEqual([[1443454528000, 'test', 'testjob', 3846]]);
  75. expect(table.columns).toMatchObject([
  76. { text: 'Time', type: 'time' },
  77. { text: '__name__' },
  78. { text: 'job' },
  79. { text: 'Value' },
  80. ]);
  81. });
  82. });
  83. describe('When resultFormat is heatmap', () => {
  84. const response = {
  85. status: 'success',
  86. data: {
  87. resultType: 'matrix',
  88. result: [
  89. {
  90. metric: { __name__: 'test', job: 'testjob', le: '1' },
  91. values: [[1445000010, '10'], [1445000020, '10'], [1445000030, '0']],
  92. },
  93. {
  94. metric: { __name__: 'test', job: 'testjob', le: '2' },
  95. values: [[1445000010, '20'], [1445000020, '10'], [1445000030, '30']],
  96. },
  97. {
  98. metric: { __name__: 'test', job: 'testjob', le: '3' },
  99. values: [[1445000010, '30'], [1445000020, '10'], [1445000030, '40']],
  100. },
  101. ],
  102. },
  103. };
  104. it('should convert cumulative histogram to regular', () => {
  105. const options = {
  106. format: 'heatmap',
  107. start: 1445000010,
  108. end: 1445000030,
  109. legendFormat: '{{le}}',
  110. };
  111. const result = ctx.resultTransformer.transform({ data: response }, options);
  112. expect(result).toEqual([
  113. { target: '1', datapoints: [[10, 1445000010000], [10, 1445000020000], [0, 1445000030000]] },
  114. { target: '2', datapoints: [[10, 1445000010000], [0, 1445000020000], [30, 1445000030000]] },
  115. { target: '3', datapoints: [[10, 1445000010000], [0, 1445000020000], [10, 1445000030000]] },
  116. ]);
  117. });
  118. it('should handle missing datapoints', () => {
  119. const seriesList = [
  120. { datapoints: [[1, 1000], [2, 2000]] },
  121. { datapoints: [[2, 1000], [5, 2000], [1, 3000]] },
  122. { datapoints: [[3, 1000], [7, 2000]] },
  123. ];
  124. const expected = [
  125. { datapoints: [[1, 1000], [2, 2000]] },
  126. { datapoints: [[1, 1000], [3, 2000], [1, 3000]] },
  127. { datapoints: [[1, 1000], [2, 2000]] },
  128. ];
  129. const result = ctx.resultTransformer.transformToHistogramOverTime(seriesList);
  130. expect(result).toEqual(expected);
  131. });
  132. it('should throw error when data in wrong format', () => {
  133. const seriesList = [{ rows: [] }, { datapoints: [] }];
  134. expect(() => {
  135. ctx.resultTransformer.transformToHistogramOverTime(seriesList);
  136. }).toThrow();
  137. });
  138. it('should throw error when prometheus returned non-timeseries', () => {
  139. // should be { metric: {}, values: [] } for timeseries
  140. const metricData = { metric: {}, value: [] };
  141. expect(() => {
  142. ctx.resultTransformer.transformMetricData(metricData, { step: 1 }, 1000, 2000);
  143. }).toThrow();
  144. });
  145. });
  146. describe('When resultFormat is time series', () => {
  147. it('should transform matrix into timeseries', () => {
  148. const response = {
  149. status: 'success',
  150. data: {
  151. resultType: 'matrix',
  152. result: [
  153. {
  154. metric: { __name__: 'test', job: 'testjob' },
  155. values: [[0, '10'], [1, '10'], [2, '0']],
  156. },
  157. ],
  158. },
  159. };
  160. const options = {
  161. format: 'timeseries',
  162. start: 0,
  163. end: 2,
  164. };
  165. const result = ctx.resultTransformer.transform({ data: response }, options);
  166. expect(result).toEqual([{ target: 'test{job="testjob"}', datapoints: [[10, 0], [10, 1000], [0, 2000]] }]);
  167. });
  168. it('should fill timeseries with null values', () => {
  169. const response = {
  170. status: 'success',
  171. data: {
  172. resultType: 'matrix',
  173. result: [
  174. {
  175. metric: { __name__: 'test', job: 'testjob' },
  176. values: [[1, '10'], [2, '0']],
  177. },
  178. ],
  179. },
  180. };
  181. const options = {
  182. format: 'timeseries',
  183. step: 1,
  184. start: 0,
  185. end: 2,
  186. };
  187. const result = ctx.resultTransformer.transform({ data: response }, options);
  188. expect(result).toEqual([{ target: 'test{job="testjob"}', datapoints: [[null, 0], [10, 1000], [0, 2000]] }]);
  189. });
  190. it('should align null values with step', () => {
  191. const response = {
  192. status: 'success',
  193. data: {
  194. resultType: 'matrix',
  195. result: [
  196. {
  197. metric: { __name__: 'test', job: 'testjob' },
  198. values: [[4, '10'], [8, '10']],
  199. },
  200. ],
  201. },
  202. };
  203. const options = {
  204. format: 'timeseries',
  205. step: 2,
  206. start: 0,
  207. end: 8,
  208. };
  209. const result = ctx.resultTransformer.transform({ data: response }, options);
  210. expect(result).toEqual([
  211. { target: 'test{job="testjob"}', datapoints: [[null, 0], [null, 2000], [10, 4000], [null, 6000], [10, 8000]] },
  212. ]);
  213. });
  214. });
  215. });