stateSaveEpic.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. ui: {
  36. showingGraph: left.showingGraph,
  37. showingLogs: true,
  38. showingTable: left.showingTable,
  39. dedupStrategy: left.dedupStrategy,
  40. },
  41. };
  42. urlStates.left = serializeStateToUrlParam(leftUrlState, true);
  43. if (split) {
  44. const rightUrlState: ExploreUrlState = {
  45. datasource: right.datasourceInstance.name,
  46. queries: right.queries.map(clearQueryKeys),
  47. range: toRawTimeRange(right.range),
  48. ui: {
  49. showingGraph: right.showingGraph,
  50. showingLogs: true,
  51. showingTable: right.showingTable,
  52. dedupStrategy: right.dedupStrategy,
  53. },
  54. };
  55. urlStates.right = serializeStateToUrlParam(rightUrlState, true);
  56. }
  57. const actions: Array<ActionOf<any>> = [updateLocation({ query: urlStates, replace })];
  58. if (replace) {
  59. actions.push(setUrlReplacedAction({ exploreId: ExploreId.left }));
  60. }
  61. return actions;
  62. })
  63. );
  64. };