datasource.jest.ts 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. import { GraphiteDatasource } from '../datasource';
  2. import moment from 'moment';
  3. import _ from 'lodash';
  4. import $q from 'q';
  5. import { TemplateSrvStub } from 'test/specs/helpers';
  6. describe('graphiteDatasource', () => {
  7. let ctx: any = {
  8. backendSrv: {},
  9. $q: $q,
  10. templateSrv: new TemplateSrvStub(),
  11. };
  12. beforeEach(function() {
  13. ctx.instanceSettings = { url: [''], name: 'graphiteProd', jsonData: {} };
  14. ctx.ds = new GraphiteDatasource(ctx.instanceSettings, ctx.$q, ctx.backendSrv, ctx.templateSrv);
  15. });
  16. describe('When querying graphite with one target using query editor target spec', function() {
  17. let query = {
  18. panelId: 3,
  19. rangeRaw: { from: 'now-1h', to: 'now' },
  20. targets: [{ target: 'prod1.count' }, { target: 'prod2.count' }],
  21. maxDataPoints: 500,
  22. };
  23. let results;
  24. let requestOptions;
  25. beforeEach(async () => {
  26. ctx.backendSrv.datasourceRequest = function(options) {
  27. requestOptions = options;
  28. return ctx.$q.when({
  29. data: [{ target: 'prod1.count', datapoints: [[10, 1], [12, 1]] }],
  30. });
  31. };
  32. await ctx.ds.query(query).then(function(data) {
  33. results = data;
  34. });
  35. });
  36. it('should generate the correct query', function() {
  37. expect(requestOptions.url).toBe('/render');
  38. });
  39. it('should set unique requestId', function() {
  40. expect(requestOptions.requestId).toBe('graphiteProd.panelId.3');
  41. });
  42. it('should query correctly', function() {
  43. let params = requestOptions.data.split('&');
  44. expect(params).toContain('target=prod1.count');
  45. expect(params).toContain('target=prod2.count');
  46. expect(params).toContain('from=-1h');
  47. expect(params).toContain('until=now');
  48. });
  49. it('should exclude undefined params', function() {
  50. let params = requestOptions.data.split('&');
  51. expect(params).not.toContain('cacheTimeout=undefined');
  52. });
  53. it('should return series list', function() {
  54. expect(results.data.length).toBe(1);
  55. expect(results.data[0].target).toBe('prod1.count');
  56. });
  57. it('should convert to millisecond resolution', function() {
  58. expect(results.data[0].datapoints[0][0]).toBe(10);
  59. });
  60. });
  61. describe('when fetching Graphite Events as annotations', () => {
  62. let results;
  63. const options = {
  64. annotation: {
  65. tags: 'tag1',
  66. },
  67. range: {
  68. from: moment(1432288354),
  69. to: moment(1432288401),
  70. },
  71. rangeRaw: { from: 'now-24h', to: 'now' },
  72. };
  73. describe('and tags are returned as string', () => {
  74. const response = {
  75. data: [
  76. {
  77. when: 1507222850,
  78. tags: 'tag1 tag2',
  79. data: 'some text',
  80. id: 2,
  81. what: 'Event - deploy',
  82. },
  83. ],
  84. };
  85. beforeEach(async () => {
  86. ctx.backendSrv.datasourceRequest = function(options) {
  87. return ctx.$q.when(response);
  88. };
  89. await ctx.ds.annotationQuery(options).then(function(data) {
  90. results = data;
  91. });
  92. });
  93. it('should parse the tags string into an array', () => {
  94. expect(_.isArray(results[0].tags)).toEqual(true);
  95. expect(results[0].tags.length).toEqual(2);
  96. expect(results[0].tags[0]).toEqual('tag1');
  97. expect(results[0].tags[1]).toEqual('tag2');
  98. });
  99. });
  100. describe('and tags are returned as an array', () => {
  101. const response = {
  102. data: [
  103. {
  104. when: 1507222850,
  105. tags: ['tag1', 'tag2'],
  106. data: 'some text',
  107. id: 2,
  108. what: 'Event - deploy',
  109. },
  110. ],
  111. };
  112. beforeEach(() => {
  113. ctx.backendSrv.datasourceRequest = function(options) {
  114. return ctx.$q.when(response);
  115. };
  116. ctx.ds.annotationQuery(options).then(function(data) {
  117. results = data;
  118. });
  119. // ctx.$rootScope.$apply();
  120. });
  121. it('should parse the tags string into an array', () => {
  122. expect(_.isArray(results[0].tags)).toEqual(true);
  123. expect(results[0].tags.length).toEqual(2);
  124. expect(results[0].tags[0]).toEqual('tag1');
  125. expect(results[0].tags[1]).toEqual('tag2');
  126. });
  127. });
  128. });
  129. describe('building graphite params', function() {
  130. it('should return empty array if no targets', function() {
  131. let results = ctx.ds.buildGraphiteParams({
  132. targets: [{}],
  133. });
  134. expect(results.length).toBe(0);
  135. });
  136. it('should uri escape targets', function() {
  137. let results = ctx.ds.buildGraphiteParams({
  138. targets: [{ target: 'prod1.{test,test2}' }, { target: 'prod2.count' }],
  139. });
  140. expect(results).toContain('target=prod1.%7Btest%2Ctest2%7D');
  141. });
  142. it('should replace target placeholder', function() {
  143. let results = ctx.ds.buildGraphiteParams({
  144. targets: [{ target: 'series1' }, { target: 'series2' }, { target: 'asPercent(#A,#B)' }],
  145. });
  146. expect(results[2]).toBe('target=asPercent(series1%2Cseries2)');
  147. });
  148. it('should replace target placeholder for hidden series', function() {
  149. let results = ctx.ds.buildGraphiteParams({
  150. targets: [
  151. { target: 'series1', hide: true },
  152. { target: 'sumSeries(#A)', hide: true },
  153. { target: 'asPercent(#A,#B)' },
  154. ],
  155. });
  156. expect(results[0]).toBe('target=' + encodeURIComponent('asPercent(series1,sumSeries(series1))'));
  157. });
  158. it('should replace target placeholder when nesting query references', function() {
  159. let results = ctx.ds.buildGraphiteParams({
  160. targets: [{ target: 'series1' }, { target: 'sumSeries(#A)' }, { target: 'asPercent(#A,#B)' }],
  161. });
  162. expect(results[2]).toBe('target=' + encodeURIComponent('asPercent(series1,sumSeries(series1))'));
  163. });
  164. it('should fix wrong minute interval parameters', function() {
  165. let results = ctx.ds.buildGraphiteParams({
  166. targets: [{ target: "summarize(prod.25m.count, '25m', 'sum')" }],
  167. });
  168. expect(results[0]).toBe('target=' + encodeURIComponent("summarize(prod.25m.count, '25min', 'sum')"));
  169. });
  170. it('should fix wrong month interval parameters', function() {
  171. let results = ctx.ds.buildGraphiteParams({
  172. targets: [{ target: "summarize(prod.5M.count, '5M', 'sum')" }],
  173. });
  174. expect(results[0]).toBe('target=' + encodeURIComponent("summarize(prod.5M.count, '5mon', 'sum')"));
  175. });
  176. it('should ignore empty targets', function() {
  177. let results = ctx.ds.buildGraphiteParams({
  178. targets: [{ target: 'series1' }, { target: '' }],
  179. });
  180. expect(results.length).toBe(2);
  181. });
  182. });
  183. describe('querying for template variables', () => {
  184. let results;
  185. let requestOptions;
  186. beforeEach(() => {
  187. ctx.backendSrv.datasourceRequest = function(options) {
  188. requestOptions = options;
  189. return ctx.$q.when({
  190. data: ['backend_01', 'backend_02'],
  191. });
  192. };
  193. });
  194. it('should generate tags query', () => {
  195. ctx.ds.metricFindQuery('tags()').then(data => {
  196. results = data;
  197. });
  198. expect(requestOptions.url).toBe('/tags/autoComplete/tags');
  199. expect(requestOptions.params.expr).toEqual([]);
  200. expect(results).not.toBe(null);
  201. });
  202. it('should generate tags query with a filter expression', () => {
  203. ctx.ds.metricFindQuery('tags(server=backend_01)').then(data => {
  204. results = data;
  205. });
  206. expect(requestOptions.url).toBe('/tags/autoComplete/tags');
  207. expect(requestOptions.params.expr).toEqual(['server=backend_01']);
  208. expect(results).not.toBe(null);
  209. });
  210. it('should generate tags query for an expression with whitespace after', () => {
  211. ctx.ds.metricFindQuery('tags(server=backend_01 )').then(data => {
  212. results = data;
  213. });
  214. expect(requestOptions.url).toBe('/tags/autoComplete/tags');
  215. expect(requestOptions.params.expr).toEqual(['server=backend_01']);
  216. expect(results).not.toBe(null);
  217. });
  218. it('should generate tag values query for one tag', () => {
  219. ctx.ds.metricFindQuery('tag_values(server)').then(data => {
  220. results = data;
  221. });
  222. expect(requestOptions.url).toBe('/tags/autoComplete/values');
  223. expect(requestOptions.params.tag).toBe('server');
  224. expect(requestOptions.params.expr).toEqual([]);
  225. expect(results).not.toBe(null);
  226. });
  227. it('should generate tag values query for a tag and expression', () => {
  228. ctx.ds.metricFindQuery('tag_values(server,server=~backend*)').then(data => {
  229. results = data;
  230. });
  231. expect(requestOptions.url).toBe('/tags/autoComplete/values');
  232. expect(requestOptions.params.tag).toBe('server');
  233. expect(requestOptions.params.expr).toEqual(['server=~backend*']);
  234. expect(results).not.toBe(null);
  235. });
  236. it('should generate tag values query for a tag with whitespace after', () => {
  237. ctx.ds.metricFindQuery('tag_values(server )').then(data => {
  238. results = data;
  239. });
  240. expect(requestOptions.url).toBe('/tags/autoComplete/values');
  241. expect(requestOptions.params.tag).toBe('server');
  242. expect(requestOptions.params.expr).toEqual([]);
  243. expect(results).not.toBe(null);
  244. });
  245. it('should generate tag values query for a tag and expression with whitespace after', () => {
  246. ctx.ds.metricFindQuery('tag_values(server , server=~backend* )').then(data => {
  247. results = data;
  248. });
  249. expect(requestOptions.url).toBe('/tags/autoComplete/values');
  250. expect(requestOptions.params.tag).toBe('server');
  251. expect(requestOptions.params.expr).toEqual(['server=~backend*']);
  252. expect(results).not.toBe(null);
  253. });
  254. });
  255. });