result_transformer.test.ts 7.7 KB

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