mirror of
https://github.com/grafana/grafana.git
synced 2024-11-24 09:50:29 -06:00
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:
parent
16efc4c1b9
commit
fddc83ec7c
@ -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();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -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"
|
||||
|
Loading…
Reference in New Issue
Block a user