Elasticsearch: use type guards instead of checking types
This commit is contained in:
@@ -51,6 +51,7 @@ import {
|
||||
isPipelineAggregationWithMultipleBucketPaths,
|
||||
} from './components/QueryEditor/MetricAggregationsEditor/aggregations';
|
||||
import { metricAggregationConfig } from './components/QueryEditor/MetricAggregationsEditor/utils';
|
||||
import { isMetricAggregationWithMeta } from './guards';
|
||||
import { trackAnnotationQuery, trackQuery } from './tracking';
|
||||
import {
|
||||
Logs,
|
||||
@@ -801,7 +802,7 @@ export class ElasticDatasource
|
||||
|
||||
if (target.bucketAggs) {
|
||||
for (const bucketAgg of target.bucketAggs) {
|
||||
if (bucketAgg.type !== 'filters' && this.templateSrv.containsTemplate(bucketAgg.field)) {
|
||||
if (isBucketAggregationWithField(bucketAgg) && this.templateSrv.containsTemplate(bucketAgg.field)) {
|
||||
return true;
|
||||
}
|
||||
if (this.objectContainsTemplate(bucketAgg.settings)) {
|
||||
@@ -812,15 +813,7 @@ export class ElasticDatasource
|
||||
|
||||
if (target.metrics) {
|
||||
for (const metric of target.metrics) {
|
||||
if (
|
||||
metric.type === 'count' ||
|
||||
metric.type === 'bucket_script' ||
|
||||
metric.type === 'raw_data' ||
|
||||
metric.type === 'raw_document' ||
|
||||
metric.type === 'logs' ||
|
||||
metric.type === 'top_metrics' ||
|
||||
metric.type === 'moving_avg'
|
||||
) {
|
||||
if (!isMetricAggregationWithField(metric)) {
|
||||
continue;
|
||||
}
|
||||
if (metric.field && this.templateSrv.containsTemplate(metric.field)) {
|
||||
@@ -829,7 +822,7 @@ export class ElasticDatasource
|
||||
if (metric.settings && this.objectContainsTemplate(metric.settings)) {
|
||||
return true;
|
||||
}
|
||||
if (metric.type === 'extended_stats' && this.objectContainsTemplate(metric.meta)) {
|
||||
if (isMetricAggregationWithMeta(metric) && this.objectContainsTemplate(metric.meta)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
import { Count, ExtendedStats } from './dataquery.gen';
|
||||
import { isMetricAggregationWithMeta } from './guards';
|
||||
|
||||
describe('Type guards', () => {
|
||||
test('Identifies metrics with meta attribute', () => {
|
||||
const metric: ExtendedStats = {
|
||||
id: 'test',
|
||||
type: 'extended_stats',
|
||||
meta: {
|
||||
test: 'test',
|
||||
},
|
||||
};
|
||||
expect(isMetricAggregationWithMeta(metric)).toBe(true);
|
||||
});
|
||||
|
||||
test('Identifies metrics without meta attribute', () => {
|
||||
const metric: Count = {
|
||||
id: 'test',
|
||||
type: 'count',
|
||||
};
|
||||
expect(isMetricAggregationWithMeta(metric)).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,8 @@
|
||||
import { ExtendedStats, MetricAggregation } from './dataquery.gen';
|
||||
|
||||
export function isMetricAggregationWithMeta(metric: MetricAggregation): metric is ExtendedStats {
|
||||
if (!metric || typeof metric !== 'object') {
|
||||
return false;
|
||||
}
|
||||
return 'meta' in metric;
|
||||
}
|
||||
Reference in New Issue
Block a user