Skip to content

Commit

Permalink
chore(mesh-wide): refactor to use links coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
selankon committed Jul 4, 2024
1 parent ff9f3f3 commit edf483b
Show file tree
Hide file tree
Showing 11 changed files with 261 additions and 104 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Trans } from "@lingui/macro";
import { useMemo } from "preact/compat";
import { useState } from "preact/hooks";
import { useCallback } from "react";

Expand All @@ -12,6 +13,7 @@ import {
getQueryByLinkType,
usePointToPointErrors,
} from "plugins/lime-plugin-mesh-wide/src/hooks/useLocatedLinks";
import { useNodes } from "plugins/lime-plugin-mesh-wide/src/hooks/useNodes";
import { MacToMacLink } from "plugins/lime-plugin-mesh-wide/src/lib/links/PointToPointLink";
import { readableBytes } from "plugins/lime-plugin-mesh-wide/src/lib/utils";
import { useSetLinkReferenceState } from "plugins/lime-plugin-mesh-wide/src/meshWideQueries";
Expand Down Expand Up @@ -227,7 +229,11 @@ export const LinkReferenceStatus = ({ reference }: LinkMapFeature) => {
type: reference.type,
});

const isDown = !errors.linkUp;
const {
allNodes: { meshWideNodesReference, meshWideNodesActual },
} = useNodes();

const isDown = !errors?.linkUp;

// Check if there are errors of global reference state to shown
const { reference: fetchDataReference } = getQueryByLinkType(
Expand All @@ -246,10 +252,28 @@ export const LinkReferenceStatus = ({ reference }: LinkMapFeature) => {
const { showToast } = useToast();

// Generate a list of nodes to update
const nodesToUpdate = reference.nodes.reduce((acc, node) => {
acc[node.ipv4] = node.hostname;
return acc;
}, {});
// const nodesToUpdate = reference.nodes.reduce((acc, node) => {
// acc[node.ipv4] = node.hostname;
// return acc;
// }, {});

// Get nodes to update
const nodesToUpdate = useMemo(() => {
// First get an object with all nodes
const allNodes = {
...(meshWideNodesReference || {}),
...(meshWideNodesActual || {}),
};
if (!allNodes) return {};
// Then reduce the nodes to update
return reference.nodes.reduce((acc, node) => {
// If the node with node name exist get the ipv4 and hostname
if (allNodes[node]) {
acc[allNodes[node].ipv4] = allNodes[node].hostname;
}
return acc;
}, {});
}, [meshWideNodesReference, meshWideNodesActual, reference.nodes]);

// todo(kon): Sync mutations, used to sync data between nodes and local node after setting the reference state
// useSharedStateSync
Expand Down
2 changes: 2 additions & 0 deletions plugins/lime-plugin-mesh-wide/src/components/Map/LinkLine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export const LinkLine = ({ referenceLink, actualLink }: ILinkLineProps) => {
};
};

if (linkToShow.hasInValidCoordinates()) return <></>;
console.log("LinkToShow", linkToShow);
const coordinates = linkToShow.coordinates.map((c) => [c.lat, c.long]);

return (
Expand Down
8 changes: 3 additions & 5 deletions plugins/lime-plugin-mesh-wide/src/containers/Map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ import {
useLoadLeaflet,
useLocation,
} from "plugins/lime-plugin-locate/src/locateQueries";
import {
BatmanLinksLayer,
WifiLinksLayer,
} from "plugins/lime-plugin-mesh-wide/src/containers/MapLayers/LinksLayers";
import { WifiLinksLayer } from "plugins/lime-plugin-mesh-wide/src/containers/MapLayers/LinksLayers";
import NodesLayer from "plugins/lime-plugin-mesh-wide/src/containers/MapLayers/NodesLayer";
import { useSelectedMapFeature } from "plugins/lime-plugin-mesh-wide/src/meshWideQueries";

Expand Down Expand Up @@ -78,13 +75,14 @@ export const MeshWideMap = ({
}
}, [loading, nodeLocation]);

// @ts-ignore
const mapSupportedLayers: Record<
keyof MeshWideMapTypes,
{ name: string; layer: ComponentChildren }
> = {
node_info: { name: "Nodes", layer: <NodesLayer /> },
wifi_links_info: { name: "Wifi Links", layer: <WifiLinksLayer /> },
bat_links_info: { name: "Batman", layer: <BatmanLinksLayer /> },
// bat_links_info: { name: "Batman", layer: <BatmanLinksLayer /> },
};

return (
Expand Down
12 changes: 6 additions & 6 deletions plugins/lime-plugin-mesh-wide/src/hooks/useLocatedLinks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,18 @@ const useCalculateLocatedLinks = ({
} = useNodes();

const locatedLinksReference: LocatedLinkData = useMemo(() => {
if (meshWideNodes && linksReference) {
if (linksReference) {
return mergeLinksAndCoordinates(
meshWideNodes,
linksReference,
type
type,
meshWideNodes
);
}
}, [meshWideNodes, linksReference, type]);
}, [linksReference, meshWideNodes, type]);

const locatedLinks: LocatedLinkData = useMemo(() => {
if (links && meshWideNodes) {
return mergeLinksAndCoordinates(meshWideNodes, links, type);
if (links) {
return mergeLinksAndCoordinates(links, type, meshWideNodes);
}
}, [links, meshWideNodes, type]);

Expand Down
12 changes: 6 additions & 6 deletions plugins/lime-plugin-mesh-wide/src/hooks/useNodes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ interface NodesContextType {
meshWideNodesReference: INodes;
meshWideNodesActual: INodes;
};
// Invalid nodes doesn't contain a correct lat long
invalidNodes: {
invalidNodesReference: INodes;
invalidNodesActual: INodes;
};
locatedNodes: {
locatedNodesReference: INodes;
locatedNodesActual: INodes;
allLocatedNodes: INodes;
locatedNewNodes: INodes;
locatedNodesReference: INodes; // All located reference nodes
locatedNodesActual: INodes; // Located nodes of the actual state
allLocatedNodes: INodes; // Sum of actual state and ref state
locatedNewNodes: INodes; // New nodes (not on the ref state)
};
}

Expand Down Expand Up @@ -99,7 +100,7 @@ export const NodesProvider = ({
}, {} as INodes);
}

// Used to have on an a single list all the located nodes
// Used to have on a single list all the located nodes
// This is used to have an easier way to draw links between nodes
// that are not active, or not on reference or new
const allLocatedNodes = {
Expand All @@ -116,7 +117,6 @@ export const NodesProvider = ({
meshWideNodesReference,
meshWideNodesActual,
},
// Invalid nodes doesn't contain a correct lat long
invalidNodes: {
invalidNodesReference,
invalidNodesActual,
Expand Down
60 changes: 43 additions & 17 deletions plugins/lime-plugin-mesh-wide/src/lib/links/PointToPointLink.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {
BaseMacToMacLink,
Coordinates,
ILocatedLink,
INodeInfo,
IBaseLink,
LinkDataTypes,
LinkType,
MacToMacLinkId,
Expand All @@ -16,19 +15,26 @@ import {
*/
export class PontToPointLink {
private _links: BaseMacToMacLink[] = [];
private _nodes: INodeInfo[] = [];
// private _nodes: INodeInfo[] = [];
public readonly id: PointToPointLinkId;
public readonly coordinates: Coordinates[] = [];

constructor(node1: INodeInfo, node2: INodeInfo) {
const coord1 = node1.coordinates;
const coord2 = node2.coordinates;
public readonly coordinates: Array<Coordinates | undefined> = [];
private _nodes: Set<string> = new Set<string>([]);

// constructor(node1: INodeInfo, node2: INodeInfo) {
constructor(
coord1?: Coordinates | undefined,
coord2?: Coordinates | undefined
) {
// const coord1 = node1.coordinates;
// const coord2 = node2.coordinates;
this.id = PontToPointLink.generateId(coord1, coord2);
this.nodes.push(node1, node2);
// this.nodes.push(node1, node2);
this.coordinates.push(coord1, coord2);
}

addLink(link: typeof this._links[number]) {
console.log("addLink INNER", link);

this.links.push(link);
}

Expand All @@ -48,23 +54,31 @@ export class PontToPointLink {
}

get nodes() {
return this._nodes;
return [...this._nodes];
}

addNodes(nodes: string[]) {
nodes.forEach((node) => {
this._nodes.add(node);
});
}

/**
* Generate a deterministic unique id based on the coordinates of a node.
*
* It accepts undefined coordinets for those links which don't have all the information yet.
* @param coord1
* @param coord2
*/
static generateId(coord1: Coordinates, coord2: Coordinates): string {
static generateId(coord1?: Coordinates, coord2?: Coordinates): string {
const _prepareCoord = (coord: string) =>
parseFloat(coord.replace("-", "").replace(".", ""));

const allCoordinates = [
_prepareCoord(coord1.long),
_prepareCoord(coord1.lat),
_prepareCoord(coord2.long),
_prepareCoord(coord2.lat),
_prepareCoord(coord1?.long ?? "0"),
_prepareCoord(coord1?.lat ?? "0"),
_prepareCoord(coord2?.long ?? "0"),
_prepareCoord(coord2?.lat ?? "0"),
];

return allCoordinates.sort((a, b) => a - b).toString();
Expand All @@ -73,17 +87,29 @@ export class PontToPointLink {
get type(): LinkType {
return this._links[0].type;
}

/**
* Return true if coordinates are not undefined
*/
hasInValidCoordinates = () => {
return this.coordinates.some((coord) => coord === undefined);
};

// // todo(kon): do this on a best way
// hasValidCoordinates = () => {
// this.coordinates.any((coord) => coord !== undefined);
// };
}

/**
* Store link info between two macs
*/
export class MacToMacLink<T extends LinkType> {
private _data: ILocatedLink<T>;
private _data: IBaseLink<T>;
private _id: MacToMacLinkId;
public type: T;

constructor(id: MacToMacLinkId, data: ILocatedLink<T>, type: T) {
constructor(id: MacToMacLinkId, data: IBaseLink<T>, type: T) {
this._data = data;
this._id = id;
this.type = type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe("tests for the algorithm that merge point and links data types", () =>
const locatedLinksReference = mergeLinksAndCoordinates(
nodesReferenceState,
linksReferenceState,
// @ts-ignore
"wifi_links_info"
);
// Iterate between merged link objects
Expand All @@ -21,6 +22,7 @@ describe("tests for the algorithm that merge point and links data types", () =>
for (const link of merged.links) {
Object.entries(link.data).map(([name, linkData], i) => {
const node = nodesReferenceState[name];
// @ts-ignore
expect(linkData.coordinates).toBe(node.coordinates);
});
}
Expand Down
Loading

0 comments on commit edf483b

Please sign in to comment.