stateSaveEpic.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { Epic } from 'redux-observable';
  2. import { mergeMap } from 'rxjs/operators';
  3. import { RawTimeRange, TimeRange } from '@grafana/data';
  4. import { isDateTime } from '@grafana/data';
  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 orgId = state$.value.user.orgId.toString();
  30. const replace = left && left.urlReplaced === false;
  31. const urlStates: { [index: string]: string } = { orgId };
  32. const leftUrlState: ExploreUrlState = {
  33. datasource: left.datasourceInstance.name,
  34. queries: left.queries.map(clearQueryKeys),
  35. range: toRawTimeRange(left.range),
  36. mode: left.mode,
  37. ui: {
  38. showingGraph: left.showingGraph,
  39. showingLogs: true,
  40. showingTable: left.showingTable,
  41. dedupStrategy: left.dedupStrategy,
  42. },
  43. };
  44. urlStates.left = serializeStateToUrlParam(leftUrlState, true);
  45. if (split) {
  46. const rightUrlState: ExploreUrlState = {
  47. datasource: right.datasourceInstance.name,
  48. queries: right.queries.map(clearQueryKeys),
  49. range: toRawTimeRange(right.range),
  50. mode: right.mode,
  51. ui: {
  52. showingGraph: right.showingGraph,
  53. showingLogs: true,
  54. showingTable: right.showingTable,
  55. dedupStrategy: right.dedupStrategy,
  56. },
  57. };
  58. urlStates.right = serializeStateToUrlParam(rightUrlState, true);
  59. }
  60. const actions: Array<ActionOf<any>> = [updateLocation({ query: urlStates, replace })];
  61. if (replace) {
  62. actions.push(setUrlReplacedAction({ exploreId: ExploreId.left }));
  63. }
  64. return actions;
  65. })
  66. );
  67. };