diff --git a/public/app/plugins/datasource/elasticsearch/components/QueryEditor/index.test.tsx b/public/app/plugins/datasource/elasticsearch/components/QueryEditor/index.test.tsx index c409810055a..ad9887055aa 100644 --- a/public/app/plugins/datasource/elasticsearch/components/QueryEditor/index.test.tsx +++ b/public/app/plugins/datasource/elasticsearch/components/QueryEditor/index.test.tsx @@ -4,6 +4,8 @@ import { QueryEditor } from '.'; import { ElasticDatasource } from '../../datasource'; import { ElasticsearchQuery } from '../../types'; +const noop = () => void 0; + describe('QueryEditor', () => { describe('Alias Field', () => { it('Should correctly render and trigger changes on blur', () => { @@ -23,9 +25,7 @@ describe('QueryEditor', () => { const onChange = jest.fn(); - render( - {}} /> - ); + render(); let aliasField = screen.getByLabelText('Alias') as HTMLInputElement; @@ -45,5 +45,41 @@ describe('QueryEditor', () => { expect(onChange).toHaveBeenCalledTimes(1); expect(onChange.mock.calls[0][0].alias).toBe(newAlias); }); + + it('Should be disabled if last bucket aggregation is not Date Histogram', () => { + const query: ElasticsearchQuery = { + refId: 'A', + query: '', + metrics: [ + { + id: '1', + type: 'avg', + }, + ], + bucketAggs: [{ id: '2', type: 'terms' }], + }; + + render(); + + expect(screen.getByLabelText('Alias')).toBeDisabled(); + }); + + it('Should be enabled if last bucket aggregation is Date Histogram', () => { + const query: ElasticsearchQuery = { + refId: 'A', + query: '', + metrics: [ + { + id: '1', + type: 'avg', + }, + ], + bucketAggs: [{ id: '2', type: 'date_histogram' }], + }; + + render(); + + expect(screen.getByLabelText('Alias')).toBeEnabled(); + }); }); }); diff --git a/public/app/plugins/datasource/elasticsearch/components/QueryEditor/index.tsx b/public/app/plugins/datasource/elasticsearch/components/QueryEditor/index.tsx index cc8da80c0e2..6e759d34c4a 100644 --- a/public/app/plugins/datasource/elasticsearch/components/QueryEditor/index.tsx +++ b/public/app/plugins/datasource/elasticsearch/components/QueryEditor/index.tsx @@ -31,6 +31,9 @@ const QueryEditorForm: FunctionComponent = ({ value }) => { const dispatch = useDispatch(); const nextId = useNextId(); + // To be considered a time series query, the last bucked aggregation must be a Date Histogram + const isTimeSeriesQuery = value.bucketAggs?.slice(-1)[0]?.type === 'date_histogram'; + return ( <> @@ -45,7 +48,12 @@ const QueryEditorForm: FunctionComponent = ({ value }) => { portalOrigin="elasticsearch" /> - +