elastic_response_specs.ts 19 KB

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