-
Notifications
You must be signed in to change notification settings - Fork 14
feat: add rate limit statistics #343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ebebedc
feat: add rate limit statistics
Mantisus d95b7c8
Update src/apify_client/_statistics.py
Mantisus b5cc378
fix increment stats calls for `HTTPClientAsync`
Mantisus d7bdbda
add describe fields
Mantisus 04ca439
add ids for test
Mantisus 43e53a6
list to dict
Mantisus a89255d
add pytest.param in test
Mantisus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from dataclasses import dataclass, field | ||
|
||
|
||
@dataclass | ||
class Statistics: | ||
"""Statistics about API client usage and rate limit errors.""" | ||
|
||
calls: int = 0 | ||
requests: int = 0 | ||
rate_limit_errors: list[int] = field(default_factory=list) | ||
vdusek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def add_rate_limit_error(self, attempt: int) -> None: | ||
"""Add rate limit error for specific attempt. | ||
|
||
Args: | ||
attempt: The attempt number (1-based indexing) | ||
Mantisus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
if attempt < 1: | ||
raise ValueError('Attempt must be greater than 0') | ||
|
||
index = attempt - 1 | ||
self._ensure_list_capacity(index) | ||
self.rate_limit_errors[index] += 1 | ||
|
||
def _ensure_list_capacity(self, index: int) -> None: | ||
"""Ensure rate_limit_errors list has enough capacity. | ||
|
||
Args: | ||
index: Required index to access | ||
""" | ||
if len(self.rate_limit_errors) <= index: | ||
self.rate_limit_errors.extend([0] * (index - len(self.rate_limit_errors) + 1)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import pytest | ||
|
||
from apify_client._statistics import Statistics | ||
|
||
|
||
@pytest.mark.parametrize( | ||
('attempts', 'expected_errors'), | ||
[ | ||
([1], [1]), | ||
([1, 5], [1, 0, 0, 0, 1]), | ||
([5, 1], [1, 0, 0, 0, 1]), | ||
([3, 5, 1], [1, 0, 1, 0, 1]), | ||
([1, 5, 3], [1, 0, 1, 0, 1]), | ||
([2, 1, 2, 1, 5, 2, 1], [3, 3, 0, 0, 1]), | ||
], | ||
) | ||
def test_add_rate_limit_error(attempts: list[int], expected_errors: list[int]) -> None: | ||
"""Test that add_rate_limit_error correctly tracks errors for different attempt sequences.""" | ||
stats = Statistics() | ||
for attempt in attempts: | ||
stats.add_rate_limit_error(attempt) | ||
assert stats.rate_limit_errors == expected_errors | ||
vdusek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def test_add_rate_limit_error_invalid_attempt() -> None: | ||
"""Test that add_rate_limit_error raises ValueError for invalid attempt.""" | ||
stats = Statistics() | ||
with pytest.raises(ValueError, match='Attempt must be greater than 0'): | ||
stats.add_rate_limit_error(0) | ||
|
||
|
||
def test_statistics_initial_state() -> None: | ||
"""Test initial state of Statistics instance.""" | ||
stats = Statistics() | ||
assert stats.calls == 0 | ||
assert stats.requests == 0 | ||
assert stats.rate_limit_errors == [] | ||
|
||
|
||
def test_add_rate_limit_error_type_validation() -> None: | ||
"""Test type validation in add_rate_limit_error.""" | ||
stats = Statistics() | ||
with pytest.raises(TypeError): | ||
stats.add_rate_limit_error('1') # type: ignore[arg-type] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.