datasource_specs.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. beforeEach(function() {
  92. createDatasource({url: 'http://es.com', index: 'metricbeat'});
  93. ctx.backendSrv.datasourceRequest = function(options) {
  94. return ctx.$q.when({data: {
  95. metricbeat: {
  96. mappings: {
  97. metricsets: {
  98. _all: {},
  99. properties: {
  100. '@timestamp': {type: 'date'},
  101. beat: {
  102. properties: {
  103. name: {
  104. fields: {raw: {type: 'keyword'}},
  105. type: 'string'
  106. },
  107. hostname: {type: 'string'},
  108. }
  109. },
  110. system: {
  111. properties: {
  112. cpu: {
  113. properties: {
  114. system: {type: 'float'},
  115. user: {type: 'float'},
  116. }
  117. },
  118. process: {
  119. properties: {
  120. cpu: {
  121. properties: {
  122. total: {type: 'float'}
  123. }
  124. },
  125. name: {type: 'string'},
  126. }
  127. },
  128. }
  129. }
  130. }
  131. }
  132. }
  133. }
  134. }});
  135. };
  136. });
  137. it('should return nested fields', function() {
  138. ctx.ds.getFields({
  139. find: 'fields',
  140. query: '*'
  141. }).then((fieldObjects) => {
  142. var fields = _.map(fieldObjects, 'text');
  143. expect(fields).to.eql([
  144. '@timestamp',
  145. 'beat.name.raw',
  146. 'beat.name',
  147. 'beat.hostname',
  148. 'system.cpu.system',
  149. 'system.cpu.user',
  150. 'system.process.cpu.total',
  151. 'system.process.name'
  152. ]);
  153. });
  154. ctx.$rootScope.$apply();
  155. });
  156. it('should return fields related to query type', function() {
  157. ctx.ds.getFields({
  158. find: 'fields',
  159. query: '*',
  160. type: 'number'
  161. }).then((fieldObjects) => {
  162. var fields = _.map(fieldObjects, 'text');
  163. expect(fields).to.eql([
  164. 'system.cpu.system',
  165. 'system.cpu.user',
  166. 'system.process.cpu.total'
  167. ]);
  168. });
  169. ctx.ds.getFields({
  170. find: 'fields',
  171. query: '*',
  172. type: 'date'
  173. }).then((fieldObjects) => {
  174. var fields = _.map(fieldObjects, 'text');
  175. expect(fields).to.eql([
  176. '@timestamp'
  177. ]);
  178. });
  179. ctx.$rootScope.$apply();
  180. });
  181. });
  182. describe('When issuing aggregation query on es5.x', function() {
  183. var requestOptions, parts, header;
  184. beforeEach(function() {
  185. createDatasource({url: 'http://es.com', index: 'test', jsonData: {esVersion: '5'}});
  186. ctx.backendSrv.datasourceRequest = function(options) {
  187. requestOptions = options;
  188. return ctx.$q.when({data: {responses: []}});
  189. };
  190. ctx.ds.query({
  191. range: { from: moment([2015, 4, 30, 10]), to: moment([2015, 5, 1, 10]) },
  192. targets: [{
  193. bucketAggs: [
  194. {type: 'date_histogram', field: '@timestamp', id: '2'}
  195. ],
  196. metrics: [
  197. {type: 'count'}], query: 'test' }
  198. ]
  199. });
  200. ctx.$rootScope.$apply();
  201. parts = requestOptions.data.split('\n');
  202. header = angular.fromJson(parts[0]);
  203. });
  204. it('should not set search type to count', function() {
  205. expect(header.search_type).to.not.eql('count');
  206. });
  207. it('should set size to 0', function() {
  208. var body = angular.fromJson(parts[1]);
  209. expect(body.size).to.be(0);
  210. });
  211. });
  212. describe('When issuing metricFind query on es5.x', function() {
  213. var requestOptions, parts, header, body, results;
  214. beforeEach(function() {
  215. createDatasource({url: 'http://es.com', index: 'test', jsonData: {esVersion: '5'}});
  216. ctx.backendSrv.datasourceRequest = function(options) {
  217. requestOptions = options;
  218. return ctx.$q.when({
  219. data: {
  220. responses: [
  221. {
  222. aggregations: {
  223. "1": {
  224. buckets: [
  225. {doc_count: 1, key: 'test'},
  226. {doc_count: 2, key: 'test2', key_as_string: 'test2_as_string'},
  227. ]
  228. }
  229. }
  230. }
  231. ]
  232. }
  233. });
  234. };
  235. ctx.ds.metricFindQuery('{"find": "terms", "field": "test"}').then(res => {
  236. results = res;
  237. });
  238. ctx.$rootScope.$apply();
  239. parts = requestOptions.data.split('\n');
  240. header = angular.fromJson(parts[0]);
  241. body = angular.fromJson(parts[1]);
  242. });
  243. it('should get results', function() {
  244. expect(results.length).to.eql(2);
  245. });
  246. it('should use key or key_as_string', function() {
  247. expect(results[0].text).to.eql('test');
  248. expect(results[1].text).to.eql('test2_as_string');
  249. });
  250. it('should not set search type to count', function() {
  251. expect(header.search_type).to.not.eql('count');
  252. });
  253. it('should set size to 0', function() {
  254. expect(body.size).to.be(0);
  255. });
  256. it('should not set terms aggregation size to 0', function() {
  257. expect(body['aggs']['1']['terms'].size).to.not.be(0);
  258. });
  259. });
  260. });