Skip to content

Commit

Permalink
Merge branch 'story-new-graphql-proxy' into add-ptp-diff-synce
Browse files Browse the repository at this point in the history
  • Loading branch information
plehocky authored Feb 2, 2024
2 parents d18cf65 + 55275f2 commit a3f613d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 36 deletions.
3 changes: 1 addition & 2 deletions src/__generated__/topology-discovery.graphql.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 4 additions & 28 deletions src/external-api/topology-discovery-graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import {
GetBackupsQuery,
GetCommonNodesQuery,
GetCommonNodesQueryVariables,
GetHasAndInterfacesQuery,
GetLinksAndDevicesQuery,
GetShortestPathQuery,
GetShortestPathQueryVariables,
NetTopologyQuery,
Expand All @@ -24,14 +22,7 @@ import {
SyncePathToGrandMasterQuery,
SyncePathToGrandMasterQueryVariables,
} from '../__generated__/topology-discovery.graphql';
import {
HasAndInterfacesOutput,
LinksAndDevicesOutput,
TopologyDiffOutput,
decodeHasAndInterfacesOutput,
decodeLinksAndDevicesOutput,
decodeTopologyDiffOutput,
} from './topology-network-types';
import { TopologyDiffOutput, decodeTopologyDiffOutput } from './topology-network-types';

type CoordinatesParam = {
device: string;
Expand Down Expand Up @@ -60,6 +51,9 @@ const GET_TOPOLOGY_DEVICES = gql`
node {
id
name
status
labels
routerId
coordinates {
x
y
Expand Down Expand Up @@ -434,22 +428,6 @@ function getTopologyDiscoveryApi() {
return json;
}

async function getHasAndInterfaces(): Promise<HasAndInterfacesOutput> {
const response = await client.request<GetHasAndInterfacesQuery>(GET_HAS_AND_INTERFACES);
const { phyHasAndInterfaces } = response;
const json = decodeHasAndInterfacesOutput(phyHasAndInterfaces.phy_has_and_interfaces_data);

return json;
}

async function getLinksAndDevices(): Promise<LinksAndDevicesOutput> {
const response = await client.request<GetLinksAndDevicesQuery>(GET_LINKS_AND_DEVICES);
const { phyLinksAndDevices } = response;
const json = decodeLinksAndDevicesOutput(phyLinksAndDevices.phy_links_and_devices_data);

return json;
}

async function getCommonNodes(selectedNodes: string[]): Promise<string[]> {
const response = await client.request<GetCommonNodesQuery, GetCommonNodesQueryVariables>(GET_COMMON_NODES, {
selectedNodes,
Expand Down Expand Up @@ -517,8 +495,6 @@ function getTopologyDiscoveryApi() {
getShortestPath,
getBackups,
getTopologyDiff,
getHasAndInterfaces,
getLinksAndDevices,
getCommonNodes,
getPtpTopology,
getPtpPathToGrandMaster,
Expand Down
65 changes: 64 additions & 1 deletion src/helpers/topology.helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { device as PrismaDevice } from '@prisma/client';
import {
NetTopologyQuery,
PhyDevice,
PtpTopologyQuery,
SynceTopologyQuery,
TopologyDevicesQuery,
Expand Down Expand Up @@ -73,6 +74,52 @@ export function getOldTopologyDevices(devices: ArangoDevice[], diffData: Topolog
return oldDevices;
}

type PhyDeviceWithoutInterfaces = Omit<PhyDevice, 'phyInterfaces' | 'netDevice'>;

export function convertPhyDeviceToArangoDevice(phyDevice: PhyDeviceWithoutInterfaces): ArangoDevice {
const { name, status, coordinates, details, labels } = phyDevice;
return {
_id: phyDevice.id,
_key: phyDevice.id,
coordinates,
details,
labels: labels ?? [],
name,
status,
};
}

export function getNodesFromTopologyQuery(query: TopologyDevicesQuery): ArangoDevice[] {
const nodes =
query.phyDevices.edges
?.map((e) => e?.node)
.filter(omitNullValue)
.map(convertPhyDeviceToArangoDevice) ?? [];
return nodes;
}

export function getEdgesFromTopologyQuery(query: TopologyDevicesQuery): ArangoEdge[] {
const currentEdges = query.phyDevices.edges?.flatMap(
(e) =>
e?.node?.phyInterfaces.edges
?.map((i) => i?.node)
.filter(omitNullValue)
.map((i) => {
if (!i.phyLink?.idLink) {
return null;
}
return {
_id: i.phyLink.idLink,
_key: i.phyLink.idLink,
_from: i.id,
_to: i.phyLink.id,
};
})
.filter(omitNullValue) ?? [],
);
return currentEdges ?? [];
}

export function getOldTopologyInterfaceEdges(
interfaceEdges: ArangoEdgeWithStatus[],
diffData: TopologyDiffOutput,
Expand Down Expand Up @@ -211,7 +258,7 @@ export function makeTopologyNodes(dbDevices: PrismaDevice[], topologyDevices?: T
return nodes;
}

function getTopologyInterfaces(topologyDevices: TopologyDevicesQuery) {
export function getTopologyInterfaces(topologyDevices: TopologyDevicesQuery) {
return (
topologyDevices.phyDevices.edges?.flatMap((e) => {
const node = e?.node;
Expand All @@ -226,6 +273,7 @@ function getTopologyInterfaces(topologyDevices: TopologyDevicesQuery) {
}
return {
...inode,
_id: inode.id,
nodeId: node.name,
};
})
Expand All @@ -235,6 +283,21 @@ function getTopologyInterfaces(topologyDevices: TopologyDevicesQuery) {
);
}

export function getDeviceInterfaceEdges(topologyDevices: TopologyDevicesQuery): ArangoEdgeWithStatus[] {
return (
topologyDevices.phyDevices.edges?.flatMap(
(d) =>
d?.node?.phyInterfaces.edges?.map((i) => ({
_id: `${d.node?.id}-${i?.node?.id}`,
_key: i?.node?.phyLink?.id ?? '', // INFO: idHas was removed
_from: d.node?.id ?? '',
_to: i?.node?.id ?? '',
status: d.node?.status ?? 'unknown',
})) ?? [],
) ?? []
);
}

export function makeTopologyEdges(topologyDevices?: TopologyDevicesQuery) {
if (!topologyDevices) {
return [];
Expand Down
18 changes: 13 additions & 5 deletions src/schema/topology.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ import {
import config from '../config';
import { fromGraphId, toGraphId } from '../helpers/id-helper';
import {
getDeviceInterfaceEdges,
getEdgesFromTopologyQuery,
getFilterQuery,
getNodesFromTopologyQuery,
getOldTopologyConnectedEdges,
getOldTopologyDevices,
getOldTopologyInterfaceEdges,
getTopologyInterfaces,
makeInterfaceDeviceMap,
makeInterfaceMap,
makeInterfaceNameMap,
Expand Down Expand Up @@ -280,14 +284,18 @@ export const TopologyVersionDataQuery = extendType({
};
}

const { nodes, edges } = await topologyDiscoveryGraphQLAPI.getLinksAndDevices();
const topologyDevicesResult = await topologyDiscoveryGraphQLAPI.getTopologyDevices();

const currentNodes = getNodesFromTopologyQuery(topologyDevicesResult);
const currentEdges = getEdgesFromTopologyQuery(topologyDevicesResult);

const interfaces = getTopologyInterfaces(topologyDevicesResult);
const interfaceEdges = getDeviceInterfaceEdges(topologyDevicesResult);

const { version } = args;
const result = await topologyDiscoveryGraphQLAPI.getTopologyDiff(version);
const oldDevices = getOldTopologyDevices(nodes, result);
const oldDevices = getOldTopologyDevices(currentNodes, result);

// get interface edges for old version
const { has: interfaceEdges, interfaces } = await topologyDiscoveryGraphQLAPI.getHasAndInterfaces();
const oldInterfaceEdges = getOldTopologyInterfaceEdges(interfaceEdges, result);
const interfaceDeviceMap = makeInterfaceDeviceMap(oldInterfaceEdges);
const interfaceNameMap = makeInterfaceNameMap(
Expand All @@ -303,7 +311,7 @@ export const TopologyVersionDataQuery = extendType({
const interfaceMap = makeInterfaceMap(oldInterfaceEdges, interfaceNameMap);
const nodesMap = makeNodesMap(oldDevices, (d) => d.name);

const oldEdges = getOldTopologyConnectedEdges(edges, result)
const oldEdges = getOldTopologyConnectedEdges(currentEdges, result)
.map((e) => ({
id: e._id,
source: {
Expand Down

0 comments on commit a3f613d

Please sign in to comment.