From d3cbe7c3a3ff4332f08fb754af50441e87ea83be Mon Sep 17 00:00:00 2001 From: Matias Chomicki Date: Thu, 8 Jun 2023 14:23:40 +0200 Subject: [PATCH] Elasticsearch: use type guards instead of checking types --- .../datasource/elasticsearch/datasource.ts | 15 ++++-------- .../datasource/elasticsearch/guards.test.ts | 23 +++++++++++++++++++ .../datasource/elasticsearch/guards.ts | 8 +++++++ 3 files changed, 35 insertions(+), 11 deletions(-) create mode 100644 public/app/plugins/datasource/elasticsearch/guards.test.ts create mode 100644 public/app/plugins/datasource/elasticsearch/guards.ts diff --git a/public/app/plugins/datasource/elasticsearch/datasource.ts b/public/app/plugins/datasource/elasticsearch/datasource.ts index 12ed6889243..2ade99dba9a 100644 --- a/public/app/plugins/datasource/elasticsearch/datasource.ts +++ b/public/app/plugins/datasource/elasticsearch/datasource.ts @@ -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; } } diff --git a/public/app/plugins/datasource/elasticsearch/guards.test.ts b/public/app/plugins/datasource/elasticsearch/guards.test.ts new file mode 100644 index 00000000000..99176096a6e --- /dev/null +++ b/public/app/plugins/datasource/elasticsearch/guards.test.ts @@ -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); + }); +}); diff --git a/public/app/plugins/datasource/elasticsearch/guards.ts b/public/app/plugins/datasource/elasticsearch/guards.ts new file mode 100644 index 00000000000..77faedc1a2a --- /dev/null +++ b/public/app/plugins/datasource/elasticsearch/guards.ts @@ -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; +}