mirror of
https://github.com/grafana/grafana.git
synced 2025-02-12 08:35:43 -06:00
* Performance: Standardize lodash imports to use destructured members Changes lodash imports of the form `import x from 'lodash/x'` to `import { x } from 'lodash'` to reduce bundle size. * Remove unnecessary _ import from Graph component * Enforce lodash import style * Fix remaining lodash imports
55 lines
2.0 KiB
TypeScript
55 lines
2.0 KiB
TypeScript
import { chain } from 'lodash';
|
|
import { getTemplateSrv } from '@grafana/runtime';
|
|
import { stringToJsRegex } from '@grafana/data';
|
|
|
|
import { toVariablePayload, VariableIdentifier } from '../state/types';
|
|
import { ThunkResult } from '../../../types';
|
|
import { createDataSourceOptions } from './reducer';
|
|
import { validateVariableSelectionState } from '../state/actions';
|
|
import { getDatasourceSrv } from '../../plugins/datasource_srv';
|
|
import { getVariable } from '../state/selectors';
|
|
import { DataSourceVariableModel } from '../types';
|
|
import { changeVariableEditorExtended } from '../editor/reducer';
|
|
|
|
export interface DataSourceVariableActionDependencies {
|
|
getDatasourceSrv: typeof getDatasourceSrv;
|
|
}
|
|
|
|
export const updateDataSourceVariableOptions = (
|
|
identifier: VariableIdentifier,
|
|
dependencies: DataSourceVariableActionDependencies = { getDatasourceSrv: getDatasourceSrv }
|
|
): ThunkResult<void> => async (dispatch, getState) => {
|
|
const sources = dependencies.getDatasourceSrv().getList({ metrics: true, variables: false });
|
|
const variableInState = getVariable<DataSourceVariableModel>(identifier.id, getState());
|
|
let regex;
|
|
|
|
if (variableInState.regex) {
|
|
regex = getTemplateSrv().replace(variableInState.regex, undefined, 'regex');
|
|
regex = stringToJsRegex(regex);
|
|
}
|
|
|
|
dispatch(createDataSourceOptions(toVariablePayload(identifier, { sources, regex })));
|
|
await dispatch(validateVariableSelectionState(identifier));
|
|
};
|
|
|
|
export const initDataSourceVariableEditor = (
|
|
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(
|
|
changeVariableEditorExtended({
|
|
propName: 'dataSourceTypes',
|
|
propValue: dataSourceTypes,
|
|
})
|
|
);
|
|
};
|