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

Add sap-system/databases linking from host overview #369

Merged
merged 2 commits into from
Apr 19, 2022
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
45 changes: 38 additions & 7 deletions assets/js/components/HostsList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Table from './Table';
import Tags from './Tags';
import { addTagToHost, removeTagFromHost } from '@state/hosts';
import ClusterLink from '@components/ClusterLink';
import SapSystemLink from '@components/SapSystemLink';
import { useSelector, useDispatch } from 'react-redux';

import { EOS_LENS_FILLED } from 'eos-icons-react';
Expand All @@ -23,6 +24,19 @@ const getHeartbeatIcon = ({ heartbeat }) => {
}
};

const getSapSystemsByHost = (sapSystems, hostId) => {
Copy link
Member

Choose a reason for hiding this comment

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

I believe this can be a selector on the state, selecting all the instances on a host.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah! nice, I see you used it in HostDetails, going to give it a try

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'll create a ticket to figure out later how to use this. It seems to work on first load, but breaks after a refresh. I suspect that useSelector is not intended to be used each time we iterate on the hosts list to display each entry of the table. The warning itself after it breaks is:

Warning: React has detected a change in the order of Hooks called by HostsList. This will lead to bugs and errors if not fixed. For more information, read the Rules of Hooks: https://reactjs.org/link/rules-of-hooks

const appInstances = sapSystems
.flatMap((sapSystem) => sapSystem.application_instances)
.filter((sapSystem) => sapSystem.host_id === hostId)
.map((sapSystem) => ({ type: 'sap-systems', instance: sapSystem }));
const dbInstances = sapSystems
.flatMap((sapSystem) => sapSystem.database_instances)
.filter((sapSystem) => sapSystem.host_id === hostId)
.map((sapSystem) => ({ type: 'databases', instance: sapSystem }));

return appInstances.concat(dbInstances);
};

const addTag = (tag, hostId) => {
axios
.post(`/api/hosts/${hostId}/tags`, {
Expand All @@ -42,6 +56,8 @@ const removeTag = (tag, hostId) => {
const HostsList = () => {
const hosts = useSelector((state) => state.hostsList.hosts);
const clusters = useSelector((state) => state.clustersList.clusters);
const sapSystems = useSelector((state) => state.sapSystemsList.sapSystems);

const dispatch = useDispatch();

const config = {
Expand Down Expand Up @@ -86,16 +102,28 @@ const HostsList = () => {
key: 'cluster',
className: 'w-40',
render: (cluster) => {
return cluster?.name;
return <ClusterLink cluster={cluster}>{cluster?.name}</ClusterLink>;
},
},
{
title: 'SID',
key: 'sid',
filter: true,
render: (content, { cluster }) => (
<ClusterLink cluster={cluster}>{content}</ClusterLink>
),
filter: (filter, key) => (element) =>
element[key].some((sid) => filter.includes(sid)),
render: (sids, { sap_systems }) => {
let sidsArray = sap_systems.map((instance, index) => [
index > 0 && ', ',
<SapSystemLink
key={index}
systemType={instance.type}
sapSystemId={instance.instance?.sap_system_id}
>
{instance.instance?.sid}
</SapSystemLink>,
]);

return sidsArray;
},
},
{
title: 'Agent version',
Expand Down Expand Up @@ -134,17 +162,20 @@ const HostsList = () => {

const data = hosts.map((host) => {
const cluster = clusters.find((cluster) => cluster.id === host.cluster_id);

const sapSystemList = getSapSystemsByHost(sapSystems, host.id);
return {
heartbeat: host.heartbeat,
hostname: host.hostname,
ip: host.ip_addresses,
provider: host.provider,
sid: cluster?.sid,
sid: sapSystemList.map((sapSystem) => {
return sapSystem.instance.sid;
}),
cluster: cluster,
agent_version: host.agent_version,
id: host.id,
tags: (host.tags && host.tags.map((tag) => tag.value)) || [],
sap_systems: sapSystemList,
};
});

Expand Down
17 changes: 17 additions & 0 deletions assets/js/components/SapSystemLink.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react';

import { Link } from 'react-router-dom';

const SapSystemLink = ({ systemType, sapSystemId, children }) => {
return (
<Link
key={sapSystemId}
className="text-jungle-green-500 hover:opacity-75"
to={`/${systemType}/${sapSystemId}`}
>
{children}
</Link>
);
};

export default SapSystemLink;