From 7f1327d1ed876037ca35dbf7ebaa6a5c2e9df1da Mon Sep 17 00:00:00 2001 From: Giordano Ricci Date: Thu, 2 Sep 2021 15:00:40 +0100 Subject: [PATCH] Elasticsearch: Prevent pipeline aggregations to show up in terms order by options (#38448) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Elasticsearch: Prevent pipeline aggregations to show up in terms order by options * fix optional props * Refactor & add tests * Fix & lower strict ts count * Update public/app/plugins/datasource/elasticsearch/components/QueryEditor/BucketAggregationsEditor/SettingsEditor/TermsSettingsEditor.test.tsx Co-authored-by: Piotr Jamróz * Update public/app/plugins/datasource/elasticsearch/components/QueryEditor/BucketAggregationsEditor/SettingsEditor/TermsSettingsEditor.test.tsx Co-authored-by: Piotr Jamróz Co-authored-by: Piotr Jamróz --- .../TermsSettingsEditor.test.tsx | 41 ++++++++++ .../SettingsEditor/TermsSettingsEditor.tsx | 79 +++++++++++++++++- .../SettingsEditor/useDescription.ts | 2 +- .../BucketAggregationsEditor/utils.ts | 80 ++----------------- .../elasticsearch/test-helpers/render.tsx | 22 +++-- scripts/ci-check-strict.sh | 2 +- 6 files changed, 141 insertions(+), 85 deletions(-) create mode 100644 public/app/plugins/datasource/elasticsearch/components/QueryEditor/BucketAggregationsEditor/SettingsEditor/TermsSettingsEditor.test.tsx diff --git a/public/app/plugins/datasource/elasticsearch/components/QueryEditor/BucketAggregationsEditor/SettingsEditor/TermsSettingsEditor.test.tsx b/public/app/plugins/datasource/elasticsearch/components/QueryEditor/BucketAggregationsEditor/SettingsEditor/TermsSettingsEditor.test.tsx new file mode 100644 index 00000000000..3243378b00a --- /dev/null +++ b/public/app/plugins/datasource/elasticsearch/components/QueryEditor/BucketAggregationsEditor/SettingsEditor/TermsSettingsEditor.test.tsx @@ -0,0 +1,41 @@ +import { screen } from '@testing-library/react'; +import { ElasticsearchQuery } from '../../../../types'; +import React from 'react'; +import { renderWithESProvider } from '../../../../test-helpers/render'; +import { Average, Derivative, TopMetrics } from '../../MetricAggregationsEditor/aggregations'; +import { Terms } from '../aggregations'; +import { TermsSettingsEditor } from './TermsSettingsEditor'; +import selectEvent from 'react-select-event'; +import { describeMetric } from 'app/plugins/datasource/elasticsearch/utils'; + +describe('Terms Settings Editor', () => { + it('Pipeline aggregations should not be in "order by" options', () => { + const termsAgg: Terms = { + id: '1', + type: 'terms', + }; + const avg: Average = { id: '2', type: 'avg', field: '@value' }; + const derivative: Derivative = { id: '3', field: avg.id, type: 'derivative' }; + const topMetrics: TopMetrics = { id: '4', type: 'top_metrics' }; + const query: ElasticsearchQuery = { + refId: 'A', + query: '', + bucketAggs: [termsAgg], + metrics: [avg, derivative, topMetrics], + }; + + renderWithESProvider(, { providerProps: { query } }); + + const selectEl = screen.getByLabelText('Order By'); + expect(selectEl).toBeInTheDocument(); + + selectEvent.openMenu(selectEl); + + // Derivative is a pipeline aggregation, it shouldn't be present in the order by options + expect(screen.queryByText(describeMetric(derivative))).not.toBeInTheDocument(); + // TopMetrics cannot be used as order by option + expect(screen.queryByText(describeMetric(topMetrics))).not.toBeInTheDocument(); + // All other metric aggregations can be used in order by + expect(screen.getByText(describeMetric(avg))).toBeInTheDocument(); + }); +}); diff --git a/public/app/plugins/datasource/elasticsearch/components/QueryEditor/BucketAggregationsEditor/SettingsEditor/TermsSettingsEditor.tsx b/public/app/plugins/datasource/elasticsearch/components/QueryEditor/BucketAggregationsEditor/SettingsEditor/TermsSettingsEditor.tsx index dfc9b5fd1db..b13e020d20f 100644 --- a/public/app/plugins/datasource/elasticsearch/components/QueryEditor/BucketAggregationsEditor/SettingsEditor/TermsSettingsEditor.tsx +++ b/public/app/plugins/datasource/elasticsearch/components/QueryEditor/BucketAggregationsEditor/SettingsEditor/TermsSettingsEditor.tsx @@ -3,10 +3,20 @@ import { InlineField, Select, Input } from '@grafana/ui'; import { Terms } from '../aggregations'; import { useDispatch } from '../../../../hooks/useStatelessReducer'; import { inlineFieldProps } from '.'; -import { bucketAggregationConfig, createOrderByOptionsFromMetrics, orderOptions, sizeOptions } from '../utils'; +import { bucketAggregationConfig, orderByOptions, orderOptions, sizeOptions } from '../utils'; import { useCreatableSelectPersistedBehaviour } from '../../../hooks/useCreatableSelectPersistedBehaviour'; import { changeBucketAggregationSetting } from '../state/actions'; import { useQuery } from '../../ElasticsearchQueryContext'; +import { SelectableValue } from '@grafana/data'; +import { describeMetric } from '../../../../utils'; +import { + ExtendedStatMetaType, + ExtendedStats, + isPipelineAggregation, + MetricAggregation, + Percentiles, +} from '../../MetricAggregationsEditor/aggregations'; +import { uniqueId } from 'lodash'; interface Props { bucketAgg: Terms; @@ -14,7 +24,7 @@ interface Props { export const TermsSettingsEditor = ({ bucketAgg }: Props) => { const { metrics } = useQuery(); - const orderBy = createOrderByOptionsFromMetrics(metrics); + const orderBy = createOrderByOptions(metrics); const dispatch = useDispatch(); @@ -60,6 +70,7 @@ export const TermsSettingsEditor = ({ bucketAgg }: Props) => {