Skip to content

Commit

Permalink
fix: fix deduplication of links when name attribute is not used
Browse files Browse the repository at this point in the history
In some twingraph instances, arcs and edges are identified by an 'id'
column, and not a 'name'. This fix makes the use of the id column the
new default behavior, because ids are supposed to be uniques (whereas
entity and link names can be reused).
If no id is defined for links, the webapp will try to fall back to
the previous behavior by looking for the 'name' attribute.
  • Loading branch information
csm-thu committed Jan 28, 2025
1 parent 338546a commit 01c4eb3
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/views/Instance/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ async function _fetchTwingraphDatasetContent(organizationId, datasetId, dataSour
'properties, src.id as src, dst.id as dst'
);
}
return { nodes, edges };
return { nodes, edges, edgeCategories };
}

async function _fetchDataFromTwingraphDatasets(organizationId, datasets) {
Expand All @@ -186,9 +186,13 @@ async function _fetchDataFromTwingraphDatasets(organizationId, datasets) {
if (content[label] === undefined) content[label] = [];
if (content[label].find((item) => item.id === node.id) === undefined) content[label].push(node);
};
const addEdge = (src, rel, dst, type) => {
const addEdge = (src, rel, dst, type, identityAttribute) => {
if (content[type] === undefined) content[type] = [];
if (!content[type].some((item) => item.name === rel.name && item.source === src && item.target === dst))
if (
!content[type].some(
(item) => item[identityAttribute] === rel[identityAttribute] && item.source === src && item.target === dst
)
)
content[type].push({
source: src,
target: dst,
Expand All @@ -197,16 +201,28 @@ async function _fetchDataFromTwingraphDatasets(organizationId, datasets) {
};

for (const datasetId of datasets) {
const { nodes, edges } = await _fetchTwingraphDatasetContent(organizationId, datasetId);
const {
nodes,
edges,
edgeCategories: edgeAttributesByType,
} = await _fetchTwingraphDatasetContent(organizationId, datasetId);

for (const label in nodes) {
for (const node of nodes[label]) {
addNode(node, label);
}
}

for (const type in edges) {
const edgeAttributes = edgeAttributesByType.find((el) => el.type === type)?.keys ?? [];
let edgeIdentityAttribute = 'id';
if (!edgeAttributes.includes('id')) {
if (!edgeAttributes.includes('name')) {
console.warn(`Links of type ${type} don't have "id" nor "name" attributes. Some arcs may be lost.`);
} else edgeIdentityAttribute = 'name';
}
for (const edge of edges[type]) {
addEdge(edge.src, edge.properties, edge.dst, type);
addEdge(edge.src, edge.properties, edge.dst, type, edgeIdentityAttribute);
}
}
}
Expand Down

0 comments on commit 01c4eb3

Please sign in to comment.