🔍 Code Extractor

function generate_diagram_data

Maturity: 41

Transforms Neo4j schema information into a structured format suitable for graph visualization, creating separate node and edge data structures.

File:
/tf/active/vicechatdev/neo4j_schema_report.py
Lines:
245 - 275
Complexity:
simple

Purpose

This function processes schema metadata from a Neo4j database and converts it into a diagram-friendly format with nodes representing entity types and edges representing relationships. It's designed to facilitate visualization of database schemas in graph diagramming tools or UI components. The function extracts node labels, properties, counts, and relationship information to create a complete picture of the database structure.

Source Code

def generate_diagram_data(schema_info):
    """Generate data for visualizing the schema as a diagram"""
    nodes = []
    edges = []
    
    # Add nodes
    for label in schema_info["node_labels"]:
        nodes.append({
            "id": label,
            "label": label,
            "type": "node",
            "properties": schema_info["node_properties"].get(label, []),
            "count": schema_info["node_counts"].get(label, 0)
        })
    
    # Add edges
    for source_label, targets in schema_info["node_relationships"].items():
        for target_label, relationships in targets.items():
            for rel_info in relationships:
                edges.append({
                    "id": f"{source_label}_{rel_info['type']}_{target_label}",
                    "source": source_label,
                    "target": target_label,
                    "label": rel_info["type"],
                    "count": rel_info["count"]
                })
    
    return {
        "nodes": nodes,
        "edges": edges
    }

Parameters

Name Type Default Kind
schema_info - - positional_or_keyword

Parameter Details

schema_info: A dictionary containing Neo4j schema metadata with the following expected keys: 'node_labels' (list of node label strings), 'node_properties' (dict mapping labels to property lists), 'node_counts' (dict mapping labels to integer counts), and 'node_relationships' (nested dict structure mapping source labels to target labels to relationship information including 'type' and 'count')

Return Value

Returns a dictionary with two keys: 'nodes' (list of node objects, each containing 'id', 'label', 'type', 'properties', and 'count') and 'edges' (list of edge objects, each containing 'id', 'source', 'target', 'label', and 'count'). The 'nodes' list represents all entity types in the schema, while 'edges' represents all relationship types between entities.

Usage Example

schema_info = {
    "node_labels": ["Person", "Company"],
    "node_properties": {
        "Person": ["name", "age"],
        "Company": ["name", "industry"]
    },
    "node_counts": {
        "Person": 150,
        "Company": 50
    },
    "node_relationships": {
        "Person": {
            "Company": [
                {"type": "WORKS_AT", "count": 120}
            ]
        }
    }
}

diagram_data = generate_diagram_data(schema_info)
print(f"Nodes: {len(diagram_data['nodes'])}")
print(f"Edges: {len(diagram_data['edges'])}")
print(f"First node: {diagram_data['nodes'][0]}")
print(f"First edge: {diagram_data['edges'][0]}")

Best Practices

  • Ensure the schema_info dictionary contains all required keys ('node_labels', 'node_properties', 'node_counts', 'node_relationships') before calling this function
  • The function uses .get() with default values for node_properties and node_counts, so missing entries won't cause errors but will result in empty lists or zero counts
  • Edge IDs are generated using the pattern '{source}_{relationship_type}_{target}', which assumes this combination is unique; if multiple relationships of the same type exist between the same nodes, only the last one will be retained
  • The returned data structure is suitable for graph visualization libraries like D3.js, Cytoscape.js, or vis.js
  • Consider validating the schema_info structure before passing it to this function to ensure data integrity

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function generate_neo4j_schema_report 68.5% similar

    Generates a comprehensive schema report of a Neo4j graph database, including node labels, relationships, properties, constraints, indexes, and sample data, outputting multiple file formats (JSON, HTML, Python snippets, Cypher examples).

    From: /tf/active/vicechatdev/neo4j_schema_report.py
  • function generate_cypher_examples 60.3% similar

    Generates a comprehensive Cypher query examples file for interacting with a Neo4j graph database based on the provided schema information.

    From: /tf/active/vicechatdev/neo4j_schema_report.py
  • function generate_python_snippets 59.1% similar

    Generates a Python file containing code snippets and helper functions for interacting with a Neo4j graph database based on the provided schema information.

    From: /tf/active/vicechatdev/neo4j_schema_report.py
  • function get_database_statistics 56.5% similar

    Retrieves statistical information about a Neo4j graph database, including counts of nodes grouped by label and counts of relationships grouped by type.

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
  • function add_document_to_graph 55.7% similar

    Creates nodes and relationships in a Neo4j graph database for a processed document, including its text and table chunks, connecting it to a folder hierarchy.

    From: /tf/active/vicechatdev/offline_docstore_multi.py
← Back to Browse