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

Fixes to service map single node banner #60072

Merged
merged 3 commits into from
Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { act, render, wait } from '@testing-library/react';
import cytoscape from 'cytoscape';
import React, { FunctionComponent } from 'react';
import { MockApmPluginContextWrapper } from '../../../utils/testHelpers';
import { CytoscapeContext } from './Cytoscape';
import { EmptyBanner } from './EmptyBanner';

const cy = cytoscape({});

const wrapper: FunctionComponent = ({ children }) => (
<MockApmPluginContextWrapper>
<CytoscapeContext.Provider value={cy}>{children}</CytoscapeContext.Provider>
</MockApmPluginContextWrapper>
);

describe('EmptyBanner', () => {
describe('when cy is undefined', () => {
it('renders null', () => {
const noCytoscapeWrapper: FunctionComponent = ({ children }) => (
<MockApmPluginContextWrapper>
<CytoscapeContext.Provider value={undefined}>
{children}
</CytoscapeContext.Provider>
</MockApmPluginContextWrapper>
);
const component = render(<EmptyBanner />, {
wrapper: noCytoscapeWrapper
});

expect(component.container.children).toHaveLength(0);
});
});

describe('with no nodes', () => {
it('renders null', () => {
const component = render(<EmptyBanner />, {
wrapper
});

expect(component.container.children).toHaveLength(0);
});
});

describe('with one node', () => {
it('does not render null', async () => {
const component = render(<EmptyBanner />, { wrapper });

await act(async () => {
cy.add({ data: { id: 'test id' } });
await wait(() => {
expect(component.container.children.length).toBeGreaterThan(0);
});
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,70 @@
import { EuiCallOut } from '@elastic/eui';
import lightTheme from '@elastic/eui/dist/eui_theme_light.json';
import { i18n } from '@kbn/i18n';
import React from 'react';
import React, { useContext, useEffect, useState } from 'react';
import styled from 'styled-components';
import { ElasticDocsLink } from '../../shared/Links/ElasticDocsLink';
import { CytoscapeContext } from './Cytoscape';

const EmptyBannerCallOut = styled(EuiCallOut)`
const EmptyBannerContainer = styled('div')`
smith marked this conversation as resolved.
Show resolved Hide resolved
margin: ${lightTheme.gutterTypes.gutterSmall};
/* Add some extra margin so it displays to the right of the controls. */
margin-left: calc(
${lightTheme.gutterTypes.gutterLarge} +
${lightTheme.gutterTypes.gutterExtraLarge}
left: calc(
${lightTheme.gutterTypes.gutterExtraLarge} +
${lightTheme.gutterTypes.gutterSmall}
);
position: absolute;
z-index: 1;
`;

export function EmptyBanner() {
const cy = useContext(CytoscapeContext);
const [nodeCount, setNodeCount] = useState(0);

useEffect(() => {
const handler: cytoscape.EventHandler = event =>
setNodeCount(event.cy.nodes().length);

if (cy) {
cy.on('add remove', 'node', handler);
}

return () => {
if (cy) {
cy.removeListener('add remove', 'node', handler);
}
};
}, [cy]);

// Only show if there's a single node.
if (!cy || nodeCount !== 1) {
return null;
}

// Since we're absolutely positioned, we need to get the full width and
// subtract the space for controls and margins.
const width =
cy.width() -
parseInt(lightTheme.gutterTypes.gutterExtraLarge, 10) -
parseInt(lightTheme.gutterTypes.gutterLarge, 10);
Copy link
Contributor

Choose a reason for hiding this comment

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

If the user resizes the window, does this width get updated?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No. We could observe the cytoscape resize event or the window event but I don't think it's worth the extra complexity.


return (
<EmptyBannerCallOut
title={i18n.translate('xpack.apm.serviceMap.emptyBanner.title', {
defaultMessage: "Looks like there's only a single service."
})}
>
{i18n.translate('xpack.apm.serviceMap.emptyBanner.message', {
defaultMessage:
"We will map out connected services and external requests if we can detect them. Please make sure you're running the latest version of the APM agent."
})}{' '}
<ElasticDocsLink section="/apm/get-started" path="/agents.html">
{i18n.translate('xpack.apm.serviceMap.emptyBanner.docsLink', {
defaultMessage: 'Learn more in the docs'
<EmptyBannerContainer style={{ width }}>
<EuiCallOut
title={i18n.translate('xpack.apm.serviceMap.emptyBanner.title', {
defaultMessage: "Looks like there's only a single service."
})}
</ElasticDocsLink>
</EmptyBannerCallOut>
>
{i18n.translate('xpack.apm.serviceMap.emptyBanner.message', {
defaultMessage:
"We will map out connected services and external requests if we can detect them. Please make sure you're running the latest version of the APM agent."
})}{' '}
<ElasticDocsLink section="/apm/get-started" path="/agents.html">
{i18n.translate('xpack.apm.serviceMap.emptyBanner.docsLink', {
defaultMessage: 'Learn more in the docs'
})}
</ElasticDocsLink>
</EuiCallOut>
</EmptyBannerContainer>
);
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,7 @@ export function ServiceMap({ serviceName }: ServiceMapProps) {
style={cytoscapeDivStyle}
>
<Controls />
{serviceName && renderedElements.current.length === 1 && (
<EmptyBanner />
)}
{serviceName && <EmptyBanner />}
<Popover focusedServiceName={serviceName} />
</Cytoscape>
</div>
Expand Down