Skip to content

Commit

Permalink
[Synthetics] Summary doc viewer in pings list (#163926)
Browse files Browse the repository at this point in the history
Co-authored-by: kibanamachine <[email protected]>
Co-authored-by: Abdul Zahid <[email protected]>
  • Loading branch information
3 people authored Sep 5, 2023
1 parent 6b6dcdb commit addb5b7
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 31 deletions.
3 changes: 2 additions & 1 deletion x-pack/plugins/synthetics/kibana.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"kibanaUtils",
"observability",
"spaces",
"indexLifecycleManagement"
"indexLifecycleManagement",
"unifiedDocViewer"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { EuiButtonIcon, EuiFlyout, EuiFlyoutBody, EuiFlyoutHeader, EuiTitle } from '@elastic/eui';
import React, { useState } from 'react';
import { useUnifiedDocViewerServices } from '@kbn/unified-doc-viewer-plugin/public';
import { buildDataTableRecord } from '@kbn/discover-utils';
import { UnifiedDocViewer } from '@kbn/unified-doc-viewer-plugin/public';
import { i18n } from '@kbn/i18n';
import { useFetcher } from '@kbn/observability-shared-plugin/public';
import { DataTableRecord } from '@kbn/discover-utils/src/types';
import { useDateFormat } from '../../../../../hooks/use_date_format';
import { LoadingState } from '../../monitors_page/overview/overview/monitor_detail_flyout';
import { useSyntheticsDataView } from '../../../contexts/synthetics_data_view_context';
import { SYNTHETICS_INDEX_PATTERN } from '../../../../../../common/constants';
import { Ping } from '../../../../../../common/runtime_types';

export const ViewDocument = ({ ping }: { ping: Ping }) => {
const { data } = useUnifiedDocViewerServices();
const [isFlyoutVisible, setIsFlyoutVisible] = useState<boolean>(false);

const dataView = useSyntheticsDataView();
const formatter = useDateFormat();

const { data: hit } = useFetcher<Promise<DataTableRecord | undefined>>(async () => {
if (!dataView?.id || !isFlyoutVisible) return;
const response = await data.search
.search({
params: {
index: SYNTHETICS_INDEX_PATTERN,
body: {
query: {
ids: {
values: [ping.docId],
},
},
fields: ['*'],
_source: false,
},
},
})
.toPromise();
const docs = response?.rawResponse?.hits?.hits ?? [];
if (docs.length > 0) {
return buildDataTableRecord(docs[0], dataView);
}
}, [data, dataView, ping.docId, isFlyoutVisible]);

return (
<>
<EuiButtonIcon
iconType="inspect"
title={INSPECT_DOCUMENT}
onClick={() => {
setIsFlyoutVisible(true);
}}
/>
{isFlyoutVisible && (
<EuiFlyout onClose={() => setIsFlyoutVisible(false)} ownFocus={true}>
<EuiFlyoutHeader>
<EuiTitle size="m">
<h4>
{INDEXED_AT} {formatter(ping.timestamp)}
</h4>
</EuiTitle>
</EuiFlyoutHeader>
<EuiFlyoutBody>
{dataView?.id && hit ? (
<UnifiedDocViewer hit={hit} dataView={dataView} />
) : (
<LoadingState />
)}
</EuiFlyoutBody>
</EuiFlyout>
)}
</>
);
};

const INDEXED_AT = i18n.translate('xpack.synthetics.monitorDetails.summary.indexedAt', {
defaultMessage: 'Indexed at',
});

export const INSPECT_DOCUMENT = i18n.translate(
'xpack.synthetics.monitorDetails.action.inspectDocument',
{
defaultMessage: 'Inspect document',
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useTimeZone } from '@kbn/observability-shared-plugin/public';
import { useParams } from 'react-router-dom';
import { useMemo } from 'react';
import { useSelectedLocation } from './use_selected_location';
import { PingState } from '../../../../../../common/runtime_types';
import { Ping, PingState } from '../../../../../../common/runtime_types';
import {
EXCLUDE_RUN_ONCE_FILTER,
SUMMARY_FILTER,
Expand Down Expand Up @@ -113,10 +113,11 @@ export function useMonitorErrors(monitorIdArg?: string) {
return prev;
}, defaultValues) ?? defaultValues;

const hits = data?.aggregations?.latest.hits.hits ?? [];

const hasActiveError: boolean =
data?.aggregations?.latest.hits.hits.length === 1 &&
(data?.aggregations?.latest.hits.hits[0]._source as { monitor: { status: string } }).monitor
.status === 'down' &&
hits.length === 1 &&
(hits[0]?._source as Ping).monitor?.status === 'down' &&
!!errorStates?.length;

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
} from '@elastic/eui';
import { Criteria } from '@elastic/eui/src/components/basic_table/basic_table';
import { EuiTableSortingType } from '@elastic/eui/src/components/basic_table/table_types';
import { INSPECT_DOCUMENT, ViewDocument } from '../../common/components/view_document';
import {
ExpandRowColumn,
toggleDetails,
Expand Down Expand Up @@ -206,6 +207,20 @@ export const TestRunsTable = ({
show: false,
},
},
{
align: 'right' as const,
actions: [
{
'data-test-subj': 'syntheticsViewPingDocument',
isPrimary: true,
name: INSPECT_DOCUMENT,
description: INSPECT_DOCUMENT,
icon: 'inspect' as const,
type: 'button' as const,
render: (ping: Ping) => <ViewDocument ping={ping} />,
},
],
},
...(!isBrowserMonitor
? [
{
Expand All @@ -229,10 +244,17 @@ export const TestRunsTable = ({
'data-test-subj': `row-${item.monitor.check_group}`,
onClick: (evt: MouseEvent) => {
const targetElem = evt.target as HTMLElement;
const isTableRow =
targetElem.parentElement?.classList.contains('euiTableCellContent') ||
targetElem.parentElement?.classList.contains('euiTableCellContent__text') ||
targetElem?.classList.contains('euiTableCellContent') ||
targetElem?.classList.contains('euiBadge__text');
// we dont want to capture image click event
if (
isTableRow &&
targetElem.tagName !== 'IMG' &&
targetElem.tagName !== 'path' &&
targetElem.tagName !== 'BUTTON' &&
!targetElem.parentElement?.classList.contains('euiLink')
) {
if (item.monitor.type !== MONITOR_TYPES.BROWSER) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export const toggleDetails = (
expandedRows: Record<string, JSX.Element>,
setExpandedRows: (update: Record<string, JSX.Element>) => any
) => {
// prevent expanding on row click if not expandable
if (!rowShouldExpand(ping)) {
return;
}

// If already expanded, collapse
if (expandedRows[ping.docId]) {
delete expandedRows[ping.docId];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import React, { createContext, useContext } from 'react';
import { useFetcher } from '@kbn/observability-shared-plugin/public';
import { DataViewsPublicPluginStart, DataView } from '@kbn/data-views-plugin/public';
import { SYNTHETICS_INDEX_PATTERN } from '../../../../common/constants';

export const SyntheticsDataViewContext = createContext({} as DataView);

export const SyntheticsDataViewContextProvider: React.FC<{
dataViews: DataViewsPublicPluginStart;
}> = ({ children, dataViews }) => {
const { data } = useFetcher<Promise<DataView | undefined>>(async () => {
return dataViews.create({ title: SYNTHETICS_INDEX_PATTERN });
}, []);

return <SyntheticsDataViewContext.Provider value={data!} children={children} />;
};

export const useSyntheticsDataView = () => useContext(SyntheticsDataViewContext);
55 changes: 29 additions & 26 deletions x-pack/plugins/synthetics/public/apps/synthetics/synthetics_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
import { EuiThemeProvider } from '@kbn/kibana-react-plugin/common';
import { InspectorContextProvider } from '@kbn/observability-shared-plugin/public';
import { ObservabilityAIAssistantProvider } from '@kbn/observability-ai-assistant-plugin/public';
import { SyntheticsDataViewContextProvider } from './contexts/synthetics_data_view_context';
import { SyntheticsAppProps } from './contexts';

import {
Expand Down Expand Up @@ -99,32 +100,34 @@ const Application = (props: SyntheticsAppProps) => {
fleet: startPlugins.fleet,
}}
>
<ObservabilityAIAssistantProvider value={startPlugins.observabilityAIAssistant}>
<Router history={appMountParameters.history}>
<EuiThemeProvider darkMode={darkMode}>
<SyntheticsRefreshContextProvider>
<SyntheticsSettingsContextProvider {...props}>
<SyntheticsThemeContextProvider darkMode={darkMode}>
<SyntheticsStartupPluginsContextProvider {...startPlugins}>
<div className={APP_WRAPPER_CLASS} data-test-subj="syntheticsApp">
<RedirectAppLinks
className={APP_WRAPPER_CLASS}
application={core.application}
>
<InspectorContextProvider>
<PageRouter />
<ActionMenu appMountParameters={appMountParameters} />
<TestNowModeFlyoutContainer />
</InspectorContextProvider>
</RedirectAppLinks>
</div>
</SyntheticsStartupPluginsContextProvider>
</SyntheticsThemeContextProvider>
</SyntheticsSettingsContextProvider>
</SyntheticsRefreshContextProvider>
</EuiThemeProvider>
</Router>
</ObservabilityAIAssistantProvider>
<SyntheticsDataViewContextProvider dataViews={startPlugins.dataViews}>
<ObservabilityAIAssistantProvider value={startPlugins.observabilityAIAssistant}>
<Router history={appMountParameters.history}>
<EuiThemeProvider darkMode={darkMode}>
<SyntheticsRefreshContextProvider>
<SyntheticsSettingsContextProvider {...props}>
<SyntheticsThemeContextProvider darkMode={darkMode}>
<SyntheticsStartupPluginsContextProvider {...startPlugins}>
<div className={APP_WRAPPER_CLASS} data-test-subj="syntheticsApp">
<RedirectAppLinks
className={APP_WRAPPER_CLASS}
application={core.application}
>
<InspectorContextProvider>
<PageRouter />
<ActionMenu appMountParameters={appMountParameters} />
<TestNowModeFlyoutContainer />
</InspectorContextProvider>
</RedirectAppLinks>
</div>
</SyntheticsStartupPluginsContextProvider>
</SyntheticsThemeContextProvider>
</SyntheticsSettingsContextProvider>
</SyntheticsRefreshContextProvider>
</EuiThemeProvider>
</Router>
</ObservabilityAIAssistantProvider>
</SyntheticsDataViewContextProvider>
</KibanaContextProvider>
</ReduxProvider>
</KibanaThemeProvider>
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/synthetics/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@
"@kbn/core-saved-objects-server-mocks",
"@kbn/shared-ux-page-kibana-template",
"@kbn/observability-ai-assistant-plugin",
"@kbn/unified-doc-viewer-plugin",
"@kbn/discover-utils",
],
"exclude": [
"target/**/*",
Expand Down

0 comments on commit addb5b7

Please sign in to comment.