datasource_specs.ts 10 KB

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