|
| 1 | +# CodeGen Analyzer |
| 2 | + |
| 3 | +The CodeGen Analyzer module provides comprehensive static analysis capabilities for codebases, focusing on code quality, dependencies, structure, and visualization. It serves as a backend API that can be used by frontend applications to analyze repositories. |
| 4 | + |
| 5 | +## Architecture |
| 6 | + |
| 7 | +The analyzer system is built with a modular plugin-based architecture: |
| 8 | + |
| 9 | +``` |
| 10 | +analyzers/ |
| 11 | +├── api.py # Main API endpoints for frontend integration |
| 12 | +├── analyzer.py # Plugin-based analyzer system |
| 13 | +├── issues.py # Issue tracking and management |
| 14 | +├── code_quality.py # Code quality analysis |
| 15 | +├── dependencies.py # Dependency analysis |
| 16 | +├── models/ |
| 17 | +│ └── analysis_result.py # Data models for analysis results |
| 18 | +├── context/ # Code context management |
| 19 | +├── visualization/ # Visualization support |
| 20 | +└── resolution/ # Issue resolution tools |
| 21 | +``` |
| 22 | + |
| 23 | +## Core Components |
| 24 | + |
| 25 | +### 1. API Interface (`api.py`) |
| 26 | + |
| 27 | +The main entry point for frontend applications. Provides REST-like endpoints for: |
| 28 | +- Codebase analysis |
| 29 | +- PR analysis |
| 30 | +- Dependency visualization |
| 31 | +- Issue reporting |
| 32 | +- Code quality assessment |
| 33 | + |
| 34 | +### 2. Analyzer System (`analyzer.py`) |
| 35 | + |
| 36 | +Plugin-based system that coordinates different types of analysis: |
| 37 | +- Code quality analysis (complexity, maintainability) |
| 38 | +- Dependency analysis (imports, cycles, coupling) |
| 39 | +- PR impact analysis |
| 40 | +- Type checking and error detection |
| 41 | + |
| 42 | +### 3. Issue Tracking (`issues.py`) |
| 43 | + |
| 44 | +Comprehensive issue model with: |
| 45 | +- Severity levels (critical, error, warning, info) |
| 46 | +- Categories (dead code, complexity, dependency, etc.) |
| 47 | +- Location information and suggestions |
| 48 | +- Filtering and grouping capabilities |
| 49 | + |
| 50 | +### 4. Dependency Analysis (`dependencies.py`) |
| 51 | + |
| 52 | +Analysis of codebase dependencies: |
| 53 | +- Import dependencies between modules |
| 54 | +- Circular dependency detection |
| 55 | +- Module coupling analysis |
| 56 | +- External dependencies tracking |
| 57 | +- Call graphs and class hierarchies |
| 58 | + |
| 59 | +### 5. Code Quality Analysis (`code_quality.py`) |
| 60 | + |
| 61 | +Analysis of code quality aspects: |
| 62 | +- Dead code detection (unused functions, variables) |
| 63 | +- Complexity metrics (cyclomatic, cognitive) |
| 64 | +- Parameter checking (types, usage) |
| 65 | +- Style issues and maintainability |
| 66 | + |
| 67 | +## Using the API |
| 68 | + |
| 69 | +### Setup |
| 70 | + |
| 71 | +```python |
| 72 | +from codegen_on_oss.analyzers.api import CodegenAnalyzerAPI |
| 73 | + |
| 74 | +# Create API instance with repository |
| 75 | +api = CodegenAnalyzerAPI(repo_path="/path/to/repo") |
| 76 | +# OR |
| 77 | +api = CodegenAnalyzerAPI(repo_url="https://github.com/owner/repo") |
| 78 | +``` |
| 79 | + |
| 80 | +### Analyzing a Codebase |
| 81 | + |
| 82 | +```python |
| 83 | +# Run comprehensive analysis |
| 84 | +results = api.analyze_codebase() |
| 85 | + |
| 86 | +# Run specific analysis types |
| 87 | +results = api.analyze_codebase(analysis_types=["code_quality", "dependency"]) |
| 88 | + |
| 89 | +# Force refresh of cached analysis |
| 90 | +results = api.analyze_codebase(force_refresh=True) |
| 91 | +``` |
| 92 | + |
| 93 | +### Analyzing a PR |
| 94 | + |
| 95 | +```python |
| 96 | +# Analyze a specific PR |
| 97 | +pr_results = api.analyze_pr(pr_number=123) |
| 98 | + |
| 99 | +# Get PR impact visualization |
| 100 | +impact_viz = api.get_pr_impact(pr_number=123, format="json") |
| 101 | +``` |
| 102 | + |
| 103 | +### Getting Issues |
| 104 | + |
| 105 | +```python |
| 106 | +# Get all issues |
| 107 | +all_issues = api.get_issues() |
| 108 | + |
| 109 | +# Get issues by severity |
| 110 | +critical_issues = api.get_issues(severity="critical") |
| 111 | +error_issues = api.get_issues(severity="error") |
| 112 | + |
| 113 | +# Get issues by category |
| 114 | +dependency_issues = api.get_issues(category="dependency_cycle") |
| 115 | +``` |
| 116 | + |
| 117 | +### Getting Visualizations |
| 118 | + |
| 119 | +```python |
| 120 | +# Get module dependency graph |
| 121 | +module_deps = api.get_module_dependencies(format="json") |
| 122 | + |
| 123 | +# Get function call graph |
| 124 | +call_graph = api.get_function_call_graph( |
| 125 | + function_name="main", |
| 126 | + depth=3, |
| 127 | + format="json" |
| 128 | +) |
| 129 | + |
| 130 | +# Export visualization to file |
| 131 | +api.export_visualization(call_graph, format="html", filename="call_graph.html") |
| 132 | +``` |
| 133 | + |
| 134 | +### Common Analysis Patterns |
| 135 | + |
| 136 | +```python |
| 137 | +# Find dead code |
| 138 | +api.analyze_codebase(analysis_types=["code_quality"]) |
| 139 | +dead_code = api.get_issues(category="dead_code") |
| 140 | + |
| 141 | +# Find circular dependencies |
| 142 | +api.analyze_codebase(analysis_types=["dependency"]) |
| 143 | +circular_deps = api.get_circular_dependencies() |
| 144 | + |
| 145 | +# Find parameter issues |
| 146 | +api.analyze_codebase(analysis_types=["code_quality"]) |
| 147 | +param_issues = api.get_parameter_issues() |
| 148 | +``` |
| 149 | + |
| 150 | +## REST API Endpoints |
| 151 | + |
| 152 | +The analyzer can be exposed as REST API endpoints for integration with frontend applications: |
| 153 | + |
| 154 | +### Codebase Analysis |
| 155 | + |
| 156 | +``` |
| 157 | +POST /api/analyze/codebase |
| 158 | +{ |
| 159 | + "repo_path": "/path/to/repo", |
| 160 | + "analysis_types": ["code_quality", "dependency"] |
| 161 | +} |
| 162 | +``` |
| 163 | + |
| 164 | +### PR Analysis |
| 165 | + |
| 166 | +``` |
| 167 | +POST /api/analyze/pr |
| 168 | +{ |
| 169 | + "repo_path": "/path/to/repo", |
| 170 | + "pr_number": 123 |
| 171 | +} |
| 172 | +``` |
| 173 | + |
| 174 | +### Visualization |
| 175 | + |
| 176 | +``` |
| 177 | +POST /api/visualize |
| 178 | +{ |
| 179 | + "repo_path": "/path/to/repo", |
| 180 | + "viz_type": "module_dependencies", |
| 181 | + "params": { |
| 182 | + "layout": "hierarchical", |
| 183 | + "format": "json" |
| 184 | + } |
| 185 | +} |
| 186 | +``` |
| 187 | + |
| 188 | +### Issues |
| 189 | + |
| 190 | +``` |
| 191 | +GET /api/issues?severity=error&category=dependency_cycle |
| 192 | +``` |
| 193 | + |
| 194 | +## Implementation Example |
| 195 | + |
| 196 | +For a web application exposing these endpoints with Flask: |
| 197 | + |
| 198 | +```python |
| 199 | +from flask import Flask, request, jsonify |
| 200 | +from codegen_on_oss.analyzers.api import ( |
| 201 | + api_analyze_codebase, |
| 202 | + api_analyze_pr, |
| 203 | + api_get_visualization, |
| 204 | + api_get_static_errors |
| 205 | +) |
| 206 | + |
| 207 | +app = Flask(__name__) |
| 208 | + |
| 209 | +@app.route("/api/analyze/codebase", methods=["POST"]) |
| 210 | +def analyze_codebase(): |
| 211 | + data = request.json |
| 212 | + result = api_analyze_codebase( |
| 213 | + repo_path=data.get("repo_path"), |
| 214 | + analysis_types=data.get("analysis_types") |
| 215 | + ) |
| 216 | + return jsonify(result) |
| 217 | + |
| 218 | +@app.route("/api/analyze/pr", methods=["POST"]) |
| 219 | +def analyze_pr(): |
| 220 | + data = request.json |
| 221 | + result = api_analyze_pr( |
| 222 | + repo_path=data.get("repo_path"), |
| 223 | + pr_number=data.get("pr_number") |
| 224 | + ) |
| 225 | + return jsonify(result) |
| 226 | + |
| 227 | +@app.route("/api/visualize", methods=["POST"]) |
| 228 | +def visualize(): |
| 229 | + data = request.json |
| 230 | + result = api_get_visualization( |
| 231 | + repo_path=data.get("repo_path"), |
| 232 | + viz_type=data.get("viz_type"), |
| 233 | + params=data.get("params", {}) |
| 234 | + ) |
| 235 | + return jsonify(result) |
| 236 | + |
| 237 | +@app.route("/api/issues", methods=["GET"]) |
| 238 | +def get_issues(): |
| 239 | + repo_path = request.args.get("repo_path") |
| 240 | + severity = request.args.get("severity") |
| 241 | + category = request.args.get("category") |
| 242 | + |
| 243 | + api = create_api(repo_path=repo_path) |
| 244 | + return jsonify(api.get_issues(severity=severity, category=category)) |
| 245 | + |
| 246 | +if __name__ == "__main__": |
| 247 | + app.run(debug=True) |
| 248 | +``` |
0 commit comments