mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
* Elasticsearch: Allow omitting field when metric supports inline script * Add tests for MetricEditor to show a None option * Add tests for useFields hook * Alerting: allow elasticsearch metrics without field
78 lines
2.7 KiB
TypeScript
78 lines
2.7 KiB
TypeScript
import React, { FunctionComponent } from 'react';
|
|
import { getDefaultTimeRange, QueryEditorProps } from '@grafana/data';
|
|
import { ElasticDatasource } from '../../datasource';
|
|
import { ElasticsearchOptions, ElasticsearchQuery } from '../../types';
|
|
import { ElasticsearchProvider } from './ElasticsearchQueryContext';
|
|
import { InlineField, InlineFieldRow, Input, QueryField } from '@grafana/ui';
|
|
import { changeAliasPattern, changeQuery } from './state';
|
|
import { MetricAggregationsEditor } from './MetricAggregationsEditor';
|
|
import { BucketAggregationsEditor } from './BucketAggregationsEditor';
|
|
import { useDispatch } from '../../hooks/useStatelessReducer';
|
|
import { useNextId } from '../../hooks/useNextId';
|
|
|
|
export type ElasticQueryEditorProps = QueryEditorProps<ElasticDatasource, ElasticsearchQuery, ElasticsearchOptions>;
|
|
|
|
export const QueryEditor: FunctionComponent<ElasticQueryEditorProps> = ({
|
|
query,
|
|
onChange,
|
|
onRunQuery,
|
|
datasource,
|
|
range,
|
|
}) => (
|
|
<ElasticsearchProvider
|
|
datasource={datasource}
|
|
onChange={onChange}
|
|
onRunQuery={onRunQuery}
|
|
query={query}
|
|
range={range || getDefaultTimeRange()}
|
|
>
|
|
<QueryEditorForm value={query} />
|
|
</ElasticsearchProvider>
|
|
);
|
|
|
|
interface Props {
|
|
value: ElasticsearchQuery;
|
|
}
|
|
|
|
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>
|
|
<InlineField label="Query" labelWidth={17} grow>
|
|
<QueryField
|
|
query={value.query}
|
|
// By default QueryField calls onChange if onBlur is not defined, this will trigger a rerender
|
|
// And slate will claim the focus, making it impossible to leave the field.
|
|
onBlur={() => {}}
|
|
onChange={(query) => dispatch(changeQuery(query))}
|
|
placeholder="Lucene Query"
|
|
portalOrigin="elasticsearch"
|
|
/>
|
|
</InlineField>
|
|
<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"
|
|
onBlur={(e) => dispatch(changeAliasPattern(e.currentTarget.value))}
|
|
defaultValue={value.alias}
|
|
/>
|
|
</InlineField>
|
|
</InlineFieldRow>
|
|
|
|
<MetricAggregationsEditor nextId={nextId} />
|
|
<BucketAggregationsEditor nextId={nextId} />
|
|
</>
|
|
);
|
|
};
|