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

[Inspector] Make Response tab faster #180035

Merged
merged 7 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,48 +9,35 @@
// We want to allow both right-clicking to open in a new tab and clicking through
// the "Open in Console" link. We could use `RedirectAppLinks` at the top level
// but that inserts a div which messes up the layout of the inspector.
/* eslint-disable @elastic/eui/href-or-on-click */

import { EuiButtonEmpty, EuiCopy, EuiFlexGroup, EuiFlexItem, EuiSpacer } from '@elastic/eui';
import type { ConnectionRequestParams } from '@elastic/transport';
import { i18n } from '@kbn/i18n';
import { XJsonLang } from '@kbn/monaco';
import { compressToEncodedURIComponent } from 'lz-string';
import React, { useCallback } from 'react';
import { useKibana } from '@kbn/kibana-react-plugin/public';
import React from 'react';
import { CodeEditor } from '@kbn/code-editor';
import { InspectorPluginStartDeps } from '../../../../plugin';
import { RequestCodeViewerLinks } from './req_code_viewer_links';

interface RequestCodeViewerProps {
indexPattern?: string;
requestParams?: ConnectionRequestParams;
json: string;
includeOpenInAppLinks: boolean;
jughosta marked this conversation as resolved.
Show resolved Hide resolved
}

const copyToClipboardLabel = i18n.translate('inspector.requests.copyToClipboardLabel', {
defaultMessage: 'Copy to clipboard',
});

const openInConsoleLabel = i18n.translate('inspector.requests.openInConsoleLabel', {
defaultMessage: 'Open in Console',
});

const openInSearchProfilerLabel = i18n.translate('inspector.requests.openInSearchProfilerLabel', {
defaultMessage: 'Open in Search Profiler',
});

/**
* @internal
*/
export const RequestCodeViewer = ({
indexPattern,
requestParams,
json,
includeOpenInAppLinks,
}: RequestCodeViewerProps) => {
const { services } = useKibana<InspectorPluginStartDeps>();

const navigateToUrl = services.application?.navigateToUrl;

function getValue() {
if (!requestParams) {
return json;
Expand All @@ -65,32 +52,6 @@ export const RequestCodeViewer = ({

const value = getValue();

const devToolsDataUri = compressToEncodedURIComponent(value);
const consoleHref = services.share.url.locators
.get('CONSOLE_APP_LOCATOR')
?.useUrl({ loadFrom: `data:text/plain,${devToolsDataUri}` });
// Check if both the Dev Tools UI and the Console UI are enabled.
const canShowDevTools =
services.application?.capabilities?.dev_tools.show && consoleHref !== undefined;
const shouldShowDevToolsLink = !!(requestParams && canShowDevTools);
const handleDevToolsLinkClick = useCallback(
() => consoleHref && navigateToUrl && navigateToUrl(consoleHref),
[consoleHref, navigateToUrl]
);

const searchProfilerDataUri = compressToEncodedURIComponent(json);
const searchProfilerHref = services.share.url.locators
.get('SEARCH_PROFILER_LOCATOR')
?.useUrl({ index: indexPattern, loadFrom: `data:text/plain,${searchProfilerDataUri}` });
// Check if both the Dev Tools UI and the SearchProfiler UI are enabled.
const canShowsearchProfiler =
services.application?.capabilities?.dev_tools.show && searchProfilerHref !== undefined;
const shouldShowsearchProfilerLink = !!(indexPattern && canShowsearchProfiler);
const handleSearchProfilerLinkClick = useCallback(
() => searchProfilerHref && navigateToUrl && navigateToUrl(searchProfilerHref),
[searchProfilerHref, navigateToUrl]
);

return (
<EuiFlexGroup
direction="column"
Expand Down Expand Up @@ -119,37 +80,13 @@ export const RequestCodeViewer = ({
</EuiCopy>
</div>
</EuiFlexItem>
{shouldShowDevToolsLink && (
<EuiFlexItem grow={false}>
<div>
<EuiButtonEmpty
size="xs"
flush="right"
iconType="wrench"
href={consoleHref}
onClick={handleDevToolsLinkClick}
data-test-subj="inspectorRequestOpenInConsoleButton"
>
{openInConsoleLabel}
</EuiButtonEmpty>
</div>
</EuiFlexItem>
)}
{shouldShowsearchProfilerLink && (
<EuiFlexItem grow={false}>
<div>
<EuiButtonEmpty
size="xs"
flush="right"
iconType="visBarHorizontal"
href={searchProfilerHref}
onClick={handleSearchProfilerLinkClick}
data-test-subj="inspectorRequestOpenInSearchProfilerButton"
>
{openInSearchProfilerLabel}
</EuiButtonEmpty>
</div>
</EuiFlexItem>
{includeOpenInAppLinks && (
jughosta marked this conversation as resolved.
Show resolved Hide resolved
<RequestCodeViewerLinks
requestParams={requestParams}
indexPattern={indexPattern}
json={json}
value={value}
/>
)}
</EuiFlexGroup>
</EuiFlexItem>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

// We want to allow both right-clicking to open in a new tab and clicking through
// the "Open in Console" link. We could use `RedirectAppLinks` at the top level
// but that inserts a div which messes up the layout of the inspector.
/* eslint-disable @elastic/eui/href-or-on-click */

import { EuiButtonEmpty, EuiFlexItem } from '@elastic/eui';
import type { ConnectionRequestParams } from '@elastic/transport';
import { i18n } from '@kbn/i18n';
import { compressToEncodedURIComponent } from 'lz-string';
import React, { useCallback } from 'react';
import { useKibana } from '@kbn/kibana-react-plugin/public';
import { InspectorPluginStartDeps } from '../../../../plugin';

interface RequestCodeViewerLinksProps {
indexPattern?: string;
requestParams?: ConnectionRequestParams;
json: string;
value: string;
}

const openInConsoleLabel = i18n.translate('inspector.requests.openInConsoleLabel', {
defaultMessage: 'Open in Console',
});

const openInSearchProfilerLabel = i18n.translate('inspector.requests.openInSearchProfilerLabel', {
defaultMessage: 'Open in Search Profiler',
});

/**
* @internal
*/
export const RequestCodeViewerLinks = ({
indexPattern,
requestParams,
json,
value,
}: RequestCodeViewerLinksProps) => {
const { services } = useKibana<InspectorPluginStartDeps>();

const navigateToUrl = services.application?.navigateToUrl;

const devToolsDataUri = compressToEncodedURIComponent(value);
const consoleHref = services.share.url.locators
.get('CONSOLE_APP_LOCATOR')
?.useUrl({ loadFrom: `data:text/plain,${devToolsDataUri}` });
// Check if both the Dev Tools UI and the Console UI are enabled.
const canShowDevTools =
services.application?.capabilities?.dev_tools.show && consoleHref !== undefined;
const shouldShowDevToolsLink = !!(requestParams && canShowDevTools);
const handleDevToolsLinkClick = useCallback(
() => consoleHref && navigateToUrl && navigateToUrl(consoleHref),
[consoleHref, navigateToUrl]
);

const searchProfilerDataUri = compressToEncodedURIComponent(json);
const searchProfilerHref = services.share.url.locators
.get('SEARCH_PROFILER_LOCATOR')
?.useUrl({ index: indexPattern, loadFrom: `data:text/plain,${searchProfilerDataUri}` });
// Check if both the Dev Tools UI and the SearchProfiler UI are enabled.
const canShowsearchProfiler =
services.application?.capabilities?.dev_tools.show && searchProfilerHref !== undefined;
const shouldShowsearchProfilerLink = !!(indexPattern && canShowsearchProfiler);
const handleSearchProfilerLinkClick = useCallback(
() => searchProfilerHref && navigateToUrl && navigateToUrl(searchProfilerHref),
[searchProfilerHref, navigateToUrl]
);

return (
<>
{shouldShowDevToolsLink && (
<EuiFlexItem grow={false}>
<div>
<EuiButtonEmpty
size="xs"
flush="right"
iconType="wrench"
href={consoleHref}
onClick={handleDevToolsLinkClick}
data-test-subj="inspectorRequestOpenInConsoleButton"
>
{openInConsoleLabel}
</EuiButtonEmpty>
</div>
</EuiFlexItem>
)}
{shouldShowsearchProfilerLink && (
<EuiFlexItem grow={false}>
<div>
<EuiButtonEmpty
size="xs"
flush="right"
iconType="visBarHorizontal"
href={searchProfilerHref}
onClick={handleSearchProfilerLinkClick}
data-test-subj="inspectorRequestOpenInSearchProfilerButton"
>
{openInSearchProfilerLabel}
</EuiButtonEmpty>
</div>
</EuiFlexItem>
)}
</>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export class RequestDetailsRequest extends Component<DetailViewProps> {
indexPattern={this.props.request.stats?.indexPattern?.value}
requestParams={this.props.request.response?.requestParams}
json={JSON.stringify(json, null, 2)}
includeOpenInAppLinks
/>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export class RequestDetailsResponse extends Component<DetailViewProps> {
return null;
}

return <RequestCodeViewer json={JSON.stringify(responseJSON, null, 2)} />;
return (
<RequestCodeViewer
json={JSON.stringify(responseJSON, null, 2)}
includeOpenInAppLinks={false}
/>
);
}
}