Tempo: Improve how Tempo Search fetches autocomplete values (#39371)
* Improve how tempo search fetches autocomplete values * Refetch autocomplete options on menu load
This commit is contained in:
@@ -4,11 +4,11 @@ import {
|
|||||||
InlineField,
|
InlineField,
|
||||||
Input,
|
Input,
|
||||||
QueryField,
|
QueryField,
|
||||||
Select,
|
|
||||||
SlatePrism,
|
SlatePrism,
|
||||||
BracesPlugin,
|
BracesPlugin,
|
||||||
TypeaheadInput,
|
TypeaheadInput,
|
||||||
TypeaheadOutput,
|
TypeaheadOutput,
|
||||||
|
Select,
|
||||||
} from '@grafana/ui';
|
} from '@grafana/ui';
|
||||||
import { tokenizer } from './syntax';
|
import { tokenizer } from './syntax';
|
||||||
import Prism from 'prismjs';
|
import Prism from 'prismjs';
|
||||||
@@ -17,6 +17,7 @@ import { css } from '@emotion/css';
|
|||||||
import { SelectableValue } from '@grafana/data';
|
import { SelectableValue } from '@grafana/data';
|
||||||
import TempoLanguageProvider from './language_provider';
|
import TempoLanguageProvider from './language_provider';
|
||||||
import { TempoDatasource, TempoQuery } from './datasource';
|
import { TempoDatasource, TempoQuery } from './datasource';
|
||||||
|
import { debounce } from 'lodash';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
datasource: TempoDatasource;
|
datasource: TempoDatasource;
|
||||||
@@ -43,22 +44,51 @@ const NativeSearch = ({ datasource, query, onChange, onBlur, onRunQuery }: Props
|
|||||||
const [hasSyntaxLoaded, setHasSyntaxLoaded] = useState(false);
|
const [hasSyntaxLoaded, setHasSyntaxLoaded] = useState(false);
|
||||||
const [autocomplete, setAutocomplete] = useState<{
|
const [autocomplete, setAutocomplete] = useState<{
|
||||||
serviceNameOptions: Array<SelectableValue<string>>;
|
serviceNameOptions: Array<SelectableValue<string>>;
|
||||||
|
selectedServiceName: SelectableValue<string> | undefined;
|
||||||
spanNameOptions: Array<SelectableValue<string>>;
|
spanNameOptions: Array<SelectableValue<string>>;
|
||||||
|
selectedSpanName: SelectableValue<string> | undefined;
|
||||||
}>({
|
}>({
|
||||||
serviceNameOptions: [],
|
serviceNameOptions: [],
|
||||||
|
selectedServiceName: undefined,
|
||||||
spanNameOptions: [],
|
spanNameOptions: [],
|
||||||
|
selectedSpanName: undefined,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const fetchServiceNameOptions = useMemo(
|
||||||
|
() =>
|
||||||
|
debounce(
|
||||||
|
async () => {
|
||||||
|
const res = await languageProvider.getOptions('service.name');
|
||||||
|
setAutocomplete((prev) => ({ ...prev, serviceNameOptions: res }));
|
||||||
|
},
|
||||||
|
500,
|
||||||
|
{ leading: true, trailing: true }
|
||||||
|
),
|
||||||
|
[languageProvider]
|
||||||
|
);
|
||||||
|
|
||||||
|
const fetchSpanNameOptions = useMemo(
|
||||||
|
() =>
|
||||||
|
debounce(
|
||||||
|
async () => {
|
||||||
|
const res = await languageProvider.getOptions('name');
|
||||||
|
setAutocomplete((prev) => ({ ...prev, spanNameOptions: res }));
|
||||||
|
},
|
||||||
|
500,
|
||||||
|
{ leading: true, trailing: true }
|
||||||
|
),
|
||||||
|
[languageProvider]
|
||||||
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchAutocomplete = async () => {
|
const fetchAutocomplete = async () => {
|
||||||
await languageProvider.start();
|
await languageProvider.start();
|
||||||
const serviceNameOptions = await languageProvider.getOptions('service.name');
|
await fetchServiceNameOptions();
|
||||||
const spanNameOptions = await languageProvider.getOptions('name');
|
await fetchSpanNameOptions();
|
||||||
setHasSyntaxLoaded(true);
|
setHasSyntaxLoaded(true);
|
||||||
setAutocomplete({ serviceNameOptions, spanNameOptions });
|
|
||||||
};
|
};
|
||||||
fetchAutocomplete();
|
fetchAutocomplete();
|
||||||
}, [languageProvider]);
|
}, [languageProvider, fetchServiceNameOptions, fetchSpanNameOptions]);
|
||||||
|
|
||||||
const onTypeahead = async (typeahead: TypeaheadInput): Promise<TypeaheadOutput> => {
|
const onTypeahead = async (typeahead: TypeaheadInput): Promise<TypeaheadOutput> => {
|
||||||
return await languageProvider.provideCompletionItems(typeahead);
|
return await languageProvider.provideCompletionItems(typeahead);
|
||||||
@@ -93,6 +123,7 @@ const NativeSearch = ({ datasource, query, onChange, onBlur, onRunQuery }: Props
|
|||||||
});
|
});
|
||||||
}}
|
}}
|
||||||
placeholder="Select a service"
|
placeholder="Select a service"
|
||||||
|
onOpenMenu={fetchServiceNameOptions}
|
||||||
isClearable
|
isClearable
|
||||||
/>
|
/>
|
||||||
</InlineField>
|
</InlineField>
|
||||||
@@ -110,6 +141,7 @@ const NativeSearch = ({ datasource, query, onChange, onBlur, onRunQuery }: Props
|
|||||||
});
|
});
|
||||||
}}
|
}}
|
||||||
placeholder="Select a span"
|
placeholder="Select a span"
|
||||||
|
onOpenMenu={fetchSpanNameOptions}
|
||||||
isClearable
|
isClearable
|
||||||
/>
|
/>
|
||||||
</InlineField>
|
</InlineField>
|
||||||
|
|||||||
Reference in New Issue
Block a user