datasource_specs.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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']));
  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 + '/_stats');
  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: [], 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.filtered.query.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: {type: 'string'},
  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',
  146. 'beat.hostname',
  147. 'system.cpu.system',
  148. 'system.cpu.user',
  149. 'system.process.cpu.total',
  150. 'system.process.name'
  151. ]);
  152. });
  153. ctx.$rootScope.$apply();
  154. });
  155. it('should return fields related to query type', function() {
  156. ctx.ds.getFields({
  157. find: 'fields',
  158. query: '*',
  159. type: 'number'
  160. }).then((fieldObjects) => {
  161. var fields = _.map(fieldObjects, 'text');
  162. expect(fields).to.eql([
  163. 'system.cpu.system',
  164. 'system.cpu.user',
  165. 'system.process.cpu.total'
  166. ]);
  167. });
  168. ctx.ds.getFields({
  169. find: 'fields',
  170. query: '*',
  171. type: 'date'
  172. }).then((fieldObjects) => {
  173. var fields = _.map(fieldObjects, 'text');
  174. expect(fields).to.eql([
  175. '@timestamp'
  176. ]);
  177. });
  178. ctx.$rootScope.$apply();
  179. });
  180. });
  181. describe('When issuing aggregation query on es5.x', function() {
  182. var requestOptions, parts, header;
  183. beforeEach(function() {
  184. createDatasource({url: 'http://es.com', index: 'test', jsonData: {esVersion: '5'}});
  185. ctx.backendSrv.datasourceRequest = function(options) {
  186. requestOptions = options;
  187. return ctx.$q.when({data: {responses: []}});
  188. };
  189. ctx.ds.query({
  190. range: { from: moment([2015, 4, 30, 10]), to: moment([2015, 5, 1, 10]) },
  191. targets: [{
  192. bucketAggs: [
  193. {type: 'date_histogram', field: '@timestamp', id: '2'}
  194. ],
  195. metrics: [
  196. {type: 'count'}], query: 'test' }
  197. ]
  198. });
  199. ctx.$rootScope.$apply();
  200. parts = requestOptions.data.split('\n');
  201. header = angular.fromJson(parts[0]);
  202. });
  203. it('should not set search type to count', function() {
  204. expect(header.search_type).to.not.eql('count');
  205. });
  206. it('should set size to 0', function() {
  207. var body = angular.fromJson(parts[1]);
  208. expect(body.size).to.be(0);
  209. });
  210. });
  211. describe('When issuing metricFind query on es5.x', function() {
  212. var requestOptions, parts, header, body;
  213. beforeEach(function() {
  214. createDatasource({url: 'http://es.com', index: 'test', jsonData: {esVersion: '5'}});
  215. ctx.backendSrv.datasourceRequest = function(options) {
  216. requestOptions = options;
  217. return ctx.$q.when({
  218. data: {
  219. responses: [{aggregations: {"1": [{buckets: {text: 'test', value: '1'}}]}}]
  220. }
  221. });
  222. };
  223. ctx.ds.metricFindQuery('{"find": "terms", "field": "test"}');
  224. ctx.$rootScope.$apply();
  225. parts = requestOptions.data.split('\n');
  226. header = angular.fromJson(parts[0]);
  227. body = angular.fromJson(parts[1]);
  228. });
  229. it('should not set search type to count', function() {
  230. expect(header.search_type).to.not.eql('count');
  231. });
  232. it('should set size to 0', function() {
  233. expect(body.size).to.be(0);
  234. });
  235. it('should not set terms aggregation size to 0', function() {
  236. expect(body['aggs']['1']['terms'].size).to.not.be(0);
  237. });
  238. });
  239. });