Elasticsearch: use type guards instead of checking types
This commit is contained in:
@@ -51,6 +51,7 @@ import {
|
|||||||
isPipelineAggregationWithMultipleBucketPaths,
|
isPipelineAggregationWithMultipleBucketPaths,
|
||||||
} from './components/QueryEditor/MetricAggregationsEditor/aggregations';
|
} from './components/QueryEditor/MetricAggregationsEditor/aggregations';
|
||||||
import { metricAggregationConfig } from './components/QueryEditor/MetricAggregationsEditor/utils';
|
import { metricAggregationConfig } from './components/QueryEditor/MetricAggregationsEditor/utils';
|
||||||
|
import { isMetricAggregationWithMeta } from './guards';
|
||||||
import { trackAnnotationQuery, trackQuery } from './tracking';
|
import { trackAnnotationQuery, trackQuery } from './tracking';
|
||||||
import {
|
import {
|
||||||
Logs,
|
Logs,
|
||||||
@@ -801,7 +802,7 @@ export class ElasticDatasource
|
|||||||
|
|
||||||
if (target.bucketAggs) {
|
if (target.bucketAggs) {
|
||||||
for (const bucketAgg of 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;
|
return true;
|
||||||
}
|
}
|
||||||
if (this.objectContainsTemplate(bucketAgg.settings)) {
|
if (this.objectContainsTemplate(bucketAgg.settings)) {
|
||||||
@@ -812,15 +813,7 @@ export class ElasticDatasource
|
|||||||
|
|
||||||
if (target.metrics) {
|
if (target.metrics) {
|
||||||
for (const metric of target.metrics) {
|
for (const metric of target.metrics) {
|
||||||
if (
|
if (!isMetricAggregationWithField(metric)) {
|
||||||
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'
|
|
||||||
) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (metric.field && this.templateSrv.containsTemplate(metric.field)) {
|
if (metric.field && this.templateSrv.containsTemplate(metric.field)) {
|
||||||
@@ -829,7 +822,7 @@ export class ElasticDatasource
|
|||||||
if (metric.settings && this.objectContainsTemplate(metric.settings)) {
|
if (metric.settings && this.objectContainsTemplate(metric.settings)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (metric.type === 'extended_stats' && this.objectContainsTemplate(metric.meta)) {
|
if (isMetricAggregationWithMeta(metric) && this.objectContainsTemplate(metric.meta)) {
|
||||||
return true;
|
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