# Code Health
<!-- MEMORY_ANCHOR: code_health_documentation -->
This directory contains tools for tracking and visualizing code health metrics.
## Why
Tracking code health metrics helps identify areas of the codebase that need attention, ensuring the codebase remains maintainable and robust. Automated metrics provide an objective measure of code quality that can be tracked over time.
## Contents
- `metrics.py`: Script to calculate code health metrics
- `dashboard.py`: Script to generate an HTML dashboard for code health metrics
- `metrics.json`: Latest code health metrics (generated by `metrics.py`)
- `history.json`: Historical code health metrics (updated by `metrics.py`)
- `dashboard.html`: HTML dashboard (generated by `dashboard.py`)
- `visualizations/`: Directory containing generated visualizations
## Metrics Tracked
The following metrics are tracked:
### Complexity Metrics
- **Cyclomatic Complexity**: Measures the number of linearly independent paths through a program's source code
- **Maintainability Index**: Measures how maintainable (easy to support and change) the source code is
### Size Metrics
- **Lines of Code (LOC)**: Total lines of code
- **Logical Lines of Code (LLOC)**: Number of logical lines of code
- **Source Lines of Code (SLOC)**: Number of source lines of code
- **Comment Lines**: Number of comment lines
- **Comment Ratio**: Ratio of comments to source lines of code
### Test Coverage Metrics
- **Test Coverage**: Percentage of code covered by tests
### Code Churn Metrics
- **Code Churn**: Rate of code changes over time
- **Added Lines**: Number of lines added
- **Deleted Lines**: Number of lines deleted
- **Commits**: Number of commits
## Usage
### Calculating Metrics
To calculate code health metrics:
```bash
python claude/code_health/metrics.py
```
This will:
1. Analyze all Python files in the repository
2. Calculate complexity, maintainability, and raw metrics for each file
3. Calculate test coverage using coverage.py
4. Calculate code churn using git history
5. Save the metrics to `metrics.json`
6. Update the history in `history.json`
7. Generate visualizations in the `visualizations/` directory
### Generating the Dashboard
To generate the HTML dashboard:
```bash
python claude/code_health/dashboard.py
```
This will:
1. Load the metrics from `metrics.json`
2. Load the history from `history.json`
3. Generate an HTML dashboard with interactive charts
4. Save the dashboard to `dashboard.html`
### Viewing the Dashboard
Open `dashboard.html` in a web browser to view the dashboard.
## Integration with CI/CD
To integrate code health metrics into your CI/CD pipeline:
1. Add the following step to your CI/CD configuration:
```yaml
- name: Calculate code health metrics
run: python claude/code_health/metrics.py
- name: Generate code health dashboard
run: python claude/code_health/dashboard.py
```
2. Archive the dashboard as an artifact:
```yaml
- name: Archive code health dashboard
uses: actions/upload-artifact@v2
with:
name: code-health-dashboard
path: claude/code_health/dashboard.html
```
## Dependencies
- Python 3.6+
- radon: For calculating complexity metrics
- coverage.py: For calculating test coverage
- pandas: For data manipulation
- matplotlib: For generating visualizations
- plotly: For generating interactive dashboard charts
Install dependencies with:
```bash
pip install radon coverage pandas matplotlib plotly
```
## Maturity
The code health component is currently in **beta** status. It is functional but may undergo changes as the codebase evolves.
MCP directory API
We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/0xjcf/MCP_CodeAnalysis'
If you have feedback or need assistance with the MCP directory API, please join our Discord server