Elasticsearch: Disable Alias field for non-timeseries queries (#32279)

* Elasticsearch: Disable Alias field for non-timeseries queries

* Update public/app/plugins/datasource/elasticsearch/components/QueryEditor/index.test.tsx
This commit is contained in:
Giordano Ricci 2021-04-02 11:06:34 +01:00 committed by GitHub
parent 16efc4c1b9
commit fddc83ec7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 4 deletions

View File

@ -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<void, [ElasticsearchQuery]>();
render(
<QueryEditor query={query} datasource={{} as ElasticDatasource} onChange={onChange} onRunQuery={() => {}} />
);
render(<QueryEditor query={query} datasource={{} as ElasticDatasource} onChange={onChange} onRunQuery={noop} />);
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(<QueryEditor query={query} datasource={{} as ElasticDatasource} onChange={noop} onRunQuery={noop} />);
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(<QueryEditor query={query} datasource={{} as ElasticDatasource} onChange={noop} onRunQuery={noop} />);
expect(screen.getByLabelText('Alias')).toBeEnabled();
});
});
});

View File

@ -31,6 +31,9 @@ const QueryEditorForm: FunctionComponent<Props> = ({ 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 (
<>
<InlineFieldRow>
@ -45,7 +48,12 @@ const QueryEditorForm: FunctionComponent<Props> = ({ value }) => {
portalOrigin="elasticsearch"
/>
</InlineField>
<InlineField label="Alias" labelWidth={15}>
<InlineField
label="Alias"
labelWidth={15}
disabled={!isTimeSeriesQuery}
tooltip="Aliasing only works for timeseries queries (when the last group is 'Date Histogram'). For all other query types this field is ignored."
>
<Input
id={`ES-query-${value.refId}_alias`}
placeholder="Alias Pattern"