1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471 |
- import { DataFrameView, FieldCache, KeyValue, MutableDataFrame } from '@grafana/data';
- import flatten from 'app/core/utils/flatten';
- import { ElasticResponse } from '../elastic_response';
- import { highlightTags } from '../query_def';
- import { ElasticsearchQuery } from '../types';
- describe('ElasticResponse', () => {
- let targets: ElasticsearchQuery[];
- let response: any;
- let result: any;
- describe('refId matching', () => {
- // We default to the old table structure to ensure backward compatibility,
- // therefore we only process responses as DataFrames when there's at least one
- // raw_data (new) query type.
- // We should test if refId gets populated wether there's such type of query or not
- interface MockedQueryData {
- target: ElasticsearchQuery;
- response: any;
- }
- const countQuery: MockedQueryData = {
- target: {
- refId: 'COUNT_GROUPBY_DATE_HISTOGRAM',
- metrics: [{ type: 'count', id: 'c_1' }],
- bucketAggs: [{ type: 'date_histogram', field: '@timestamp', id: 'c_2' }],
- } as ElasticsearchQuery,
- response: {
- aggregations: {
- c_2: {
- buckets: [
- {
- doc_count: 10,
- key: 1000,
- },
- ],
- },
- },
- },
- };
- const countGroupByHistogramQuery: MockedQueryData = {
- target: {
- refId: 'COUNT_GROUPBY_HISTOGRAM',
- metrics: [{ type: 'count', id: 'h_3' }],
- bucketAggs: [{ type: 'histogram', field: 'bytes', id: 'h_4' }],
- },
- response: {
- aggregations: {
- h_4: {
- buckets: [{ doc_count: 1, key: 1000 }],
- },
- },
- },
- };
- const rawDocumentQuery: MockedQueryData = {
- target: {
- refId: 'RAW_DOC',
- metrics: [{ type: 'raw_document', id: 'r_5' }],
- bucketAggs: [],
- },
- response: {
- hits: {
- total: 2,
- hits: [
- {
- _id: '5',
- _type: 'type',
- _index: 'index',
- _source: { sourceProp: 'asd' },
- fields: { fieldProp: 'field' },
- },
- {
- _source: { sourceProp: 'asd2' },
- fields: { fieldProp: 'field2' },
- },
- ],
- },
- },
- };
- const percentilesQuery: MockedQueryData = {
- target: {
- refId: 'PERCENTILE',
- metrics: [{ type: 'percentiles', settings: { percents: ['75', '90'] }, id: 'p_1' }],
- bucketAggs: [{ type: 'date_histogram', field: '@timestamp', id: 'p_3' }],
- },
- response: {
- aggregations: {
- p_3: {
- buckets: [
- {
- p_1: { values: { '75': 3.3, '90': 5.5 } },
- doc_count: 10,
- key: 1000,
- },
- {
- p_1: { values: { '75': 2.3, '90': 4.5 } },
- doc_count: 15,
- key: 2000,
- },
- ],
- },
- },
- },
- };
- const extendedStatsQuery: MockedQueryData = {
- target: {
- refId: 'EXTENDEDSTATS',
- metrics: [
- {
- type: 'extended_stats',
- meta: { max: true, std_deviation_bounds_upper: true },
- id: 'e_1',
- },
- ],
- bucketAggs: [
- { type: 'terms', field: 'host', id: 'e_3' },
- { type: 'date_histogram', id: 'e_4' },
- ],
- },
- response: {
- aggregations: {
- e_3: {
- buckets: [
- {
- key: 'server1',
- e_4: {
- buckets: [
- {
- e_1: {
- max: 10.2,
- min: 5.5,
- std_deviation_bounds: { upper: 3, lower: -2 },
- },
- doc_count: 10,
- key: 1000,
- },
- ],
- },
- },
- {
- key: 'server2',
- e_4: {
- buckets: [
- {
- e_1: {
- max: 10.2,
- min: 5.5,
- std_deviation_bounds: { upper: 3, lower: -2 },
- },
- doc_count: 10,
- key: 1000,
- },
- ],
- },
- },
- ],
- },
- },
- },
- };
- const commonTargets = [
- { ...countQuery.target },
- { ...countGroupByHistogramQuery.target },
- { ...rawDocumentQuery.target },
- { ...percentilesQuery.target },
- { ...extendedStatsQuery.target },
- ];
- const commonResponses = [
- { ...countQuery.response },
- { ...countGroupByHistogramQuery.response },
- { ...rawDocumentQuery.response },
- { ...percentilesQuery.response },
- { ...extendedStatsQuery.response },
- ];
- describe('When processing responses as DataFrames (raw_data query present)', () => {
- beforeEach(() => {
- targets = [
- ...commonTargets,
- // Raw Data Query
- {
- refId: 'D',
- metrics: [{ type: 'raw_data', id: '6' }],
- bucketAggs: [],
- },
- ];
- response = {
- responses: [
- ...commonResponses,
- // Raw Data Query
- {
- hits: {
- total: {
- relation: 'eq',
- value: 1,
- },
- hits: [
- {
- _id: '6',
- _type: '_doc',
- _index: 'index',
- _source: { sourceProp: 'asd' },
- },
- ],
- },
- },
- ],
- };
- result = new ElasticResponse(targets, response).getTimeSeries();
- });
- it('should add the correct refId to each returned series', () => {
- expect(result.data[0].refId).toBe(countQuery.target.refId);
- expect(result.data[1].refId).toBe(countGroupByHistogramQuery.target.refId);
- expect(result.data[2].refId).toBe(rawDocumentQuery.target.refId);
- expect(result.data[3].refId).toBe(percentilesQuery.target.refId);
- expect(result.data[4].refId).toBe(percentilesQuery.target.refId);
- expect(result.data[5].refId).toBe(extendedStatsQuery.target.refId);
- // Raw Data query
- expect(result.data[result.data.length - 1].refId).toBe('D');
- });
- });
- describe('When NOT processing responses as DataFrames (raw_data query NOT present)', () => {
- beforeEach(() => {
- targets = [...commonTargets];
- response = {
- responses: [...commonResponses],
- };
- result = new ElasticResponse(targets, response).getTimeSeries();
- });
- it('should add the correct refId to each returned series', () => {
- expect(result.data[0].refId).toBe(countQuery.target.refId);
- expect(result.data[1].refId).toBe(countGroupByHistogramQuery.target.refId);
- expect(result.data[2].refId).toBe(rawDocumentQuery.target.refId);
- expect(result.data[3].refId).toBe(percentilesQuery.target.refId);
- expect(result.data[4].refId).toBe(percentilesQuery.target.refId);
- expect(result.data[5].refId).toBe(extendedStatsQuery.target.refId);
- });
- });
- });
- describe('simple query and count', () => {
- beforeEach(() => {
- targets = [
- {
- refId: 'A',
- metrics: [{ type: 'count', id: '1' }],
- bucketAggs: [{ type: 'date_histogram', field: '@timestamp', id: '2' }],
- },
- ];
- response = {
- responses: [
- {
- aggregations: {
- '2': {
- buckets: [
- {
- doc_count: 10,
- key: 1000,
- },
- {
- doc_count: 15,
- key: 2000,
- },
- ],
- },
- },
- },
- ],
- };
- result = new ElasticResponse(targets, response).getTimeSeries();
- });
- it('should return 1 series', () => {
- expect(result.data.length).toBe(1);
- expect(result.data[0].target).toBe('Count');
- expect(result.data[0].datapoints.length).toBe(2);
- expect(result.data[0].datapoints[0][0]).toBe(10);
- expect(result.data[0].datapoints[0][1]).toBe(1000);
- });
- });
- describe('simple query count & avg aggregation', () => {
- let result: any;
- beforeEach(() => {
- targets = [
- {
- refId: 'A',
- metrics: [
- { type: 'count', id: '1' },
- { type: 'avg', field: 'value', id: '2' },
- ],
- bucketAggs: [{ type: 'date_histogram', field: '@timestamp', id: '3' }],
- },
- ];
- response = {
- responses: [
- {
- aggregations: {
- '3': {
- buckets: [
- {
- '2': { value: 88 },
- doc_count: 10,
- key: 1000,
- },
- {
- '2': { value: 99 },
- doc_count: 15,
- key: 2000,
- },
- ],
- },
- },
- },
- ],
- };
- result = new ElasticResponse(targets, response).getTimeSeries();
- });
- it('should return 2 series', () => {
- expect(result.data.length).toBe(2);
- expect(result.data[0].datapoints.length).toBe(2);
- expect(result.data[0].datapoints[0][0]).toBe(10);
- expect(result.data[0].datapoints[0][1]).toBe(1000);
- expect(result.data[1].target).toBe('Average value');
- expect(result.data[1].datapoints[0][0]).toBe(88);
- expect(result.data[1].datapoints[1][0]).toBe(99);
- });
- });
- describe('single group by query one metric', () => {
- let result: any;
- beforeEach(() => {
- targets = [
- {
- refId: 'A',
- metrics: [{ type: 'count', id: '1' }],
- bucketAggs: [
- { type: 'terms', field: 'host', id: '2' },
- { type: 'date_histogram', field: '@timestamp', id: '3' },
- ],
- },
- ];
- response = {
- responses: [
- {
- aggregations: {
- '2': {
- buckets: [
- {
- '3': {
- buckets: [
- { doc_count: 1, key: 1000 },
- { doc_count: 3, key: 2000 },
- ],
- },
- doc_count: 4,
- key: 'server1',
- },
- {
- '3': {
- buckets: [
- { doc_count: 2, key: 1000 },
- { doc_count: 8, key: 2000 },
- ],
- },
- doc_count: 10,
- key: 'server2',
- },
- ],
- },
- },
- },
- ],
- };
- result = new ElasticResponse(targets, response).getTimeSeries();
- });
- it('should return 2 series', () => {
- expect(result.data.length).toBe(2);
- expect(result.data[0].datapoints.length).toBe(2);
- expect(result.data[0].target).toBe('server1');
- expect(result.data[1].target).toBe('server2');
- });
- });
- describe('single group by query two metrics', () => {
- let result: any;
- beforeEach(() => {
- targets = [
- {
- refId: 'A',
- metrics: [
- { type: 'count', id: '1' },
- { type: 'avg', field: '@value', id: '4' },
- ],
- bucketAggs: [
- { type: 'terms', field: 'host', id: '2' },
- { type: 'date_histogram', field: '@timestamp', id: '3' },
- ],
- },
- ];
- response = {
- responses: [
- {
- aggregations: {
- '2': {
- buckets: [
- {
- '3': {
- buckets: [
- { '4': { value: 10 }, doc_count: 1, key: 1000 },
- { '4': { value: 12 }, doc_count: 3, key: 2000 },
- ],
- },
- doc_count: 4,
- key: 'server1',
- },
- {
- '3': {
- buckets: [
- { '4': { value: 20 }, doc_count: 1, key: 1000 },
- { '4': { value: 32 }, doc_count: 3, key: 2000 },
- ],
- },
- doc_count: 10,
- key: 'server2',
- },
- ],
- },
- },
- },
- ],
- };
- result = new ElasticResponse(targets, response).getTimeSeries();
- });
- it('should return 2 series', () => {
- expect(result.data.length).toBe(4);
- expect(result.data[0].datapoints.length).toBe(2);
- expect(result.data[0].target).toBe('server1 Count');
- expect(result.data[1].target).toBe('server1 Average @value');
- expect(result.data[2].target).toBe('server2 Count');
- expect(result.data[3].target).toBe('server2 Average @value');
- });
- });
- describe('with percentiles ', () => {
- let result: any;
- beforeEach(() => {
- targets = [
- {
- refId: 'A',
- metrics: [{ type: 'percentiles', settings: { percents: ['75', '90'] }, id: '1', field: '@value' }],
- bucketAggs: [{ type: 'date_histogram', field: '@timestamp', id: '3' }],
- },
- ];
- response = {
- responses: [
- {
- aggregations: {
- '3': {
- buckets: [
- {
- '1': { values: { '75': 3.3, '90': 5.5 } },
- doc_count: 10,
- key: 1000,
- },
- {
- '1': { values: { '75': 2.3, '90': 4.5 } },
- doc_count: 15,
- key: 2000,
- },
- ],
- },
- },
- },
- ],
- };
- result = new ElasticResponse(targets, response).getTimeSeries();
- });
- it('should return 2 series', () => {
- expect(result.data.length).toBe(2);
- expect(result.data[0].datapoints.length).toBe(2);
- expect(result.data[0].target).toBe('p75 @value');
- expect(result.data[1].target).toBe('p90 @value');
- expect(result.data[0].datapoints[0][0]).toBe(3.3);
- expect(result.data[0].datapoints[0][1]).toBe(1000);
- expect(result.data[1].datapoints[1][0]).toBe(4.5);
- });
- });
- describe('with extended_stats', () => {
- let result: any;
- beforeEach(() => {
- targets = [
- {
- refId: 'A',
- metrics: [
- {
- type: 'extended_stats',
- meta: { max: true, std_deviation_bounds_upper: true },
- id: '1',
- field: '@value',
- },
- ],
- bucketAggs: [
- { type: 'terms', field: 'host', id: '3' },
- { type: 'date_histogram', id: '4' },
- ],
- },
- ];
- response = {
- responses: [
- {
- aggregations: {
- '3': {
- buckets: [
- {
- key: 'server1',
- '4': {
- buckets: [
- {
- '1': {
- max: 10.2,
- min: 5.5,
- std_deviation_bounds: { upper: 3, lower: -2 },
- },
- doc_count: 10,
- key: 1000,
- },
- ],
- },
- },
- {
- key: 'server2',
- '4': {
- buckets: [
- {
- '1': {
- max: 10.2,
- min: 5.5,
- std_deviation_bounds: { upper: 3, lower: -2 },
- },
- doc_count: 10,
- key: 1000,
- },
- ],
- },
- },
- ],
- },
- },
- },
- ],
- };
- result = new ElasticResponse(targets, response).getTimeSeries();
- });
- it('should return 4 series', () => {
- expect(result.data.length).toBe(4);
- expect(result.data[0].datapoints.length).toBe(1);
- expect(result.data[0].target).toBe('server1 Max @value');
- expect(result.data[1].target).toBe('server1 Std Dev Upper @value');
- expect(result.data[0].datapoints[0][0]).toBe(10.2);
- expect(result.data[1].datapoints[0][0]).toBe(3);
- });
- });
- describe('with top_metrics', () => {
- beforeEach(() => {
- targets = [
- {
- refId: 'A',
- metrics: [
- {
- type: 'top_metrics',
- settings: {
- order: 'top',
- orderBy: '@timestamp',
- metrics: ['@value', '@anotherValue'],
- },
- id: '1',
- },
- ],
- bucketAggs: [{ type: 'date_histogram', id: '2' }],
- },
- ];
- response = {
- responses: [
- {
- aggregations: {
- '2': {
- buckets: [
- {
- key: new Date('2021-01-01T00:00:00.000Z').valueOf(),
- key_as_string: '2021-01-01T00:00:00.000Z',
- '1': {
- top: [{ sort: ['2021-01-01T00:00:00.000Z'], metrics: { '@value': 1, '@anotherValue': 2 } }],
- },
- },
- {
- key: new Date('2021-01-01T00:00:10.000Z').valueOf(),
- key_as_string: '2021-01-01T00:00:10.000Z',
- '1': {
- top: [{ sort: ['2021-01-01T00:00:10.000Z'], metrics: { '@value': 1, '@anotherValue': 2 } }],
- },
- },
- ],
- },
- },
- },
- ],
- };
- });
- it('should return 2 series', () => {
- const result = new ElasticResponse(targets, response).getTimeSeries();
- expect(result.data.length).toBe(2);
- const firstSeries = result.data[0];
- expect(firstSeries.target).toBe('Top Metrics @value');
- expect(firstSeries.datapoints.length).toBe(2);
- expect(firstSeries.datapoints).toEqual([
- [1, new Date('2021-01-01T00:00:00.000Z').valueOf()],
- [1, new Date('2021-01-01T00:00:10.000Z').valueOf()],
- ]);
- const secondSeries = result.data[1];
- expect(secondSeries.target).toBe('Top Metrics @anotherValue');
- expect(secondSeries.datapoints.length).toBe(2);
- expect(secondSeries.datapoints).toEqual([
- [2, new Date('2021-01-01T00:00:00.000Z').valueOf()],
- [2, new Date('2021-01-01T00:00:10.000Z').valueOf()],
- ]);
- });
- });
- describe('single group by with alias pattern', () => {
- let result: any;
- beforeEach(() => {
- targets = [
- {
- refId: 'A',
- metrics: [{ type: 'count', id: '1' }],
- alias: '{{term @host}} {{metric}} and {{not_exist}} {{@host}}',
- bucketAggs: [
- { type: 'terms', field: '@host', id: '2' },
- { type: 'date_histogram', field: '@timestamp', id: '3' },
- ],
- },
- ];
- response = {
- responses: [
- {
- aggregations: {
- '2': {
- buckets: [
- {
- '3': {
- buckets: [
- { doc_count: 1, key: 1000 },
- { doc_count: 3, key: 2000 },
- ],
- },
- doc_count: 4,
- key: 'server1',
- },
- {
- '3': {
- buckets: [
- { doc_count: 2, key: 1000 },
- { doc_count: 8, key: 2000 },
- ],
- },
- doc_count: 10,
- key: 'server2',
- },
- {
- '3': {
- buckets: [
- { doc_count: 2, key: 1000 },
- { doc_count: 8, key: 2000 },
- ],
- },
- doc_count: 10,
- key: 0,
- },
- ],
- },
- },
- },
- ],
- };
- result = new ElasticResponse(targets, response).getTimeSeries();
- });
- it('should return 2 series', () => {
- expect(result.data.length).toBe(3);
- expect(result.data[0].datapoints.length).toBe(2);
- expect(result.data[0].target).toBe('server1 Count and {{not_exist}} server1');
- expect(result.data[1].target).toBe('server2 Count and {{not_exist}} server2');
- expect(result.data[2].target).toBe('0 Count and {{not_exist}} 0');
- });
- });
- describe('histogram response', () => {
- let result: any;
- beforeEach(() => {
- targets = [
- {
- refId: 'A',
- metrics: [{ type: 'count', id: '1' }],
- bucketAggs: [{ type: 'histogram', field: 'bytes', id: '3' }],
- },
- ];
- response = {
- responses: [
- {
- aggregations: {
- '3': {
- buckets: [
- { doc_count: 1, key: 1000 },
- { doc_count: 3, key: 2000 },
- { doc_count: 2, key: 1000 },
- ],
- },
- },
- },
- ],
- };
- result = new ElasticResponse(targets, response).getTimeSeries();
- });
- it('should return table with byte and count', () => {
- expect(result.data[0].rows.length).toBe(3);
- expect(result.data[0].columns).toEqual([{ text: 'bytes', filterable: true }, { text: 'Count' }]);
- });
- });
- describe('with two filters agg', () => {
- let result: any;
- beforeEach(() => {
- targets = [
- {
- refId: 'A',
- metrics: [{ type: 'count', id: '1' }],
- bucketAggs: [
- {
- id: '2',
- type: 'filters',
- settings: {
- filters: [
- { query: '@metric:cpu', label: '' },
- { query: '@metric:logins.count', label: '' },
- ],
- },
- },
- { type: 'date_histogram', field: '@timestamp', id: '3' },
- ],
- },
- ];
- response = {
- responses: [
- {
- aggregations: {
- '2': {
- buckets: {
- '@metric:cpu': {
- '3': {
- buckets: [
- { doc_count: 1, key: 1000 },
- { doc_count: 3, key: 2000 },
- ],
- },
- },
- '@metric:logins.count': {
- '3': {
- buckets: [
- { doc_count: 2, key: 1000 },
- { doc_count: 8, key: 2000 },
- ],
- },
- },
- },
- },
- },
- },
- ],
- };
- result = new ElasticResponse(targets, response).getTimeSeries();
- });
- it('should return 2 series', () => {
- expect(result.data.length).toBe(2);
- expect(result.data[0].datapoints.length).toBe(2);
- expect(result.data[0].target).toBe('@metric:cpu');
- expect(result.data[1].target).toBe('@metric:logins.count');
- });
- });
- describe('with dropfirst and last aggregation', () => {
- beforeEach(() => {
- targets = [
- {
- refId: 'A',
- metrics: [
- { type: 'avg', id: '1', field: '@value' },
- { type: 'count', id: '3' },
- ],
- bucketAggs: [
- {
- id: '2',
- type: 'date_histogram',
- field: 'host',
- settings: { trimEdges: '1' },
- },
- ],
- },
- ];
- response = {
- responses: [
- {
- aggregations: {
- '2': {
- buckets: [
- {
- '1': { value: 1000 },
- key: 1,
- doc_count: 369,
- },
- {
- '1': { value: 2000 },
- key: 2,
- doc_count: 200,
- },
- {
- '1': { value: 2000 },
- key: 3,
- doc_count: 200,
- },
- ],
- },
- },
- },
- ],
- };
- result = new ElasticResponse(targets, response).getTimeSeries();
- });
- it('should remove first and last value', () => {
- expect(result.data.length).toBe(2);
- expect(result.data[0].datapoints.length).toBe(1);
- });
- });
- describe('No group by time', () => {
- beforeEach(() => {
- targets = [
- {
- refId: 'A',
- metrics: [
- { type: 'avg', id: '1', field: '@value' },
- { type: 'count', id: '3' },
- ],
- bucketAggs: [{ id: '2', type: 'terms', field: 'host' }],
- },
- ];
- response = {
- responses: [
- {
- aggregations: {
- '2': {
- buckets: [
- {
- '1': { value: 1000 },
- key: 'server-1',
- doc_count: 369,
- },
- {
- '1': { value: 2000 },
- key: 'server-2',
- doc_count: 200,
- },
- ],
- },
- },
- },
- ],
- };
- result = new ElasticResponse(targets, response).getTimeSeries();
- });
- it('should return table', () => {
- expect(result.data.length).toBe(1);
- expect(result.data[0].type).toBe('table');
- expect(result.data[0].rows.length).toBe(2);
- expect(result.data[0].rows[0][0]).toBe('server-1');
- expect(result.data[0].rows[0][1]).toBe(1000);
- expect(result.data[0].rows[0][2]).toBe(369);
- expect(result.data[0].rows[1][0]).toBe('server-2');
- expect(result.data[0].rows[1][1]).toBe(2000);
- });
- });
- describe('No group by time with percentiles ', () => {
- let result: any;
- beforeEach(() => {
- targets = [
- {
- refId: 'A',
- metrics: [{ type: 'percentiles', field: 'value', settings: { percents: ['75', '90'] }, id: '1' }],
- bucketAggs: [{ type: 'terms', field: 'id', id: '3' }],
- },
- ];
- response = {
- responses: [
- {
- aggregations: {
- '3': {
- buckets: [
- {
- '1': { values: { '75': 3.3, '90': 5.5 } },
- doc_count: 10,
- key: 'id1',
- },
- {
- '1': { values: { '75': 2.3, '90': 4.5 } },
- doc_count: 15,
- key: 'id2',
- },
- ],
- },
- },
- },
- ],
- };
- result = new ElasticResponse(targets, response).getTimeSeries();
- });
- it('should return table', () => {
- expect(result.data.length).toBe(1);
- expect(result.data[0].type).toBe('table');
- expect(result.data[0].columns[0].text).toBe('id');
- expect(result.data[0].columns[1].text).toBe('p75 value');
- expect(result.data[0].columns[2].text).toBe('p90 value');
- expect(result.data[0].rows.length).toBe(2);
- expect(result.data[0].rows[0][0]).toBe('id1');
- expect(result.data[0].rows[0][1]).toBe(3.3);
- expect(result.data[0].rows[0][2]).toBe(5.5);
- expect(result.data[0].rows[1][0]).toBe('id2');
- expect(result.data[0].rows[1][1]).toBe(2.3);
- expect(result.data[0].rows[1][2]).toBe(4.5);
- });
- });
- describe('Multiple metrics of same type', () => {
- beforeEach(() => {
- targets = [
- {
- refId: 'A',
- metrics: [
- { type: 'avg', id: '1', field: 'test' },
- { type: 'avg', id: '2', field: 'test2' },
- ],
- bucketAggs: [{ id: '2', type: 'terms', field: 'host' }],
- },
- ];
- response = {
- responses: [
- {
- aggregations: {
- '2': {
- buckets: [
- {
- '1': { value: 1000 },
- '2': { value: 3000 },
- key: 'server-1',
- doc_count: 369,
- },
- ],
- },
- },
- },
- ],
- };
- result = new ElasticResponse(targets, response).getTimeSeries();
- });
- it('should include field in metric name', () => {
- expect(result.data[0].type).toBe('table');
- expect(result.data[0].rows[0][1]).toBe(1000);
- expect(result.data[0].rows[0][2]).toBe(3000);
- });
- });
- describe('Raw documents query', () => {
- beforeEach(() => {
- targets = [
- {
- refId: 'A',
- metrics: [{ type: 'raw_document', id: '1' }],
- bucketAggs: [],
- },
- ];
- response = {
- responses: [
- {
- hits: {
- total: 100,
- hits: [
- {
- _id: '1',
- _type: 'type',
- _index: 'index',
- _source: { sourceProp: 'asd' },
- fields: { fieldProp: 'field' },
- },
- {
- _source: { sourceProp: 'asd2' },
- fields: { fieldProp: 'field2' },
- },
- ],
- },
- },
- ],
- };
- result = new ElasticResponse(targets, response).getTimeSeries();
- });
- it('should return docs', () => {
- expect(result.data.length).toBe(1);
- expect(result.data[0].type).toBe('docs');
- expect(result.data[0].total).toBe(100);
- expect(result.data[0].datapoints.length).toBe(2);
- expect(result.data[0].datapoints[0].sourceProp).toBe('asd');
- expect(result.data[0].datapoints[0].fieldProp).toBe('field');
- });
- });
- describe('with bucket_script ', () => {
- let result: any;
- beforeEach(() => {
- targets = [
- {
- refId: 'A',
- metrics: [
- { id: '1', type: 'sum', field: '@value' },
- { id: '3', type: 'max', field: '@value' },
- {
- id: '4',
- pipelineVariables: [
- { name: 'var1', pipelineAgg: '1' },
- { name: 'var2', pipelineAgg: '3' },
- ],
- settings: { script: 'params.var1 * params.var2' },
- type: 'bucket_script',
- },
- ],
- bucketAggs: [{ type: 'date_histogram', field: '@timestamp', id: '2' }],
- },
- ];
- response = {
- responses: [
- {
- aggregations: {
- '2': {
- buckets: [
- {
- 1: { value: 2 },
- 3: { value: 3 },
- 4: { value: 6 },
- doc_count: 60,
- key: 1000,
- },
- {
- 1: { value: 3 },
- 3: { value: 4 },
- 4: { value: 12 },
- doc_count: 60,
- key: 2000,
- },
- ],
- },
- },
- },
- ],
- };
- result = new ElasticResponse(targets, response).getTimeSeries();
- });
- it('should return 3 series', () => {
- expect(result.data.length).toBe(3);
- expect(result.data[0].datapoints.length).toBe(2);
- expect(result.data[0].target).toBe('Sum @value');
- expect(result.data[1].target).toBe('Max @value');
- expect(result.data[2].target).toBe('Sum @value * Max @value');
- expect(result.data[0].datapoints[0][0]).toBe(2);
- expect(result.data[1].datapoints[0][0]).toBe(3);
- expect(result.data[2].datapoints[0][0]).toBe(6);
- expect(result.data[0].datapoints[1][0]).toBe(3);
- expect(result.data[1].datapoints[1][0]).toBe(4);
- expect(result.data[2].datapoints[1][0]).toBe(12);
- });
- });
- describe('terms with bucket_script and two scripts', () => {
- let result: any;
- beforeEach(() => {
- targets = [
- {
- refId: 'A',
- metrics: [
- { id: '1', type: 'sum', field: '@value' },
- { id: '3', type: 'max', field: '@value' },
- {
- id: '4',
- pipelineVariables: [
- { name: 'var1', pipelineAgg: '1' },
- { name: 'var2', pipelineAgg: '3' },
- ],
- settings: { script: 'params.var1 * params.var2' },
- type: 'bucket_script',
- },
- {
- id: '5',
- pipelineVariables: [
- { name: 'var1', pipelineAgg: '1' },
- { name: 'var2', pipelineAgg: '3' },
- ],
- settings: { script: 'params.var1 * params.var2 * 4' },
- type: 'bucket_script',
- },
- ],
- bucketAggs: [{ type: 'terms', field: '@timestamp', id: '2' }],
- },
- ];
- response = {
- responses: [
- {
- aggregations: {
- '2': {
- buckets: [
- {
- 1: { value: 2 },
- 3: { value: 3 },
- 4: { value: 6 },
- 5: { value: 24 },
- doc_count: 60,
- key: 1000,
- },
- {
- 1: { value: 3 },
- 3: { value: 4 },
- 4: { value: 12 },
- 5: { value: 48 },
- doc_count: 60,
- key: 2000,
- },
- ],
- },
- },
- },
- ],
- };
- result = new ElasticResponse(targets, response).getTimeSeries();
- });
- it('should return 2 rows with 5 columns', () => {
- expect(result.data[0].columns.length).toBe(5);
- expect(result.data[0].rows.length).toBe(2);
- expect(result.data[0].rows[0][1]).toBe(2);
- expect(result.data[0].rows[0][2]).toBe(3);
- expect(result.data[0].rows[0][3]).toBe(6);
- expect(result.data[0].rows[0][4]).toBe(24);
- expect(result.data[0].rows[1][1]).toBe(3);
- expect(result.data[0].rows[1][2]).toBe(4);
- expect(result.data[0].rows[1][3]).toBe(12);
- expect(result.data[0].rows[1][4]).toBe(48);
- });
- });
- describe('Raw Data Query', () => {
- beforeEach(() => {
- targets = [
- {
- refId: 'A',
- metrics: [{ type: 'raw_data', id: '1' }],
- bucketAggs: [],
- },
- ];
- response = {
- responses: [
- {
- hits: {
- total: {
- relation: 'eq',
- value: 1,
- },
- hits: [
- {
- _id: '1',
- _type: '_doc',
- _index: 'index',
- _source: { sourceProp: 'asd' },
- },
- ],
- },
- },
- ],
- };
- result = new ElasticResponse(targets, response).getTimeSeries();
- });
- it('should create dataframes with filterable fields', () => {
- for (const field of result.data[0].fields) {
- expect(field.config.filterable).toBe(true);
- }
- });
- });
- describe('simple logs query and count', () => {
- const targets: ElasticsearchQuery[] = [
- {
- refId: 'A',
- metrics: [{ type: 'count', id: '1' }],
- bucketAggs: [{ type: 'date_histogram', settings: { interval: 'auto' }, id: '2' }],
- key: 'Q-1561369883389-0.7611823271062786-0',
- query: 'hello AND message',
- timeField: '@timestamp',
- },
- ];
- const response = {
- responses: [
- {
- aggregations: {
- '2': {
- buckets: [
- {
- doc_count: 10,
- key: 1000,
- },
- {
- doc_count: 15,
- key: 2000,
- },
- ],
- },
- },
- hits: {
- hits: [
- {
- _id: 'fdsfs',
- _type: '_doc',
- _index: 'mock-index',
- _source: {
- '@timestamp': '2019-06-24T09:51:19.765Z',
- host: 'djisaodjsoad',
- number: 1,
- message: 'hello, i am a message',
- level: 'debug',
- fields: {
- lvl: 'debug',
- },
- },
- highlight: {
- message: [
- `${highlightTags.pre}hello${highlightTags.post}, i am a ${highlightTags.pre}message${highlightTags.post}`,
- ],
- },
- },
- {
- _id: 'kdospaidopa',
- _type: '_doc',
- _index: 'mock-index',
- _source: {
- '@timestamp': '2019-06-24T09:52:19.765Z',
- host: 'dsalkdakdop',
- number: 2,
- message: 'hello, i am also message',
- level: 'error',
- fields: {
- lvl: 'info',
- },
- },
- highlight: {
- message: [
- `${highlightTags.pre}hello${highlightTags.post}, i am a ${highlightTags.pre}message${highlightTags.post}`,
- ],
- },
- },
- ],
- },
- },
- ],
- };
- it('should return histogram aggregation and documents', () => {
- const result = new ElasticResponse(targets, response).getLogs();
- expect(result.data.length).toBe(2);
- const logResults = result.data[0] as MutableDataFrame;
- expect(logResults).toHaveProperty('meta');
- expect(logResults.meta).toEqual({
- searchWords: ['hello', 'message'],
- preferredVisualisationType: 'logs',
- });
- const fields = logResults.fields.map((f) => {
- return {
- name: f.name,
- type: f.type,
- };
- });
- expect(fields).toContainEqual({ name: '@timestamp', type: 'time' });
- expect(fields).toContainEqual({ name: 'host', type: 'string' });
- expect(fields).toContainEqual({ name: 'message', type: 'string' });
- let rows = new DataFrameView(logResults);
- for (let i = 0; i < rows.length; i++) {
- const r = rows.get(i);
- expect(r._id).toEqual(response.responses[0].hits.hits[i]._id);
- expect(r._type).toEqual(response.responses[0].hits.hits[i]._type);
- expect(r._index).toEqual(response.responses[0].hits.hits[i]._index);
- expect(r._source).toEqual(
- flatten(
- response.responses[0].hits.hits[i]._source,
- null as unknown as { delimiter?: any; maxDepth?: any; safe?: any }
- )
- );
- }
- // Make a map from the histogram results
- const hist: KeyValue<number> = {};
- const histogramResults = new MutableDataFrame(result.data[1]);
- rows = new DataFrameView(histogramResults);
- for (let i = 0; i < rows.length; i++) {
- const row = rows.get(i);
- hist[row.Time] = row.Value;
- }
- response.responses[0].aggregations['2'].buckets.forEach((bucket: any) => {
- expect(hist[bucket.key]).toEqual(bucket.doc_count);
- });
- });
- it('should map levels field', () => {
- const result = new ElasticResponse(targets, response).getLogs(undefined, 'level');
- const fieldCache = new FieldCache(result.data[0]);
- const field = fieldCache.getFieldByName('level');
- expect(field?.values.toArray()).toEqual(['debug', 'error']);
- });
- it('should re map levels field to new field', () => {
- const result = new ElasticResponse(targets, response).getLogs(undefined, 'fields.lvl');
- const fieldCache = new FieldCache(result.data[0]);
- const field = fieldCache.getFieldByName('level');
- expect(field?.values.toArray()).toEqual(['debug', 'info']);
- });
- it('should correctly guess field types', () => {
- const result = new ElasticResponse(targets, response).getLogs();
- const logResults = result.data[0] as MutableDataFrame;
- const fields = logResults.fields.map((f) => {
- return {
- name: f.name,
- type: f.type,
- };
- });
- expect(fields).toContainEqual({ name: '@timestamp', type: 'time' });
- expect(fields).toContainEqual({ name: 'number', type: 'number' });
- expect(fields).toContainEqual({ name: 'message', type: 'string' });
- });
- });
- describe('logs query with empty response', () => {
- const targets: ElasticsearchQuery[] = [
- {
- refId: 'A',
- metrics: [{ type: 'logs', id: '2' }],
- bucketAggs: [{ type: 'date_histogram', settings: { interval: 'auto' }, id: '1' }],
- key: 'Q-1561369883389-0.7611823271062786-0',
- query: 'hello AND message',
- timeField: '@timestamp',
- },
- ];
- const response = {
- responses: [
- {
- hits: { hits: [] },
- aggregations: {
- '1': {
- buckets: [
- { key_as_string: '1633676760000', key: 1633676760000, doc_count: 0 },
- { key_as_string: '1633676770000', key: 1633676770000, doc_count: 0 },
- { key_as_string: '1633676780000', key: 1633676780000, doc_count: 0 },
- ],
- },
- },
- status: 200,
- },
- ],
- };
- it('should return histogram aggregation and documents', () => {
- const result = new ElasticResponse(targets, response).getLogs('message', 'level');
- expect(result.data.length).toBe(2);
- });
- });
- });
|