# OWS Dead Endpoints Analyzer

Analyzes Flask endpoints in OWS services and identifies dead endpoints by comparing against Datadog APM data.

## Features

- Extracts all Flask endpoints from a service codebase
- Compares against Datadog APM request counts to identify active/inactive endpoints
- Identifies dead logic and model functions that would become unused if dead endpoints are removed
- Performs transitive dead code analysis (model methods only called by dead logic methods)

## Installation

```bash
python -m venv .venv
source .venv/bin/activate
pip install -e .
```

## Usage

```bash
python src/analyze_endpoints.py \
    --service permissions \
    --datadog-export ./data/ows-permissions/flask-endpoint-request-counts-table.csv \
    --service-path ~/work/ows/ows-permissions \
    --threshold 10
```

### Arguments

| Argument | Required | Description |
|----------|----------|-------------|
| `--service` | Yes | Service name (e.g., `permissions`, `users`, `track`) |
| `--datadog-export` | Yes | Path to CSV exported from Datadog |
| `--service-path` | Yes | Path to the service codebase |
| `--threshold` | No | Request count below which endpoint is "low traffic" (default: 100) |

## Data Directory Structure

```
data/
└── ows-<service>/
    ├── flask-endpoint-request-counts-table.csv  # Input: Datadog export
    ├── all_endpoints.csv                        # Output: All endpoints with Datadog match info
    ├── dead_endpoints.csv                       # Output: Dead endpoints with dead functions
    └── summary.json                             # Output: Analysis summary statistics
```

## Getting Datadog Data

1. Go to the [OWS Endpoints Analysis Dashboard](https://sonymusic-pde.datadoghq.com/dashboard/suf-n4g-yit/ows--endpoints-analysis?fromUser=false&offset=1&refresh_mode=yearly&tpl_var_env%5B0%5D=prod&tpl_var_service%5B0%5D=ows-track&from_ts=1735707600000&to_ts=1767243599999&live=true)

2. Select the service you want to analyze using the `service` template variable dropdown

3. Set an appropriate time range (recommend at least 1 year to capture infrequent endpoints)

4. Find the "Flask Endpoint Request Counts" table widget

5. Click the export button and select "Export as CSV"

6. Save the file to `data/ows-<service>/flask-endpoint-request-counts-table.csv`

## Output

### all_endpoints.csv

All endpoints found in code with their Datadog match status.

### dead_endpoints.csv

Endpoints classified as dead, low traffic, or not found in Datadog, along with their associated dead functions.

### summary.json

```json
{
  "endpoints": {
    "total": 50,
    "active": {"count": 30, "percent": 60.0},
    "low_traffic": {"count": 5, "percent": 10.0},
    "dead": {"count": 10, "percent": 20.0},
    "not_in_datadog": {"count": 5, "percent": 10.0}
  },
  "functions": {
    "total": 100,
    "dead": 25,
    "dead_percent": 25.0
  },
  "dead_functions": ["logic/user_info.py::get_user", "models/user.py::User.find"]
}
```

## Endpoint Status Definitions

| Status | Description |
|--------|-------------|
| `active` | Has traffic above threshold |
| `low_traffic` | Has traffic but below threshold |
| `dead` | Found in Datadog with 0 requests |
| `not_in_datadog` | Exists in code but not found in Datadog data |
