mirror of
https://github.com/grafana/grafana.git
synced 2025-09-27 09:54:09 +08:00

* Variables: turns on newVariables as default * Chore: adds default templating state * Some small refactorings to get the template_srv tests to get green when toggle is enabled by default. * Refactor: adds getVariables dependency to DashboardModel * Tests: fixes StackDriver tests * Tests: updates snapshots * Tests: updates snapshot for DashboardGrid.test.tsx * Tests: fixes DashboardModel.test.ts * fixed initDashboard tests. * renamed variable. * changed so we use the templating.list when running the migration work. * changed so we always returns the variables in sorted order. * Tests: fixed cloudwatch tests * added so we set the global template variable props. * Fixed tests and added moved logic to complete templateSrv variables. * removed unneccesary updateIndex. Co-authored-by: Marcus Andersson <marcus.andersson@grafana.com>
40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
import { StoreState } from '../../../types';
|
|
import { VariableModel } from '../../templating/types';
|
|
import { getState } from '../../../store/store';
|
|
import { NEW_VARIABLE_ID } from './types';
|
|
|
|
export const getVariable = <T extends VariableModel = VariableModel>(
|
|
id: string,
|
|
state: StoreState = getState(),
|
|
throwWhenMissing = true
|
|
): T => {
|
|
if (!state.templating.variables[id]) {
|
|
if (throwWhenMissing) {
|
|
throw new Error(`Couldn't find variable with id:${id}`);
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
return state.templating.variables[id] as T;
|
|
};
|
|
|
|
export const getFilteredVariables = (filter: (model: VariableModel) => boolean, state: StoreState = getState()) => {
|
|
return Object.values(state.templating.variables)
|
|
.filter(filter)
|
|
.sort((s1, s2) => s1.index! - s2.index!);
|
|
};
|
|
|
|
export const getVariableWithName = (name: string, state: StoreState = getState()) => {
|
|
return getVariable(name, state, false);
|
|
};
|
|
|
|
export const getVariables = (state: StoreState = getState(), includeNewVariable = false): VariableModel[] => {
|
|
return getFilteredVariables(variable => (includeNewVariable ? true : variable.id! !== NEW_VARIABLE_ID), state);
|
|
};
|
|
|
|
export type GetVariables = typeof getVariables;
|
|
|
|
export const getNewVariabelIndex = (state: StoreState = getState()): number => {
|
|
return Object.values(state.templating.variables).length;
|
|
};
|