Fixes bug with rison decode on Kibana links (#25214) (#25284)

* Fixes bug with rison decode vs encode

* Adds and updates tests for kibana link component
This commit is contained in:
Jason Rhodes 2018-11-07 11:01:28 -05:00 committed by GitHub
parent f55b39d195
commit 51b1f4febf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 21 deletions

View file

@ -10,13 +10,34 @@ exports[`RelativeLinkComponent should render correct markup 1`] = `
</a>
`;
exports[`UnconnectedKibanaLink should render correct markup 1`] = `
<a
className="euiLink euiLink--primary"
href="myBasePath/app/kibana#/discover?_a=(interval:auto,query:(language:lucene,query:'context.service.name:\\"myServiceName\\" AND error.grouping_key:\\"myGroupId\\"'),sort:('@timestamp':desc))&_g=(time:(from:now-24h,mode:quick,to:now))"
exports[`UnconnectedKibanaLink should include existing _g values in link href 1`] = `
<EuiLink
color="primary"
href="myBasePath/app/kibana#/discover?_a=(interval:auto,query:(language:lucene,query:'context.service.name:\\"myServiceName\\" AND error.grouping_key:\\"myGroupId\\"'),sort:('@timestamp':desc))&_g=(refreshInterval:(pause:!t,value:0),time:(from:now-7d,mode:relative,to:now-1d))"
type="button"
>
Go to Discover
</a>
</EuiLink>
`;
exports[`UnconnectedKibanaLink should include existing _g values in link href 2`] = `
<EuiLink
color="primary"
href="myBasePath/app/kibana#/discover?_a=(interval:auto,query:(language:lucene,query:'context.service.name:\\"myServiceName\\" AND error.grouping_key:\\"myGroupId\\"'),sort:('@timestamp':desc))&_g=H@whatever"
type="button"
>
Go to Discover
</EuiLink>
`;
exports[`UnconnectedKibanaLink should render correct markup 1`] = `
<EuiLink
color="primary"
href="myBasePath/app/kibana#/discover?_a=(interval:auto,query:(language:lucene,query:'context.service.name:\\"myServiceName\\" AND error.grouping_key:\\"myGroupId\\"'),sort:('@timestamp':desc))&_g=(time:(from:now-24h,mode:quick,to:now))"
type="button"
>
Go to Discover
</EuiLink>
`;
exports[`ViewMLJob should render component 1`] = `

View file

@ -197,7 +197,7 @@ describe('UnconnectedKibanaLink', () => {
}
};
wrapper = mount(
wrapper = shallow(
<UnconnectedKibanaLink
location={{ search: '' }}
pathname={'/app/kibana'}
@ -210,13 +210,26 @@ describe('UnconnectedKibanaLink', () => {
});
it('should have correct url', () => {
expect(wrapper.find('a').prop('href')).toBe(
expect(wrapper.find('EuiLink').prop('href')).toBe(
'myBasePath/app/kibana#/discover?_a=(interval:auto,query:(language:lucene,query:\'context.service.name:"myServiceName" AND error.grouping_key:"myGroupId"\'),sort:(\'@timestamp\':desc))&_g=(time:(from:now-24h,mode:quick,to:now))'
);
});
it('should render correct markup', () => {
expect(toJson(wrapper)).toMatchSnapshot();
expect(wrapper).toMatchSnapshot();
});
it('should include existing _g values in link href', () => {
wrapper.setProps({
location: {
search:
'?_g=(refreshInterval:(pause:!t,value:0),time:(from:now-7d,mode:relative,to:now-1d))'
}
});
expect(wrapper).toMatchSnapshot();
wrapper.setProps({ location: { search: '?_g=H@whatever' } });
expect(wrapper).toMatchSnapshot();
});
});

View file

@ -6,7 +6,7 @@
import { EuiLink, EuiLinkAnchorProps } from '@elastic/eui';
import createHistory from 'history/createHashHistory';
import { get, isEmpty, isPlainObject, mapValues } from 'lodash';
import { get, isPlainObject, mapValues } from 'lodash';
import qs from 'querystring';
import React from 'react';
import { connect } from 'react-redux';
@ -16,6 +16,10 @@ import chrome from 'ui/chrome';
import url from 'url';
import { StringMap } from '../../typings/common';
// Kibana default set in: https://github.com/elastic/kibana/blob/e13e47fc4eb6112f2a5401408e9f765eae90f55d/x-pack/plugins/apm/public/utils/timepicker/index.js#L31-L35
// TODO: store this in config or a shared constant?
const DEFAULT_KIBANA_TIME_RANGE = '(time:(from:now-24h,mode:quick,to:now))';
interface ViewMlJobArgs {
serviceName: string;
transactionType: string;
@ -152,17 +156,6 @@ export function RelativeLinkComponent({
// The two components have different APIs: `path` vs `pathname` and one uses EuiLink the other react-router's Link (which behaves differently)
// Suggestion: Deprecate RelativeLink, and clean up KibanaLink
// _g is always retrieved from the url - it can not be changed via query prop
function getGArg(g: string) {
// use "g" if it's set in the url
if (g && !isEmpty(rison.decode(g))) {
return g;
}
// use default 24h (default set in: https://github.com/elastic/kibana/blob/e13e47fc4eb6112f2a5401408e9f765eae90f55d/x-pack/plugins/apm/public/utils/timepicker/index.js#L31-L35)
return '(time:(from:now-24h,mode:quick,to:now))';
}
export interface KibanaLinkArgs {
location: {
search?: string;
@ -194,7 +187,8 @@ export const UnconnectedKibanaLink: React.SFC<KibanaLinkArgs> = ({
const currentQuery = toQuery(location.search);
const nextQuery = {
...query,
_g: getGArg(currentQuery._g),
// use "_g" if it's set in the url, otherwise use default
_g: currentQuery._g || DEFAULT_KIBANA_TIME_RANGE,
_a: query._a ? rison.encode(query._a) : ''
};