Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SIEM] update url state between page if date is relative #56813

Merged
merged 10 commits into from
Feb 7, 2020
100 changes: 50 additions & 50 deletions x-pack/legacy/plugins/siem/public/components/url_state/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { decode, encode, RisonValue } from 'rison-node';
import { decode, encode } from 'rison-node';
import * as H from 'history';
import { QueryString } from 'ui/utils/query_string';
import { Query, esFilters } from 'src/plugins/data/public';
Expand All @@ -24,13 +24,12 @@ import {
UpdateUrlStateString,
} from './types';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const decodeRisonUrlState = (value: string | undefined): RisonValue | any | undefined => {
export const decodeRisonUrlState = <T>(value: string | undefined): T | null => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice refactor!

try {
return value ? decode(value) : undefined;
return value ? ((decode(value) as unknown) as T) : null;
} catch (error) {
if (error instanceof Error && error.message.startsWith('rison decoder error')) {
return {};
return null;
}
throw error;
}
Expand Down Expand Up @@ -207,60 +206,61 @@ export const updateUrlStateString = ({
search,
urlKey,
}: UpdateUrlStateString): string => {
const queryState: Query | Timeline | esFilters.Filter[] | UrlInputsModel = decodeRisonUrlState(
newUrlStateString
);
if (urlKey === CONSTANTS.appQuery && queryState != null && (queryState as Query).query === '') {
return replaceStateInLocation({
history,
pathName,
search,
urlStateToReplace: '',
urlStateKey: urlKey,
});
} else if (
urlKey === CONSTANTS.timerange &&
queryState != null &&
(queryState as UrlInputsModel).global != null
) {
return replaceStateInLocation({
history,
pathName,
search,
urlStateToReplace: updateTimerangeUrl(queryState as UrlInputsModel),
urlStateKey: urlKey,
});
} else if (urlKey === CONSTANTS.filters && isEmpty(queryState)) {
return replaceStateInLocation({
history,
pathName,
search,
urlStateToReplace: '',
urlStateKey: urlKey,
});
} else if (
urlKey === CONSTANTS.timeline &&
queryState != null &&
(queryState as Timeline).id === ''
) {
return replaceStateInLocation({
history,
pathName,
search,
urlStateToReplace: '',
urlStateKey: urlKey,
});
if (urlKey === CONSTANTS.appQuery) {
const queryState = decodeRisonUrlState<Query>(newUrlStateString);
if (queryState != null && queryState.query === '') {
return replaceStateInLocation({
history,
pathName,
search,
urlStateToReplace: '',
urlStateKey: urlKey,
});
}
} else if (urlKey === CONSTANTS.timerange) {
const queryState = decodeRisonUrlState<UrlInputsModel>(newUrlStateString);
if (queryState != null && queryState.global != null) {
return replaceStateInLocation({
history,
pathName,
search,
urlStateToReplace: updateTimerangeUrl(queryState),
urlStateKey: urlKey,
});
}
} else if (urlKey === CONSTANTS.filters) {
const queryState = decodeRisonUrlState<esFilters.Filter[]>(newUrlStateString);
if (isEmpty(queryState)) {
return replaceStateInLocation({
history,
pathName,
search,
urlStateToReplace: '',
urlStateKey: urlKey,
});
}
} else if (urlKey === CONSTANTS.timeline) {
const queryState = decodeRisonUrlState<Timeline>(newUrlStateString);
if (queryState != null && queryState.id === '') {
return replaceStateInLocation({
history,
pathName,
search,
urlStateToReplace: '',
urlStateKey: urlKey,
});
}
}
return search;
};

export const replaceStateInLocation = ({
export const replaceStateInLocation = <T>({
history,
urlStateToReplace,
urlStateKey,
pathName,
search,
}: ReplaceStateInLocation) => {
}: ReplaceStateInLocation<T>) => {
const newLocation = replaceQueryStringInLocation(
{
hash: '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { savedQueryService, siemFilterManager } from '../search_bar';
import { CONSTANTS } from './constants';
import { decodeRisonUrlState } from './helpers';
import { normalizeTimeRange } from './normalize_time_range';
import { DispatchSetInitialStateFromUrl, SetInitialStateFromUrl } from './types';
import { DispatchSetInitialStateFromUrl, SetInitialStateFromUrl, Timeline } from './types';
import { queryTimelineById } from '../open_timeline/helpers';

export const dispatchSetInitialStateFromUrl = (
Expand All @@ -38,80 +38,10 @@ export const dispatchSetInitialStateFromUrl = (
}: SetInitialStateFromUrl<unknown>): (() => void) => () => {
urlStateToUpdate.forEach(({ urlKey, newUrlStateString }) => {
if (urlKey === CONSTANTS.timerange) {
const timerangeStateData: UrlInputsModel = decodeRisonUrlState(newUrlStateString);

const globalId: InputsModelId = 'global';
const globalLinkTo: LinkTo = { linkTo: get('global.linkTo', timerangeStateData) };
const globalType: TimeRangeKinds = get('global.timerange.kind', timerangeStateData);

const timelineId: InputsModelId = 'timeline';
const timelineLinkTo: LinkTo = { linkTo: get('timeline.linkTo', timerangeStateData) };
const timelineType: TimeRangeKinds = get('timeline.timerange.kind', timerangeStateData);

if (isEmpty(globalLinkTo.linkTo)) {
dispatch(inputsActions.removeGlobalLinkTo());
} else {
dispatch(inputsActions.addGlobalLinkTo({ linkToId: 'timeline' }));
}

if (isEmpty(timelineLinkTo.linkTo)) {
dispatch(inputsActions.removeTimelineLinkTo());
} else {
dispatch(inputsActions.addTimelineLinkTo({ linkToId: 'global' }));
}

if (timelineType) {
if (timelineType === 'absolute') {
const absoluteRange = normalizeTimeRange<AbsoluteTimeRange>(
get('timeline.timerange', timerangeStateData)
);
dispatch(
inputsActions.setAbsoluteRangeDatePicker({
...absoluteRange,
id: timelineId,
})
);
}
if (timelineType === 'relative') {
const relativeRange = normalizeTimeRange<RelativeTimeRange>(
get('timeline.timerange', timerangeStateData)
);
dispatch(
inputsActions.setRelativeRangeDatePicker({
...relativeRange,
id: timelineId,
})
);
}
}

if (globalType) {
if (globalType === 'absolute') {
const absoluteRange = normalizeTimeRange<AbsoluteTimeRange>(
get('global.timerange', timerangeStateData)
);
dispatch(
inputsActions.setAbsoluteRangeDatePicker({
...absoluteRange,
id: globalId,
})
);
}
if (globalType === 'relative') {
const relativeRange = normalizeTimeRange<RelativeTimeRange>(
get('global.timerange', timerangeStateData)
);
dispatch(
inputsActions.setRelativeRangeDatePicker({
...relativeRange,
id: globalId,
})
);
}
}
updateTimerange(newUrlStateString, dispatch);
}
if (urlKey === CONSTANTS.appQuery && indexPattern != null) {
const appQuery: Query = decodeRisonUrlState(newUrlStateString);
const appQuery = decodeRisonUrlState<Query>(newUrlStateString);
if (appQuery != null) {
dispatch(
inputsActions.setFilterQuery({
Expand All @@ -124,13 +54,13 @@ export const dispatchSetInitialStateFromUrl = (
}

if (urlKey === CONSTANTS.filters) {
const filters: esFilters.Filter[] = decodeRisonUrlState(newUrlStateString);
const filters = decodeRisonUrlState<esFilters.Filter[]>(newUrlStateString);
siemFilterManager.setFilters(filters || []);
}

if (urlKey === CONSTANTS.savedQuery) {
const savedQueryId: string = decodeRisonUrlState(newUrlStateString);
if (savedQueryId !== '') {
const savedQueryId = decodeRisonUrlState<string>(newUrlStateString);
if (savedQueryId != null && savedQueryId !== '') {
savedQueryService.getSavedQuery(savedQueryId).then((savedQueryData: SavedQuery) => {
siemFilterManager.setFilters(savedQueryData.attributes.filters || []);
dispatch(
Expand All @@ -145,7 +75,7 @@ export const dispatchSetInitialStateFromUrl = (
}

if (urlKey === CONSTANTS.timeline) {
const timeline = decodeRisonUrlState(newUrlStateString);
const timeline = decodeRisonUrlState<Timeline>(newUrlStateString);
if (timeline != null && timeline.id !== '') {
queryTimelineById({
apolloClient,
Expand All @@ -159,3 +89,77 @@ export const dispatchSetInitialStateFromUrl = (
}
});
};

const updateTimerange = (newUrlStateString: string, dispatch: Dispatch) => {
const timerangeStateData = decodeRisonUrlState<UrlInputsModel>(newUrlStateString);

const globalId: InputsModelId = 'global';
const globalLinkTo: LinkTo = { linkTo: get('global.linkTo', timerangeStateData) };
const globalType: TimeRangeKinds = get('global.timerange.kind', timerangeStateData);

const timelineId: InputsModelId = 'timeline';
const timelineLinkTo: LinkTo = { linkTo: get('timeline.linkTo', timerangeStateData) };
const timelineType: TimeRangeKinds = get('timeline.timerange.kind', timerangeStateData);

if (isEmpty(globalLinkTo.linkTo)) {
dispatch(inputsActions.removeGlobalLinkTo());
} else {
dispatch(inputsActions.addGlobalLinkTo({ linkToId: 'timeline' }));
}

if (isEmpty(timelineLinkTo.linkTo)) {
dispatch(inputsActions.removeTimelineLinkTo());
} else {
dispatch(inputsActions.addTimelineLinkTo({ linkToId: 'global' }));
}

if (timelineType) {
if (timelineType === 'absolute') {
const absoluteRange = normalizeTimeRange<AbsoluteTimeRange>(
get('timeline.timerange', timerangeStateData)
);
dispatch(
inputsActions.setAbsoluteRangeDatePicker({
...absoluteRange,
id: timelineId,
})
);
}
if (timelineType === 'relative') {
const relativeRange = normalizeTimeRange<RelativeTimeRange>(
get('timeline.timerange', timerangeStateData)
);
dispatch(
inputsActions.setRelativeRangeDatePicker({
...relativeRange,
id: timelineId,
})
);
}
}

if (globalType) {
if (globalType === 'absolute') {
const absoluteRange = normalizeTimeRange<AbsoluteTimeRange>(
get('global.timerange', timerangeStateData)
);
dispatch(
inputsActions.setAbsoluteRangeDatePicker({
...absoluteRange,
id: globalId,
})
);
}
if (globalType === 'relative') {
const relativeRange = normalizeTimeRange<RelativeTimeRange>(
get('global.timerange', timerangeStateData)
);
dispatch(
inputsActions.setRelativeRangeDatePicker({
...relativeRange,
id: globalId,
})
);
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ export type DispatchSetInitialStateFromUrl = <TCache>({
urlStateToUpdate,
}: SetInitialStateFromUrl<TCache>) => () => void;

export interface ReplaceStateInLocation {
export interface ReplaceStateInLocation<T> {
history?: H.History;
urlStateToReplace: UrlInputsModel | Query | esFilters.Filter[] | Timeline | string;
urlStateToReplace: T;
urlStateKey: string;
pathName: string;
search: string;
Expand Down