|
| 1 | +# Repository Analytics |
| 2 | + |
| 3 | +This example demonstrates how to use Codegen to analyze repository metrics and generate comprehensive codebase analytics. For a complete walkthrough, check out our [tutorial](https://docs.codegen.com/tutorials/codebase-analytics-dashboard). |
| 4 | + |
| 5 | +## How the Analytics Script Works |
| 6 | + |
| 7 | +The script calculates four main categories of metrics: |
| 8 | + |
| 9 | +1. **Line Metrics** |
| 10 | + |
| 11 | + ```python |
| 12 | + def count_lines(source: str): |
| 13 | + """Count different types of lines in source code.""" |
| 14 | + lines = [line.strip() for line in source.splitlines()] |
| 15 | + loc = len(lines) |
| 16 | + sloc = len([line for line in lines if line]) |
| 17 | + # ... additional line counting logic ... |
| 18 | + return loc, lloc, sloc, comments |
| 19 | + ``` |
| 20 | + |
| 21 | + - Lines of Code (LOC): Total lines including blanks and comments |
| 22 | + - Logical Lines (LLOC): Lines containing functional statements |
| 23 | + - Source Lines (SLOC): Non-blank lines of code |
| 24 | + - Comment Density: Percentage of comments relative to total lines |
| 25 | + |
| 26 | +1. **Cyclomatic Complexity** |
| 27 | + |
| 28 | + ```python |
| 29 | + def calculate_cyclomatic_complexity(function): |
| 30 | + def analyze_statement(statement): |
| 31 | + complexity = 0 |
| 32 | + if isinstance(statement, IfBlockStatement): |
| 33 | + complexity += 1 |
| 34 | + if hasattr(statement, "elif_statements"): |
| 35 | + complexity += len(statement.elif_statements) |
| 36 | + # ... additional complexity calculations ... |
| 37 | + return complexity |
| 38 | + |
| 39 | + return 1 + analyze_block(function.code_block) |
| 40 | + ``` |
| 41 | + |
| 42 | + - Measures number of code paths through the program |
| 43 | + - Higher values indicate more complex control flow |
| 44 | + - Helps identify functions that may need refactoring |
| 45 | + |
| 46 | +1. **Halstead Volume** |
| 47 | + |
| 48 | + ```python |
| 49 | + def calculate_halstead_volume(operators, operands): |
| 50 | + n1 = len(set(operators)) # unique operators |
| 51 | + n2 = len(set(operands)) # unique operands |
| 52 | + N1 = len(operators) # total operators |
| 53 | + N2 = len(operands) # total operands |
| 54 | + N = N1 + N2 |
| 55 | + n = n1 + n2 |
| 56 | + # ... volume calculation ... |
| 57 | + return volume, N1, N2, n1, n2 |
| 58 | + ``` |
| 59 | + |
| 60 | + - Measures program size based on vocabulary and length |
| 61 | + - Uses distinct operators and operands to calculate volume |
| 62 | + - Indicates cognitive load and program understanding effort |
| 63 | + |
| 64 | +1. **Depth of Inheritance** |
| 65 | + |
| 66 | + ```python |
| 67 | + def calculate_doi(cls): |
| 68 | + """Calculate the depth of inheritance for a given class.""" |
| 69 | + return len(cls.superclasses) |
| 70 | + ``` |
| 71 | + |
| 72 | + - Length of inheritance chain for classes |
| 73 | + |
| 74 | +1. **Maintainability Index** |
| 75 | + |
| 76 | + ```python |
| 77 | + def calculate_maintainability_index(halstead_volume: float, cyclomatic_complexity: float, loc: int) -> int: |
| 78 | + """Calculate the normalized maintainability index for a given function.""" |
| 79 | + raw_mi = 171 - 5.2 * math.log(max(1, halstead_volume)) - 0.23 * cyclomatic_complexity - 16.2 * math.log(max(1, loc)) |
| 80 | + normalized_mi = max(0, min(100, raw_mi * 100 / 171)) |
| 81 | + return int(normalized_mi) |
| 82 | + ``` |
| 83 | + |
| 84 | + - Normalized score (0-100) based on complexity, volume, and size |
| 85 | + - Higher scores indicate better maintainability |
| 86 | + |
| 87 | +## Running the Analysis |
| 88 | + |
| 89 | +```bash |
| 90 | +# Install Codegen |
| 91 | +pip install codegen |
| 92 | + |
| 93 | +# Run the analysis |
| 94 | +python run.py |
| 95 | +``` |
| 96 | + |
| 97 | +The script will output a detailed report including: |
| 98 | + |
| 99 | +- Basic repository statistics |
| 100 | +- Line metrics and comment density |
| 101 | +- Complexity measurements |
| 102 | +- Object-oriented metrics |
| 103 | +- Overall maintainability scores |
| 104 | + |
| 105 | +## Example Output |
| 106 | + |
| 107 | +``` |
| 108 | +📊 Repository Analysis Report 📊 |
| 109 | +================================================== |
| 110 | +📁 Repository: codegen-sh/codegen |
| 111 | +📝 Description: [Repository description from GitHub] |
| 112 | +
|
| 113 | +📈 Basic Metrics: |
| 114 | + • Files: 42 |
| 115 | + • Functions: 156 |
| 116 | + • Classes: 23 |
| 117 | +
|
| 118 | +📏 Line Metrics: |
| 119 | + • Lines of Code: 4,521 |
| 120 | + • Logical Lines: 2,845 |
| 121 | + • Source Lines: 3,892 |
| 122 | + • Comments: 629 |
| 123 | + • Comment Density: 13.9% |
| 124 | +
|
| 125 | +🔄 Complexity Metrics: |
| 126 | + • Average Cyclomatic Complexity: 3.2 |
| 127 | + • Average Maintainability Index: 72 |
| 128 | + • Average Depth of Inheritance: 1.4 |
| 129 | + • Total Halstead Volume: 52,436 |
| 130 | + • Average Halstead Volume: 336 |
| 131 | +``` |
| 132 | + |
| 133 | +## Learn More |
| 134 | + |
| 135 | +- [Analytics Tutorial](https://docs.codegen.com/tutorials/codebase-analytics-dashboard) |
| 136 | +- [Codegen Documentation](https://docs.codegen.com) |
| 137 | + |
| 138 | +## Contributing |
| 139 | + |
| 140 | +Feel free to submit issues and enhancement requests! |
0 commit comments