elastic_response.test.ts 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471
  1. import { DataFrameView, FieldCache, KeyValue, MutableDataFrame } from '@grafana/data';
  2. import flatten from 'app/core/utils/flatten';
  3. import { ElasticResponse } from '../elastic_response';
  4. import { highlightTags } from '../query_def';
  5. import { ElasticsearchQuery } from '../types';
  6. describe('ElasticResponse', () => {
  7. let targets: ElasticsearchQuery[];
  8. let response: any;
  9. let result: any;
  10. describe('refId matching', () => {
  11. // We default to the old table structure to ensure backward compatibility,
  12. // therefore we only process responses as DataFrames when there's at least one
  13. // raw_data (new) query type.
  14. // We should test if refId gets populated wether there's such type of query or not
  15. interface MockedQueryData {
  16. target: ElasticsearchQuery;
  17. response: any;
  18. }
  19. const countQuery: MockedQueryData = {
  20. target: {
  21. refId: 'COUNT_GROUPBY_DATE_HISTOGRAM',
  22. metrics: [{ type: 'count', id: 'c_1' }],
  23. bucketAggs: [{ type: 'date_histogram', field: '@timestamp', id: 'c_2' }],
  24. } as ElasticsearchQuery,
  25. response: {
  26. aggregations: {
  27. c_2: {
  28. buckets: [
  29. {
  30. doc_count: 10,
  31. key: 1000,
  32. },
  33. ],
  34. },
  35. },
  36. },
  37. };
  38. const countGroupByHistogramQuery: MockedQueryData = {
  39. target: {
  40. refId: 'COUNT_GROUPBY_HISTOGRAM',
  41. metrics: [{ type: 'count', id: 'h_3' }],
  42. bucketAggs: [{ type: 'histogram', field: 'bytes', id: 'h_4' }],
  43. },
  44. response: {
  45. aggregations: {
  46. h_4: {
  47. buckets: [{ doc_count: 1, key: 1000 }],
  48. },
  49. },
  50. },
  51. };
  52. const rawDocumentQuery: MockedQueryData = {
  53. target: {
  54. refId: 'RAW_DOC',
  55. metrics: [{ type: 'raw_document', id: 'r_5' }],
  56. bucketAggs: [],
  57. },
  58. response: {
  59. hits: {
  60. total: 2,
  61. hits: [
  62. {
  63. _id: '5',
  64. _type: 'type',
  65. _index: 'index',
  66. _source: { sourceProp: 'asd' },
  67. fields: { fieldProp: 'field' },
  68. },
  69. {
  70. _source: { sourceProp: 'asd2' },
  71. fields: { fieldProp: 'field2' },
  72. },
  73. ],
  74. },
  75. },
  76. };
  77. const percentilesQuery: MockedQueryData = {
  78. target: {
  79. refId: 'PERCENTILE',
  80. metrics: [{ type: 'percentiles', settings: { percents: ['75', '90'] }, id: 'p_1' }],
  81. bucketAggs: [{ type: 'date_histogram', field: '@timestamp', id: 'p_3' }],
  82. },
  83. response: {
  84. aggregations: {
  85. p_3: {
  86. buckets: [
  87. {
  88. p_1: { values: { '75': 3.3, '90': 5.5 } },
  89. doc_count: 10,
  90. key: 1000,
  91. },
  92. {
  93. p_1: { values: { '75': 2.3, '90': 4.5 } },
  94. doc_count: 15,
  95. key: 2000,
  96. },
  97. ],
  98. },
  99. },
  100. },
  101. };
  102. const extendedStatsQuery: MockedQueryData = {
  103. target: {
  104. refId: 'EXTENDEDSTATS',
  105. metrics: [
  106. {
  107. type: 'extended_stats',
  108. meta: { max: true, std_deviation_bounds_upper: true },
  109. id: 'e_1',
  110. },
  111. ],
  112. bucketAggs: [
  113. { type: 'terms', field: 'host', id: 'e_3' },
  114. { type: 'date_histogram', id: 'e_4' },
  115. ],
  116. },
  117. response: {
  118. aggregations: {
  119. e_3: {
  120. buckets: [
  121. {
  122. key: 'server1',
  123. e_4: {
  124. buckets: [
  125. {
  126. e_1: {
  127. max: 10.2,
  128. min: 5.5,
  129. std_deviation_bounds: { upper: 3, lower: -2 },
  130. },
  131. doc_count: 10,
  132. key: 1000,
  133. },
  134. ],
  135. },
  136. },
  137. {
  138. key: 'server2',
  139. e_4: {
  140. buckets: [
  141. {
  142. e_1: {
  143. max: 10.2,
  144. min: 5.5,
  145. std_deviation_bounds: { upper: 3, lower: -2 },
  146. },
  147. doc_count: 10,
  148. key: 1000,
  149. },
  150. ],
  151. },
  152. },
  153. ],
  154. },
  155. },
  156. },
  157. };
  158. const commonTargets = [
  159. { ...countQuery.target },
  160. { ...countGroupByHistogramQuery.target },
  161. { ...rawDocumentQuery.target },
  162. { ...percentilesQuery.target },
  163. { ...extendedStatsQuery.target },
  164. ];
  165. const commonResponses = [
  166. { ...countQuery.response },
  167. { ...countGroupByHistogramQuery.response },
  168. { ...rawDocumentQuery.response },
  169. { ...percentilesQuery.response },
  170. { ...extendedStatsQuery.response },
  171. ];
  172. describe('When processing responses as DataFrames (raw_data query present)', () => {
  173. beforeEach(() => {
  174. targets = [
  175. ...commonTargets,
  176. // Raw Data Query
  177. {
  178. refId: 'D',
  179. metrics: [{ type: 'raw_data', id: '6' }],
  180. bucketAggs: [],
  181. },
  182. ];
  183. response = {
  184. responses: [
  185. ...commonResponses,
  186. // Raw Data Query
  187. {
  188. hits: {
  189. total: {
  190. relation: 'eq',
  191. value: 1,
  192. },
  193. hits: [
  194. {
  195. _id: '6',
  196. _type: '_doc',
  197. _index: 'index',
  198. _source: { sourceProp: 'asd' },
  199. },
  200. ],
  201. },
  202. },
  203. ],
  204. };
  205. result = new ElasticResponse(targets, response).getTimeSeries();
  206. });
  207. it('should add the correct refId to each returned series', () => {
  208. expect(result.data[0].refId).toBe(countQuery.target.refId);
  209. expect(result.data[1].refId).toBe(countGroupByHistogramQuery.target.refId);
  210. expect(result.data[2].refId).toBe(rawDocumentQuery.target.refId);
  211. expect(result.data[3].refId).toBe(percentilesQuery.target.refId);
  212. expect(result.data[4].refId).toBe(percentilesQuery.target.refId);
  213. expect(result.data[5].refId).toBe(extendedStatsQuery.target.refId);
  214. // Raw Data query
  215. expect(result.data[result.data.length - 1].refId).toBe('D');
  216. });
  217. });
  218. describe('When NOT processing responses as DataFrames (raw_data query NOT present)', () => {
  219. beforeEach(() => {
  220. targets = [...commonTargets];
  221. response = {
  222. responses: [...commonResponses],
  223. };
  224. result = new ElasticResponse(targets, response).getTimeSeries();
  225. });
  226. it('should add the correct refId to each returned series', () => {
  227. expect(result.data[0].refId).toBe(countQuery.target.refId);
  228. expect(result.data[1].refId).toBe(countGroupByHistogramQuery.target.refId);
  229. expect(result.data[2].refId).toBe(rawDocumentQuery.target.refId);
  230. expect(result.data[3].refId).toBe(percentilesQuery.target.refId);
  231. expect(result.data[4].refId).toBe(percentilesQuery.target.refId);
  232. expect(result.data[5].refId).toBe(extendedStatsQuery.target.refId);
  233. });
  234. });
  235. });
  236. describe('simple query and count', () => {
  237. beforeEach(() => {
  238. targets = [
  239. {
  240. refId: 'A',
  241. metrics: [{ type: 'count', id: '1' }],
  242. bucketAggs: [{ type: 'date_histogram', field: '@timestamp', id: '2' }],
  243. },
  244. ];
  245. response = {
  246. responses: [
  247. {
  248. aggregations: {
  249. '2': {
  250. buckets: [
  251. {
  252. doc_count: 10,
  253. key: 1000,
  254. },
  255. {
  256. doc_count: 15,
  257. key: 2000,
  258. },
  259. ],
  260. },
  261. },
  262. },
  263. ],
  264. };
  265. result = new ElasticResponse(targets, response).getTimeSeries();
  266. });
  267. it('should return 1 series', () => {
  268. expect(result.data.length).toBe(1);
  269. expect(result.data[0].target).toBe('Count');
  270. expect(result.data[0].datapoints.length).toBe(2);
  271. expect(result.data[0].datapoints[0][0]).toBe(10);
  272. expect(result.data[0].datapoints[0][1]).toBe(1000);
  273. });
  274. });
  275. describe('simple query count & avg aggregation', () => {
  276. let result: any;
  277. beforeEach(() => {
  278. targets = [
  279. {
  280. refId: 'A',
  281. metrics: [
  282. { type: 'count', id: '1' },
  283. { type: 'avg', field: 'value', id: '2' },
  284. ],
  285. bucketAggs: [{ type: 'date_histogram', field: '@timestamp', id: '3' }],
  286. },
  287. ];
  288. response = {
  289. responses: [
  290. {
  291. aggregations: {
  292. '3': {
  293. buckets: [
  294. {
  295. '2': { value: 88 },
  296. doc_count: 10,
  297. key: 1000,
  298. },
  299. {
  300. '2': { value: 99 },
  301. doc_count: 15,
  302. key: 2000,
  303. },
  304. ],
  305. },
  306. },
  307. },
  308. ],
  309. };
  310. result = new ElasticResponse(targets, response).getTimeSeries();
  311. });
  312. it('should return 2 series', () => {
  313. expect(result.data.length).toBe(2);
  314. expect(result.data[0].datapoints.length).toBe(2);
  315. expect(result.data[0].datapoints[0][0]).toBe(10);
  316. expect(result.data[0].datapoints[0][1]).toBe(1000);
  317. expect(result.data[1].target).toBe('Average value');
  318. expect(result.data[1].datapoints[0][0]).toBe(88);
  319. expect(result.data[1].datapoints[1][0]).toBe(99);
  320. });
  321. });
  322. describe('single group by query one metric', () => {
  323. let result: any;
  324. beforeEach(() => {
  325. targets = [
  326. {
  327. refId: 'A',
  328. metrics: [{ type: 'count', id: '1' }],
  329. bucketAggs: [
  330. { type: 'terms', field: 'host', id: '2' },
  331. { type: 'date_histogram', field: '@timestamp', id: '3' },
  332. ],
  333. },
  334. ];
  335. response = {
  336. responses: [
  337. {
  338. aggregations: {
  339. '2': {
  340. buckets: [
  341. {
  342. '3': {
  343. buckets: [
  344. { doc_count: 1, key: 1000 },
  345. { doc_count: 3, key: 2000 },
  346. ],
  347. },
  348. doc_count: 4,
  349. key: 'server1',
  350. },
  351. {
  352. '3': {
  353. buckets: [
  354. { doc_count: 2, key: 1000 },
  355. { doc_count: 8, key: 2000 },
  356. ],
  357. },
  358. doc_count: 10,
  359. key: 'server2',
  360. },
  361. ],
  362. },
  363. },
  364. },
  365. ],
  366. };
  367. result = new ElasticResponse(targets, response).getTimeSeries();
  368. });
  369. it('should return 2 series', () => {
  370. expect(result.data.length).toBe(2);
  371. expect(result.data[0].datapoints.length).toBe(2);
  372. expect(result.data[0].target).toBe('server1');
  373. expect(result.data[1].target).toBe('server2');
  374. });
  375. });
  376. describe('single group by query two metrics', () => {
  377. let result: any;
  378. beforeEach(() => {
  379. targets = [
  380. {
  381. refId: 'A',
  382. metrics: [
  383. { type: 'count', id: '1' },
  384. { type: 'avg', field: '@value', id: '4' },
  385. ],
  386. bucketAggs: [
  387. { type: 'terms', field: 'host', id: '2' },
  388. { type: 'date_histogram', field: '@timestamp', id: '3' },
  389. ],
  390. },
  391. ];
  392. response = {
  393. responses: [
  394. {
  395. aggregations: {
  396. '2': {
  397. buckets: [
  398. {
  399. '3': {
  400. buckets: [
  401. { '4': { value: 10 }, doc_count: 1, key: 1000 },
  402. { '4': { value: 12 }, doc_count: 3, key: 2000 },
  403. ],
  404. },
  405. doc_count: 4,
  406. key: 'server1',
  407. },
  408. {
  409. '3': {
  410. buckets: [
  411. { '4': { value: 20 }, doc_count: 1, key: 1000 },
  412. { '4': { value: 32 }, doc_count: 3, key: 2000 },
  413. ],
  414. },
  415. doc_count: 10,
  416. key: 'server2',
  417. },
  418. ],
  419. },
  420. },
  421. },
  422. ],
  423. };
  424. result = new ElasticResponse(targets, response).getTimeSeries();
  425. });
  426. it('should return 2 series', () => {
  427. expect(result.data.length).toBe(4);
  428. expect(result.data[0].datapoints.length).toBe(2);
  429. expect(result.data[0].target).toBe('server1 Count');
  430. expect(result.data[1].target).toBe('server1 Average @value');
  431. expect(result.data[2].target).toBe('server2 Count');
  432. expect(result.data[3].target).toBe('server2 Average @value');
  433. });
  434. });
  435. describe('with percentiles ', () => {
  436. let result: any;
  437. beforeEach(() => {
  438. targets = [
  439. {
  440. refId: 'A',
  441. metrics: [{ type: 'percentiles', settings: { percents: ['75', '90'] }, id: '1', field: '@value' }],
  442. bucketAggs: [{ type: 'date_histogram', field: '@timestamp', id: '3' }],
  443. },
  444. ];
  445. response = {
  446. responses: [
  447. {
  448. aggregations: {
  449. '3': {
  450. buckets: [
  451. {
  452. '1': { values: { '75': 3.3, '90': 5.5 } },
  453. doc_count: 10,
  454. key: 1000,
  455. },
  456. {
  457. '1': { values: { '75': 2.3, '90': 4.5 } },
  458. doc_count: 15,
  459. key: 2000,
  460. },
  461. ],
  462. },
  463. },
  464. },
  465. ],
  466. };
  467. result = new ElasticResponse(targets, response).getTimeSeries();
  468. });
  469. it('should return 2 series', () => {
  470. expect(result.data.length).toBe(2);
  471. expect(result.data[0].datapoints.length).toBe(2);
  472. expect(result.data[0].target).toBe('p75 @value');
  473. expect(result.data[1].target).toBe('p90 @value');
  474. expect(result.data[0].datapoints[0][0]).toBe(3.3);
  475. expect(result.data[0].datapoints[0][1]).toBe(1000);
  476. expect(result.data[1].datapoints[1][0]).toBe(4.5);
  477. });
  478. });
  479. describe('with extended_stats', () => {
  480. let result: any;
  481. beforeEach(() => {
  482. targets = [
  483. {
  484. refId: 'A',
  485. metrics: [
  486. {
  487. type: 'extended_stats',
  488. meta: { max: true, std_deviation_bounds_upper: true },
  489. id: '1',
  490. field: '@value',
  491. },
  492. ],
  493. bucketAggs: [
  494. { type: 'terms', field: 'host', id: '3' },
  495. { type: 'date_histogram', id: '4' },
  496. ],
  497. },
  498. ];
  499. response = {
  500. responses: [
  501. {
  502. aggregations: {
  503. '3': {
  504. buckets: [
  505. {
  506. key: 'server1',
  507. '4': {
  508. buckets: [
  509. {
  510. '1': {
  511. max: 10.2,
  512. min: 5.5,
  513. std_deviation_bounds: { upper: 3, lower: -2 },
  514. },
  515. doc_count: 10,
  516. key: 1000,
  517. },
  518. ],
  519. },
  520. },
  521. {
  522. key: 'server2',
  523. '4': {
  524. buckets: [
  525. {
  526. '1': {
  527. max: 10.2,
  528. min: 5.5,
  529. std_deviation_bounds: { upper: 3, lower: -2 },
  530. },
  531. doc_count: 10,
  532. key: 1000,
  533. },
  534. ],
  535. },
  536. },
  537. ],
  538. },
  539. },
  540. },
  541. ],
  542. };
  543. result = new ElasticResponse(targets, response).getTimeSeries();
  544. });
  545. it('should return 4 series', () => {
  546. expect(result.data.length).toBe(4);
  547. expect(result.data[0].datapoints.length).toBe(1);
  548. expect(result.data[0].target).toBe('server1 Max @value');
  549. expect(result.data[1].target).toBe('server1 Std Dev Upper @value');
  550. expect(result.data[0].datapoints[0][0]).toBe(10.2);
  551. expect(result.data[1].datapoints[0][0]).toBe(3);
  552. });
  553. });
  554. describe('with top_metrics', () => {
  555. beforeEach(() => {
  556. targets = [
  557. {
  558. refId: 'A',
  559. metrics: [
  560. {
  561. type: 'top_metrics',
  562. settings: {
  563. order: 'top',
  564. orderBy: '@timestamp',
  565. metrics: ['@value', '@anotherValue'],
  566. },
  567. id: '1',
  568. },
  569. ],
  570. bucketAggs: [{ type: 'date_histogram', id: '2' }],
  571. },
  572. ];
  573. response = {
  574. responses: [
  575. {
  576. aggregations: {
  577. '2': {
  578. buckets: [
  579. {
  580. key: new Date('2021-01-01T00:00:00.000Z').valueOf(),
  581. key_as_string: '2021-01-01T00:00:00.000Z',
  582. '1': {
  583. top: [{ sort: ['2021-01-01T00:00:00.000Z'], metrics: { '@value': 1, '@anotherValue': 2 } }],
  584. },
  585. },
  586. {
  587. key: new Date('2021-01-01T00:00:10.000Z').valueOf(),
  588. key_as_string: '2021-01-01T00:00:10.000Z',
  589. '1': {
  590. top: [{ sort: ['2021-01-01T00:00:10.000Z'], metrics: { '@value': 1, '@anotherValue': 2 } }],
  591. },
  592. },
  593. ],
  594. },
  595. },
  596. },
  597. ],
  598. };
  599. });
  600. it('should return 2 series', () => {
  601. const result = new ElasticResponse(targets, response).getTimeSeries();
  602. expect(result.data.length).toBe(2);
  603. const firstSeries = result.data[0];
  604. expect(firstSeries.target).toBe('Top Metrics @value');
  605. expect(firstSeries.datapoints.length).toBe(2);
  606. expect(firstSeries.datapoints).toEqual([
  607. [1, new Date('2021-01-01T00:00:00.000Z').valueOf()],
  608. [1, new Date('2021-01-01T00:00:10.000Z').valueOf()],
  609. ]);
  610. const secondSeries = result.data[1];
  611. expect(secondSeries.target).toBe('Top Metrics @anotherValue');
  612. expect(secondSeries.datapoints.length).toBe(2);
  613. expect(secondSeries.datapoints).toEqual([
  614. [2, new Date('2021-01-01T00:00:00.000Z').valueOf()],
  615. [2, new Date('2021-01-01T00:00:10.000Z').valueOf()],
  616. ]);
  617. });
  618. });
  619. describe('single group by with alias pattern', () => {
  620. let result: any;
  621. beforeEach(() => {
  622. targets = [
  623. {
  624. refId: 'A',
  625. metrics: [{ type: 'count', id: '1' }],
  626. alias: '{{term @host}} {{metric}} and {{not_exist}} {{@host}}',
  627. bucketAggs: [
  628. { type: 'terms', field: '@host', id: '2' },
  629. { type: 'date_histogram', field: '@timestamp', id: '3' },
  630. ],
  631. },
  632. ];
  633. response = {
  634. responses: [
  635. {
  636. aggregations: {
  637. '2': {
  638. buckets: [
  639. {
  640. '3': {
  641. buckets: [
  642. { doc_count: 1, key: 1000 },
  643. { doc_count: 3, key: 2000 },
  644. ],
  645. },
  646. doc_count: 4,
  647. key: 'server1',
  648. },
  649. {
  650. '3': {
  651. buckets: [
  652. { doc_count: 2, key: 1000 },
  653. { doc_count: 8, key: 2000 },
  654. ],
  655. },
  656. doc_count: 10,
  657. key: 'server2',
  658. },
  659. {
  660. '3': {
  661. buckets: [
  662. { doc_count: 2, key: 1000 },
  663. { doc_count: 8, key: 2000 },
  664. ],
  665. },
  666. doc_count: 10,
  667. key: 0,
  668. },
  669. ],
  670. },
  671. },
  672. },
  673. ],
  674. };
  675. result = new ElasticResponse(targets, response).getTimeSeries();
  676. });
  677. it('should return 2 series', () => {
  678. expect(result.data.length).toBe(3);
  679. expect(result.data[0].datapoints.length).toBe(2);
  680. expect(result.data[0].target).toBe('server1 Count and {{not_exist}} server1');
  681. expect(result.data[1].target).toBe('server2 Count and {{not_exist}} server2');
  682. expect(result.data[2].target).toBe('0 Count and {{not_exist}} 0');
  683. });
  684. });
  685. describe('histogram response', () => {
  686. let result: any;
  687. beforeEach(() => {
  688. targets = [
  689. {
  690. refId: 'A',
  691. metrics: [{ type: 'count', id: '1' }],
  692. bucketAggs: [{ type: 'histogram', field: 'bytes', id: '3' }],
  693. },
  694. ];
  695. response = {
  696. responses: [
  697. {
  698. aggregations: {
  699. '3': {
  700. buckets: [
  701. { doc_count: 1, key: 1000 },
  702. { doc_count: 3, key: 2000 },
  703. { doc_count: 2, key: 1000 },
  704. ],
  705. },
  706. },
  707. },
  708. ],
  709. };
  710. result = new ElasticResponse(targets, response).getTimeSeries();
  711. });
  712. it('should return table with byte and count', () => {
  713. expect(result.data[0].rows.length).toBe(3);
  714. expect(result.data[0].columns).toEqual([{ text: 'bytes', filterable: true }, { text: 'Count' }]);
  715. });
  716. });
  717. describe('with two filters agg', () => {
  718. let result: any;
  719. beforeEach(() => {
  720. targets = [
  721. {
  722. refId: 'A',
  723. metrics: [{ type: 'count', id: '1' }],
  724. bucketAggs: [
  725. {
  726. id: '2',
  727. type: 'filters',
  728. settings: {
  729. filters: [
  730. { query: '@metric:cpu', label: '' },
  731. { query: '@metric:logins.count', label: '' },
  732. ],
  733. },
  734. },
  735. { type: 'date_histogram', field: '@timestamp', id: '3' },
  736. ],
  737. },
  738. ];
  739. response = {
  740. responses: [
  741. {
  742. aggregations: {
  743. '2': {
  744. buckets: {
  745. '@metric:cpu': {
  746. '3': {
  747. buckets: [
  748. { doc_count: 1, key: 1000 },
  749. { doc_count: 3, key: 2000 },
  750. ],
  751. },
  752. },
  753. '@metric:logins.count': {
  754. '3': {
  755. buckets: [
  756. { doc_count: 2, key: 1000 },
  757. { doc_count: 8, key: 2000 },
  758. ],
  759. },
  760. },
  761. },
  762. },
  763. },
  764. },
  765. ],
  766. };
  767. result = new ElasticResponse(targets, response).getTimeSeries();
  768. });
  769. it('should return 2 series', () => {
  770. expect(result.data.length).toBe(2);
  771. expect(result.data[0].datapoints.length).toBe(2);
  772. expect(result.data[0].target).toBe('@metric:cpu');
  773. expect(result.data[1].target).toBe('@metric:logins.count');
  774. });
  775. });
  776. describe('with dropfirst and last aggregation', () => {
  777. beforeEach(() => {
  778. targets = [
  779. {
  780. refId: 'A',
  781. metrics: [
  782. { type: 'avg', id: '1', field: '@value' },
  783. { type: 'count', id: '3' },
  784. ],
  785. bucketAggs: [
  786. {
  787. id: '2',
  788. type: 'date_histogram',
  789. field: 'host',
  790. settings: { trimEdges: '1' },
  791. },
  792. ],
  793. },
  794. ];
  795. response = {
  796. responses: [
  797. {
  798. aggregations: {
  799. '2': {
  800. buckets: [
  801. {
  802. '1': { value: 1000 },
  803. key: 1,
  804. doc_count: 369,
  805. },
  806. {
  807. '1': { value: 2000 },
  808. key: 2,
  809. doc_count: 200,
  810. },
  811. {
  812. '1': { value: 2000 },
  813. key: 3,
  814. doc_count: 200,
  815. },
  816. ],
  817. },
  818. },
  819. },
  820. ],
  821. };
  822. result = new ElasticResponse(targets, response).getTimeSeries();
  823. });
  824. it('should remove first and last value', () => {
  825. expect(result.data.length).toBe(2);
  826. expect(result.data[0].datapoints.length).toBe(1);
  827. });
  828. });
  829. describe('No group by time', () => {
  830. beforeEach(() => {
  831. targets = [
  832. {
  833. refId: 'A',
  834. metrics: [
  835. { type: 'avg', id: '1', field: '@value' },
  836. { type: 'count', id: '3' },
  837. ],
  838. bucketAggs: [{ id: '2', type: 'terms', field: 'host' }],
  839. },
  840. ];
  841. response = {
  842. responses: [
  843. {
  844. aggregations: {
  845. '2': {
  846. buckets: [
  847. {
  848. '1': { value: 1000 },
  849. key: 'server-1',
  850. doc_count: 369,
  851. },
  852. {
  853. '1': { value: 2000 },
  854. key: 'server-2',
  855. doc_count: 200,
  856. },
  857. ],
  858. },
  859. },
  860. },
  861. ],
  862. };
  863. result = new ElasticResponse(targets, response).getTimeSeries();
  864. });
  865. it('should return table', () => {
  866. expect(result.data.length).toBe(1);
  867. expect(result.data[0].type).toBe('table');
  868. expect(result.data[0].rows.length).toBe(2);
  869. expect(result.data[0].rows[0][0]).toBe('server-1');
  870. expect(result.data[0].rows[0][1]).toBe(1000);
  871. expect(result.data[0].rows[0][2]).toBe(369);
  872. expect(result.data[0].rows[1][0]).toBe('server-2');
  873. expect(result.data[0].rows[1][1]).toBe(2000);
  874. });
  875. });
  876. describe('No group by time with percentiles ', () => {
  877. let result: any;
  878. beforeEach(() => {
  879. targets = [
  880. {
  881. refId: 'A',
  882. metrics: [{ type: 'percentiles', field: 'value', settings: { percents: ['75', '90'] }, id: '1' }],
  883. bucketAggs: [{ type: 'terms', field: 'id', id: '3' }],
  884. },
  885. ];
  886. response = {
  887. responses: [
  888. {
  889. aggregations: {
  890. '3': {
  891. buckets: [
  892. {
  893. '1': { values: { '75': 3.3, '90': 5.5 } },
  894. doc_count: 10,
  895. key: 'id1',
  896. },
  897. {
  898. '1': { values: { '75': 2.3, '90': 4.5 } },
  899. doc_count: 15,
  900. key: 'id2',
  901. },
  902. ],
  903. },
  904. },
  905. },
  906. ],
  907. };
  908. result = new ElasticResponse(targets, response).getTimeSeries();
  909. });
  910. it('should return table', () => {
  911. expect(result.data.length).toBe(1);
  912. expect(result.data[0].type).toBe('table');
  913. expect(result.data[0].columns[0].text).toBe('id');
  914. expect(result.data[0].columns[1].text).toBe('p75 value');
  915. expect(result.data[0].columns[2].text).toBe('p90 value');
  916. expect(result.data[0].rows.length).toBe(2);
  917. expect(result.data[0].rows[0][0]).toBe('id1');
  918. expect(result.data[0].rows[0][1]).toBe(3.3);
  919. expect(result.data[0].rows[0][2]).toBe(5.5);
  920. expect(result.data[0].rows[1][0]).toBe('id2');
  921. expect(result.data[0].rows[1][1]).toBe(2.3);
  922. expect(result.data[0].rows[1][2]).toBe(4.5);
  923. });
  924. });
  925. describe('Multiple metrics of same type', () => {
  926. beforeEach(() => {
  927. targets = [
  928. {
  929. refId: 'A',
  930. metrics: [
  931. { type: 'avg', id: '1', field: 'test' },
  932. { type: 'avg', id: '2', field: 'test2' },
  933. ],
  934. bucketAggs: [{ id: '2', type: 'terms', field: 'host' }],
  935. },
  936. ];
  937. response = {
  938. responses: [
  939. {
  940. aggregations: {
  941. '2': {
  942. buckets: [
  943. {
  944. '1': { value: 1000 },
  945. '2': { value: 3000 },
  946. key: 'server-1',
  947. doc_count: 369,
  948. },
  949. ],
  950. },
  951. },
  952. },
  953. ],
  954. };
  955. result = new ElasticResponse(targets, response).getTimeSeries();
  956. });
  957. it('should include field in metric name', () => {
  958. expect(result.data[0].type).toBe('table');
  959. expect(result.data[0].rows[0][1]).toBe(1000);
  960. expect(result.data[0].rows[0][2]).toBe(3000);
  961. });
  962. });
  963. describe('Raw documents query', () => {
  964. beforeEach(() => {
  965. targets = [
  966. {
  967. refId: 'A',
  968. metrics: [{ type: 'raw_document', id: '1' }],
  969. bucketAggs: [],
  970. },
  971. ];
  972. response = {
  973. responses: [
  974. {
  975. hits: {
  976. total: 100,
  977. hits: [
  978. {
  979. _id: '1',
  980. _type: 'type',
  981. _index: 'index',
  982. _source: { sourceProp: 'asd' },
  983. fields: { fieldProp: 'field' },
  984. },
  985. {
  986. _source: { sourceProp: 'asd2' },
  987. fields: { fieldProp: 'field2' },
  988. },
  989. ],
  990. },
  991. },
  992. ],
  993. };
  994. result = new ElasticResponse(targets, response).getTimeSeries();
  995. });
  996. it('should return docs', () => {
  997. expect(result.data.length).toBe(1);
  998. expect(result.data[0].type).toBe('docs');
  999. expect(result.data[0].total).toBe(100);
  1000. expect(result.data[0].datapoints.length).toBe(2);
  1001. expect(result.data[0].datapoints[0].sourceProp).toBe('asd');
  1002. expect(result.data[0].datapoints[0].fieldProp).toBe('field');
  1003. });
  1004. });
  1005. describe('with bucket_script ', () => {
  1006. let result: any;
  1007. beforeEach(() => {
  1008. targets = [
  1009. {
  1010. refId: 'A',
  1011. metrics: [
  1012. { id: '1', type: 'sum', field: '@value' },
  1013. { id: '3', type: 'max', field: '@value' },
  1014. {
  1015. id: '4',
  1016. pipelineVariables: [
  1017. { name: 'var1', pipelineAgg: '1' },
  1018. { name: 'var2', pipelineAgg: '3' },
  1019. ],
  1020. settings: { script: 'params.var1 * params.var2' },
  1021. type: 'bucket_script',
  1022. },
  1023. ],
  1024. bucketAggs: [{ type: 'date_histogram', field: '@timestamp', id: '2' }],
  1025. },
  1026. ];
  1027. response = {
  1028. responses: [
  1029. {
  1030. aggregations: {
  1031. '2': {
  1032. buckets: [
  1033. {
  1034. 1: { value: 2 },
  1035. 3: { value: 3 },
  1036. 4: { value: 6 },
  1037. doc_count: 60,
  1038. key: 1000,
  1039. },
  1040. {
  1041. 1: { value: 3 },
  1042. 3: { value: 4 },
  1043. 4: { value: 12 },
  1044. doc_count: 60,
  1045. key: 2000,
  1046. },
  1047. ],
  1048. },
  1049. },
  1050. },
  1051. ],
  1052. };
  1053. result = new ElasticResponse(targets, response).getTimeSeries();
  1054. });
  1055. it('should return 3 series', () => {
  1056. expect(result.data.length).toBe(3);
  1057. expect(result.data[0].datapoints.length).toBe(2);
  1058. expect(result.data[0].target).toBe('Sum @value');
  1059. expect(result.data[1].target).toBe('Max @value');
  1060. expect(result.data[2].target).toBe('Sum @value * Max @value');
  1061. expect(result.data[0].datapoints[0][0]).toBe(2);
  1062. expect(result.data[1].datapoints[0][0]).toBe(3);
  1063. expect(result.data[2].datapoints[0][0]).toBe(6);
  1064. expect(result.data[0].datapoints[1][0]).toBe(3);
  1065. expect(result.data[1].datapoints[1][0]).toBe(4);
  1066. expect(result.data[2].datapoints[1][0]).toBe(12);
  1067. });
  1068. });
  1069. describe('terms with bucket_script and two scripts', () => {
  1070. let result: any;
  1071. beforeEach(() => {
  1072. targets = [
  1073. {
  1074. refId: 'A',
  1075. metrics: [
  1076. { id: '1', type: 'sum', field: '@value' },
  1077. { id: '3', type: 'max', field: '@value' },
  1078. {
  1079. id: '4',
  1080. pipelineVariables: [
  1081. { name: 'var1', pipelineAgg: '1' },
  1082. { name: 'var2', pipelineAgg: '3' },
  1083. ],
  1084. settings: { script: 'params.var1 * params.var2' },
  1085. type: 'bucket_script',
  1086. },
  1087. {
  1088. id: '5',
  1089. pipelineVariables: [
  1090. { name: 'var1', pipelineAgg: '1' },
  1091. { name: 'var2', pipelineAgg: '3' },
  1092. ],
  1093. settings: { script: 'params.var1 * params.var2 * 4' },
  1094. type: 'bucket_script',
  1095. },
  1096. ],
  1097. bucketAggs: [{ type: 'terms', field: '@timestamp', id: '2' }],
  1098. },
  1099. ];
  1100. response = {
  1101. responses: [
  1102. {
  1103. aggregations: {
  1104. '2': {
  1105. buckets: [
  1106. {
  1107. 1: { value: 2 },
  1108. 3: { value: 3 },
  1109. 4: { value: 6 },
  1110. 5: { value: 24 },
  1111. doc_count: 60,
  1112. key: 1000,
  1113. },
  1114. {
  1115. 1: { value: 3 },
  1116. 3: { value: 4 },
  1117. 4: { value: 12 },
  1118. 5: { value: 48 },
  1119. doc_count: 60,
  1120. key: 2000,
  1121. },
  1122. ],
  1123. },
  1124. },
  1125. },
  1126. ],
  1127. };
  1128. result = new ElasticResponse(targets, response).getTimeSeries();
  1129. });
  1130. it('should return 2 rows with 5 columns', () => {
  1131. expect(result.data[0].columns.length).toBe(5);
  1132. expect(result.data[0].rows.length).toBe(2);
  1133. expect(result.data[0].rows[0][1]).toBe(2);
  1134. expect(result.data[0].rows[0][2]).toBe(3);
  1135. expect(result.data[0].rows[0][3]).toBe(6);
  1136. expect(result.data[0].rows[0][4]).toBe(24);
  1137. expect(result.data[0].rows[1][1]).toBe(3);
  1138. expect(result.data[0].rows[1][2]).toBe(4);
  1139. expect(result.data[0].rows[1][3]).toBe(12);
  1140. expect(result.data[0].rows[1][4]).toBe(48);
  1141. });
  1142. });
  1143. describe('Raw Data Query', () => {
  1144. beforeEach(() => {
  1145. targets = [
  1146. {
  1147. refId: 'A',
  1148. metrics: [{ type: 'raw_data', id: '1' }],
  1149. bucketAggs: [],
  1150. },
  1151. ];
  1152. response = {
  1153. responses: [
  1154. {
  1155. hits: {
  1156. total: {
  1157. relation: 'eq',
  1158. value: 1,
  1159. },
  1160. hits: [
  1161. {
  1162. _id: '1',
  1163. _type: '_doc',
  1164. _index: 'index',
  1165. _source: { sourceProp: 'asd' },
  1166. },
  1167. ],
  1168. },
  1169. },
  1170. ],
  1171. };
  1172. result = new ElasticResponse(targets, response).getTimeSeries();
  1173. });
  1174. it('should create dataframes with filterable fields', () => {
  1175. for (const field of result.data[0].fields) {
  1176. expect(field.config.filterable).toBe(true);
  1177. }
  1178. });
  1179. });
  1180. describe('simple logs query and count', () => {
  1181. const targets: ElasticsearchQuery[] = [
  1182. {
  1183. refId: 'A',
  1184. metrics: [{ type: 'count', id: '1' }],
  1185. bucketAggs: [{ type: 'date_histogram', settings: { interval: 'auto' }, id: '2' }],
  1186. key: 'Q-1561369883389-0.7611823271062786-0',
  1187. query: 'hello AND message',
  1188. timeField: '@timestamp',
  1189. },
  1190. ];
  1191. const response = {
  1192. responses: [
  1193. {
  1194. aggregations: {
  1195. '2': {
  1196. buckets: [
  1197. {
  1198. doc_count: 10,
  1199. key: 1000,
  1200. },
  1201. {
  1202. doc_count: 15,
  1203. key: 2000,
  1204. },
  1205. ],
  1206. },
  1207. },
  1208. hits: {
  1209. hits: [
  1210. {
  1211. _id: 'fdsfs',
  1212. _type: '_doc',
  1213. _index: 'mock-index',
  1214. _source: {
  1215. '@timestamp': '2019-06-24T09:51:19.765Z',
  1216. host: 'djisaodjsoad',
  1217. number: 1,
  1218. message: 'hello, i am a message',
  1219. level: 'debug',
  1220. fields: {
  1221. lvl: 'debug',
  1222. },
  1223. },
  1224. highlight: {
  1225. message: [
  1226. `${highlightTags.pre}hello${highlightTags.post}, i am a ${highlightTags.pre}message${highlightTags.post}`,
  1227. ],
  1228. },
  1229. },
  1230. {
  1231. _id: 'kdospaidopa',
  1232. _type: '_doc',
  1233. _index: 'mock-index',
  1234. _source: {
  1235. '@timestamp': '2019-06-24T09:52:19.765Z',
  1236. host: 'dsalkdakdop',
  1237. number: 2,
  1238. message: 'hello, i am also message',
  1239. level: 'error',
  1240. fields: {
  1241. lvl: 'info',
  1242. },
  1243. },
  1244. highlight: {
  1245. message: [
  1246. `${highlightTags.pre}hello${highlightTags.post}, i am a ${highlightTags.pre}message${highlightTags.post}`,
  1247. ],
  1248. },
  1249. },
  1250. ],
  1251. },
  1252. },
  1253. ],
  1254. };
  1255. it('should return histogram aggregation and documents', () => {
  1256. const result = new ElasticResponse(targets, response).getLogs();
  1257. expect(result.data.length).toBe(2);
  1258. const logResults = result.data[0] as MutableDataFrame;
  1259. expect(logResults).toHaveProperty('meta');
  1260. expect(logResults.meta).toEqual({
  1261. searchWords: ['hello', 'message'],
  1262. preferredVisualisationType: 'logs',
  1263. });
  1264. const fields = logResults.fields.map((f) => {
  1265. return {
  1266. name: f.name,
  1267. type: f.type,
  1268. };
  1269. });
  1270. expect(fields).toContainEqual({ name: '@timestamp', type: 'time' });
  1271. expect(fields).toContainEqual({ name: 'host', type: 'string' });
  1272. expect(fields).toContainEqual({ name: 'message', type: 'string' });
  1273. let rows = new DataFrameView(logResults);
  1274. for (let i = 0; i < rows.length; i++) {
  1275. const r = rows.get(i);
  1276. expect(r._id).toEqual(response.responses[0].hits.hits[i]._id);
  1277. expect(r._type).toEqual(response.responses[0].hits.hits[i]._type);
  1278. expect(r._index).toEqual(response.responses[0].hits.hits[i]._index);
  1279. expect(r._source).toEqual(
  1280. flatten(
  1281. response.responses[0].hits.hits[i]._source,
  1282. null as unknown as { delimiter?: any; maxDepth?: any; safe?: any }
  1283. )
  1284. );
  1285. }
  1286. // Make a map from the histogram results
  1287. const hist: KeyValue<number> = {};
  1288. const histogramResults = new MutableDataFrame(result.data[1]);
  1289. rows = new DataFrameView(histogramResults);
  1290. for (let i = 0; i < rows.length; i++) {
  1291. const row = rows.get(i);
  1292. hist[row.Time] = row.Value;
  1293. }
  1294. response.responses[0].aggregations['2'].buckets.forEach((bucket: any) => {
  1295. expect(hist[bucket.key]).toEqual(bucket.doc_count);
  1296. });
  1297. });
  1298. it('should map levels field', () => {
  1299. const result = new ElasticResponse(targets, response).getLogs(undefined, 'level');
  1300. const fieldCache = new FieldCache(result.data[0]);
  1301. const field = fieldCache.getFieldByName('level');
  1302. expect(field?.values.toArray()).toEqual(['debug', 'error']);
  1303. });
  1304. it('should re map levels field to new field', () => {
  1305. const result = new ElasticResponse(targets, response).getLogs(undefined, 'fields.lvl');
  1306. const fieldCache = new FieldCache(result.data[0]);
  1307. const field = fieldCache.getFieldByName('level');
  1308. expect(field?.values.toArray()).toEqual(['debug', 'info']);
  1309. });
  1310. it('should correctly guess field types', () => {
  1311. const result = new ElasticResponse(targets, response).getLogs();
  1312. const logResults = result.data[0] as MutableDataFrame;
  1313. const fields = logResults.fields.map((f) => {
  1314. return {
  1315. name: f.name,
  1316. type: f.type,
  1317. };
  1318. });
  1319. expect(fields).toContainEqual({ name: '@timestamp', type: 'time' });
  1320. expect(fields).toContainEqual({ name: 'number', type: 'number' });
  1321. expect(fields).toContainEqual({ name: 'message', type: 'string' });
  1322. });
  1323. });
  1324. describe('logs query with empty response', () => {
  1325. const targets: ElasticsearchQuery[] = [
  1326. {
  1327. refId: 'A',
  1328. metrics: [{ type: 'logs', id: '2' }],
  1329. bucketAggs: [{ type: 'date_histogram', settings: { interval: 'auto' }, id: '1' }],
  1330. key: 'Q-1561369883389-0.7611823271062786-0',
  1331. query: 'hello AND message',
  1332. timeField: '@timestamp',
  1333. },
  1334. ];
  1335. const response = {
  1336. responses: [
  1337. {
  1338. hits: { hits: [] },
  1339. aggregations: {
  1340. '1': {
  1341. buckets: [
  1342. { key_as_string: '1633676760000', key: 1633676760000, doc_count: 0 },
  1343. { key_as_string: '1633676770000', key: 1633676770000, doc_count: 0 },
  1344. { key_as_string: '1633676780000', key: 1633676780000, doc_count: 0 },
  1345. ],
  1346. },
  1347. },
  1348. status: 200,
  1349. },
  1350. ],
  1351. };
  1352. it('should return histogram aggregation and documents', () => {
  1353. const result = new ElasticResponse(targets, response).getLogs('message', 'level');
  1354. expect(result.data.length).toBe(2);
  1355. });
  1356. });
  1357. });