|
| 1 | +<!-- |
| 2 | +Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +contributor license agreements. See the NOTICE file distributed with |
| 4 | +this work for additional information regarding copyright ownership. |
| 5 | +The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +(the "License"); you may not use this file except in compliance with |
| 7 | +the License. You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +--> |
| 17 | +# Cursor Plugin (Usage & Cost) |
| 18 | + |
| 19 | +This plugin ingests **Cursor team usage, billing, and adoption metrics** from the [Cursor Admin API](https://cursor.com/docs/account/teams/admin-api) and stores them in DevLake tool-layer tables for Grafana dashboards and SQL analysis. |
| 20 | + |
| 21 | +It follows the same structure and patterns as other DevLake AI usage plugins (notably `backend/plugins/gh-copilot`). |
| 22 | + |
| 23 | +## What it collects |
| 24 | + |
| 25 | +**Cursor Admin API endpoints:** |
| 26 | + |
| 27 | +| Endpoint | Method | Data | |
| 28 | +|----------|--------|------| |
| 29 | +| `/teams/members` | GET | Team roster | |
| 30 | +| `/teams/spend` | POST | Per-user billing cycle spend | |
| 31 | +| `/teams/filtered-usage-events` | POST | Event-level usage and charges | |
| 32 | +| `/teams/daily-usage-data` | POST | Per-user per-day adoption metrics | |
| 33 | + |
| 34 | +**Stored data (tool layer):** |
| 35 | + |
| 36 | +| Table | Description | |
| 37 | +|-------|-------------| |
| 38 | +| `_tool_cursor_members` | Team member roster (email, name, role) | |
| 39 | +| `_tool_cursor_usage_events` | Billable usage events with model, tokens, and charged amounts | |
| 40 | +| `_tool_cursor_user_spend` | Per-user spend for the current billing cycle (on-demand and included) | |
| 41 | +| `_tool_cursor_daily_usage` | Daily adoption metrics: completions, requests by feature, tab acceptance, line edits | |
| 42 | + |
| 43 | +Data is collected in the **Raw → Tool** layers only. There is no domain-layer converter in this plugin; Grafana dashboards query `_tool_cursor_*` tables directly. |
| 44 | + |
| 45 | +## Data flow |
| 46 | + |
| 47 | +```mermaid |
| 48 | +flowchart LR |
| 49 | + API[Cursor Admin API] |
| 50 | + RAW[(Raw tables\n_raw_cursor_*)] |
| 51 | + TOOL[(Tool tables\n_tool_cursor_*)] |
| 52 | + GRAF[Grafana Dashboards] |
| 53 | +
|
| 54 | + API --> RAW --> TOOL --> GRAF |
| 55 | +``` |
| 56 | + |
| 57 | +**Pipeline subtasks (in order):** |
| 58 | + |
| 59 | +1. `collectMembers` → `extractMembers` |
| 60 | +2. `collectUsageEvents` → `extractUsageEvents` |
| 61 | +3. `collectUserSpend` → `extractUserSpend` |
| 62 | +4. `collectDailyUsage` → `extractDailyUsage` |
| 63 | + |
| 64 | +## Repository layout |
| 65 | + |
| 66 | +- `api/` — REST layer for connections, scopes, and scope configs |
| 67 | +- `impl/` — plugin meta, blueprint v200, connection helpers |
| 68 | +- `models/` — tool-layer models and migration scripts |
| 69 | +- `tasks/` — collectors, extractors, and pipeline registration |
| 70 | +- `service/` — connection test logic (Admin API permission probes) |
| 71 | +- `e2e/` — E2E fixtures and golden CSV assertions |
| 72 | + |
| 73 | +## Setup |
| 74 | + |
| 75 | +### Prerequisites |
| 76 | + |
| 77 | +- A Cursor **Team or Business** plan with Admin API access |
| 78 | +- A **Team Admin API key** from the Cursor dashboard (Dashboard → API Keys) |
| 79 | +- Do **not** use a User API key from Settings → Integrations — user keys cannot access `/teams/*` endpoints |
| 80 | + |
| 81 | +Authentication uses HTTP Basic auth: the API key as username with an empty password. |
| 82 | + |
| 83 | +### 1) Create a connection |
| 84 | + |
| 85 | +1. DevLake UI → **Connections → Add Connection → Cursor** |
| 86 | +2. Fill in: |
| 87 | + - **Name**: e.g. `Cursor Production Team` |
| 88 | + - **Endpoint**: defaults to `https://api.cursor.com` |
| 89 | + - **Token**: Team Admin API key |
| 90 | + - **Rate Limit**: defaults to 1,200 requests/hour (Cursor documents 20 requests/minute) |
| 91 | +3. Click **Test Connection**. DevLake probes `/teams/members`, `/teams/spend`, and `/teams/filtered-usage-events` and reports which endpoints the key can access. |
| 92 | +4. Save the connection. |
| 93 | + |
| 94 | +When updating an existing connection, omit the token field to keep the encrypted value already stored in DevLake. |
| 95 | + |
| 96 | +### 2) Add a scope |
| 97 | + |
| 98 | +Cursor data is team-level. Add a **Team** scope for the connection. The default scope ID is `team`. |
| 99 | + |
| 100 | +### 3) Create a blueprint |
| 101 | + |
| 102 | +Use a blueprint plan like: |
| 103 | + |
| 104 | +```json |
| 105 | +[ |
| 106 | + [ |
| 107 | + { |
| 108 | + "plugin": "cursor", |
| 109 | + "options": { |
| 110 | + "connectionId": 1, |
| 111 | + "scopeId": "team" |
| 112 | + } |
| 113 | + } |
| 114 | + ] |
| 115 | +] |
| 116 | +``` |
| 117 | + |
| 118 | +Run the blueprint on a daily schedule to keep usage and cost data current. |
| 119 | + |
| 120 | +### Collection behavior |
| 121 | + |
| 122 | +- **Initial backfill**: usage events and daily usage collect up to **90 days** of history on the first run. |
| 123 | +- **Incremental runs**: subsequent runs use the pipeline sync policy (`TimeAfter`) to collect only new data. |
| 124 | +- **Date range chunking**: both `/teams/daily-usage-data` and `/teams/filtered-usage-events` requests are split into **30-day** chunks (API limit for daily usage; applied to usage events for resilience). |
| 125 | +- **Extract**: `extractUsageEvents` and `extractDailyUsage` use **StatefulApiExtractor** (incremental by default). A config version bump triggers a one-time full re-extract after upgrade. |
| 126 | +- **Rate limiting**: collectors honor `Retry-After` response headers and respect the configured `rateLimitPerHour`. |
| 127 | + |
| 128 | +## Dashboards |
| 129 | + |
| 130 | +Grafana dashboard JSON lives under `grafana/dashboards/mysql/`: |
| 131 | + |
| 132 | +| Dashboard | File | UID | |
| 133 | +|-----------|------|-----| |
| 134 | +| Cursor Usage & Cost | `cursor-usage.json` | `cursor_usage` | |
| 135 | +| AI Cost Efficiency (Cursor panels) | `ai-cost-efficiency.json` | — | |
| 136 | +| Multi-AI Comparison (Cursor panels) | `multi-ai-comparison.json` | — | |
| 137 | + |
| 138 | +See `grafana/dashboards/mysql/CursorREADME.md` for dashboard prerequisites, variables, and panel descriptions. |
| 139 | + |
| 140 | +## Error handling |
| 141 | + |
| 142 | +| Symptom | Likely cause | |
| 143 | +|---------|--------------| |
| 144 | +| **401 Unauthorized** on test connection | Invalid API key, or a User API key instead of a Team Admin key | |
| 145 | +| **403 Forbidden** on spend or usage events | Key lacks permission for that Admin API endpoint | |
| 146 | +| **429 Too Many Requests** | Rate limit exceeded — lower `rateLimitPerHour` or wait for `Retry-After` | |
| 147 | +| Empty usage events after successful run | Selected time range has no billable events, or sync policy excludes the date range | |
| 148 | +| Reconciliation delta on dashboard | Expected when comparing event-level charges to billing-cycle spend snapshots; see dashboard notes | |
| 149 | + |
| 150 | +Tokens are sanitized before persisting. Connection test results include a `permissions` object showing which Admin API endpoints succeeded. |
| 151 | + |
| 152 | +## Limitations |
| 153 | + |
| 154 | +- **Team/Business Admin API only** — Enterprise-only Analytics API endpoints (`/analytics/*`) are not collected in this plugin. |
| 155 | +- **Tool layer only** — no domain-layer tables; cross-plugin joins (Jira, GitHub PRs, etc.) are done in Grafana SQL or separate tooling. |
| 156 | +- **Team-level scope** — one scope per connection represents the whole team; per-team multi-tenant collection is not supported. |
| 157 | +- **Beta** — the plugin is marked beta in Config UI while the Admin API surface continues to evolve. |
| 158 | + |
| 159 | +## Testing |
| 160 | + |
| 161 | +```sh |
| 162 | +# Unit tests |
| 163 | +cd backend && go test ./plugins/cursor/... |
| 164 | + |
| 165 | +# E2E (requires E2E_DB_URL) |
| 166 | +make e2e-test |
| 167 | +``` |
| 168 | + |
| 169 | +E2E fixtures live in `backend/plugins/cursor/e2e/raw_tables/` and `e2e/snapshot_tables/`. |
0 commit comments