datasource_specs.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. import _ from 'lodash';
  2. import {describe, beforeEach, it, expect, angularMocks} from 'test/lib/common';
  3. import moment from 'moment';
  4. import angular from 'angular';
  5. import helpers from 'test/specs/helpers';
  6. import {ElasticDatasource} from "../datasource";
  7. describe('ElasticDatasource', function() {
  8. var ctx = new helpers.ServiceTestContext();
  9. beforeEach(angularMocks.module('grafana.core'));
  10. beforeEach(angularMocks.module('grafana.services'));
  11. beforeEach(ctx.providePhase(['templateSrv', 'backendSrv', 'timeSrv']));
  12. beforeEach(angularMocks.inject(function($q, $rootScope, $httpBackend, $injector) {
  13. ctx.$q = $q;
  14. ctx.$httpBackend = $httpBackend;
  15. ctx.$rootScope = $rootScope;
  16. ctx.$injector = $injector;
  17. $httpBackend.when('GET', /\.html$/).respond('');
  18. }));
  19. function createDatasource(instanceSettings) {
  20. instanceSettings.jsonData = instanceSettings.jsonData || {};
  21. ctx.ds = ctx.$injector.instantiate(ElasticDatasource, {instanceSettings: instanceSettings});
  22. }
  23. describe('When testing datasource with index pattern', function() {
  24. beforeEach(function() {
  25. createDatasource({url: 'http://es.com', index: '[asd-]YYYY.MM.DD', jsonData: {interval: 'Daily', esVersion: '2'}});
  26. });
  27. it('should translate index pattern to current day', function() {
  28. var requestOptions;
  29. ctx.backendSrv.datasourceRequest = function(options) {
  30. requestOptions = options;
  31. return ctx.$q.when({data: {}});
  32. };
  33. ctx.ds.testDatasource();
  34. ctx.$rootScope.$apply();
  35. var today = moment.utc().format("YYYY.MM.DD");
  36. expect(requestOptions.url).to.be("http://es.com/asd-" + today + '/_mapping');
  37. });
  38. });
  39. describe('When issueing metric query with interval pattern', function() {
  40. var requestOptions, parts, header;
  41. beforeEach(function() {
  42. createDatasource({url: 'http://es.com', index: '[asd-]YYYY.MM.DD', jsonData: {interval: 'Daily', esVersion: '2'}});
  43. ctx.backendSrv.datasourceRequest = function(options) {
  44. requestOptions = options;
  45. return ctx.$q.when({data: {responses: []}});
  46. };
  47. ctx.ds.query({
  48. range: {
  49. from: moment.utc([2015, 4, 30, 10]),
  50. to: moment.utc([2015, 5, 1, 10])
  51. },
  52. targets: [{ bucketAggs: [], metrics: [{type: 'raw_document'}], query: 'escape\\:test' }]
  53. });
  54. ctx.$rootScope.$apply();
  55. parts = requestOptions.data.split('\n');
  56. header = angular.fromJson(parts[0]);
  57. });
  58. it('should translate index pattern to current day', function() {
  59. expect(header.index).to.eql(['asd-2015.05.30', 'asd-2015.05.31', 'asd-2015.06.01']);
  60. });
  61. it('should json escape lucene query', function() {
  62. var body = angular.fromJson(parts[1]);
  63. expect(body.query.bool.filter[1].query_string.query).to.be('escape\\:test');
  64. });
  65. });
  66. describe('When issueing document query', function() {
  67. var requestOptions, parts, header;
  68. beforeEach(function() {
  69. createDatasource({url: 'http://es.com', index: 'test', jsonData: {esVersion: '2'}});
  70. ctx.backendSrv.datasourceRequest = function(options) {
  71. requestOptions = options;
  72. return ctx.$q.when({data: {responses: []}});
  73. };
  74. ctx.ds.query({
  75. range: { from: moment([2015, 4, 30, 10]), to: moment([2015, 5, 1, 10]) },
  76. targets: [{ bucketAggs: [], metrics: [{type: 'raw_document'}], query: 'test' }]
  77. });
  78. ctx.$rootScope.$apply();
  79. parts = requestOptions.data.split('\n');
  80. header = angular.fromJson(parts[0]);
  81. });
  82. it('should set search type to query_then_fetch', function() {
  83. expect(header.search_type).to.eql('query_then_fetch');
  84. });
  85. it('should set size', function() {
  86. var body = angular.fromJson(parts[1]);
  87. expect(body.size).to.be(500);
  88. });
  89. });
  90. describe('When getting fields', function() {
  91. var requestOptions;
  92. beforeEach(function() {
  93. createDatasource({url: 'http://es.com', index: 'metricbeat'});
  94. ctx.backendSrv.datasourceRequest = function(options) {
  95. requestOptions = options;
  96. return ctx.$q.when({data: {
  97. metricbeat: {
  98. mappings: {
  99. metricsets: {
  100. _all: {},
  101. properties: {
  102. '@timestamp': {type: 'date'},
  103. beat: {
  104. properties: {
  105. name: {
  106. fields: {raw: {type: 'keyword'}},
  107. type: 'string'
  108. },
  109. hostname: {type: 'string'},
  110. }
  111. },
  112. system: {
  113. properties: {
  114. cpu: {
  115. properties: {
  116. system: {type: 'float'},
  117. user: {type: 'float'},
  118. }
  119. },
  120. process: {
  121. properties: {
  122. cpu: {
  123. properties: {
  124. total: {type: 'float'}
  125. }
  126. },
  127. name: {type: 'string'},
  128. }
  129. },
  130. }
  131. }
  132. }
  133. }
  134. }
  135. }
  136. }});
  137. };
  138. });
  139. it('should return nested fields', function() {
  140. ctx.ds.getFields({
  141. find: 'fields',
  142. query: '*'
  143. }).then((fieldObjects) => {
  144. var fields = _.map(fieldObjects, 'text');
  145. expect(fields).to.eql([
  146. '@timestamp',
  147. 'beat.name.raw',
  148. 'beat.name',
  149. 'beat.hostname',
  150. 'system.cpu.system',
  151. 'system.cpu.user',
  152. 'system.process.cpu.total',
  153. 'system.process.name'
  154. ]);
  155. });
  156. ctx.$rootScope.$apply();
  157. });
  158. it('should return fields related to query type', function() {
  159. ctx.ds.getFields({
  160. find: 'fields',
  161. query: '*',
  162. type: 'number'
  163. }).then((fieldObjects) => {
  164. var fields = _.map(fieldObjects, 'text');
  165. expect(fields).to.eql([
  166. 'system.cpu.system',
  167. 'system.cpu.user',
  168. 'system.process.cpu.total'
  169. ]);
  170. });
  171. ctx.ds.getFields({
  172. find: 'fields',
  173. query: '*',
  174. type: 'date'
  175. }).then((fieldObjects) => {
  176. var fields = _.map(fieldObjects, 'text');
  177. expect(fields).to.eql([
  178. '@timestamp'
  179. ]);
  180. });
  181. ctx.$rootScope.$apply();
  182. });
  183. });
  184. describe('When issuing aggregation query on es5.x', function() {
  185. var requestOptions, parts, header;
  186. beforeEach(function() {
  187. createDatasource({url: 'http://es.com', index: 'test', jsonData: {esVersion: '5'}});
  188. ctx.backendSrv.datasourceRequest = function(options) {
  189. requestOptions = options;
  190. return ctx.$q.when({data: {responses: []}});
  191. };
  192. ctx.ds.query({
  193. range: { from: moment([2015, 4, 30, 10]), to: moment([2015, 5, 1, 10]) },
  194. targets: [{
  195. bucketAggs: [
  196. {type: 'date_histogram', field: '@timestamp', id: '2'}
  197. ],
  198. metrics: [
  199. {type: 'count'}], query: 'test' }
  200. ]
  201. });
  202. ctx.$rootScope.$apply();
  203. parts = requestOptions.data.split('\n');
  204. header = angular.fromJson(parts[0]);
  205. });
  206. it('should not set search type to count', function() {
  207. expect(header.search_type).to.not.eql('count');
  208. });
  209. it('should set size to 0', function() {
  210. var body = angular.fromJson(parts[1]);
  211. expect(body.size).to.be(0);
  212. });
  213. });
  214. describe('When issuing metricFind query on es5.x', function() {
  215. var requestOptions, parts, header, body, results;
  216. beforeEach(function() {
  217. createDatasource({url: 'http://es.com', index: 'test', jsonData: {esVersion: '5'}});
  218. ctx.backendSrv.datasourceRequest = function(options) {
  219. requestOptions = options;
  220. return ctx.$q.when({
  221. data: {
  222. responses: [
  223. {
  224. aggregations: {
  225. "1": {
  226. buckets: [
  227. {doc_count: 1, key: 'test'},
  228. {doc_count: 2, key: 'test2', key_as_string: 'test2_as_string'},
  229. ]
  230. }
  231. }
  232. }
  233. ]
  234. }
  235. });
  236. };
  237. ctx.ds.metricFindQuery('{"find": "terms", "field": "test"}').then(res => {
  238. results = res;
  239. });
  240. ctx.$rootScope.$apply();
  241. parts = requestOptions.data.split('\n');
  242. header = angular.fromJson(parts[0]);
  243. body = angular.fromJson(parts[1]);
  244. });
  245. it('should get results', function() {
  246. expect(results.length).to.eql(2);
  247. });
  248. it('should use key or key_as_string', function() {
  249. expect(results[0].text).to.eql('test');
  250. expect(results[1].text).to.eql('test2_as_string');
  251. });
  252. it('should not set search type to count', function() {
  253. expect(header.search_type).to.not.eql('count');
  254. });
  255. it('should set size to 0', function() {
  256. expect(body.size).to.be(0);
  257. });
  258. it('should not set terms aggregation size to 0', function() {
  259. expect(body['aggs']['1']['terms'].size).to.not.be(0);
  260. });
  261. });
  262. });