Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions codegen-on-oss/codegen_on_oss/analyzers/visualization/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Codebase Visualization

This directory contains tools and utilities for visualizing various aspects of a codebase.

## Directory Structure

- **call_graph/**: Visualizations related to function call relationships and method interactions
- `call_trace.py`: Traces function call paths through a codebase
- `graph_viz_call_graph.py`: Creates directed call graphs for functions
- `method_relationships.py`: Visualizes relationships between methods in a class
- `viz_cal_graph.py`: Generates call graphs with detailed metadata

- **dependency_graph/**: Visualizations related to code dependencies and impact analysis
- `blast_radius.py`: Shows the "blast radius" of changes to a function
- `dependency_trace.py`: Traces symbol dependencies through a codebase
- `viz_dead_code.py`: Identifies and visualizes dead/unused code

- **structure_graph/**: Visualizations related to code structure and organization
- `graph_viz_dir_tree.py`: Displays directory structure as a graph
- `graph_viz_foreign_key.py`: Visualizes database schema relationships

- **docs/**: Documentation and examples for visualization tools
- `codebase-visualization.mdx`: Comprehensive guide to codebase visualization

## Base Visualization Files

- `analysis_visualizer.py`: Core visualization for analysis results
- `code_visualizer.py`: Visualization tools for code elements
- `codebase_visualizer.py`: Main visualization engine for codebases
- `visualizer.py`: Base visualization framework

## Usage

These visualization tools can be used to:

1. Understand complex codebases
2. Plan refactoring efforts
3. Identify tightly coupled components
4. Analyze critical paths
5. Document system architecture
6. Find dead code
7. Visualize database schemas
8. Understand directory structures

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""
Call Graph Visualization Module

This module provides tools for visualizing call graphs and function relationships in a codebase.
"""

Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import codegen
import networkx as nx
from codegen import Codebase
from codegen.sdk.core.class_definition import Class
from codegen.sdk.core.import_resolution import Import
from codegen.sdk.core.symbol import Symbol

G = nx.DiGraph()

IGNORE_EXTERNAL_MODULE_CALLS = True
IGNORE_CLASS_CALLS = False
MAX_DEPTH = 10

COLOR_PALETTE = {
"StartFunction": "#9cdcfe", # Light blue for the starting function
"PyFunction": "#a277ff", # Purple for Python functions
"PyClass": "#ffca85", # Orange for Python classes
"ExternalModule": "#f694ff", # Pink for external module references
}

# Dictionary to track visited nodes and prevent cycles
visited = {}


def create_dependencies_visualization(symbol: Symbol, depth: int = 0):
"""Creates a visualization of symbol dependencies in the codebase

Recursively traverses the dependency tree of a symbol (function, class, etc.)
and creates a directed graph representation. Dependencies can be either direct
symbol references or imports.

Args:
symbol (Symbol): The starting symbol whose dependencies will be mapped
depth (int): Current depth in the recursive traversal
"""
if depth >= MAX_DEPTH:
return

for dep in symbol.dependencies:
dep_symbol = None

if isinstance(dep, Symbol):
dep_symbol = dep
elif isinstance(dep, Import):
dep_symbol = dep.resolved_symbol if dep.resolved_symbol else None

if dep_symbol:
G.add_node(dep_symbol, color=COLOR_PALETTE.get(dep_symbol.__class__.__name__, "#f694ff"))
G.add_edge(symbol, dep_symbol)

if not isinstance(dep_symbol, Class):
create_dependencies_visualization(dep_symbol, depth + 1)


@codegen.function("visualize-symbol-dependencies")
def run(codebase: Codebase):
"""Generate a visualization of symbol dependencies in a codebase.

This codemod:
1. Creates a directed graph of symbol dependencies starting from a target function
2. Tracks relationships between functions, classes, and imports
3. Generates a visual representation of the dependency hierarchy
"""
global G
G = nx.DiGraph()

target_func = codebase.get_function("get_query_runner")
G.add_node(target_func, color=COLOR_PALETTE.get("StartFunction"))

create_dependencies_visualization(target_func)

print(G)
print("Use codegen.sh to visualize the graph!")


if __name__ == "__main__":
print("Initializing codebase...")
codebase = Codebase.from_repo("codegen-oss/posthog", commit="b174f2221ea4ae50e715eb6a7e70e9a2b0760800", language="python")
print(f"Codebase with {len(codebase.files)} files and {len(codebase.functions)} functions.")
print("Creating graph...")

run(codebase)

Loading
Loading