mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Elasticsearch: fix type-related bug within targetContainsTemplate (#69798)
* Elasticsearch: fix type-related bug within targetContainsTemplate * Refactor to fix found regression
This commit is contained in:
parent
65d85ca9bf
commit
912c5ccb6a
@ -94,7 +94,6 @@ function getTestContext({
|
|||||||
data = { responses: [] },
|
data = { responses: [] },
|
||||||
from = 'now-5m',
|
from = 'now-5m',
|
||||||
jsonData,
|
jsonData,
|
||||||
database = '[test-]YYYY.MM.DD',
|
|
||||||
fetchMockImplementation,
|
fetchMockImplementation,
|
||||||
templateSrvMock,
|
templateSrvMock,
|
||||||
}: TestContext = {}) {
|
}: TestContext = {}) {
|
||||||
@ -127,6 +126,7 @@ function getTestContext({
|
|||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
containsTemplate: jest.fn().mockImplementation((text?: string) => text?.includes('$') ?? false),
|
||||||
getAdhocFilters: () => [],
|
getAdhocFilters: () => [],
|
||||||
} as unknown as TemplateSrv);
|
} as unknown as TemplateSrv);
|
||||||
|
|
||||||
@ -1295,6 +1295,57 @@ const logsResponse = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
describe('targetContainsTemplate', () => {
|
||||||
|
let ds: ElasticDatasource;
|
||||||
|
let target: ElasticsearchQuery;
|
||||||
|
beforeEach(() => {
|
||||||
|
const context = getTestContext();
|
||||||
|
ds = context.ds;
|
||||||
|
target = {
|
||||||
|
refId: 'test',
|
||||||
|
bucketAggs: [{ type: 'date_histogram', field: '@timestamp', id: '1' }],
|
||||||
|
metrics: [{ type: 'count', id: '1' }],
|
||||||
|
query: 'escape\\:test',
|
||||||
|
};
|
||||||
|
});
|
||||||
|
it('returns false when there is no variable in the query', () => {
|
||||||
|
expect(ds.targetContainsTemplate(target)).toBe(false);
|
||||||
|
});
|
||||||
|
it('returns true when there are variables in the query alias', () => {
|
||||||
|
target.alias = '$variable';
|
||||||
|
expect(ds.targetContainsTemplate(target)).toBe(true);
|
||||||
|
});
|
||||||
|
it('returns true when there are variables in the query', () => {
|
||||||
|
target.query = '$variable:something';
|
||||||
|
expect(ds.targetContainsTemplate(target)).toBe(true);
|
||||||
|
});
|
||||||
|
it('returns true when there are variables in the bucket aggregation', () => {
|
||||||
|
target.bucketAggs = [{ type: 'date_histogram', field: '$field', id: '1' }];
|
||||||
|
expect(ds.targetContainsTemplate(target)).toBe(true);
|
||||||
|
target.bucketAggs = [{ type: 'date_histogram', field: '@timestamp', id: '1', settings: { interval: '$interval' } }];
|
||||||
|
expect(ds.targetContainsTemplate(target)).toBe(true);
|
||||||
|
});
|
||||||
|
it('returns true when there are variables in the metric aggregation', () => {
|
||||||
|
target.metrics = [{ type: 'moving_avg', id: '1', settings: { window: '$window' } }];
|
||||||
|
expect(ds.targetContainsTemplate(target)).toBe(true);
|
||||||
|
target.metrics = [{ type: 'moving_avg', id: '1', field: '$field' }];
|
||||||
|
expect(ds.targetContainsTemplate(target)).toBe(true);
|
||||||
|
target.metrics = [{ type: 'extended_stats', id: '1', meta: { something: '$something' } }];
|
||||||
|
expect(ds.targetContainsTemplate(target)).toBe(true);
|
||||||
|
});
|
||||||
|
it('returns true when there are variables in an array inside an object in metrics', () => {
|
||||||
|
target.metrics = [
|
||||||
|
{
|
||||||
|
field: 'counter',
|
||||||
|
id: '1',
|
||||||
|
settings: { percents: ['20', '40', '$qqq'] },
|
||||||
|
type: 'percentiles',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
expect(ds.targetContainsTemplate(target)).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('ElasticDatasource using backend', () => {
|
describe('ElasticDatasource using backend', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
console.error = jest.fn();
|
console.error = jest.fn();
|
||||||
|
@ -828,37 +828,23 @@ export class ElasticDatasource
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private isPrimitive(obj: unknown) {
|
|
||||||
if (obj === null || obj === undefined) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (['string', 'number', 'boolean'].some((type) => type === typeof true)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private objectContainsTemplate(obj: any) {
|
private objectContainsTemplate(obj: any) {
|
||||||
if (!obj) {
|
if (typeof obj === 'string') {
|
||||||
|
return this.templateSrv.containsTemplate(obj);
|
||||||
|
}
|
||||||
|
if (!obj || typeof obj !== 'object') {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const key of Object.keys(obj)) {
|
for (const key of Object.keys(obj)) {
|
||||||
if (this.isPrimitive(obj[key])) {
|
if (Array.isArray(obj[key])) {
|
||||||
if (this.templateSrv.containsTemplate(obj[key])) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
} else if (Array.isArray(obj[key])) {
|
|
||||||
for (const item of obj[key]) {
|
for (const item of obj[key]) {
|
||||||
if (this.objectContainsTemplate(item)) {
|
if (this.objectContainsTemplate(item)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else if (this.objectContainsTemplate(obj[key])) {
|
||||||
if (this.objectContainsTemplate(obj[key])) {
|
return true;
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user