timeEpic.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { Epic } from 'redux-observable';
  2. import { map } from 'rxjs/operators';
  3. import { AbsoluteTimeRange, RawTimeRange } from '@grafana/data';
  4. import { ActionOf } from 'app/core/redux/actionCreatorFactory';
  5. import { StoreState } from 'app/types/store';
  6. import { updateTimeRangeAction, UpdateTimeRangePayload, changeRangeAction } from '../actionTypes';
  7. export const timeEpic: Epic<ActionOf<any>, ActionOf<any>, StoreState> = (
  8. action$,
  9. state$,
  10. { getTimeSrv, getTimeRange, getTimeZone, toUtc, dateTime }
  11. ) => {
  12. return action$.ofType(updateTimeRangeAction.type).pipe(
  13. map((action: ActionOf<UpdateTimeRangePayload>) => {
  14. const { exploreId, absoluteRange: absRange, rawRange: actionRange } = action.payload;
  15. const itemState = state$.value.explore[exploreId];
  16. const timeZone = getTimeZone(state$.value.user);
  17. const { range: rangeInState } = itemState;
  18. let rawRange: RawTimeRange = rangeInState.raw;
  19. if (absRange) {
  20. rawRange = {
  21. from: timeZone.isUtc ? toUtc(absRange.from) : dateTime(absRange.from),
  22. to: timeZone.isUtc ? toUtc(absRange.to) : dateTime(absRange.to),
  23. };
  24. }
  25. if (actionRange) {
  26. rawRange = actionRange;
  27. }
  28. const range = getTimeRange(timeZone, rawRange);
  29. const absoluteRange: AbsoluteTimeRange = { from: range.from.valueOf(), to: range.to.valueOf() };
  30. getTimeSrv().init({
  31. time: range.raw,
  32. refresh: false,
  33. getTimezone: () => timeZone.raw,
  34. timeRangeUpdated: (): any => undefined,
  35. });
  36. return changeRangeAction({ exploreId, range, absoluteRange });
  37. })
  38. );
  39. };