Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 50x 28x 6x 6x 1x 6x 6x 6x 6x 6x 6x 6x 6x 50x 6x 6x 6x 50x 6x 6x | import type { ReactNode } from "react";
import { useMemo } from "react";
import { formatBytes } from "@canonical/maas-react-components";
import type { ColumnDef, Row } from "@tanstack/react-table";
import { Link } from "react-router";
import IPColumn from "../components/IPColumn";
import StatusColumn from "../components/StatusColumn";
import DoubleRow from "@/app/base/components/DoubleRow";
import urls from "@/app/base/urls";
import type { Machine } from "@/app/store/machine/types";
import type { Tag } from "@/app/store/tag/types";
import { getTagNamesForIds } from "@/app/store/tag/utils";
import { getRanges } from "@/app/utils";
export enum Label {
Name = "Name",
Status = "Status",
Ipv4 = "Ipv4",
Ipv6 = "Ipv6",
Hugepages = "Hugepages",
Cores = "Cores",
Ram = "Ram",
Pool = "Pool",
EmptyList = "No VMs available",
}
export type GetHostColumn = (vm: Machine) => ReactNode;
export type GetResources = (vm: Machine) => {
hugepagesBacked: boolean;
pinnedCores: number[];
unpinnedCores: number;
};
type Props = {
callId?: string | null;
getHostColumn?: GetHostColumn;
getResources: GetResources;
tags: Tag[];
};
type VMsColumnDef = ColumnDef<Machine, Partial<Machine>>;
const useVMsTableColumns = ({
getHostColumn,
getResources,
tags,
}: Props): VMsColumnDef[] => {
return useMemo(
() => [
{
id: "hostname",
header: "VM name",
accessorKey: "hostname",
enableSorting: true,
cell: ({ row }) => (
<DoubleRow
data-testid="name-col"
primary={
<Link
to={urls.machines.machine.index({ id: row.original.system_id })}
>
<strong>{row.original.hostname}</strong>
</Link>
}
primaryTitle={row.original.hostname}
/>
),
},
{
id: "status",
header: "STATUS",
accessorKey: "status",
enableSorting: true,
cell: ({ row }) => (
<StatusColumn
aria-label={Label.Status}
systemId={row.original.system_id}
/>
),
},
...(getHostColumn
? [
{
id: "host",
header: "KVM host",
accessorKey: "host",
cell: ({ row }: { row: Row<Machine> }) => (
<>{getHostColumn?.(row.original)}</>
),
},
]
: []),
{
id: "ipv4",
header: "IPV4",
accessorKey: "ipv4",
enableSorting: false,
cell: ({ row }) => (
<IPColumn
aria-label={Label.Ipv4}
systemId={row.original.system_id}
version={4}
/>
),
},
{
id: "ipv6",
header: "IPV6",
accessorKey: "ipv6",
enableSorting: false,
cell: ({ row }) => (
<IPColumn
aria-label={Label.Ipv6}
systemId={row.original.system_id}
version={6}
/>
),
},
{
id: "hugepages",
header: "HUGEPAGES",
accessorKey: "hugepages",
enableSorting: false,
cell: ({ row }) => (
<span>
{getResources(row.original).hugepagesBacked ? "Enabled" : ""}
</span>
),
},
{
id: "cores",
header: "CORES",
accessorKey: "cores",
enableSorting: false,
cell: ({ row }) => {
const { pinnedCores, unpinnedCores } = getResources(row.original);
const pinnedRanges = getRanges(pinnedCores).join(", ");
const primaryText = pinnedRanges || `Any ${unpinnedCores}`;
const secondaryText = pinnedRanges && "pinned";
return (
<DoubleRow
primary={primaryText}
primaryTitle={primaryText}
secondary={secondaryText}
secondaryTitle={secondaryText}
/>
);
},
},
{
id: "memory",
header: () => (
<DoubleRow
primary="RAM"
primaryTitle="RAM"
secondary="STORAGE"
secondaryTitle="STORAGE"
/>
),
accessorKey: "memory",
enableSorting: true,
cell: ({ row }) => {
const memory = formatBytes(
{ value: row.original.memory, unit: "GiB" },
{ binary: true }
);
const storage = formatBytes({
value: row.original.storage,
unit: "GB",
});
return (
<DoubleRow
aria-label={Label.Ram}
primary={
<>
<span>{memory.value} </span>
<small className="u-text--muted">{memory.unit}</small>
</>
}
secondary={
<>
<span>{storage.value} </span>
<small className="u-text--muted">{storage.unit}</small>
</>
}
/>
);
},
},
{
id: "pool",
header: () => (
<DoubleRow
primary="POOL"
primaryTitle="POOL"
secondary="TAG"
secondaryTitle="TAG"
/>
),
accessorKey: "pool",
enableSorting: true,
cell: ({ row }) => {
const tagString = getTagNamesForIds(row.original.tags, tags).join(
", "
);
return (
<DoubleRow
aria-label={Label.Pool}
data-testid="pool-col"
primary={row.original.pool.name}
secondary={tagString}
secondaryTitle={tagString}
/>
);
},
},
],
[getHostColumn, getResources, tags]
);
};
export default useVMsTableColumns;
|