2020-10-02 00:02:06 -05:00
|
|
|
import { rangeUtil } from '@grafana/data';
|
2020-03-16 07:45:51 -05:00
|
|
|
|
|
|
|
import { ThunkResult } from '../../../types';
|
|
|
|
import { getTimeSrv } from '../../dashboard/services/TimeSrv';
|
2020-10-01 12:51:23 -05:00
|
|
|
import { getTemplateSrv, TemplateSrv } from '../../templating/template_srv';
|
2022-04-22 08:33:13 -05:00
|
|
|
import { validateVariableSelectionState } from '../state/actions';
|
2022-02-17 23:06:04 -06:00
|
|
|
import { toKeyedAction } from '../state/keyedVariablesReducer';
|
2022-04-22 08:33:13 -05:00
|
|
|
import { getVariable } from '../state/selectors';
|
|
|
|
import { KeyedVariableIdentifier } from '../state/types';
|
2022-02-17 23:06:04 -06:00
|
|
|
import { toVariablePayload } from '../utils';
|
2020-03-16 07:45:51 -05:00
|
|
|
|
2022-04-22 08:33:13 -05:00
|
|
|
import { createIntervalOptions } from './reducer';
|
|
|
|
|
2022-02-02 06:02:32 -06:00
|
|
|
export const updateIntervalVariableOptions =
|
2022-02-17 23:06:04 -06:00
|
|
|
(identifier: KeyedVariableIdentifier): ThunkResult<void> =>
|
2022-02-02 06:02:32 -06:00
|
|
|
async (dispatch) => {
|
2022-02-17 23:06:04 -06:00
|
|
|
const { rootStateKey } = identifier;
|
|
|
|
await dispatch(toKeyedAction(rootStateKey, createIntervalOptions(toVariablePayload(identifier))));
|
2022-02-02 06:02:32 -06:00
|
|
|
await dispatch(updateAutoValue(identifier));
|
|
|
|
await dispatch(validateVariableSelectionState(identifier));
|
|
|
|
};
|
2020-03-16 07:45:51 -05:00
|
|
|
|
|
|
|
export interface UpdateAutoValueDependencies {
|
2020-09-03 01:54:06 -05:00
|
|
|
calculateInterval: typeof rangeUtil.calculateInterval;
|
2020-03-16 07:45:51 -05:00
|
|
|
getTimeSrv: typeof getTimeSrv;
|
2020-10-01 12:51:23 -05:00
|
|
|
templateSrv: TemplateSrv;
|
2020-03-16 07:45:51 -05:00
|
|
|
}
|
|
|
|
|
2022-02-02 06:02:32 -06:00
|
|
|
export const updateAutoValue =
|
|
|
|
(
|
2022-02-17 23:06:04 -06:00
|
|
|
identifier: KeyedVariableIdentifier,
|
2022-02-02 06:02:32 -06:00
|
|
|
dependencies: UpdateAutoValueDependencies = {
|
|
|
|
calculateInterval: rangeUtil.calculateInterval,
|
|
|
|
getTimeSrv: getTimeSrv,
|
|
|
|
templateSrv: getTemplateSrv(),
|
|
|
|
}
|
|
|
|
): ThunkResult<void> =>
|
|
|
|
(dispatch, getState) => {
|
2022-08-05 07:44:52 -05:00
|
|
|
const variableInState = getVariable(identifier, getState());
|
|
|
|
if (variableInState.type !== 'interval') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-02 06:02:32 -06:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|