2020-03-16 00:32:04 -05:00
|
|
|
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
|
2020-10-02 00:02:06 -05:00
|
|
|
import { DataSourceVariableModel, initialVariableModelState, VariableOption, VariableRefresh } from '../types';
|
|
|
|
import { ALL_VARIABLE_TEXT, ALL_VARIABLE_VALUE, getInstanceState, VariablePayload } from '../state/types';
|
2020-03-16 00:32:04 -05:00
|
|
|
import { initialVariablesState, VariablesState } from '../state/variablesReducer';
|
|
|
|
import { DataSourceSelectItem } from '@grafana/data';
|
|
|
|
|
|
|
|
export interface DataSourceVariableEditorState {
|
|
|
|
dataSourceTypes: Array<{ text: string; value: string }>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const initialDataSourceVariableModelState: DataSourceVariableModel = {
|
2020-10-02 00:02:06 -05:00
|
|
|
...initialVariableModelState,
|
2020-03-16 00:32:04 -05:00
|
|
|
type: 'datasource',
|
|
|
|
current: {} as VariableOption,
|
|
|
|
regex: '',
|
|
|
|
options: [],
|
|
|
|
query: '',
|
|
|
|
multi: false,
|
|
|
|
includeAll: false,
|
|
|
|
refresh: VariableRefresh.onDashboardLoad,
|
|
|
|
};
|
|
|
|
|
|
|
|
export const dataSourceVariableSlice = createSlice({
|
|
|
|
name: 'templating/datasource',
|
|
|
|
initialState: initialVariablesState,
|
|
|
|
reducers: {
|
|
|
|
createDataSourceOptions: (
|
|
|
|
state: VariablesState,
|
|
|
|
action: PayloadAction<VariablePayload<{ sources: DataSourceSelectItem[]; regex: RegExp | undefined }>>
|
|
|
|
) => {
|
|
|
|
const { sources, regex } = action.payload.data;
|
|
|
|
const options: VariableOption[] = [];
|
2020-03-23 07:45:08 -05:00
|
|
|
const instanceState = getInstanceState<DataSourceVariableModel>(state, action.payload.id);
|
2020-03-16 00:32:04 -05:00
|
|
|
for (let i = 0; i < sources.length; i++) {
|
|
|
|
const source = sources[i];
|
|
|
|
// must match on type
|
|
|
|
if (source.meta.id !== instanceState.query) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (regex && !regex.exec(source.name)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
options.push({ text: source.name, value: source.name, selected: false });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.length === 0) {
|
|
|
|
options.push({ text: 'No data sources found', value: '', selected: false });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (instanceState.includeAll) {
|
|
|
|
options.unshift({ text: ALL_VARIABLE_TEXT, value: ALL_VARIABLE_VALUE, selected: false });
|
|
|
|
}
|
|
|
|
|
|
|
|
instanceState.options = options;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
export const dataSourceVariableReducer = dataSourceVariableSlice.reducer;
|
|
|
|
export const { createDataSourceOptions } = dataSourceVariableSlice.actions;
|