From eab71c2ac850662c66ae67b21161917f05f82bad Mon Sep 17 00:00:00 2001 From: selankon Date: Wed, 18 Sep 2024 16:54:46 +0200 Subject: [PATCH] chore(meshwide): implement babel links --- .../components/FeatureDetail/LinkDetail.tsx | 85 +++++++++++++++---- .../src/containers/Map.tsx | 8 ++ .../src/containers/MapLayers/LinksLayers.tsx | 20 +++++ .../src/hooks/useLocatedLinks.tsx | 32 ++++++- .../src/hooks/useMeshWideDataErrors.tsx | 22 ++++- .../src/lib/links/processLinkErrors.ts | 54 ++++++++---- .../lime-plugin-mesh-wide/src/lib/utils.ts | 4 + .../src/meshWidePage.tsx | 9 +- .../src/meshWideQueries.tsx | 27 ++++++ .../src/meshWideTypes.tsx | 20 ++++- .../shared-state/SharedStateTypes.ts | 2 + 11 files changed, 242 insertions(+), 41 deletions(-) diff --git a/plugins/lime-plugin-mesh-wide/src/components/FeatureDetail/LinkDetail.tsx b/plugins/lime-plugin-mesh-wide/src/components/FeatureDetail/LinkDetail.tsx index 4ae5478c..92dc3d69 100644 --- a/plugins/lime-plugin-mesh-wide/src/components/FeatureDetail/LinkDetail.tsx +++ b/plugins/lime-plugin-mesh-wide/src/components/FeatureDetail/LinkDetail.tsx @@ -21,8 +21,10 @@ import { } from "plugins/lime-plugin-mesh-wide/src/lib/utils"; import { useSetLinkReferenceState } from "plugins/lime-plugin-mesh-wide/src/meshWideQueries"; import { + BabelLinkErrorCodes, BaseMacToMacLink, BatmanLinkErrorCodes, + IBabelLinkData, IBatManLinkData, ILinkMtoMErrors, IWifiLinkData, @@ -63,6 +65,37 @@ const BatmanDetail = ({ ); }; +const BabelDetail = ({ + name, + errorsArray, + node, +}: { + name: string; + errorsArray: BabelLinkErrorCodes[] | undefined; + node: IBabelLinkData; +}) => { + return ( + <> + +
+ {name} + {errorsArray?.length > 0 && } +
+
+ + Iface}> + {node?.iface} + + + + Source IP}> + {node?.src_ip} + + + + ); +}; + const WifiDetail = ({ name, errorsArray, @@ -155,21 +188,43 @@ const SelectedLink = ({ {names.map((name, i) => { const node = linkDetail.linkByName(name); const errorsArray = errors?.linkErrors[name] ?? []; - return linkType === "wifi_links_info" ? ( - - ) : ( - - ); + switch (linkType) { + case "wifi_links_info": + return ( + + ); + case "babel_links_info": + return ( + + ); + case "bat_links_info": + return ( + + ); + default: + return Unknown link type; + } })} ); diff --git a/plugins/lime-plugin-mesh-wide/src/containers/Map.tsx b/plugins/lime-plugin-mesh-wide/src/containers/Map.tsx index d382b2e2..2da02c11 100644 --- a/plugins/lime-plugin-mesh-wide/src/containers/Map.tsx +++ b/plugins/lime-plugin-mesh-wide/src/containers/Map.tsx @@ -15,6 +15,7 @@ import { useLocation, } from "plugins/lime-plugin-locate/src/locateQueries"; import { + BabelLinksLayer, BatmanLinksLayer, WifiLinksLayer, } from "plugins/lime-plugin-mesh-wide/src/containers/MapLayers/LinksLayers"; @@ -29,12 +30,14 @@ interface ILayersChecked { nodes?: boolean; wifiLinks?: boolean; batmanLinks?: boolean; + babelLinks?: boolean; } export const MeshWideMap = ({ nodes = true, wifiLinks = true, batmanLinks = false, + babelLinks = false, }: ILayersChecked) => { const { data: selectedMapFeature, setData: setSelectedMapFeature } = useSelectedMapFeature(); @@ -94,6 +97,11 @@ export const MeshWideMap = ({ layer: , checked: batmanLinks, }, + babel_links_info: { + name: "Babel", + layer: , + checked: babelLinks, + }, }; return ( diff --git a/plugins/lime-plugin-mesh-wide/src/containers/MapLayers/LinksLayers.tsx b/plugins/lime-plugin-mesh-wide/src/containers/MapLayers/LinksLayers.tsx index 35f426b4..921f1603 100644 --- a/plugins/lime-plugin-mesh-wide/src/containers/MapLayers/LinksLayers.tsx +++ b/plugins/lime-plugin-mesh-wide/src/containers/MapLayers/LinksLayers.tsx @@ -81,3 +81,23 @@ export const BatmanLinksLayer = () => { ); }; + +export const BabelLinksLayer = () => { + const { + locatedNewLinks: newLinks, + locatedLinks, + locatedLinksReference, + linksLoaded, + } = useLocatedLinks({ type: "babel_links_info" }); + + return ( +
+ +
+ ); +}; diff --git a/plugins/lime-plugin-mesh-wide/src/hooks/useLocatedLinks.tsx b/plugins/lime-plugin-mesh-wide/src/hooks/useLocatedLinks.tsx index d9a0c14f..e27cacf1 100644 --- a/plugins/lime-plugin-mesh-wide/src/hooks/useLocatedLinks.tsx +++ b/plugins/lime-plugin-mesh-wide/src/hooks/useLocatedLinks.tsx @@ -8,6 +8,8 @@ import { PontToPointLink } from "plugins/lime-plugin-mesh-wide/src/lib/links/Poi import { mergeLinksAndCoordinates } from "plugins/lime-plugin-mesh-wide/src/lib/links/getLinksCoordinates"; import { compareLinks } from "plugins/lime-plugin-mesh-wide/src/lib/links/processLinkErrors"; import { + useMeshWideBabel, + useMeshWideBabelReference, useMeshWideBatman, useMeshWideBatmanReference, useMeshWideLinks, @@ -41,6 +43,11 @@ export const getQueryByLinkType = ( state: useMeshWideBatman, reference: useMeshWideBatmanReference, } as getQueryByLinkTypeReturnType; + case "babel_links_info": + return { + state: useMeshWideBabel, + reference: useMeshWideBabelReference, + } as getQueryByLinkTypeReturnType; case "wifi_links_info": default: return { @@ -166,18 +173,25 @@ export const usePointToPointErrors = ({ // Define separate contexts for each type of link const BatmanLinksContext = createContext(null); const MeshWideLinksContext = createContext(null); +const BabelLinksContext = createContext(null); // Export a hook that return the proper context based on the type of link export const useLocatedLinks = ({ type }: { type: LinkType }) => { + // By default use wifi links let requestedContext = MeshWideLinksContext; - if (type === "bat_links_info") { - requestedContext = BatmanLinksContext; + switch (type) { + case "bat_links_info": + requestedContext = BatmanLinksContext; + break; + case "babel_links_info": + requestedContext = BabelLinksContext; + break; } const context = useContext(requestedContext); if (context === null) { throw new Error( - `useLocatedLinks must be used within a provider for ${requestedContext} links` + `useLocatedLinks must be used within a provider for ${type} links` ); } return context; @@ -206,3 +220,15 @@ export const MeshWideLinksProvider = ({ children }) => { ); }; + +export const BabelLinksProvider = ({ children }) => { + const babelLinksData = useCalculateLocatedLinks({ + type: "babel_links_info", + }); + + return ( + + {children} + + ); +}; diff --git a/plugins/lime-plugin-mesh-wide/src/hooks/useMeshWideDataErrors.tsx b/plugins/lime-plugin-mesh-wide/src/hooks/useMeshWideDataErrors.tsx index 003eb16b..426f46c3 100644 --- a/plugins/lime-plugin-mesh-wide/src/hooks/useMeshWideDataErrors.tsx +++ b/plugins/lime-plugin-mesh-wide/src/hooks/useMeshWideDataErrors.tsx @@ -3,6 +3,8 @@ import { QueryKey } from "@tanstack/react-query"; import { sharedStateQueries } from "components/shared-state/SharedStateQueriesKeys"; import { + useMeshWideBabel, + useMeshWideBabelReference, useMeshWideBatman, useMeshWideBatmanReference, useMeshWideLinks, @@ -11,6 +13,7 @@ import { useMeshWideNodesReference, } from "plugins/lime-plugin-mesh-wide/src/meshWideQueries"; import { + IBabelLinks, IBatmanLinks, INodes, IWifiLinks, @@ -37,7 +40,7 @@ export const useMeshWideDataErrors = () => { const addError = ( queryKey: QueryKey, error?: unknown, - data?: IWifiLinks | IBatmanLinks | INodes + data?: IWifiLinks | IBatmanLinks | INodes | IBabelLinks ) => { if (data) { // Check also node by node if reference state is empty @@ -80,6 +83,23 @@ export const useMeshWideDataErrors = () => { batmanReferenceData ); + const { error: babelError } = useMeshWideBabel({}); + addError( + sharedStateQueries.getFromSharedState("babel_links_info"), + babelError + ); + + const { + data: babelReferenceData, + error: babelReferenceError, + isError: babelReferenceIsError, + } = useMeshWideBabelReference({}); + addError( + sharedStateQueries.getFromSharedState("babel_links_info_ref"), + babelReferenceIsError ? babelReferenceError : null, + babelReferenceData + ); + const { error: batmanError } = useMeshWideBatman({}); addError( sharedStateQueries.getFromSharedState("bat_links_info"), diff --git a/plugins/lime-plugin-mesh-wide/src/lib/links/processLinkErrors.ts b/plugins/lime-plugin-mesh-wide/src/lib/links/processLinkErrors.ts index ecc7f9aa..41892264 100644 --- a/plugins/lime-plugin-mesh-wide/src/lib/links/processLinkErrors.ts +++ b/plugins/lime-plugin-mesh-wide/src/lib/links/processLinkErrors.ts @@ -1,9 +1,12 @@ import { PontToPointLink } from "plugins/lime-plugin-mesh-wide/src/lib/links/PointToPointLink"; import { + BabelLinkErrorCodes, BatmanLinkErrorCodes, + IBabelLinkData, IBatManLinkData, ILinkPtoPErrors, IWifiLinkData, + LinksErrorCodesTypes, WifiLinkErrorCodes, } from "plugins/lime-plugin-mesh-wide/src/meshWideTypes"; @@ -49,6 +52,17 @@ const compareBatmanData = ( return errors; }; +const compareBabelData = ( + reference: IBabelLinkData, + actual: IBabelLinkData +) => { + const errors: BabelLinkErrorCodes[] = []; + if (actual === undefined) { + return [BabelLinkErrorCodes.LINK_DOWN]; + } + return errors; +}; + /** * Function that receive 2 PontToPointLink and iterate over every mac to mac link and its data executing a function * to compare the wifi data. @@ -94,16 +108,27 @@ export const compareLinks = ({ Object.entries(macToMacReference.data).forEach( ([nodeNameReference, wifiDataReference]) => { const wifiDataActual = macToMacActual?.data[nodeNameReference]; - const errors = - referenceLink.type === "wifi_links_info" - ? compareWifiData( - wifiDataReference as IWifiLinkData, - wifiDataActual as IWifiLinkData - ) - : compareBatmanData( - wifiDataReference as IBatManLinkData, - wifiDataActual as IBatManLinkData - ); + let errors: LinksErrorCodesTypes[] = []; + switch (referenceLink.type) { + case "wifi_links_info": + errors = compareWifiData( + wifiDataReference as IWifiLinkData, + wifiDataActual as IWifiLinkData + ); + break; + case "bat_links_info": + errors = compareBatmanData( + wifiDataReference as IBatManLinkData, + wifiDataActual as IBatManLinkData + ); + break; + case "babel_links_info": + errors = compareBabelData( + wifiDataReference as IBabelLinkData, + wifiDataActual as IBabelLinkData + ); + break; + } ptoPErrors.macToMacErrors[macToMacReference.id].linkErrors[ nodeNameReference ] = errors; @@ -112,14 +137,7 @@ export const compareLinks = ({ true; ptoPErrors.hasErrors = true; - if ( - (errors as WifiLinkErrorCodes[]).includes( - WifiLinkErrorCodes.LINK_DOWN - ) || - (errors as BatmanLinkErrorCodes[]).includes( - BatmanLinkErrorCodes.LINK_DOWN - ) - ) { + if (errors.includes(WifiLinkErrorCodes.LINK_DOWN)) { ptoPErrors.macToMacErrors[macToMacReference.id].linkUp = false; downCounter++; diff --git a/plugins/lime-plugin-mesh-wide/src/lib/utils.ts b/plugins/lime-plugin-mesh-wide/src/lib/utils.ts index 030a6a2e..0a979902 100644 --- a/plugins/lime-plugin-mesh-wide/src/lib/utils.ts +++ b/plugins/lime-plugin-mesh-wide/src/lib/utils.ts @@ -82,6 +82,10 @@ export const dataTypeNameMapping = (dataType: SharedStateDataTypeKeys) => { return t`Batman Links`; case "bat_links_info_ref": return t`Batman Links Reference`; + case "babel_links_info": + return t`Babel Links`; + case "babel_links_info_ref": + return t`Babel Links Reference`; default: return dataType; } diff --git a/plugins/lime-plugin-mesh-wide/src/meshWidePage.tsx b/plugins/lime-plugin-mesh-wide/src/meshWidePage.tsx index 45f0ab48..66a7fc56 100644 --- a/plugins/lime-plugin-mesh-wide/src/meshWidePage.tsx +++ b/plugins/lime-plugin-mesh-wide/src/meshWidePage.tsx @@ -8,6 +8,7 @@ import { FloatingAlert } from "plugins/lime-plugin-mesh-wide/src/components/Map/ import { MeshWideMap } from "plugins/lime-plugin-mesh-wide/src/containers/Map"; import { SelectedFeatureBottomSheet } from "plugins/lime-plugin-mesh-wide/src/containers/SelectedFeatureBottomSheet"; import { + BabelLinksProvider, BatmanLinksProvider, MeshWideLinksProvider, } from "plugins/lime-plugin-mesh-wide/src/hooks/useLocatedLinks"; @@ -54,9 +55,11 @@ const MeshWidePage = () => { return ( - - - + + + + + ); diff --git a/plugins/lime-plugin-mesh-wide/src/meshWideQueries.tsx b/plugins/lime-plugin-mesh-wide/src/meshWideQueries.tsx index f3bd9713..334c2dea 100644 --- a/plugins/lime-plugin-mesh-wide/src/meshWideQueries.tsx +++ b/plugins/lime-plugin-mesh-wide/src/meshWideQueries.tsx @@ -10,6 +10,7 @@ import { getQueryByLinkType } from "plugins/lime-plugin-mesh-wide/src/hooks/useL import { PontToPointLink } from "plugins/lime-plugin-mesh-wide/src/lib/links/PointToPointLink"; import { getMeshWideConfig } from "plugins/lime-plugin-mesh-wide/src/meshWideMocks"; import { + IBabelLinks, IBaseLink, IBatmanLinks, ILinks, @@ -79,6 +80,32 @@ export function useMeshWideBatman(params) { ); } +export function useMeshWideBabelReference(params) { + const dataType: MeshWideMapDataTypeKeys = "babel_links_info_ref"; + const queryKey = sharedStateQueries.getFromSharedState(dataType); + return useQuery( + queryKey, + () => doSharedStateApiCall(queryKey), + { + refetchInterval, + ...params, + } + ); +} + +export function useMeshWideBabel(params) { + const dataType: MeshWideMapDataTypeKeys = "babel_links_info"; + const queryKey = sharedStateQueries.getFromSharedState(dataType); + return useQuery( + queryKey, + () => doSharedStateApiCall(queryKey), + { + refetchInterval, + ...params, + } + ); +} + export function useMeshWideNodesReference(params) { const dataType: MeshWideMapDataTypeKeys = "node_info_ref"; const queryKey = sharedStateQueries.getFromSharedState(dataType); diff --git a/plugins/lime-plugin-mesh-wide/src/meshWideTypes.tsx b/plugins/lime-plugin-mesh-wide/src/meshWideTypes.tsx index ba5b5999..b8d0ca7c 100644 --- a/plugins/lime-plugin-mesh-wide/src/meshWideTypes.tsx +++ b/plugins/lime-plugin-mesh-wide/src/meshWideTypes.tsx @@ -17,6 +17,7 @@ import { export type LinkDataTypes = { wifi_links_info: IWifiLinkData; bat_links_info: IBatManLinkData; + babel_links_info: IBabelLinkData; }; export type LinkType = keyof LinkDataTypes; export type IBaseLink = { @@ -60,6 +61,11 @@ export type IBatManLinkData = { iface: string; }; +export type IBabelLinkData = { + src_ip: string; + iface: string; +}; + /** * List of Link info retrieved from the API */ @@ -72,6 +78,7 @@ export interface ILinks { export type IWifiLinks = ILinks<"wifi_links_info">; export type IBatmanLinks = ILinks<"bat_links_info">; +export type IBabelLinks = ILinks<"bat_links_info">; export type Coordinates = { lat: string; @@ -152,6 +159,15 @@ export enum BatmanLinkErrorCodes { LINK_DOWN = "LINK_DOWN", } +export enum BabelLinkErrorCodes { + LINK_DOWN = "LINK_DOWN", +} + +export type LinksErrorCodesTypes = + | WifiLinkErrorCodes + | BatmanLinkErrorCodes + | BabelLinkErrorCodes; + export enum NodeErrorCodes { NODE_DOWN = "NODE_DOWN", MACS_MISSMATCH = "MACS_MISSMATCH", @@ -162,7 +178,7 @@ export enum NodeErrorCodes { */ export type ILinkMtoMErrors = { linkErrors: { - [nodeName: string]: WifiLinkErrorCodes[] | BatmanLinkErrorCodes[]; + [nodeName: string]: LinksErrorCodesTypes[]; }; hasErrors: boolean; linkUp: boolean; @@ -202,9 +218,11 @@ const completeDataTypeKeys: CompleteDataTypeKeys = { node_info: true, wifi_links_info: true, bat_links_info: true, + babel_links_info: true, node_info_ref: true, wifi_links_info_ref: true, bat_links_info_ref: true, + babel_links_info_ref: true, }; export const getMeshWideMapTypes = () => { return Object.keys(completeDataTypeKeys) as MeshWideMapDataTypeKeys[]; diff --git a/src/components/shared-state/SharedStateTypes.ts b/src/components/shared-state/SharedStateTypes.ts index 79db7ffc..6f249666 100644 --- a/src/components/shared-state/SharedStateTypes.ts +++ b/src/components/shared-state/SharedStateTypes.ts @@ -1,5 +1,6 @@ import { MeshWideUpgradeInfo } from "plugins/lime-plugin-mesh-wide-upgrade/src/meshUpgradeTypes"; import { + IBabelLinks, IBatmanLinks, INodes, IWifiLinks, @@ -13,6 +14,7 @@ export type MeshWideMapTypes = { node_info: INodes; wifi_links_info: IWifiLinks; bat_links_info: IBatmanLinks; + babel_links_info: IBabelLinks; }; // Reference state types