mirror of
https://github.com/grafana/grafana.git
synced 2025-02-13 00:55:47 -06:00
* 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
55 lines
2.0 KiB
TypeScript
55 lines
2.0 KiB
TypeScript
import { rangeUtil } from '@grafana/data';
|
|
|
|
import { ThunkResult } from '../../../types';
|
|
import { getTimeSrv } from '../../dashboard/services/TimeSrv';
|
|
import { getTemplateSrv, TemplateSrv } from '../../templating/template_srv';
|
|
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 { createIntervalOptions } from './reducer';
|
|
|
|
export const updateIntervalVariableOptions =
|
|
(identifier: KeyedVariableIdentifier): ThunkResult<void> =>
|
|
async (dispatch) => {
|
|
const { rootStateKey } = identifier;
|
|
await dispatch(toKeyedAction(rootStateKey, createIntervalOptions(toVariablePayload(identifier))));
|
|
await dispatch(updateAutoValue(identifier));
|
|
await dispatch(validateVariableSelectionState(identifier));
|
|
};
|
|
|
|
export interface UpdateAutoValueDependencies {
|
|
calculateInterval: typeof rangeUtil.calculateInterval;
|
|
getTimeSrv: typeof getTimeSrv;
|
|
templateSrv: TemplateSrv;
|
|
}
|
|
|
|
export const updateAutoValue =
|
|
(
|
|
identifier: KeyedVariableIdentifier,
|
|
dependencies: UpdateAutoValueDependencies = {
|
|
calculateInterval: rangeUtil.calculateInterval,
|
|
getTimeSrv: getTimeSrv,
|
|
templateSrv: getTemplateSrv(),
|
|
}
|
|
): ThunkResult<void> =>
|
|
(dispatch, getState) => {
|
|
const variableInState = getVariable(identifier, getState());
|
|
if (variableInState.type !== 'interval') {
|
|
return;
|
|
}
|
|
|
|
if (variableInState.auto) {
|
|
const res = dependencies.calculateInterval(
|
|
dependencies.getTimeSrv().timeRange(),
|
|
variableInState.auto_count,
|
|
variableInState.auto_min
|
|
);
|
|
dependencies.templateSrv.setGrafanaVariable('$__auto_interval_' + variableInState.name, res.interval);
|
|
// for backward compatibility, to be removed eventually
|
|
dependencies.templateSrv.setGrafanaVariable('$__auto_interval', res.interval);
|
|
}
|
|
};
|