mirror of
https://github.com/grafana/grafana.git
synced 2025-02-13 00:55:47 -06:00
Elasticsearch: fix handling of null values in query_builder (#30234)
This commit is contained in:
parent
a21c1b0841
commit
d8b4ed3a0e
@ -334,10 +334,11 @@ export class ElasticQueryBuilder {
|
||||
metricAgg = { field: metric.field };
|
||||
}
|
||||
|
||||
metricAgg = {
|
||||
...metricAgg,
|
||||
...(isMetricAggregationWithSettings(metric) && metric.settings),
|
||||
};
|
||||
if (isMetricAggregationWithSettings(metric)) {
|
||||
Object.entries(metric.settings || {})
|
||||
.filter(([_, v]) => v !== null)
|
||||
.forEach(([k, v]) => (metricAgg[k] = v));
|
||||
}
|
||||
|
||||
aggField[metric.type] = metricAgg;
|
||||
nestedAggs.aggs[metric.id] = aggField;
|
||||
|
@ -24,6 +24,22 @@ describe('ElasticQueryBuilder', () => {
|
||||
expect(query.aggs['1'].date_histogram.extended_bounds.min).toBe('$timeFrom');
|
||||
});
|
||||
|
||||
it('should clean settings from null values', () => {
|
||||
const query = builder.build({
|
||||
refId: 'A',
|
||||
// The following `missing: null as any` is because previous versions of the DS where
|
||||
// storing null in the query model when inputting an empty string,
|
||||
// which were then removed in the query builder.
|
||||
// The new version doesn't store empty strings at all. This tests ensures backward compatinility.
|
||||
metrics: [{ type: 'avg', id: '0', settings: { missing: null as any, script: '1' } }],
|
||||
timeField: '@timestamp',
|
||||
bucketAggs: [{ type: 'date_histogram', field: '@timestamp', id: '1' }],
|
||||
});
|
||||
|
||||
expect(query.aggs['1'].aggs['0'].avg.missing).not.toBeDefined();
|
||||
expect(query.aggs['1'].aggs['0'].avg.script).toBeDefined();
|
||||
});
|
||||
|
||||
it('with multiple bucket aggs', () => {
|
||||
const query = builder.build({
|
||||
refId: 'A',
|
||||
|
Loading…
Reference in New Issue
Block a user