mirror of
https://github.com/grafana/grafana.git
synced 2025-02-12 08:35:43 -06:00
4b4d546e32
* wip * make diff easier to read * Update template_srv getVariables to return new TypedVariableModel * update VariableType to use the type from TypedVariableModel * tidy things up * Chore: Use type-accurate mock variables in tests * Chore: Type VariableState to use TypedVariableModel * fix typo * remove type assertion from template_srv.getVariables * use typescript/no-redeclare for compatibility with ts overloads * remove generics from getVariable() and overload it to only return undefined based on arguments * update usages of getVariable() * Fix Interval variable options picker not working
63 lines
2.2 KiB
TypeScript
63 lines
2.2 KiB
TypeScript
import { chain } from 'lodash';
|
|
|
|
import { stringToJsRegex } from '@grafana/data';
|
|
import { getTemplateSrv } from '@grafana/runtime';
|
|
|
|
import { ThunkResult } from '../../../types';
|
|
import { getDatasourceSrv } from '../../plugins/datasource_srv';
|
|
import { changeVariableEditorExtended } from '../editor/reducer';
|
|
import { validateVariableSelectionState } from '../state/actions';
|
|
import { toKeyedAction } from '../state/keyedVariablesReducer';
|
|
import { getVariable } from '../state/selectors';
|
|
import { KeyedVariableIdentifier } from '../state/types';
|
|
import { toVariablePayload } from '../utils';
|
|
|
|
import { createDataSourceOptions } from './reducer';
|
|
|
|
export interface DataSourceVariableActionDependencies {
|
|
getDatasourceSrv: typeof getDatasourceSrv;
|
|
}
|
|
|
|
export const updateDataSourceVariableOptions =
|
|
(
|
|
identifier: KeyedVariableIdentifier,
|
|
dependencies: DataSourceVariableActionDependencies = { getDatasourceSrv: getDatasourceSrv }
|
|
): ThunkResult<void> =>
|
|
async (dispatch, getState) => {
|
|
const { rootStateKey } = identifier;
|
|
const sources = dependencies.getDatasourceSrv().getList({ metrics: true, variables: false });
|
|
const variableInState = getVariable(identifier, getState());
|
|
if (variableInState.type !== 'datasource') {
|
|
return;
|
|
}
|
|
|
|
let regex;
|
|
|
|
if (variableInState.regex) {
|
|
regex = getTemplateSrv().replace(variableInState.regex, undefined, 'regex');
|
|
regex = stringToJsRegex(regex);
|
|
}
|
|
|
|
dispatch(toKeyedAction(rootStateKey, createDataSourceOptions(toVariablePayload(identifier, { sources, regex }))));
|
|
await dispatch(validateVariableSelectionState(identifier));
|
|
};
|
|
|
|
export const initDataSourceVariableEditor =
|
|
(
|
|
key: string,
|
|
dependencies: DataSourceVariableActionDependencies = { getDatasourceSrv: getDatasourceSrv }
|
|
): ThunkResult<void> =>
|
|
(dispatch) => {
|
|
const dataSources = dependencies.getDatasourceSrv().getList({ metrics: true, variables: true });
|
|
const dataSourceTypes = chain(dataSources)
|
|
.uniqBy('meta.id')
|
|
.map((ds: any) => {
|
|
return { text: ds.meta.name, value: ds.meta.id };
|
|
})
|
|
.value();
|
|
|
|
dataSourceTypes.unshift({ text: '', value: '' });
|
|
|
|
dispatch(toKeyedAction(key, changeVariableEditorExtended({ dataSourceTypes })));
|
|
};
|