actions.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { rangeUtil } from '@grafana/data';
  2. import { ThunkResult } from '../../../types';
  3. import { getTimeSrv } from '../../dashboard/services/TimeSrv';
  4. import { getTemplateSrv, TemplateSrv } from '../../templating/template_srv';
  5. import { validateVariableSelectionState } from '../state/actions';
  6. import { toKeyedAction } from '../state/keyedVariablesReducer';
  7. import { getVariable } from '../state/selectors';
  8. import { KeyedVariableIdentifier } from '../state/types';
  9. import { IntervalVariableModel } from '../types';
  10. import { toVariablePayload } from '../utils';
  11. import { createIntervalOptions } from './reducer';
  12. export const updateIntervalVariableOptions =
  13. (identifier: KeyedVariableIdentifier): ThunkResult<void> =>
  14. async (dispatch) => {
  15. const { rootStateKey } = identifier;
  16. await dispatch(toKeyedAction(rootStateKey, createIntervalOptions(toVariablePayload(identifier))));
  17. await dispatch(updateAutoValue(identifier));
  18. await dispatch(validateVariableSelectionState(identifier));
  19. };
  20. export interface UpdateAutoValueDependencies {
  21. calculateInterval: typeof rangeUtil.calculateInterval;
  22. getTimeSrv: typeof getTimeSrv;
  23. templateSrv: TemplateSrv;
  24. }
  25. export const updateAutoValue =
  26. (
  27. identifier: KeyedVariableIdentifier,
  28. dependencies: UpdateAutoValueDependencies = {
  29. calculateInterval: rangeUtil.calculateInterval,
  30. getTimeSrv: getTimeSrv,
  31. templateSrv: getTemplateSrv(),
  32. }
  33. ): ThunkResult<void> =>
  34. (dispatch, getState) => {
  35. const variableInState = getVariable<IntervalVariableModel>(identifier, getState());
  36. if (variableInState.auto) {
  37. const res = dependencies.calculateInterval(
  38. dependencies.getTimeSrv().timeRange(),
  39. variableInState.auto_count,
  40. variableInState.auto_min
  41. );
  42. dependencies.templateSrv.setGrafanaVariable('$__auto_interval_' + variableInState.name, res.interval);
  43. // for backward compatibility, to be removed eventually
  44. dependencies.templateSrv.setGrafanaVariable('$__auto_interval', res.interval);
  45. }
  46. };