|
|
|
|
@ -3,7 +3,7 @@ import debounce from 'debounce-promise'; |
|
|
|
|
import React, { useCallback, useState } from 'react'; |
|
|
|
|
import Highlighter from 'react-highlight-words'; |
|
|
|
|
|
|
|
|
|
import { SelectableValue, toOption, GrafanaTheme2 } from '@grafana/data'; |
|
|
|
|
import { GrafanaTheme2, SelectableValue, toOption } from '@grafana/data'; |
|
|
|
|
import { EditorField, EditorFieldGroup } from '@grafana/experimental'; |
|
|
|
|
import { AsyncSelect, FormatOptionLabelMeta, useStyles2 } from '@grafana/ui'; |
|
|
|
|
|
|
|
|
|
@ -16,6 +16,7 @@ import { PromVisualQuery } from '../types'; |
|
|
|
|
const splitSeparator = ' '; |
|
|
|
|
|
|
|
|
|
export interface Props { |
|
|
|
|
metricLookupDisabled: boolean; |
|
|
|
|
query: PromVisualQuery; |
|
|
|
|
onChange: (query: PromVisualQuery) => void; |
|
|
|
|
onGetMetrics: () => Promise<SelectableValue[]>; |
|
|
|
|
@ -25,7 +26,14 @@ export interface Props { |
|
|
|
|
|
|
|
|
|
export const PROMETHEUS_QUERY_BUILDER_MAX_RESULTS = 1000; |
|
|
|
|
|
|
|
|
|
export function MetricSelect({ datasource, query, onChange, onGetMetrics, labelsFilters }: Props) { |
|
|
|
|
export function MetricSelect({ |
|
|
|
|
datasource, |
|
|
|
|
query, |
|
|
|
|
onChange, |
|
|
|
|
onGetMetrics, |
|
|
|
|
labelsFilters, |
|
|
|
|
metricLookupDisabled, |
|
|
|
|
}: Props) { |
|
|
|
|
const styles = useStyles2(getStyles); |
|
|
|
|
const [state, setState] = useState<{ |
|
|
|
|
metrics?: Array<SelectableValue<any>>; |
|
|
|
|
@ -114,6 +122,9 @@ export function MetricSelect({ datasource, query, onChange, onGetMetrics, labels |
|
|
|
|
}); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
// When metric and label lookup is disabled we won't request labels
|
|
|
|
|
const metricLookupDisabledSearch = () => Promise.resolve([]); |
|
|
|
|
|
|
|
|
|
const debouncedSearch = debounce( |
|
|
|
|
(query: string) => getMetricLabels(query), |
|
|
|
|
datasource.getDebounceTimeInMilliseconds() |
|
|
|
|
@ -126,11 +137,14 @@ export function MetricSelect({ datasource, query, onChange, onGetMetrics, labels |
|
|
|
|
inputId="prometheus-metric-select" |
|
|
|
|
className={styles.select} |
|
|
|
|
value={query.metric ? toOption(query.metric) : undefined} |
|
|
|
|
placeholder="Select metric" |
|
|
|
|
placeholder={'Select metric'} |
|
|
|
|
allowCustomValue |
|
|
|
|
formatOptionLabel={formatOptionLabel} |
|
|
|
|
filterOption={customFilterOption} |
|
|
|
|
onOpenMenu={async () => { |
|
|
|
|
if (metricLookupDisabled) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
setState({ isLoading: true }); |
|
|
|
|
const metrics = await onGetMetrics(); |
|
|
|
|
if (metrics.length > PROMETHEUS_QUERY_BUILDER_MAX_RESULTS) { |
|
|
|
|
@ -138,7 +152,7 @@ export function MetricSelect({ datasource, query, onChange, onGetMetrics, labels |
|
|
|
|
} |
|
|
|
|
setState({ metrics, isLoading: undefined }); |
|
|
|
|
}} |
|
|
|
|
loadOptions={debouncedSearch} |
|
|
|
|
loadOptions={metricLookupDisabled ? metricLookupDisabledSearch : debouncedSearch} |
|
|
|
|
isLoading={state.isLoading} |
|
|
|
|
defaultOptions={state.metrics} |
|
|
|
|
onChange={({ value }) => { |
|
|
|
|
|