elastic_response_specs.ts 19 KB

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