Skip to content

Commit

Permalink
feat: allow for multiple relationships between different configs
Browse files Browse the repository at this point in the history
Fixes #1999
  • Loading branch information
mainawycliffe committed Jun 6, 2024
1 parent 9516a8b commit 0a62c7f
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 54 deletions.
2 changes: 1 addition & 1 deletion src/api/types/configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export interface ConfigItem extends Timestamped, Avatar, Agent, Costs {
config?: Record<string, any>;
description?: string | null;
health?: "healthy" | "unhealthy" | "warn" | "unknown";
related_id?: string;
related_ids?: string[];
agent?: {
id: string;
name: string;
Expand Down
45 changes: 24 additions & 21 deletions src/components/Configs/Graph/ConfigRelationshipGraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type ConfigGraphNodes =
config: Pick<
ConfigRelationships,
| "id"
| "related_id"
| "related_ids"
| "direction"
| "type"
| "name"
Expand All @@ -33,7 +33,7 @@ export type ConfigGraphNodes =
configs: Pick<
ConfigRelationships,
| "id"
| "related_id"
| "related_ids"
| "direction"
| "type"
| "name"
Expand Down Expand Up @@ -61,32 +61,35 @@ export function ConfigRelationshipGraph({
);

const edges: Edge<ConfigGraphNodes>[] = useMemo(() => {
return configsForGraph.map((config) => {
const e: Edge<ConfigGraphNodes>[] = [];
configsForGraph.forEach((config) => {
if (config.nodeType === "config") {
return {
id: `${config.config.id}-related-to-${config.config.related_id}`,
config.config.related_ids?.forEach((related_id) => {
e.push({
id: `${config.config.id}-related-to-${related_id}`,
source:
config.config.direction === "incoming"
? config.config.id
: related_id,
target:
config.config.direction === "incoming"
? related_id
: config.config.id
} satisfies Edge);
});
} else {
e.push({
id: `${config.id}-related-to-${config.related_id}`,
source:
config.config.direction === "incoming"
? config.config.id
: config.config.related_id!,
config.direction === "incoming" ? config.id : config.related_id!,
target:
config.config.direction === "incoming"
? config.config.related_id!
: config.config.id
} satisfies Edge;
config.direction === "incoming" ? config.related_id! : config.id
} satisfies Edge);
}

return {
id: `${config.id}-related-to-${config.related_id}`,
source:
config.direction === "incoming" ? config.id : config.related_id!,
target: config.direction === "incoming" ? config.related_id! : config.id
} satisfies Edge;
});
return e;
}, [configsForGraph]);

console.log("configsForGraph", JSON.stringify(configsForGraph, null, 2));

const nodes: Node<ConfigGraphNodes>[] = useMemo(() => {
// break this down by config types
return configsForGraph.map((config) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,32 @@ import {
// Test data for ConfigRelationships
const configs: Pick<
ConfigRelationships,
"id" | "related_id" | "direction" | "type"
"id" | "related_ids" | "direction" | "type"
>[] = [
{
id: "1",
related_id: "2",
related_ids: ["2"],
direction: "outgoing",
type: "type1"
},
{
id: "2",
related_id: "3",
related_ids: ["3"],
direction: "incoming",
type: "type2"
},
{
id: "3",
related_id: "1",
related_ids: ["1"],
direction: "outgoing",
type: "type3"
}
];

// Test data for ConfigItem
const currentConfig: Pick<ConfigItem, "id" | "related_id" | "type"> = {
const currentConfig: Pick<ConfigItem, "id" | "related_ids" | "type"> = {
id: "4",
related_id: "1",
related_ids: ["1"],
type: "type4"
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const applicationSetController = [
type: "Kubernetes::ReplicaSet",
relation_type: "outgoing",
direction: "outgoing",
related_id: "9580a93f-7b74-437b-836f-9a419252f3dd",
related_ids: ["9580a93f-7b74-437b-836f-9a419252f3dd"],
depth: 1,
tags: {
cluster: "aws",
Expand Down Expand Up @@ -38,7 +38,7 @@ export const applicationSetController = [
type: "Kubernetes::ReplicaSet",
relation_type: "outgoing",
direction: "outgoing",
related_id: "9580a93f-7b74-437b-836f-9a419252f3dd",
related_ids: ["9580a93f-7b74-437b-836f-9a419252f3dd"],
depth: 1,
tags: {
cluster: "aws",
Expand Down Expand Up @@ -70,7 +70,7 @@ export const applicationSetController = [
type: "Kubernetes::ReplicaSet",
relation_type: "outgoing",
direction: "outgoing",
related_id: "9580a93f-7b74-437b-836f-9a419252f3dd",
related_ids: ["9580a93f-7b74-437b-836f-9a419252f3dd"],
depth: 1,
tags: {
cluster: "aws",
Expand All @@ -90,7 +90,7 @@ export const applicationSetController = [
type: "Kubernetes::Namespace",
relation_type: "incoming",
direction: "incoming",
related_id: "9580a93f-7b74-437b-836f-9a419252f3dd",
related_ids: ["9580a93f-7b74-437b-836f-9a419252f3dd"],
depth: 1,
tags: {
cluster: "aws"
Expand All @@ -109,7 +109,7 @@ export const applicationSetController = [
type: "Kubernetes::ReplicaSet",
relation_type: "outgoing",
direction: "outgoing",
related_id: "9580a93f-7b74-437b-836f-9a419252f3dd",
related_ids: ["9580a93f-7b74-437b-836f-9a419252f3dd"],
depth: 1,
tags: {
cluster: "aws",
Expand Down Expand Up @@ -141,7 +141,7 @@ export const applicationSetController = [
type: "Kubernetes::Cluster",
relation_type: "hard",
direction: "incoming",
related_id: "f5c71026-fcab-4709-ae00-2edcc7e3086a",
related_ids: ["f5c71026-fcab-4709-ae00-2edcc7e3086a"],
depth: 2,
tags: {
cluster: "aws"
Expand All @@ -159,7 +159,7 @@ export const applicationSetController = [
type: "Kubernetes::Pod",
relation_type: "hard",
direction: "outgoing",
related_id: "532fb54a-5325-431a-a618-0617020b1ab2",
related_ids: ["532fb54a-5325-431a-a618-0617020b1ab2"],
depth: 2,
tags: {
cluster: "aws",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const argoArgocdNotificationsController = [
type: "Kubernetes::ReplicaSet",
relation_type: "outgoing",
direction: "outgoing",
related_id: "b59c1752-1be4-4a51-9f84-8225ebe7c136",
related_ids: ["b59c1752-1be4-4a51-9f84-8225ebe7c136"],
depth: 1,
tags: {
cluster: "aws",
Expand Down Expand Up @@ -39,7 +39,7 @@ export const argoArgocdNotificationsController = [
type: "Kubernetes::ReplicaSet",
relation_type: "outgoing",
direction: "outgoing",
related_id: "b59c1752-1be4-4a51-9f84-8225ebe7c136",
related_ids: ["b59c1752-1be4-4a51-9f84-8225ebe7c136"],
depth: 1,
tags: {
cluster: "aws",
Expand Down Expand Up @@ -71,7 +71,7 @@ export const argoArgocdNotificationsController = [
type: "Kubernetes::ReplicaSet",
relation_type: "outgoing",
direction: "outgoing",
related_id: "b59c1752-1be4-4a51-9f84-8225ebe7c136",
related_ids: ["b59c1752-1be4-4a51-9f84-8225ebe7c136"],
depth: 1,
tags: {
cluster: "aws",
Expand Down Expand Up @@ -103,7 +103,7 @@ export const argoArgocdNotificationsController = [
type: "Kubernetes::Namespace",
relation_type: "incoming",
direction: "incoming",
related_id: "b59c1752-1be4-4a51-9f84-8225ebe7c136",
related_ids: ["b59c1752-1be4-4a51-9f84-8225ebe7c136"],
depth: 1,
tags: {
cluster: "aws"
Expand All @@ -122,7 +122,7 @@ export const argoArgocdNotificationsController = [
type: "Kubernetes::ReplicaSet",
relation_type: "outgoing",
direction: "outgoing",
related_id: "b59c1752-1be4-4a51-9f84-8225ebe7c136",
related_ids: ["b59c1752-1be4-4a51-9f84-8225ebe7c136"],
depth: 1,
tags: {
cluster: "aws",
Expand All @@ -149,7 +149,7 @@ export const argoArgocdNotificationsController = [
type: "Kubernetes::Pod",
relation_type: "hard",
direction: "outgoing",
related_id: "301902be-86af-4791-95fe-3f432c190276",
related_ids: ["301902be-86af-4791-95fe-3f432c190276"],
depth: 2,
tags: {
cluster: "aws",
Expand Down Expand Up @@ -186,7 +186,7 @@ export const argoArgocdNotificationsController = [
type: "Kubernetes::Cluster",
relation_type: "hard",
direction: "incoming",
related_id: "f5c71026-fcab-4709-ae00-2edcc7e3086a",
related_ids: ["f5c71026-fcab-4709-ae00-2edcc7e3086a"],
depth: 2,
tags: {
cluster: "aws"
Expand Down
25 changes: 13 additions & 12 deletions src/components/Configs/Graph/formatConfigsForGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import { ConfigGraphNodes } from "./ConfigRelationshipGraph";
export function checkIfConfigHasMoreThan3Siblings(
configList: Pick<
ConfigRelationships,
"id" | "related_id" | "direction" | "type"
"id" | "related_ids" | "direction" | "type"
>[],
configItem: Pick<
ConfigRelationships,
"id" | "related_id" | "direction" | "type"
"id" | "related_ids" | "direction" | "type"
>
) {
return (
configList.filter(
(config) =>
config.related_id === configItem.related_id &&
config.related_ids === configItem.related_ids &&
config.direction === configItem.direction
).length > 3
);
Expand All @@ -26,17 +26,18 @@ export function checkIfConfigHasMoreThan3Siblings(
export function shouldCreateRootIntermediaryNode(
configList: Pick<
ConfigRelationships,
"id" | "related_id" | "direction" | "type"
"id" | "related_ids" | "direction" | "type"
>[],
configItem: Pick<
ConfigRelationships,
"id" | "related_id" | "direction" | "type"
"id" | "related_ids" | "direction" | "type"
>
) {
const types = configList
.filter(
(config) =>
config.related_id === configItem.id && config.direction === "outgoing"
config.related_ids?.includes(configItem.id) &&
config.direction === "outgoing"
)
.reduce((acc, config) => {
acc.add(config.type);
Expand All @@ -49,9 +50,9 @@ export function shouldCreateRootIntermediaryNode(
export function prepareConfigsForGraph(
configs: Pick<
ConfigRelationships,
"id" | "related_id" | "direction" | "type"
"id" | "related_ids" | "direction" | "type"
>[],
currentConfig: Pick<ConfigItem, "id" | "related_id" | "type">
currentConfig: Pick<ConfigItem, "id" | "related_ids" | "type">
) {
const transformedConfigs: ConfigGraphNodes[] = [];

Expand All @@ -78,7 +79,7 @@ export function prepareConfigsForGraph(
// intermediary node id
const childrenConfigs = allConfigs.filter(
(configItem) =>
configItem.related_id === config.id &&
configItem.related_ids?.includes(config.id) &&
configItem.direction === "outgoing"
);

Expand Down Expand Up @@ -124,7 +125,7 @@ export function prepareConfigsForGraph(
// determine the number of children the config has, based on the type
const siblingsConfigByType = allConfigs.filter(
(configItem) =>
configItem.related_id === config.id &&
configItem.related_ids?.includes(config.id) &&
configItem.direction === "outgoing" &&
configItem.type === child.type
);
Expand Down Expand Up @@ -162,7 +163,7 @@ export function prepareConfigsForGraph(
nodeType: "config",
config: {
...(child as ConfigRelationships),
related_id: intermediaryNodeID
related_ids: [intermediaryNodeID]
}
});
} else {
Expand All @@ -172,7 +173,7 @@ export function prepareConfigsForGraph(
nodeType: "config",
config: {
...(child as ConfigRelationships),
related_id: rootConfigIntermediaryNodeID ?? config.id
related_ids: [rootConfigIntermediaryNodeID ?? config.id]
}
} satisfies ConfigGraphNodes);
}
Expand Down

0 comments on commit 0a62c7f

Please sign in to comment.