mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Variables: Adds back default option for data source variable (#31208)
* Variables: Adds back default option for data source variable * Chore: updates after PR comments
This commit is contained in:
parent
d27a72f859
commit
f993f2c7cc
@ -594,6 +594,9 @@ export interface DataSourceInstanceSettings<T extends DataSourceJsonData = DataS
|
|||||||
withCredentials?: boolean;
|
withCredentials?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated -- use {@link DataSourceInstanceSettings} instead
|
||||||
|
*/
|
||||||
export interface DataSourceSelectItem {
|
export interface DataSourceSelectItem {
|
||||||
name: string;
|
name: string;
|
||||||
value: string | null;
|
value: string | null;
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
import { DataSourceInstanceSettings } from '@grafana/data';
|
||||||
|
|
||||||
import { reduxTester } from '../../../../test/core/redux/reduxTester';
|
import { reduxTester } from '../../../../test/core/redux/reduxTester';
|
||||||
import { TemplatingState } from '../state/reducers';
|
import { TemplatingState } from '../state/reducers';
|
||||||
import { getRootReducer } from '../state/helpers';
|
import { getRootReducer } from '../state/helpers';
|
||||||
@ -9,12 +11,12 @@ import {
|
|||||||
initDataSourceVariableEditor,
|
initDataSourceVariableEditor,
|
||||||
updateDataSourceVariableOptions,
|
updateDataSourceVariableOptions,
|
||||||
} from './actions';
|
} from './actions';
|
||||||
import { DataSourceInstanceSettings, DataSourceJsonData, DataSourcePluginMeta } from '@grafana/data';
|
|
||||||
import { getMockPlugin } from '../../plugins/__mocks__/pluginMocks';
|
import { getMockPlugin } from '../../plugins/__mocks__/pluginMocks';
|
||||||
import { createDataSourceOptions } from './reducer';
|
import { createDataSourceOptions } from './reducer';
|
||||||
import { addVariable, setCurrentVariableValue } from '../state/sharedReducer';
|
import { addVariable, setCurrentVariableValue } from '../state/sharedReducer';
|
||||||
import { changeVariableEditorExtended } from '../editor/reducer';
|
import { changeVariableEditorExtended } from '../editor/reducer';
|
||||||
import { datasourceBuilder } from '../shared/testing/builders';
|
import { datasourceBuilder } from '../shared/testing/builders';
|
||||||
|
import { getDataSourceInstanceSetting } from '../shared/testing/helpers';
|
||||||
|
|
||||||
interface Args {
|
interface Args {
|
||||||
sources?: DataSourceInstanceSettings[];
|
sources?: DataSourceInstanceSettings[];
|
||||||
@ -62,10 +64,7 @@ describe('data source actions', () => {
|
|||||||
toVariablePayload(
|
toVariablePayload(
|
||||||
{ type: 'datasource', id: '0' },
|
{ type: 'datasource', id: '0' },
|
||||||
{
|
{
|
||||||
sources: [
|
sources,
|
||||||
{ name: 'first-name', value: 'first-name', meta },
|
|
||||||
{ name: 'second-name', value: 'second-name', meta },
|
|
||||||
],
|
|
||||||
regex: (undefined as unknown) as RegExp,
|
regex: (undefined as unknown) as RegExp,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -113,10 +112,7 @@ describe('data source actions', () => {
|
|||||||
toVariablePayload(
|
toVariablePayload(
|
||||||
{ type: 'datasource', id: '0' },
|
{ type: 'datasource', id: '0' },
|
||||||
{
|
{
|
||||||
sources: [
|
sources,
|
||||||
{ name: 'first-name', value: 'first-name', meta },
|
|
||||||
{ name: 'second-name', value: 'second-name', meta },
|
|
||||||
],
|
|
||||||
regex: /.*(second-name).*/,
|
regex: /.*(second-name).*/,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -165,14 +161,3 @@ describe('data source actions', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
function getDataSourceInstanceSetting(name: string, meta: DataSourcePluginMeta): DataSourceInstanceSettings {
|
|
||||||
return {
|
|
||||||
id: 1,
|
|
||||||
uid: '',
|
|
||||||
type: '',
|
|
||||||
name,
|
|
||||||
meta,
|
|
||||||
jsonData: ({} as unknown) as DataSourceJsonData,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
@ -1,13 +1,14 @@
|
|||||||
|
import _ from 'lodash';
|
||||||
|
import { getTemplateSrv } from '@grafana/runtime';
|
||||||
|
import { stringToJsRegex } from '@grafana/data';
|
||||||
|
|
||||||
import { toVariablePayload, VariableIdentifier } from '../state/types';
|
import { toVariablePayload, VariableIdentifier } from '../state/types';
|
||||||
import { ThunkResult } from '../../../types';
|
import { ThunkResult } from '../../../types';
|
||||||
import { createDataSourceOptions } from './reducer';
|
import { createDataSourceOptions } from './reducer';
|
||||||
import { validateVariableSelectionState } from '../state/actions';
|
import { validateVariableSelectionState } from '../state/actions';
|
||||||
import { DataSourceInstanceSettings, stringToJsRegex } from '@grafana/data';
|
|
||||||
import { getDatasourceSrv } from '../../plugins/datasource_srv';
|
import { getDatasourceSrv } from '../../plugins/datasource_srv';
|
||||||
import { getVariable } from '../state/selectors';
|
import { getVariable } from '../state/selectors';
|
||||||
import { DataSourceVariableModel } from '../types';
|
import { DataSourceVariableModel } from '../types';
|
||||||
import { getTemplateSrv } from '@grafana/runtime';
|
|
||||||
import _ from 'lodash';
|
|
||||||
import { changeVariableEditorExtended } from '../editor/reducer';
|
import { changeVariableEditorExtended } from '../editor/reducer';
|
||||||
|
|
||||||
export interface DataSourceVariableActionDependencies {
|
export interface DataSourceVariableActionDependencies {
|
||||||
@ -18,10 +19,7 @@ export const updateDataSourceVariableOptions = (
|
|||||||
identifier: VariableIdentifier,
|
identifier: VariableIdentifier,
|
||||||
dependencies: DataSourceVariableActionDependencies = { getDatasourceSrv: getDatasourceSrv }
|
dependencies: DataSourceVariableActionDependencies = { getDatasourceSrv: getDatasourceSrv }
|
||||||
): ThunkResult<void> => async (dispatch, getState) => {
|
): ThunkResult<void> => async (dispatch, getState) => {
|
||||||
const sources = dependencies
|
const sources = dependencies.getDatasourceSrv().getList({ metrics: true, variables: false });
|
||||||
.getDatasourceSrv()
|
|
||||||
.getList({ metrics: true, variables: false })
|
|
||||||
.map(toDataSourceSelectItem);
|
|
||||||
const variableInState = getVariable<DataSourceVariableModel>(identifier.id, getState());
|
const variableInState = getVariable<DataSourceVariableModel>(identifier.id, getState());
|
||||||
let regex;
|
let regex;
|
||||||
|
|
||||||
@ -37,10 +35,7 @@ export const updateDataSourceVariableOptions = (
|
|||||||
export const initDataSourceVariableEditor = (
|
export const initDataSourceVariableEditor = (
|
||||||
dependencies: DataSourceVariableActionDependencies = { getDatasourceSrv: getDatasourceSrv }
|
dependencies: DataSourceVariableActionDependencies = { getDatasourceSrv: getDatasourceSrv }
|
||||||
): ThunkResult<void> => (dispatch) => {
|
): ThunkResult<void> => (dispatch) => {
|
||||||
const dataSources = dependencies
|
const dataSources = dependencies.getDatasourceSrv().getList({ metrics: true, variables: true });
|
||||||
.getDatasourceSrv()
|
|
||||||
.getList({ metrics: true, variables: true })
|
|
||||||
.map(toDataSourceSelectItem);
|
|
||||||
const dataSourceTypes = _(dataSources)
|
const dataSourceTypes = _(dataSources)
|
||||||
.uniqBy('meta.id')
|
.uniqBy('meta.id')
|
||||||
.map((ds: any) => {
|
.map((ds: any) => {
|
||||||
@ -57,11 +52,3 @@ export const initDataSourceVariableEditor = (
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
function toDataSourceSelectItem(setting: DataSourceInstanceSettings) {
|
|
||||||
return {
|
|
||||||
name: setting.name,
|
|
||||||
value: setting.name,
|
|
||||||
meta: setting.meta,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
import { DataSourceInstanceSettings } from '@grafana/data';
|
||||||
|
|
||||||
import { reducerTester } from '../../../../test/core/redux/reducerTester';
|
import { reducerTester } from '../../../../test/core/redux/reducerTester';
|
||||||
import { VariablesState } from '../state/variablesReducer';
|
import { VariablesState } from '../state/variablesReducer';
|
||||||
import { createDataSourceOptions, dataSourceVariableReducer } from './reducer';
|
import { createDataSourceOptions, dataSourceVariableReducer } from './reducer';
|
||||||
@ -5,20 +7,15 @@ import { DataSourceVariableModel } from '../types';
|
|||||||
import { getVariableTestContext } from '../state/helpers';
|
import { getVariableTestContext } from '../state/helpers';
|
||||||
import cloneDeep from 'lodash/cloneDeep';
|
import cloneDeep from 'lodash/cloneDeep';
|
||||||
import { createDataSourceVariableAdapter } from './adapter';
|
import { createDataSourceVariableAdapter } from './adapter';
|
||||||
import { DataSourceSelectItem } from '@grafana/data';
|
|
||||||
import { toVariablePayload } from '../state/types';
|
import { toVariablePayload } from '../state/types';
|
||||||
import { getMockPlugins } from '../../plugins/__mocks__/pluginMocks';
|
import { getMockPlugins } from '../../plugins/__mocks__/pluginMocks';
|
||||||
|
import { getDataSourceInstanceSetting } from '../shared/testing/helpers';
|
||||||
|
|
||||||
describe('dataSourceVariableReducer', () => {
|
describe('dataSourceVariableReducer', () => {
|
||||||
const adapter = createDataSourceVariableAdapter();
|
const adapter = createDataSourceVariableAdapter();
|
||||||
describe('when createDataSourceOptions is dispatched', () => {
|
describe('when createDataSourceOptions is dispatched', () => {
|
||||||
const plugins = getMockPlugins(3);
|
const plugins = getMockPlugins(3);
|
||||||
const sources: DataSourceSelectItem[] = plugins.map((p) => ({
|
const sources: DataSourceInstanceSettings[] = plugins.map((p) => getDataSourceInstanceSetting(p.name, p));
|
||||||
name: p.name,
|
|
||||||
value: `${p.name} value`,
|
|
||||||
meta: p,
|
|
||||||
sort: '',
|
|
||||||
}));
|
|
||||||
|
|
||||||
it.each`
|
it.each`
|
||||||
query | regex | includeAll | expected
|
query | regex | includeAll | expected
|
||||||
@ -49,4 +46,32 @@ describe('dataSourceVariableReducer', () => {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('when createDataSourceOptions is dispatched and item is default data source', () => {
|
||||||
|
it('then the state should include an extra default option', () => {
|
||||||
|
const plugins = getMockPlugins(3);
|
||||||
|
const sources: DataSourceInstanceSettings[] = plugins.map((p) => getDataSourceInstanceSetting(p.name, p));
|
||||||
|
sources[1].isDefault = true;
|
||||||
|
|
||||||
|
const { initialState } = getVariableTestContext<DataSourceVariableModel>(adapter, {
|
||||||
|
query: sources[1].meta.id,
|
||||||
|
includeAll: false,
|
||||||
|
});
|
||||||
|
const payload = toVariablePayload({ id: '0', type: 'datasource' }, { sources, regex: undefined });
|
||||||
|
|
||||||
|
reducerTester<VariablesState>()
|
||||||
|
.givenReducer(dataSourceVariableReducer, cloneDeep(initialState))
|
||||||
|
.whenActionIsDispatched(createDataSourceOptions(payload))
|
||||||
|
.thenStateShouldEqual({
|
||||||
|
...initialState,
|
||||||
|
['0']: ({
|
||||||
|
...initialState['0'],
|
||||||
|
options: [
|
||||||
|
{ text: 'pretty cool plugin-1', value: 'pretty cool plugin-1', selected: false },
|
||||||
|
{ text: 'default', value: 'default', selected: false },
|
||||||
|
],
|
||||||
|
} as unknown) as DataSourceVariableModel,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
|
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
|
||||||
|
import { DataSourceInstanceSettings } from '@grafana/data';
|
||||||
|
|
||||||
import { DataSourceVariableModel, initialVariableModelState, VariableOption, VariableRefresh } from '../types';
|
import { DataSourceVariableModel, initialVariableModelState, VariableOption, VariableRefresh } from '../types';
|
||||||
import { ALL_VARIABLE_TEXT, ALL_VARIABLE_VALUE, getInstanceState, VariablePayload } from '../state/types';
|
import { ALL_VARIABLE_TEXT, ALL_VARIABLE_VALUE, getInstanceState, VariablePayload } from '../state/types';
|
||||||
import { initialVariablesState, VariablesState } from '../state/variablesReducer';
|
import { initialVariablesState, VariablesState } from '../state/variablesReducer';
|
||||||
import { DataSourceSelectItem } from '@grafana/data';
|
|
||||||
|
|
||||||
export interface DataSourceVariableEditorState {
|
export interface DataSourceVariableEditorState {
|
||||||
dataSourceTypes: Array<{ text: string; value: string }>;
|
dataSourceTypes: Array<{ text: string; value: string }>;
|
||||||
@ -26,7 +27,7 @@ export const dataSourceVariableSlice = createSlice({
|
|||||||
reducers: {
|
reducers: {
|
||||||
createDataSourceOptions: (
|
createDataSourceOptions: (
|
||||||
state: VariablesState,
|
state: VariablesState,
|
||||||
action: PayloadAction<VariablePayload<{ sources: DataSourceSelectItem[]; regex: RegExp | undefined }>>
|
action: PayloadAction<VariablePayload<{ sources: DataSourceInstanceSettings[]; regex: RegExp | undefined }>>
|
||||||
) => {
|
) => {
|
||||||
const { sources, regex } = action.payload.data;
|
const { sources, regex } = action.payload.data;
|
||||||
const options: VariableOption[] = [];
|
const options: VariableOption[] = [];
|
||||||
@ -43,6 +44,10 @@ export const dataSourceVariableSlice = createSlice({
|
|||||||
}
|
}
|
||||||
|
|
||||||
options.push({ text: source.name, value: source.name, selected: false });
|
options.push({ text: source.name, value: source.name, selected: false });
|
||||||
|
|
||||||
|
if (source.isDefault) {
|
||||||
|
options.push({ text: 'default', value: 'default', selected: false });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.length === 0) {
|
if (options.length === 0) {
|
||||||
|
12
public/app/features/variables/shared/testing/helpers.ts
Normal file
12
public/app/features/variables/shared/testing/helpers.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import { DataSourceInstanceSettings, DataSourceJsonData, DataSourcePluginMeta } from '@grafana/data';
|
||||||
|
|
||||||
|
export function getDataSourceInstanceSetting(name: string, meta: DataSourcePluginMeta): DataSourceInstanceSettings {
|
||||||
|
return {
|
||||||
|
id: 1,
|
||||||
|
uid: '',
|
||||||
|
type: '',
|
||||||
|
name,
|
||||||
|
meta,
|
||||||
|
jsonData: ({} as unknown) as DataSourceJsonData,
|
||||||
|
};
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user