mirror of
https://github.com/prometheus/prometheus.git
synced 2024-12-26 14:09:41 -08:00
UI: Add toggle to enable/disable metric autocomplete (#8134)
* UI: Add toggle to enable/disable metric autocomplete This change adds a toggle to enable or disable the metric autocomplete functionality. By default it is enabled. This is a port of a change I did in [Thanos][1]. [1]: https://github.com/thanos-io/thanos/pull/3381 Signed-off-by: Jarod Watkins <jarod@42lines.net> * Adding full variable name Signed-off-by: Jarod Watkins <jarod@42lines.net>
This commit is contained in:
parent
7558e9d3c3
commit
388a2d20c9
|
@ -30,6 +30,7 @@ describe('ExpressionInput', () => {
|
||||||
// Do nothing.
|
// Do nothing.
|
||||||
},
|
},
|
||||||
loading: false,
|
loading: false,
|
||||||
|
enableMetricAutocomplete: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
let expressionInput: ReactWrapper;
|
let expressionInput: ReactWrapper;
|
||||||
|
@ -174,8 +175,27 @@ describe('ExpressionInput', () => {
|
||||||
instance.createAutocompleteSection({ closeMenu: spyCloseMenu });
|
instance.createAutocompleteSection({ closeMenu: spyCloseMenu });
|
||||||
setTimeout(() => expect(spyCloseMenu).toHaveBeenCalled());
|
setTimeout(() => expect(spyCloseMenu).toHaveBeenCalled());
|
||||||
});
|
});
|
||||||
|
it('should not render list if enableMetricAutocomplete is false', () => {
|
||||||
|
const input = mount(
|
||||||
|
<ExpressionInput
|
||||||
|
autocompleteSections={{ title: ['foo', 'bar', 'baz'] }}
|
||||||
|
{...({} as any)}
|
||||||
|
enableMetricAutocomplete={false}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
const instance: any = input.instance();
|
||||||
|
const spyCloseMenu = jest.fn();
|
||||||
|
instance.createAutocompleteSection({ closeMenu: spyCloseMenu });
|
||||||
|
setTimeout(() => expect(spyCloseMenu).toHaveBeenCalled());
|
||||||
|
});
|
||||||
it('should render autosuggest-dropdown', () => {
|
it('should render autosuggest-dropdown', () => {
|
||||||
const input = mount(<ExpressionInput autocompleteSections={{ title: ['foo', 'bar', 'baz'] }} {...({} as any)} />);
|
const input = mount(
|
||||||
|
<ExpressionInput
|
||||||
|
autocompleteSections={{ title: ['foo', 'bar', 'baz'] }}
|
||||||
|
{...({} as any)}
|
||||||
|
enableMetricAutocomplete={true}
|
||||||
|
/>
|
||||||
|
);
|
||||||
const instance: any = input.instance();
|
const instance: any = input.instance();
|
||||||
const spyGetMenuProps = jest.fn();
|
const spyGetMenuProps = jest.fn();
|
||||||
const sections = instance.createAutocompleteSection({
|
const sections = instance.createAutocompleteSection({
|
||||||
|
|
|
@ -14,6 +14,7 @@ interface ExpressionInputProps {
|
||||||
autocompleteSections: { [key: string]: string[] };
|
autocompleteSections: { [key: string]: string[] };
|
||||||
executeQuery: () => void;
|
executeQuery: () => void;
|
||||||
loading: boolean;
|
loading: boolean;
|
||||||
|
enableMetricAutocomplete: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ExpressionInputState {
|
interface ExpressionInputState {
|
||||||
|
@ -76,7 +77,8 @@ class ExpressionInput extends Component<ExpressionInputProps, ExpressionInputSta
|
||||||
const { inputValue = '', closeMenu, highlightedIndex } = downshift;
|
const { inputValue = '', closeMenu, highlightedIndex } = downshift;
|
||||||
const { autocompleteSections } = this.props;
|
const { autocompleteSections } = this.props;
|
||||||
let index = 0;
|
let index = 0;
|
||||||
const sections = inputValue!.length
|
const sections =
|
||||||
|
inputValue!.length && this.props.enableMetricAutocomplete
|
||||||
? Object.entries(autocompleteSections).reduce((acc, [title, items]) => {
|
? Object.entries(autocompleteSections).reduce((acc, [title, items]) => {
|
||||||
const matches = this.getSearchMatches(inputValue!, items);
|
const matches = this.getSearchMatches(inputValue!, items);
|
||||||
return !matches.length
|
return !matches.length
|
||||||
|
|
|
@ -32,6 +32,7 @@ const defaultProps = {
|
||||||
onExecuteQuery: (): void => {
|
onExecuteQuery: (): void => {
|
||||||
// Do nothing.
|
// Do nothing.
|
||||||
},
|
},
|
||||||
|
enableMetricAutocomplete: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
describe('Panel', () => {
|
describe('Panel', () => {
|
||||||
|
|
|
@ -22,6 +22,7 @@ interface PanelProps {
|
||||||
removePanel: () => void;
|
removePanel: () => void;
|
||||||
onExecuteQuery: (query: string) => void;
|
onExecuteQuery: (query: string) => void;
|
||||||
pathPrefix: string;
|
pathPrefix: string;
|
||||||
|
enableMetricAutocomplete: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface PanelState {
|
interface PanelState {
|
||||||
|
@ -233,6 +234,7 @@ class Panel extends Component<PanelProps, PanelState> {
|
||||||
onExpressionChange={this.handleExpressionChange}
|
onExpressionChange={this.handleExpressionChange}
|
||||||
executeQuery={this.executeQuery}
|
executeQuery={this.executeQuery}
|
||||||
loading={this.state.loading}
|
loading={this.state.loading}
|
||||||
|
enableMetricAutocomplete={this.props.enableMetricAutocomplete}
|
||||||
autocompleteSections={{
|
autocompleteSections={{
|
||||||
'Query History': pastQueries,
|
'Query History': pastQueries,
|
||||||
'Metric Names': metricNames,
|
'Metric Names': metricNames,
|
||||||
|
|
|
@ -22,9 +22,16 @@ interface PanelListProps extends RouteComponentProps {
|
||||||
metrics: string[];
|
metrics: string[];
|
||||||
useLocalTime: boolean;
|
useLocalTime: boolean;
|
||||||
queryHistoryEnabled: boolean;
|
queryHistoryEnabled: boolean;
|
||||||
|
enableMetricAutocomplete: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const PanelListContent: FC<PanelListProps> = ({ metrics = [], useLocalTime, queryHistoryEnabled, ...rest }) => {
|
export const PanelListContent: FC<PanelListProps> = ({
|
||||||
|
metrics = [],
|
||||||
|
useLocalTime,
|
||||||
|
queryHistoryEnabled,
|
||||||
|
enableMetricAutocomplete,
|
||||||
|
...rest
|
||||||
|
}) => {
|
||||||
const [panels, setPanels] = useState(rest.panels);
|
const [panels, setPanels] = useState(rest.panels);
|
||||||
const [historyItems, setLocalStorageHistoryItems] = useLocalStorage<string[]>('history', []);
|
const [historyItems, setLocalStorageHistoryItems] = useLocalStorage<string[]>('history', []);
|
||||||
|
|
||||||
|
@ -95,6 +102,7 @@ export const PanelListContent: FC<PanelListProps> = ({ metrics = [], useLocalTim
|
||||||
useLocalTime={useLocalTime}
|
useLocalTime={useLocalTime}
|
||||||
metricNames={metrics}
|
metricNames={metrics}
|
||||||
pastQueries={queryHistoryEnabled ? historyItems : []}
|
pastQueries={queryHistoryEnabled ? historyItems : []}
|
||||||
|
enableMetricAutocomplete={enableMetricAutocomplete}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
<Button className="mb-3" color="primary" onClick={addPanel}>
|
<Button className="mb-3" color="primary" onClick={addPanel}>
|
||||||
|
@ -108,6 +116,7 @@ const PanelList: FC<RouteComponentProps> = () => {
|
||||||
const [delta, setDelta] = useState(0);
|
const [delta, setDelta] = useState(0);
|
||||||
const [useLocalTime, setUseLocalTime] = useLocalStorage('use-local-time', false);
|
const [useLocalTime, setUseLocalTime] = useLocalStorage('use-local-time', false);
|
||||||
const [enableQueryHistory, setEnableQueryHistory] = useLocalStorage('enable-query-history', false);
|
const [enableQueryHistory, setEnableQueryHistory] = useLocalStorage('enable-query-history', false);
|
||||||
|
const [enableMetricAutocomplete, setEnableMetricAutocomplete] = useLocalStorage('enable-metric-autocomplete', true);
|
||||||
|
|
||||||
const pathPrefix = usePathPrefix();
|
const pathPrefix = usePathPrefix();
|
||||||
const { response: metricsRes, error: metricsErr } = useFetch<string[]>(`${pathPrefix}/${API_PATH}/label/__name__/values`);
|
const { response: metricsRes, error: metricsErr } = useFetch<string[]>(`${pathPrefix}/${API_PATH}/label/__name__/values`);
|
||||||
|
@ -148,6 +157,14 @@ const PanelList: FC<RouteComponentProps> = () => {
|
||||||
>
|
>
|
||||||
Use local time
|
Use local time
|
||||||
</Checkbox>
|
</Checkbox>
|
||||||
|
<Checkbox
|
||||||
|
wrapperStyles={{ marginLeft: 20, display: 'inline-block' }}
|
||||||
|
id="metric-autocomplete"
|
||||||
|
onChange={({ target }) => setEnableMetricAutocomplete(target.checked)}
|
||||||
|
defaultChecked={enableMetricAutocomplete}
|
||||||
|
>
|
||||||
|
Enable metric autocomplete
|
||||||
|
</Checkbox>
|
||||||
{(delta > 30 || timeErr) && (
|
{(delta > 30 || timeErr) && (
|
||||||
<Alert color="danger">
|
<Alert color="danger">
|
||||||
<strong>Warning: </strong>
|
<strong>Warning: </strong>
|
||||||
|
@ -167,6 +184,7 @@ const PanelList: FC<RouteComponentProps> = () => {
|
||||||
useLocalTime={useLocalTime}
|
useLocalTime={useLocalTime}
|
||||||
metrics={metricsRes.data}
|
metrics={metricsRes.data}
|
||||||
queryHistoryEnabled={enableQueryHistory}
|
queryHistoryEnabled={enableQueryHistory}
|
||||||
|
enableMetricAutocomplete={enableMetricAutocomplete}
|
||||||
/>
|
/>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in a new issue