Skip to content

Commit 8203aaf

Browse files
authored
feat(ocm): add sorting to the ocm ClusterStatusPage table (#1052)
1 parent 66c3566 commit 8203aaf

File tree

5 files changed

+94
-45
lines changed

5 files changed

+94
-45
lines changed

plugins/ocm/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
"@material-ui/lab": "^4.0.0-alpha.45",
4040
"@patternfly/patternfly": "^5.1.0",
4141
"@patternfly/react-icons": "^5.1.1",
42-
"react-use": "^17.4.0"
42+
"react-use": "^17.4.0",
43+
"semver": "^7.5.4"
4344
},
4445
"peerDependencies": {
4546
"react": "^16.13.1 || ^17.0.0"

plugins/ocm/src/components/ClusterStatusPage/ClusterStatusPage.tsx

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ import {
2626
} from '@janus-idp/backstage-plugin-ocm-common';
2727

2828
import { OcmApiRef } from '../../api';
29+
import { ClusterStatusRowData } from '../../types';
2930
import { Status, Update } from '../common';
31+
import { columns } from './tableHeading';
3032

3133
const useStylesTwo = makeStyles({
3234
container: {
@@ -53,17 +55,15 @@ const NodeChip = ({
5355
}) => (
5456
<>
5557
{count > 0 ? (
56-
<>
57-
<Chip
58-
label={
59-
<>
60-
{indicator}
61-
{count}
62-
</>
63-
}
64-
variant="outlined"
65-
/>
66-
</>
58+
<Chip
59+
label={
60+
<>
61+
{indicator}
62+
{count}
63+
</>
64+
}
65+
variant="outlined"
66+
/>
6767
) : (
6868
<></>
6969
)}
@@ -140,7 +140,7 @@ const CatalogClusters = () => {
140140
return <CircularProgress />;
141141
}
142142

143-
const data = clusterEntities
143+
const data: ClusterStatusRowData[] = clusterEntities
144144
? clusterEntities.map(ce => {
145145
return {
146146
name: (
@@ -168,29 +168,7 @@ const CatalogClusters = () => {
168168
<Table
169169
options={{ paging: false }}
170170
data={data}
171-
columns={[
172-
{
173-
title: 'Name',
174-
field: 'name',
175-
highlight: true,
176-
},
177-
{
178-
title: 'Status',
179-
field: 'status',
180-
},
181-
{
182-
title: 'Infrastructure',
183-
field: 'infrastructure',
184-
},
185-
{
186-
title: 'Version',
187-
field: 'version',
188-
},
189-
{
190-
title: 'Nodes',
191-
field: 'nodes',
192-
},
193-
]}
171+
columns={columns}
194172
title="All"
195173
/>
196174
</div>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { TableColumn } from '@backstage/core-components';
2+
3+
import semver from 'semver';
4+
5+
import { ClusterStatusRowData } from '../../types';
6+
7+
export const columns: TableColumn<ClusterStatusRowData>[] = [
8+
{
9+
title: 'Name',
10+
field: 'name',
11+
highlight: true,
12+
customSort: (a, b) => {
13+
// The children type here is actually a ReactNode, but we know it's a string
14+
return (a.name.props.children as string).localeCompare(
15+
b.name.props.children as string,
16+
'en',
17+
);
18+
},
19+
},
20+
{
21+
title: 'Status',
22+
field: 'status',
23+
customSort: (a, b) => {
24+
const availabilityA = a.status.props.status.available;
25+
const availabilityB = b.status.props.status.available;
26+
if (availabilityA === availabilityB) return 0;
27+
return availabilityA ? -1 : 1;
28+
},
29+
},
30+
{
31+
title: 'Infrastructure',
32+
field: 'infrastructure',
33+
},
34+
{
35+
title: 'Version',
36+
field: 'version',
37+
customSort: (a, b) => {
38+
return semver.gt(
39+
a.version.props.data.version,
40+
b.version.props.data.version,
41+
)
42+
? 1
43+
: -1;
44+
},
45+
},
46+
{
47+
title: 'Nodes',
48+
field: 'nodes',
49+
customSort: (a, b) => {
50+
return a.nodes.props.nodes.length - b.nodes.props.nodes.length;
51+
},
52+
},
53+
];

plugins/ocm/src/components/common.tsx

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ import {
99
import { Button, Grid, makeStyles, Tooltip } from '@material-ui/core';
1010
import { ArrowCircleUpIcon } from '@patternfly/react-icons';
1111

12-
import {
13-
ClusterStatus,
14-
ClusterUpdate,
15-
} from '@janus-idp/backstage-plugin-ocm-common';
12+
import { ClusterStatus } from '@janus-idp/backstage-plugin-ocm-common';
13+
14+
import { versionDetails } from '../types';
1615

1716
const useStyles = makeStyles({
1817
button: {
@@ -48,11 +47,7 @@ export const Status = ({ status }: { status: ClusterStatus }) => {
4847
);
4948
};
5049

51-
export const Update = ({
52-
data,
53-
}: {
54-
data: { version: string; update: ClusterUpdate };
55-
}) => {
50+
export const Update = ({ data }: { data: versionDetails }) => {
5651
const classes = useStyles();
5752
return (
5853
<>

plugins/ocm/src/types.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React from 'react';
2+
3+
import { EntityRefLinkProps } from '@backstage/plugin-catalog-react';
4+
5+
import {
6+
ClusterNodesStatus,
7+
ClusterStatus,
8+
ClusterUpdate,
9+
} from '@janus-idp/backstage-plugin-ocm-common';
10+
11+
export type versionDetails = {
12+
version: string;
13+
update: ClusterUpdate;
14+
};
15+
16+
export type ClusterStatusRowData = {
17+
name: React.ReactElement<EntityRefLinkProps>;
18+
status: React.ReactElement<{ status: ClusterStatus }>;
19+
infrastructure: string;
20+
version: React.ReactElement<{ data: versionDetails }>;
21+
nodes: React.ReactElement<{ nodes: Array<ClusterNodesStatus> }>;
22+
};

0 commit comments

Comments
 (0)