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

Sid linking from clusters #1715

Merged
merged 18 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 24 additions & 1 deletion assets/js/components/ClustersList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import Tags from '@components/Tags';
import PageHeader from '@components/PageHeader';
import { addTagToCluster, removeTagFromCluster } from '@state/clusters';
import ClusterLink from '@components/ClusterLink';
import SapSystemLink from '@components/SapSystemLink';
import { ExecutionIcon } from '@components/ClusterDetails';
import { post, del } from '@lib/network';
import { useSearchParams } from 'react-router-dom';
import HealthSummary from '@components/HealthSummary/HealthSummary';
import { getCounters } from '@components/HealthSummary/summarySelection';
import { getAllSAPInstances } from '@state/selectors';

const getClusterTypeLabel = (type) => {
switch (type) {
Expand All @@ -24,6 +26,9 @@ const getClusterTypeLabel = (type) => {
}
};

const getSapSystemBySID = (instances, sid) =>
instances.find((instance) => instance.sid === sid);

const addTag = (tag, clusterId) => {
post(`/clusters/${clusterId}/tags`, {
value: tag,
Expand All @@ -36,6 +41,7 @@ const removeTag = (tag, clusterId) => {

function ClustersList() {
const clusters = useSelector((state) => state.clustersList.clusters);
const allInstances = useSelector(getAllSAPInstances());
const dispatch = useDispatch();
const [searchParams, setSearchParams] = useSearchParams();

Expand Down Expand Up @@ -71,7 +77,24 @@ function ClustersList() {
filterFromParams: true,
filter: (filter, key) => (element) =>
element[key].some((sid) => filter.includes(sid)),
render: (_, { sid }) => sid.join(', '),
render: (_, { sid, id }) => {
const sidsArray = sid.map((singleSid, index) => {
const sapSystemData = getSapSystemBySID(allInstances, singleSid);

return [
index > 0 && ', ',
<SapSystemLink
Copy link
Contributor

Choose a reason for hiding this comment

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

mapping and returning JSX elements should require a key prop to be passed, can you do that?

key={`${id}-${singleSid}`}
systemType={sapSystemData?.type}
sapSystemId={sapSystemData?.sap_system_id}
>
{singleSid}
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens on multiple sids here?
Are they separated by spaces?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've used the tests to render how it would look like:

Captura desde 2023-08-21 13-40-38

Captura desde 2023-08-21 13-50-23

Copy link
Contributor Author

Choose a reason for hiding this comment

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

After speaking with @dottorblaster we decided it might look better if we add the coma back. @jagabomb wdyt?
Captura desde 2023-08-21 15-37-22

</SapSystemLink>,
];
});

return sidsArray;
},
},
{
title: 'Hosts',
Expand Down
3 changes: 2 additions & 1 deletion assets/js/components/ClustersList.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ describe('ClustersList component', () => {

renderWithRouter(StatefulClustersList);

expect(screen.getByText('HA1, HA2')).toBeVisible();
expect(screen.getByText('HA1')).toBeVisible();
expect(screen.getByText('HA2')).toBeVisible();
rtorrero marked this conversation as resolved.
Show resolved Hide resolved
});

it('should put the filters values in the query string when filters are selected', () => {
Expand Down
22 changes: 5 additions & 17 deletions assets/js/components/HostsList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,13 @@ import Tooltip from '@components/Tooltip';
import CleanUpButton from '@components/CleanUpButton';

import { addTagToHost, removeTagFromHost, deregisterHost } from '@state/hosts';
import { getAllSAPInstances } from '@state/selectors';

import { post, del } from '@lib/network';
import { agentVersionWarning } from '@lib/agent';

const getInstancesByHost = (applicationInstances, databaseInstances, hostId) =>
applicationInstances
.map((instance) => ({ ...instance, type: 'sap_systems' }))
.concat(
databaseInstances.map((instance) => ({
...instance,
type: 'databases',
}))
)
.filter((instance) => instance.host_id === hostId);
const getInstancesByHost = (instances, hostId) =>
instances.filter((instance) => instance.host_id === hostId);

const addTag = (tag, hostId) => {
post(`/hosts/${hostId}/tags`, {
Expand All @@ -48,8 +41,7 @@ const removeTag = (tag, hostId) => {
function HostsList() {
const hosts = useSelector((state) => state.hostsList.hosts);
const clusters = useSelector((state) => state.clustersList.clusters);
const { applicationInstances } = useSelector((state) => state.sapSystemsList);
const { databaseInstances } = useSelector((state) => state.databasesList);
const allInstances = useSelector(getAllSAPInstances());

const [searchParams, setSearchParams] = useSearchParams();
const [cleanUpModalOpen, setCleanUpModalOpen] = useState(false);
Expand Down Expand Up @@ -212,11 +204,7 @@ function HostsList() {

const data = hosts.map((host) => {
const cluster = clusters.find((c) => c.id === host.cluster_id);
const sapSystemList = getInstancesByHost(
applicationInstances,
databaseInstances,
host.id
);
const sapSystemList = getInstancesByHost(allInstances, host.id);

return {
heartbeat: host.heartbeat,
Expand Down
14 changes: 12 additions & 2 deletions assets/js/components/SapSystemLink.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import React from 'react';
import { EOS_WARNING_OUTLINED } from 'eos-icons-react';
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we sure we want to change this component?
In the past, for this exact feature (adding a not registered tooltip to the link) we have created a new component, to not change the behaviour of this one (ClusterNodeLink).
Maybe we should follow the same pattern

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As I was looking how to add this I noticed that we have already a bunch of of *Link components that to basically very similar things. I'm leaning more towards keeping less components and add more when we need them. Wdyt?


import Tooltip from '@components/Tooltip';
import { Link } from 'react-router-dom';

function SapSystemLink({ systemType, sapSystemId, children }) {
return sapSystemId ? (
return sapSystemId && systemType ? (
<Link
key={sapSystemId}
className="text-jungle-green-500 hover:opacity-75"
Expand All @@ -12,7 +14,15 @@ function SapSystemLink({ systemType, sapSystemId, children }) {
{children}
</Link>
) : (
<span>{children}</span>
<Tooltip content="SAP System currently not registered." place="bottom">
<span className="group flex items-center relative">
<EOS_WARNING_OUTLINED
size="base"
className="centered fill-yellow-500"
/>
<span className="ml-1 truncate max-w-[100px]">{children}</span>
</span>
</Tooltip>
);
}

Expand Down
39 changes: 39 additions & 0 deletions assets/js/components/SapSystemLink.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React from 'react';
import { act, screen, render } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import '@testing-library/jest-dom';
import { renderWithRouter } from '@lib/test-utils';
import { sapSystemFactory } from '@lib/test-utils/factories';
import SapSystemLink from '@components/SapSystemLink';

describe('SapSystemLink', () => {
it('renders Link when sapSystemId and systemType are provided', () => {
const { id, sid } = sapSystemFactory.build();

renderWithRouter(
<SapSystemLink systemType="databases" sapSystemId={id}>
{sid}
</SapSystemLink>
);

const sapSystemLinkElement = screen.getByRole('link', { sid });

expect(sapSystemLinkElement).toBeInTheDocument();
expect(sapSystemLinkElement).toHaveAttribute('href', `/databases/${id}`);
});

it('renders tooltip when sapSystemId or systemType are not provided', async () => {
const user = userEvent.setup();
const { sid } = sapSystemFactory.build();

render(<SapSystemLink>{sid}</SapSystemLink>);

const sidElement = screen.getByText(sid);

await act(async () => user.hover(sidElement));

expect(
screen.getByText('SAP System currently not registered.')
).toBeInTheDocument();
});
});
13 changes: 13 additions & 0 deletions assets/js/state/selectors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,16 @@ export const getInstancesOnHost = (hostId) => (state) => {

return [...foundApplicationInstances, ...foundDatabaseInstances];
};

export const getAllSAPInstances = () => (state) => {
const { databaseInstances, applicationInstances } = state.sapSystemsList;

return applicationInstances
.map((instance) => ({ ...instance, type: 'sap_systems' }))
.concat(
databaseInstances.map((instance) => ({
...instance,
type: 'databases',
}))
);
};
27 changes: 27 additions & 0 deletions assets/js/state/selectors/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { getAllSAPInstances } from '.';

describe('getAllSAPInstances selector', () => {
it('should correctly merge and format applicationInstances and databaseInstances', () => {
const state = {
sapSystemsList: {
applicationInstances: [
{ id: 1, name: 'APP1' },
{ id: 2, name: 'APP2' },
],
databaseInstances: [
{ id: 3, name: 'DB1' },
{ id: 4, name: 'DB2' },
],
},
};

const expectedOutput = [
{ id: 1, name: 'APP1', type: 'sap_systems' },
{ id: 2, name: 'APP2', type: 'sap_systems' },
{ id: 3, name: 'DB1', type: 'databases' },
{ id: 4, name: 'DB2', type: 'databases' },
];

expect(getAllSAPInstances()(state)).toEqual(expectedOutput);
});
});