stateSaveEpic.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { Epic } from 'redux-observable';
  2. import { mergeMap } from 'rxjs/operators';
  3. import { RawTimeRange, TimeRange } from '@grafana/ui/src/types/time';
  4. import { isDateTime } from '@grafana/ui/src/utils/moment_wrapper';
  5. import { ActionOf } from 'app/core/redux/actionCreatorFactory';
  6. import { StoreState } from 'app/types/store';
  7. import { ExploreUrlState, ExploreId } from 'app/types/explore';
  8. import { clearQueryKeys, serializeStateToUrlParam } from 'app/core/utils/explore';
  9. import { updateLocation } from 'app/core/actions/location';
  10. import { setUrlReplacedAction, stateSaveAction } from '../actionTypes';
  11. const toRawTimeRange = (range: TimeRange): RawTimeRange => {
  12. let from = range.raw.from;
  13. if (isDateTime(from)) {
  14. from = from.valueOf().toString(10);
  15. }
  16. let to = range.raw.to;
  17. if (isDateTime(to)) {
  18. to = to.valueOf().toString(10);
  19. }
  20. return {
  21. from,
  22. to,
  23. };
  24. };
  25. export const stateSaveEpic: Epic<ActionOf<any>, ActionOf<any>, StoreState> = (action$, state$) => {
  26. return action$.ofType(stateSaveAction.type).pipe(
  27. mergeMap(() => {
  28. const { left, right, split } = state$.value.explore;
  29. const replace = left && left.urlReplaced === false;
  30. const urlStates: { [index: string]: string } = {};
  31. const leftUrlState: ExploreUrlState = {
  32. datasource: left.datasourceInstance.name,
  33. queries: left.queries.map(clearQueryKeys),
  34. range: toRawTimeRange(left.range),
  35. mode: left.mode,
  36. ui: {
  37. showingGraph: left.showingGraph,
  38. showingLogs: true,
  39. showingTable: left.showingTable,
  40. dedupStrategy: left.dedupStrategy,
  41. },
  42. };
  43. urlStates.left = serializeStateToUrlParam(leftUrlState, true);
  44. if (split) {
  45. const rightUrlState: ExploreUrlState = {
  46. datasource: right.datasourceInstance.name,
  47. queries: right.queries.map(clearQueryKeys),
  48. range: toRawTimeRange(right.range),
  49. mode: right.mode,
  50. ui: {
  51. showingGraph: right.showingGraph,
  52. showingLogs: true,
  53. showingTable: right.showingTable,
  54. dedupStrategy: right.dedupStrategy,
  55. },
  56. };
  57. urlStates.right = serializeStateToUrlParam(rightUrlState, true);
  58. }
  59. const actions: Array<ActionOf<any>> = [updateLocation({ query: urlStates, replace })];
  60. if (replace) {
  61. actions.push(setUrlReplacedAction({ exploreId: ExploreId.left }));
  62. }
  63. return actions;
  64. })
  65. );
  66. };