2020-09-03 01:54:06 -05:00
|
|
|
import { AppEvents, rangeUtil } from '@grafana/data';
|
2020-03-16 07:45:51 -05:00
|
|
|
|
|
|
|
import { toVariablePayload, VariableIdentifier } from '../state/types';
|
|
|
|
import { ThunkResult } from '../../../types';
|
|
|
|
import { createIntervalOptions } from './reducer';
|
|
|
|
import { validateVariableSelectionState } from '../state/actions';
|
|
|
|
import { getVariable } from '../state/selectors';
|
2020-06-04 06:44:48 -05:00
|
|
|
import { IntervalVariableModel } from '../types';
|
2020-03-16 07:45:51 -05:00
|
|
|
import { getTimeSrv } from '../../dashboard/services/TimeSrv';
|
|
|
|
import templateSrv from '../../templating/template_srv';
|
|
|
|
import appEvents from '../../../core/app_events';
|
|
|
|
|
|
|
|
export interface UpdateIntervalVariableOptionsDependencies {
|
|
|
|
appEvents: typeof appEvents;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const updateIntervalVariableOptions = (
|
|
|
|
identifier: VariableIdentifier,
|
|
|
|
dependencies: UpdateIntervalVariableOptionsDependencies = { appEvents: appEvents }
|
|
|
|
): ThunkResult<void> => async dispatch => {
|
|
|
|
try {
|
|
|
|
await dispatch(createIntervalOptions(toVariablePayload(identifier)));
|
|
|
|
await dispatch(updateAutoValue(identifier));
|
|
|
|
await dispatch(validateVariableSelectionState(identifier));
|
|
|
|
} catch (error) {
|
|
|
|
dependencies.appEvents.emit(AppEvents.alertError, ['Templating', error.message]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
templateSrv: typeof templateSrv;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const updateAutoValue = (
|
|
|
|
identifier: VariableIdentifier,
|
|
|
|
dependencies: UpdateAutoValueDependencies = {
|
2020-09-03 01:54:06 -05:00
|
|
|
calculateInterval: rangeUtil.calculateInterval,
|
2020-03-16 07:45:51 -05:00
|
|
|
getTimeSrv: getTimeSrv,
|
|
|
|
templateSrv: templateSrv,
|
|
|
|
}
|
|
|
|
): ThunkResult<void> => (dispatch, getState) => {
|
2020-03-23 07:45:08 -05:00
|
|
|
const variableInState = getVariable<IntervalVariableModel>(identifier.id, getState());
|
2020-03-16 07:45:51 -05:00
|
|
|
if (variableInState.auto) {
|
2020-09-03 01:54:06 -05:00
|
|
|
const res = dependencies.calculateInterval(
|
2020-03-16 07:45:51 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|