elastic_response_specs.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. import {describe, beforeEach, it, expect} from 'test/lib/common';
  2. import {ElasticResponse} from '../elastic_response';
  3. describe('ElasticResponse', function() {
  4. var targets;
  5. var response;
  6. var result;
  7. describe('simple query and count', function() {
  8. beforeEach(function() {
  9. targets = [{
  10. refId: 'A',
  11. metrics: [{type: 'count', id: '1'}],
  12. bucketAggs: [{type: 'date_histogram', field: '@timestamp', id: '2'}],
  13. }];
  14. response = {
  15. responses: [{
  16. aggregations: {
  17. "2": {
  18. buckets: [
  19. {
  20. doc_count: 10,
  21. key: 1000
  22. },
  23. {
  24. doc_count: 15,
  25. key: 2000
  26. }
  27. ]
  28. }
  29. }
  30. }]
  31. };
  32. result = new ElasticResponse(targets, response).getTimeSeries();
  33. });
  34. it('should return 1 series', function() {
  35. expect(result.data.length).to.be(1);
  36. expect(result.data[0].target).to.be('Count');
  37. expect(result.data[0].datapoints.length).to.be(2);
  38. expect(result.data[0].datapoints[0][0]).to.be(10);
  39. expect(result.data[0].datapoints[0][1]).to.be(1000);
  40. });
  41. });
  42. describe('simple query count & avg aggregation', function() {
  43. var result;
  44. beforeEach(function() {
  45. targets = [{
  46. refId: 'A',
  47. metrics: [{type: 'count', id: '1'}, {type: 'avg', field: 'value', id: '2'}],
  48. bucketAggs: [{type: 'date_histogram', field: '@timestamp', id: '3'}],
  49. }];
  50. response = {
  51. responses: [{
  52. aggregations: {
  53. "3": {
  54. buckets: [
  55. {
  56. "2": {value: 88},
  57. doc_count: 10,
  58. key: 1000
  59. },
  60. {
  61. "2": {value: 99},
  62. doc_count: 15,
  63. key: 2000
  64. }
  65. ]
  66. }
  67. }
  68. }]
  69. };
  70. result = new ElasticResponse(targets, response).getTimeSeries();
  71. });
  72. it('should return 2 series', function() {
  73. expect(result.data.length).to.be(2);
  74. expect(result.data[0].datapoints.length).to.be(2);
  75. expect(result.data[0].datapoints[0][0]).to.be(10);
  76. expect(result.data[0].datapoints[0][1]).to.be(1000);
  77. expect(result.data[1].target).to.be("Average value");
  78. expect(result.data[1].datapoints[0][0]).to.be(88);
  79. expect(result.data[1].datapoints[1][0]).to.be(99);
  80. });
  81. });
  82. describe('single group by query one metric', function() {
  83. var result;
  84. beforeEach(function() {
  85. targets = [{
  86. refId: 'A',
  87. metrics: [{type: 'count', id: '1'}],
  88. bucketAggs: [{type: 'terms', field: 'host', id: '2'}, {type: 'date_histogram', field: '@timestamp', id: '3'}],
  89. }];
  90. response = {
  91. responses: [{
  92. aggregations: {
  93. "2": {
  94. buckets: [
  95. {
  96. "3": {
  97. buckets: [
  98. {doc_count: 1, key: 1000},
  99. {doc_count: 3, key: 2000}
  100. ]
  101. },
  102. doc_count: 4,
  103. key: 'server1',
  104. },
  105. {
  106. "3": {
  107. buckets: [
  108. {doc_count: 2, key: 1000},
  109. {doc_count: 8, key: 2000}
  110. ]
  111. },
  112. doc_count: 10,
  113. key: 'server2',
  114. },
  115. ]
  116. }
  117. }
  118. }]
  119. };
  120. result = new ElasticResponse(targets, response).getTimeSeries();
  121. });
  122. it('should return 2 series', function() {
  123. expect(result.data.length).to.be(2);
  124. expect(result.data[0].datapoints.length).to.be(2);
  125. expect(result.data[0].target).to.be('server1');
  126. expect(result.data[1].target).to.be('server2');
  127. });
  128. });
  129. describe('single group by query two metrics', function() {
  130. var result;
  131. beforeEach(function() {
  132. targets = [{
  133. refId: 'A',
  134. metrics: [{type: 'count', id: '1'}, {type: 'avg', field: '@value', id: '4'}],
  135. bucketAggs: [{type: 'terms', field: 'host', id: '2'}, {type: 'date_histogram', field: '@timestamp', id: '3'}],
  136. }];
  137. response = {
  138. responses: [{
  139. aggregations: {
  140. "2": {
  141. buckets: [
  142. {
  143. "3": {
  144. buckets: [
  145. { "4": {value: 10}, doc_count: 1, key: 1000},
  146. { "4": {value: 12}, doc_count: 3, key: 2000}
  147. ]
  148. },
  149. doc_count: 4,
  150. key: 'server1',
  151. },
  152. {
  153. "3": {
  154. buckets: [
  155. { "4": {value: 20}, doc_count: 1, key: 1000},
  156. { "4": {value: 32}, doc_count: 3, key: 2000}
  157. ]
  158. },
  159. doc_count: 10,
  160. key: 'server2',
  161. },
  162. ]
  163. }
  164. }
  165. }]
  166. };
  167. result = new ElasticResponse(targets, response).getTimeSeries();
  168. });
  169. it('should return 2 series', function() {
  170. expect(result.data.length).to.be(4);
  171. expect(result.data[0].datapoints.length).to.be(2);
  172. expect(result.data[0].target).to.be('server1 Count');
  173. expect(result.data[1].target).to.be('server1 Average @value');
  174. expect(result.data[2].target).to.be('server2 Count');
  175. expect(result.data[3].target).to.be('server2 Average @value');
  176. });
  177. });
  178. describe('with percentiles ', function() {
  179. var result;
  180. beforeEach(function() {
  181. targets = [{
  182. refId: 'A',
  183. metrics: [{type: 'percentiles', settings: {percents: [75, 90]}, id: '1'}],
  184. bucketAggs: [{type: 'date_histogram', field: '@timestamp', id: '3'}],
  185. }];
  186. response = {
  187. responses: [{
  188. aggregations: {
  189. "3": {
  190. buckets: [
  191. {
  192. "1": {values: {"75": 3.3, "90": 5.5}},
  193. doc_count: 10,
  194. key: 1000
  195. },
  196. {
  197. "1": {values: {"75": 2.3, "90": 4.5}},
  198. doc_count: 15,
  199. key: 2000
  200. }
  201. ]
  202. }
  203. }
  204. }]
  205. };
  206. result = new ElasticResponse(targets, response).getTimeSeries();
  207. });
  208. it('should return 2 series', function() {
  209. expect(result.data.length).to.be(2);
  210. expect(result.data[0].datapoints.length).to.be(2);
  211. expect(result.data[0].target).to.be('p75');
  212. expect(result.data[1].target).to.be('p90');
  213. expect(result.data[0].datapoints[0][0]).to.be(3.3);
  214. expect(result.data[0].datapoints[0][1]).to.be(1000);
  215. expect(result.data[1].datapoints[1][0]).to.be(4.5);
  216. });
  217. });
  218. describe('with extended_stats', function() {
  219. var result;
  220. beforeEach(function() {
  221. targets = [{
  222. refId: 'A',
  223. metrics: [{type: 'extended_stats', meta: {max: true, std_deviation_bounds_upper: true}, id: '1'}],
  224. bucketAggs: [{type: 'terms', field: 'host', id: '3'}, {type: 'date_histogram', id: '4'}],
  225. }];
  226. response = {
  227. responses: [{
  228. aggregations: {
  229. "3": {
  230. buckets: [
  231. {
  232. key: 'server1',
  233. "4": {
  234. buckets: [{
  235. "1": {max: 10.2, min: 5.5, std_deviation_bounds: {upper: 3, lower: -2}},
  236. doc_count: 10,
  237. key: 1000
  238. }]
  239. }
  240. },
  241. {
  242. key: 'server2',
  243. "4": {
  244. buckets: [{
  245. "1": {max: 10.2, min: 5.5, std_deviation_bounds: {upper: 3, lower: -2}},
  246. doc_count: 10,
  247. key: 1000
  248. }]
  249. }
  250. },
  251. ]
  252. }
  253. }
  254. }]
  255. };
  256. result = new ElasticResponse(targets, response).getTimeSeries();
  257. });
  258. it('should return 4 series', function() {
  259. expect(result.data.length).to.be(4);
  260. expect(result.data[0].datapoints.length).to.be(1);
  261. expect(result.data[0].target).to.be('server1 Max');
  262. expect(result.data[1].target).to.be('server1 Std Dev Upper');
  263. expect(result.data[0].datapoints[0][0]).to.be(10.2);
  264. expect(result.data[1].datapoints[0][0]).to.be(3);
  265. });
  266. });
  267. describe('single group by with alias pattern', function() {
  268. var result;
  269. beforeEach(function() {
  270. targets = [{
  271. refId: 'A',
  272. metrics: [{type: 'count', id: '1'}],
  273. alias: '{{term @host}} {{metric}} and {{not_exist}} {{@host}}',
  274. bucketAggs: [
  275. {type: 'terms', field: '@host', id: '2'},
  276. {type: 'date_histogram', field: '@timestamp', id: '3'}
  277. ],
  278. }];
  279. response = {
  280. responses: [{
  281. aggregations: {
  282. "2": {
  283. buckets: [
  284. {
  285. "3": {
  286. buckets: [
  287. {doc_count: 1, key: 1000},
  288. {doc_count: 3, key: 2000}
  289. ]
  290. },
  291. doc_count: 4,
  292. key: 'server1',
  293. },
  294. {
  295. "3": {
  296. buckets: [
  297. {doc_count: 2, key: 1000},
  298. {doc_count: 8, key: 2000}
  299. ]
  300. },
  301. doc_count: 10,
  302. key: 'server2',
  303. },
  304. {
  305. "3": {
  306. buckets: [
  307. {doc_count: 2, key: 1000},
  308. {doc_count: 8, key: 2000}
  309. ]
  310. },
  311. doc_count: 10,
  312. key: 0,
  313. },
  314. ]
  315. }
  316. }
  317. }]
  318. };
  319. result = new ElasticResponse(targets, response).getTimeSeries();
  320. });
  321. it('should return 2 series', function() {
  322. expect(result.data.length).to.be(3);
  323. expect(result.data[0].datapoints.length).to.be(2);
  324. expect(result.data[0].target).to.be('server1 Count and {{not_exist}} server1');
  325. expect(result.data[1].target).to.be('server2 Count and {{not_exist}} server2');
  326. expect(result.data[2].target).to.be('0 Count and {{not_exist}} 0');
  327. });
  328. });
  329. describe('histogram response', function() {
  330. var result;
  331. beforeEach(function() {
  332. targets = [{
  333. refId: 'A',
  334. metrics: [{type: 'count', id: '1'}],
  335. bucketAggs: [{type: 'histogram', field: 'bytes', id: '3'}],
  336. }];
  337. response = {
  338. responses: [{
  339. aggregations: {
  340. "3": {
  341. buckets: [
  342. {doc_count: 1, key: 1000},
  343. {doc_count: 3, key: 2000},
  344. {doc_count: 2, key: 1000},
  345. ]
  346. }
  347. }
  348. }]
  349. };
  350. result = new ElasticResponse(targets, response).getTimeSeries();
  351. });
  352. it('should return table with byte and count', function() {
  353. expect(result.data[0].rows.length).to.be(3);
  354. expect(result.data[0].columns).to.eql([{text: 'bytes', filterable: true}, {text: 'Count'}]);
  355. });
  356. });
  357. describe('with two filters agg', function() {
  358. var result;
  359. beforeEach(function() {
  360. targets = [{
  361. refId: 'A',
  362. metrics: [{type: 'count', id: '1'}],
  363. bucketAggs: [
  364. {
  365. id: '2',
  366. type: 'filters',
  367. settings: {
  368. filters: [
  369. {query: '@metric:cpu' },
  370. {query: '@metric:logins.count' },
  371. ]
  372. }
  373. },
  374. {type: 'date_histogram', field: '@timestamp', id: '3'}
  375. ],
  376. }];
  377. response = {
  378. responses: [{
  379. aggregations: {
  380. "2": {
  381. buckets: {
  382. "@metric:cpu": {
  383. "3": {
  384. buckets: [
  385. {doc_count: 1, key: 1000},
  386. {doc_count: 3, key: 2000}
  387. ]
  388. },
  389. },
  390. "@metric:logins.count": {
  391. "3": {
  392. buckets: [
  393. {doc_count: 2, key: 1000},
  394. {doc_count: 8, key: 2000}
  395. ]
  396. },
  397. },
  398. }
  399. }
  400. }
  401. }]
  402. };
  403. result = new ElasticResponse(targets, response).getTimeSeries();
  404. });
  405. it('should return 2 series', function() {
  406. expect(result.data.length).to.be(2);
  407. expect(result.data[0].datapoints.length).to.be(2);
  408. expect(result.data[0].target).to.be('@metric:cpu');
  409. expect(result.data[1].target).to.be('@metric:logins.count');
  410. });
  411. });
  412. describe('with dropfirst and last aggregation', function() {
  413. beforeEach(function() {
  414. targets = [{
  415. refId: 'A',
  416. metrics: [{ type: 'avg', id: '1' }, { type: 'count' }],
  417. bucketAggs: [{ id: '2', type: 'date_histogram', field: 'host', settings: { trimEdges: 1} }],
  418. }];
  419. response = {
  420. responses: [{
  421. aggregations: {
  422. "2": {
  423. buckets: [
  424. {
  425. "1": { value: 1000 },
  426. key: 1,
  427. doc_count: 369,
  428. },
  429. {
  430. "1": { value: 2000 },
  431. key: 2,
  432. doc_count: 200,
  433. },
  434. {
  435. "1": { value: 2000 },
  436. key: 3,
  437. doc_count: 200,
  438. },
  439. ]
  440. }
  441. }
  442. }]
  443. };
  444. result = new ElasticResponse(targets, response).getTimeSeries();
  445. });
  446. it('should remove first and last value', function() {
  447. expect(result.data.length).to.be(2);
  448. expect(result.data[0].datapoints.length).to.be(1);
  449. });
  450. });
  451. describe('No group by time', function() {
  452. beforeEach(function() {
  453. targets = [{
  454. refId: 'A',
  455. metrics: [{type: 'avg', id: '1'}, {type: 'count' }],
  456. bucketAggs: [{id: '2', type: 'terms', field: 'host'}],
  457. }];
  458. response = {
  459. responses: [{
  460. aggregations: {
  461. "2": {
  462. buckets: [
  463. {
  464. "1": { value: 1000},
  465. key: "server-1",
  466. doc_count: 369,
  467. },
  468. {
  469. "1": { value: 2000},
  470. key: "server-2",
  471. doc_count: 200,
  472. },
  473. ]
  474. }
  475. }
  476. }]
  477. };
  478. result = new ElasticResponse(targets, response).getTimeSeries();
  479. });
  480. it('should return table', function() {
  481. expect(result.data.length).to.be(1);
  482. expect(result.data[0].type).to.be('table');
  483. expect(result.data[0].rows.length).to.be(2);
  484. expect(result.data[0].rows[0][0]).to.be("server-1");
  485. expect(result.data[0].rows[0][1]).to.be(1000);
  486. expect(result.data[0].rows[0][2]).to.be(369);
  487. expect(result.data[0].rows[1][0]).to.be("server-2");
  488. expect(result.data[0].rows[1][1]).to.be(2000);
  489. });
  490. });
  491. describe('Multiple metrics of same type', function() {
  492. beforeEach(function() {
  493. targets = [{
  494. refId: 'A',
  495. metrics: [
  496. {type: 'avg', id: '1', field: 'test'},
  497. {type: 'avg', id: '2', field: 'test2'}
  498. ],
  499. bucketAggs: [{id: '2', type: 'terms', field: 'host'}],
  500. }];
  501. response = {
  502. responses: [{
  503. aggregations: {
  504. "2": {
  505. buckets: [
  506. {
  507. "1": { value: 1000},
  508. "2": { value: 3000},
  509. key: "server-1",
  510. doc_count: 369,
  511. }
  512. ]
  513. }
  514. }
  515. }]
  516. };
  517. result = new ElasticResponse(targets, response).getTimeSeries();
  518. });
  519. it('should include field in metric name', function() {
  520. expect(result.data[0].type).to.be('table');
  521. expect(result.data[0].rows[0][1]).to.be(1000);
  522. expect(result.data[0].rows[0][2]).to.be(3000);
  523. });
  524. });
  525. describe('Raw documents query', function() {
  526. beforeEach(function() {
  527. targets = [{ refId: 'A', metrics: [{type: 'raw_document', id: '1'}], bucketAggs: [] }];
  528. response = {
  529. responses: [{
  530. hits: {
  531. total: 100,
  532. hits: [
  533. {
  534. _id: '1',
  535. _type: 'type',
  536. _index: 'index',
  537. _source: {sourceProp: "asd"},
  538. fields: {fieldProp: "field" },
  539. },
  540. {
  541. _source: {sourceProp: "asd2"},
  542. fields: {fieldProp: "field2" },
  543. }
  544. ]
  545. }
  546. }]
  547. };
  548. result = new ElasticResponse(targets, response).getTimeSeries();
  549. });
  550. it('should return docs', function() {
  551. expect(result.data.length).to.be(1);
  552. expect(result.data[0].type).to.be('docs');
  553. expect(result.data[0].total).to.be(100);
  554. expect(result.data[0].datapoints.length).to.be(2);
  555. expect(result.data[0].datapoints[0].sourceProp).to.be("asd");
  556. expect(result.data[0].datapoints[0].fieldProp).to.be("field");
  557. });
  558. });
  559. });