mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
AdHocVariable: Adds default data source (#32470)
This commit is contained in:
parent
3b087db6ac
commit
84ea3a73c0
@ -1,30 +1,31 @@
|
|||||||
import React, { PureComponent } from 'react';
|
import React, { PureComponent } from 'react';
|
||||||
import { MapDispatchToProps, MapStateToProps } from 'react-redux';
|
import { connect, ConnectedProps } from 'react-redux';
|
||||||
|
import { Alert, InlineFieldRow, VerticalGroup } from '@grafana/ui';
|
||||||
|
import { SelectableValue } from '@grafana/data';
|
||||||
|
|
||||||
import { AdHocVariableModel } from '../types';
|
import { AdHocVariableModel } from '../types';
|
||||||
import { VariableEditorProps } from '../editor/types';
|
import { VariableEditorProps } from '../editor/types';
|
||||||
import { VariableEditorState } from '../editor/reducer';
|
import { VariableEditorState } from '../editor/reducer';
|
||||||
import { AdHocVariableEditorState } from './reducer';
|
import { AdHocVariableEditorState } from './reducer';
|
||||||
import { changeVariableDatasource, initAdHocVariableEditor } from './actions';
|
import { changeVariableDatasource, initAdHocVariableEditor } from './actions';
|
||||||
import { connectWithStore } from 'app/core/utils/connectWithReduxStore';
|
|
||||||
import { StoreState } from 'app/types';
|
import { StoreState } from 'app/types';
|
||||||
import { Alert, InlineFieldRow, VerticalGroup } from '@grafana/ui';
|
|
||||||
import { VariableSectionHeader } from '../editor/VariableSectionHeader';
|
import { VariableSectionHeader } from '../editor/VariableSectionHeader';
|
||||||
import { VariableSelectField } from '../editor/VariableSelectField';
|
import { VariableSelectField } from '../editor/VariableSelectField';
|
||||||
import { SelectableValue } from '@grafana/data';
|
|
||||||
|
const mapStateToProps = (state: StoreState) => ({
|
||||||
|
editor: state.templating.editor as VariableEditorState<AdHocVariableEditorState>,
|
||||||
|
});
|
||||||
|
|
||||||
|
const mapDispatchToProps = {
|
||||||
|
initAdHocVariableEditor,
|
||||||
|
changeVariableDatasource,
|
||||||
|
};
|
||||||
|
|
||||||
|
const connector = connect(mapStateToProps, mapDispatchToProps);
|
||||||
|
|
||||||
export interface OwnProps extends VariableEditorProps<AdHocVariableModel> {}
|
export interface OwnProps extends VariableEditorProps<AdHocVariableModel> {}
|
||||||
|
|
||||||
interface ConnectedProps {
|
type Props = OwnProps & ConnectedProps<typeof connector>;
|
||||||
editor: VariableEditorState<AdHocVariableEditorState>;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface DispatchProps {
|
|
||||||
initAdHocVariableEditor: typeof initAdHocVariableEditor;
|
|
||||||
changeVariableDatasource: typeof changeVariableDatasource;
|
|
||||||
}
|
|
||||||
|
|
||||||
type Props = OwnProps & ConnectedProps & DispatchProps;
|
|
||||||
|
|
||||||
export class AdHocVariableEditorUnConnected extends PureComponent<Props> {
|
export class AdHocVariableEditorUnConnected extends PureComponent<Props> {
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
@ -32,14 +33,14 @@ export class AdHocVariableEditorUnConnected extends PureComponent<Props> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onDatasourceChanged = (option: SelectableValue<string>) => {
|
onDatasourceChanged = (option: SelectableValue<string>) => {
|
||||||
this.props.changeVariableDatasource(option.value ?? '');
|
this.props.changeVariableDatasource(option.value);
|
||||||
};
|
};
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { variable, editor } = this.props;
|
const { variable, editor } = this.props;
|
||||||
const dataSources = editor.extended?.dataSources ?? [];
|
const dataSources = editor.extended?.dataSources ?? [];
|
||||||
const infoText = editor.extended?.infoText ?? null;
|
const infoText = editor.extended?.infoText ?? null;
|
||||||
const options = dataSources.map((ds) => ({ label: ds.text, value: ds.value ?? '' }));
|
const options = dataSources.map((ds) => ({ label: ds.text, value: ds.value }));
|
||||||
const value = options.find((o) => o.value === variable.datasource) ?? options[0];
|
const value = options.find((o) => o.value === variable.datasource) ?? options[0];
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -62,17 +63,4 @@ export class AdHocVariableEditorUnConnected extends PureComponent<Props> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const mapStateToProps: MapStateToProps<ConnectedProps, OwnProps, StoreState> = (state, ownProps) => ({
|
export const AdHocVariableEditor = connector(AdHocVariableEditorUnConnected);
|
||||||
editor: state.templating.editor as VariableEditorState<AdHocVariableEditorState>,
|
|
||||||
});
|
|
||||||
|
|
||||||
const mapDispatchToProps: MapDispatchToProps<DispatchProps, OwnProps> = {
|
|
||||||
initAdHocVariableEditor,
|
|
||||||
changeVariableDatasource,
|
|
||||||
};
|
|
||||||
|
|
||||||
export const AdHocVariableEditor = connectWithStore(
|
|
||||||
AdHocVariableEditorUnConnected,
|
|
||||||
mapStateToProps,
|
|
||||||
mapDispatchToProps
|
|
||||||
);
|
|
||||||
|
@ -351,6 +351,7 @@ describe('adhoc actions', () => {
|
|||||||
describe('when initAdHocVariableEditor is dispatched', () => {
|
describe('when initAdHocVariableEditor is dispatched', () => {
|
||||||
it('then correct actions are dispatched', async () => {
|
it('then correct actions are dispatched', async () => {
|
||||||
const datasources = [
|
const datasources = [
|
||||||
|
{ ...createDatasource('default', true), value: null },
|
||||||
createDatasource('elasticsearch-v1'),
|
createDatasource('elasticsearch-v1'),
|
||||||
createDatasource('loki', false),
|
createDatasource('loki', false),
|
||||||
createDatasource('influx'),
|
createDatasource('influx'),
|
||||||
@ -367,6 +368,7 @@ describe('adhoc actions', () => {
|
|||||||
|
|
||||||
const expectedDatasources = [
|
const expectedDatasources = [
|
||||||
{ text: '', value: '' },
|
{ text: '', value: '' },
|
||||||
|
{ text: 'default (default)', value: null },
|
||||||
{ text: 'elasticsearch-v1', value: 'elasticsearch-v1' },
|
{ text: 'elasticsearch-v1', value: 'elasticsearch-v1' },
|
||||||
{ text: 'influx', value: 'influx' },
|
{ text: 'influx', value: 'influx' },
|
||||||
{ text: 'elasticsearch-v7', value: 'elasticsearch-v7' },
|
{ text: 'elasticsearch-v7', value: 'elasticsearch-v7' },
|
||||||
|
@ -80,7 +80,7 @@ export const setFiltersFromUrl = (id: string, filters: AdHocVariableFilter[]): T
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const changeVariableDatasource = (datasource: string): ThunkResult<void> => {
|
export const changeVariableDatasource = (datasource?: string): ThunkResult<void> => {
|
||||||
return async (dispatch, getState) => {
|
return async (dispatch, getState) => {
|
||||||
const { editor } = getState().templating;
|
const { editor } = getState().templating;
|
||||||
const variable = getVariable(editor.id, getState());
|
const variable = getVariable(editor.id, getState());
|
||||||
@ -111,15 +111,13 @@ export const changeVariableDatasource = (datasource: string): ThunkResult<void>
|
|||||||
export const initAdHocVariableEditor = (): ThunkResult<void> => (dispatch) => {
|
export const initAdHocVariableEditor = (): ThunkResult<void> => (dispatch) => {
|
||||||
const dataSources = getDatasourceSrv().getMetricSources();
|
const dataSources = getDatasourceSrv().getMetricSources();
|
||||||
const selectable = dataSources.reduce(
|
const selectable = dataSources.reduce(
|
||||||
(all: Array<{ text: string; value: string }>, ds) => {
|
(all: Array<{ text: string; value: string | null }>, ds) => {
|
||||||
if (ds.meta.mixed || ds.value === null) {
|
if (ds.meta.mixed) {
|
||||||
return all;
|
return all;
|
||||||
}
|
}
|
||||||
|
|
||||||
all.push({
|
const text = ds.value === null ? `${ds.name} (default)` : ds.name;
|
||||||
text: ds.name,
|
all.push({ text: text, value: ds.value });
|
||||||
value: ds.value,
|
|
||||||
});
|
|
||||||
|
|
||||||
return all;
|
return all;
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user