datasource_specs.ts 9.2 KB

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