|
| 1 | +--- |
| 2 | +title: "Simple Metrics API Endpoint" |
| 3 | +description: "API endpoint for retrieving finding metrics by product type with severity breakdown" |
| 4 | +draft: false |
| 5 | +weight: 3 |
| 6 | +--- |
| 7 | + |
| 8 | +## Simple Metrics API Endpoint |
| 9 | + |
| 10 | +The Simple Metrics API endpoint provides finding counts by product type, broken down by severity levels and month status. This endpoint replicates the data from the UI's `/metrics/simple` page in JSON format, making it easier to integrate with other tools and dashboards. |
| 11 | + |
| 12 | +### Endpoint Details |
| 13 | + |
| 14 | +**URL:** `/api/v2/metrics/simple` |
| 15 | + |
| 16 | +**Method:** `GET` |
| 17 | + |
| 18 | +**Authentication:** Required (Token authentication) |
| 19 | + |
| 20 | +**Authorization:** User must have `Product_Type_View` permission for the product types |
| 21 | + |
| 22 | +### Query Parameters |
| 23 | + |
| 24 | +| Parameter | Type | Required | Description | |
| 25 | +|-----------|------|----------|-------------| |
| 26 | +| `date` | String (YYYY-MM-DD) | No | Date to filter metrics by month/year (defaults to current month) | |
| 27 | +| `product_type_id` | Integer | No | Optional product type ID to filter metrics. If not provided, returns all accessible product types | |
| 28 | + |
| 29 | +### Response Format |
| 30 | + |
| 31 | +The endpoint returns an array of objects, each representing metrics for a product type: |
| 32 | + |
| 33 | +```json |
| 34 | +[ |
| 35 | + { |
| 36 | + "product_type_id": 1, |
| 37 | + "product_type_name": "Web Application", |
| 38 | + "Total": 150, |
| 39 | + "S0": 5, // Critical |
| 40 | + "S1": 25, // High |
| 41 | + "S2": 75, // Medium |
| 42 | + "S3": 40, // Low |
| 43 | + "S4": 5, // Info |
| 44 | + "Opened": 10, |
| 45 | + "Closed": 8 |
| 46 | + }, |
| 47 | + { |
| 48 | + "product_type_id": 2, |
| 49 | + "product_type_name": "Mobile Application", |
| 50 | + "Total": 89, |
| 51 | + "S0": 2, // Critical |
| 52 | + "S1": 15, // High |
| 53 | + "S2": 45, // Medium |
| 54 | + "S3": 25, // Low |
| 55 | + "S4": 2, // Info |
| 56 | + "Opened": 7, |
| 57 | + "Closed": 5 |
| 58 | + } |
| 59 | +] |
| 60 | +``` |
| 61 | + |
| 62 | +### Response Fields |
| 63 | + |
| 64 | +| Field | Type | Description | |
| 65 | +|-------|------|-------------| |
| 66 | +| `product_type_id` | Integer | Unique identifier for the product type | |
| 67 | +| `product_type_name` | String | Name of the product type | |
| 68 | +| `Total` | Integer | Total number of findings for the product type in the specified month | |
| 69 | +| `S0` | Integer | Number of Critical severity findings | |
| 70 | +| `S1` | Integer | Number of High severity findings | |
| 71 | +| `S2` | Integer | Number of Medium severity findings | |
| 72 | +| `S3` | Integer | Number of Low severity findings | |
| 73 | +| `S4` | Integer | Number of Info severity findings | |
| 74 | +| `Opened` | Integer | Number of findings opened in the specified month | |
| 75 | +| `Closed` | Integer | Number of findings closed in the specified month | |
| 76 | + |
| 77 | +### Example Usage |
| 78 | + |
| 79 | +#### Get current month metrics |
| 80 | +```bash |
| 81 | +GET /api/v2/metrics/simple |
| 82 | +``` |
| 83 | + |
| 84 | +#### Get metrics for January 2024 |
| 85 | +```bash |
| 86 | +GET /api/v2/metrics/simple?date=2024-01-15 |
| 87 | +``` |
| 88 | + |
| 89 | +#### Get metrics for a specific product type |
| 90 | +```bash |
| 91 | +GET /api/v2/metrics/simple?product_type_id=1 |
| 92 | +``` |
| 93 | + |
| 94 | +#### Get metrics for a specific product type and date |
| 95 | +```bash |
| 96 | +GET /api/v2/metrics/simple?date=2024-05-01&product_type_id=2 |
| 97 | +``` |
| 98 | + |
| 99 | +### Error Responses |
| 100 | + |
| 101 | +#### 400 Bad Request - Invalid date characters |
| 102 | +```json |
| 103 | +{ |
| 104 | + "error": "Invalid date format. Only numbers and hyphens allowed." |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +#### 400 Bad Request - Invalid date format |
| 109 | +```json |
| 110 | +{ |
| 111 | + "error": "Invalid date format. Use YYYY-MM-DD format." |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +#### 400 Bad Request - Date out of range |
| 116 | +```json |
| 117 | +{ |
| 118 | + "error": "Date must be between 2000-01-01 and one year from now." |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +#### 400 Bad Request - Invalid product_type_id format |
| 123 | +```json |
| 124 | +{ |
| 125 | + "error": "Invalid product_type_id format." |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +#### 404 Not Found - Product type not found or access denied |
| 130 | +```json |
| 131 | +{ |
| 132 | + "error": "Product type not found or access denied." |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +#### 403 Unauthorized - Missing or invalid authentication |
| 137 | +```json |
| 138 | +{ |
| 139 | + "detail": "Authentication credentials were not provided." |
| 140 | +} |
| 141 | +``` |
| 142 | + |
| 143 | +#### 403 Forbidden - Insufficient permissions |
| 144 | +```json |
| 145 | +{ |
| 146 | + "detail": "You do not have permission to perform this action." |
| 147 | +} |
| 148 | +``` |
| 149 | + |
| 150 | +### Notes |
| 151 | + |
| 152 | +- **Authorization Model**: This endpoint uses the same authorization model as the UI's `/metrics/simple` page, ensuring consistent access control |
| 153 | +- **Performance**: The endpoint is optimized with database aggregation instead of Python loops for better performance |
| 154 | +- **Date Handling**: If no date is provided, the current month is used by default |
| 155 | +- **Timezone**: All dates are handled in the server's configured timezone |
| 156 | +- **Product Type Access**: Users will only see metrics for product types they have permission to view |
| 157 | +- **Data Consistency**: The data returned by this API endpoint matches exactly what is displayed on the `/metrics/simple` UI page |
| 158 | +- **Field Naming**: The API uses specific field names (`S0`, `S1`, `S2`, `S3`, `S4` for severity levels and `Total`, `Opened`, `Closed` for counts) to maintain consistency with the internal data structure |
| 159 | +- **URL Format**: The endpoint automatically redirects requests without trailing slash to include one (301 redirect) |
| 160 | +- **Date Validation**: The API performs two levels of date validation: first checking for valid characters (only numbers and hyphens allowed), then validating the YYYY-MM-DD format |
| 161 | + |
| 162 | +### Use Cases |
| 163 | + |
| 164 | +This endpoint is useful for: |
| 165 | +- **Dashboard Integration**: Integrating DefectDojo metrics into external dashboards and reporting tools |
| 166 | +- **Automated Reporting**: Creating automated reports showing security metrics trends over time |
| 167 | +- **CI/CD Integration**: Monitoring security metrics as part of continuous integration pipelines |
| 168 | +- **Executive Reporting**: Generating high-level security metrics for management reporting |
| 169 | +- **Data Analysis**: Performing custom analysis on security finding trends and patterns |
0 commit comments