Individual stats for account rate limits

This commit is contained in:
Jonas Linter
2025-11-04 11:53:11 +01:00
parent 6127ea4650
commit f962a1a83d
3 changed files with 127 additions and 42 deletions

View File

@@ -24,7 +24,7 @@ Tracks application-level rate limits across all users.
```
### 2. **X-Ad-Account-Usage** (Ad Account Specific)
Tracks rate limits for specific ad accounts.
Tracks rate limits for specific ad accounts. **Stored per account ID** to support multiple accounts.
**Fields:**
- `acc_id_util_pct`: Percentage of ad account usage (0-100)
@@ -40,6 +40,8 @@ Tracks rate limits for specific ad accounts.
}
```
**Note:** Metrics are tracked separately for each ad account in a dictionary keyed by account ID (e.g., `act_123456789`).
### 3. **X-Business-Use-Case-Usage** (Business Use Case Limits)
Tracks rate limits per business use case (ads_insights, ads_management, etc.).
@@ -156,6 +158,17 @@ logging.basicConfig(
)
```
### Updating Usage with Account ID
When calling `update_usage()`, you can optionally provide an account ID to track per-account metrics:
```python
# Option 1: Provide account_id explicitly
limiter.update_usage(response, account_id='act_123456789')
# Option 2: Let the limiter try to extract it from the response
limiter.update_usage(response) # Will attempt to extract account_id
```
### Access New Metrics
All metrics are available through the `get_stats()` method:
@@ -163,10 +176,14 @@ All metrics are available through the `get_stats()` method:
stats = limiter.get_stats()
print(f"App call count: {stats['app_call_count']}%")
print(f"Ad account usage: {stats['ad_account_usage_pct']}%")
print(f"Reset in: {stats['reset_time_duration']}s")
print(f"Regain access in: {stats['estimated_time_to_regain_access']} min")
print(f"API tier: {stats['ads_api_access_tier']}")
# Per-account metrics
for account_id, usage in stats['ad_account_usage'].items():
print(f"Account {account_id}:")
print(f" Usage: {usage['acc_id_util_pct']}%")
print(f" Reset in: {usage['reset_time_duration']}s")
print(f" API tier: {usage['ads_api_access_tier']}")
# Business use case details
for buc in stats['buc_usage']: