From f863f7a0464476676456f9286ffe20291adb8dc5 Mon Sep 17 00:00:00 2001 From: Josue Date: Mon, 19 Apr 2021 19:46:38 -0400 Subject: [PATCH] feat: add network component and data processing module --- striker-ui/components/Network.tsx | 98 +++++++++++++++++++++++-- striker-ui/components/processNetwork.ts | 36 +++++++++ striker-ui/types/AnvilNetwork.d.ts | 43 +++++++++++ 3 files changed, 169 insertions(+), 8 deletions(-) create mode 100644 striker-ui/components/processNetwork.ts create mode 100644 striker-ui/types/AnvilNetwork.d.ts diff --git a/striker-ui/components/Network.tsx b/striker-ui/components/Network.tsx index 1e39bf5a..63d56565 100644 --- a/striker-ui/components/Network.tsx +++ b/striker-ui/components/Network.tsx @@ -1,15 +1,97 @@ -import { Grid } from '@material-ui/core'; +import { Box } from '@material-ui/core'; +import { makeStyles } from '@material-ui/core/styles'; +import { ClassNameMap } from '@material-ui/styles'; import Panel from './Panel'; -import { HeaderText } from './Text'; +import { HeaderText, BodyText } from './Text'; +import PeriodicFetch from '../lib/fetchers/periodicFetch'; +import { BLUE, GREY, TEXT, HOVER } from '../lib/consts/DEFAULT_THEME'; +import processNetworkData from './processNetwork'; + +const useStyles = makeStyles(() => ({ + root: { + width: '100%', + }, + divider: { + background: TEXT, + }, + button: { + '&:hover': { + backgroundColor: HOVER, + }, + paddingLeft: 0, + }, + noPaddingLeft: { + paddingLeft: 0, + }, + decorator: { + width: '20px', + height: '100%', + borderRadius: 2, + }, + started: { + backgroundColor: BLUE, + }, + stopped: { + backgroundColor: GREY, + }, +})); + +const selectDecorator = ( + state: string, +): keyof ClassNameMap<'started' | 'stopped'> => { + switch (state) { + case 'Started': + return 'started'; + case 'Stopped': + return 'stopped'; + default: + return 'stopped'; + } +}; + +const Network = ({ anvil }: { anvil: AnvilListItem }): JSX.Element => { + const classes = useStyles(); + + const { data } = PeriodicFetch( + `${process.env.NEXT_PUBLIC_API_URL}/anvils/get_network?anvil_uuid=`, + anvil?.anvil_uuid, + ); + + const processed = processNetworkData(data); -const Network = (): JSX.Element => { return ( - - - - - + + {data && + processed.bonds.map((bond: ProcessedBond) => { + return ( + <> + + +
+ + + + + + {bond.nodes.map( + (node): JSX.Element => ( + + + + + + + ), + )} + + + ); + })} ); }; diff --git a/striker-ui/components/processNetwork.ts b/striker-ui/components/processNetwork.ts new file mode 100644 index 00000000..bff730dc --- /dev/null +++ b/striker-ui/components/processNetwork.ts @@ -0,0 +1,36 @@ +const processNetworkData = (data: AnvilNetwork): ProcessedNetwork => { + const processedBonds: string[] = []; + const thingy: ProcessedNetwork = { bonds: [] }; + + data?.nodes.forEach((node) => { + node.bonds.forEach((bond) => { + const index = processedBonds.findIndex( + (processed: string) => processed === bond.bond_name, + ); + + if (index === -1) { + processedBonds.push(bond.bond_name); + thingy.bonds.push({ + bond_name: bond.bond_name, + bond_uuid: bond.bond_uuid, + nodes: [ + { + host_name: node.host_name, + host_uuid: node.host_uuid, + link: bond.links[0].is_active ? bond.links[0] : bond.links[1], + }, + ], + }); + } else { + thingy.bonds[index].nodes.push({ + host_name: node.host_name, + host_uuid: node.host_uuid, + link: bond.links[0].is_active ? bond.links[0] : bond.links[1], + }); + } + }); + }); + return thingy; +}; + +export default processNetworkData; diff --git a/striker-ui/types/AnvilNetwork.d.ts b/striker-ui/types/AnvilNetwork.d.ts new file mode 100644 index 00000000..338917c1 --- /dev/null +++ b/striker-ui/types/AnvilNetwork.d.ts @@ -0,0 +1,43 @@ +declare type AnvilNetworkBondLink = { + link_name: string; + link_uuid: string; + link_speed: number; + link_state: 'optimal' | 'degraded'; + is_active: boolean; +}; + +declare type AnvilNetworkNodeBond = { + bond_name: string; + bond_uuid: string; + links: Array; +}; + +declare type AnvilNetworkNode = { + host_name: string; + host_uuid: string; + bonds: Array; +}; + +declare type AnvilNetwork = { + nodes: Array; +}; + +declare type ProcessedBond = { + bond_name: string; + bond_uuid: string; + nodes: Array<{ + host_name: string; + host_uuid: string; + link: { + link_name: string; + link_uuid: string; + link_speed: number; + link_state: 'optimal' | 'degraded'; + is_active: boolean; + }; + }>; +}; + +declare type ProcessedNetwork = { + bonds: Array; +};