mirror of https://github.com/grafana/grafana.git
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 { ElasticDatasource } from '../../datasource';
|
||||||
import { ElasticsearchQuery } from '../../types';
|
import { ElasticsearchQuery } from '../../types';
|
||||||
|
|
||||||
|
const noop = () => void 0;
|
||||||
|
|
||||||
describe('QueryEditor', () => {
|
describe('QueryEditor', () => {
|
||||||
describe('Alias Field', () => {
|
describe('Alias Field', () => {
|
||||||
it('Should correctly render and trigger changes on blur', () => {
|
it('Should correctly render and trigger changes on blur', () => {
|
||||||
|
|
@ -23,9 +25,7 @@ describe('QueryEditor', () => {
|
||||||
|
|
||||||
const onChange = jest.fn<void, [ElasticsearchQuery]>();
|
const onChange = jest.fn<void, [ElasticsearchQuery]>();
|
||||||
|
|
||||||
render(
|
render(<QueryEditor query={query} datasource={{} as ElasticDatasource} onChange={onChange} onRunQuery={noop} />);
|
||||||
<QueryEditor query={query} datasource={{} as ElasticDatasource} onChange={onChange} onRunQuery={() => {}} />
|
|
||||||
);
|
|
||||||
|
|
||||||
let aliasField = screen.getByLabelText('Alias') as HTMLInputElement;
|
let aliasField = screen.getByLabelText('Alias') as HTMLInputElement;
|
||||||
|
|
||||||
|
|
@ -45,5 +45,41 @@ describe('QueryEditor', () => {
|
||||||
expect(onChange).toHaveBeenCalledTimes(1);
|
expect(onChange).toHaveBeenCalledTimes(1);
|
||||||
expect(onChange.mock.calls[0][0].alias).toBe(newAlias);
|
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 dispatch = useDispatch();
|
||||||
const nextId = useNextId();
|
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 (
|
return (
|
||||||
<>
|
<>
|
||||||
<InlineFieldRow>
|
<InlineFieldRow>
|
||||||
|
|
@ -45,7 +48,12 @@ const QueryEditorForm: FunctionComponent<Props> = ({ value }) => {
|
||||||
portalOrigin="elasticsearch"
|
portalOrigin="elasticsearch"
|
||||||
/>
|
/>
|
||||||
</InlineField>
|
</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
|
<Input
|
||||||
id={`ES-query-${value.refId}_alias`}
|
id={`ES-query-${value.refId}_alias`}
|
||||||
placeholder="Alias Pattern"
|
placeholder="Alias Pattern"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue