-
Notifications
You must be signed in to change notification settings - Fork 22
fix: Keep track of synthetic apify-default-dataset-item events #814
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
vdusek
merged 11 commits into
apify:master
from
Mantisus:apify-default-dataset-item-event
Mar 11, 2026
Merged
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
209ee79
Keep track of synthetic apify-default-dataset-item events
Mantisus d49ca40
update name
Mantisus b097706
Update src/apify/storage_clients/_ppe_dataset_mixin.py
Mantisus 7347d30
add docstring and fix
Mantisus 55f13e8
Merge branch 'master' into apify-default-dataset-item-event
Mantisus e4fc4bf
Update src/apify/_charging.py
Mantisus 34e4617
merge master
Mantisus 51e28b5
update cherge_lock
Mantisus ddfaf9e
resolve conflict
Mantisus d919005
fix
Mantisus 5316f4b
update `charge_lock`
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
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,67 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| from typing_extensions import Self, override | ||
|
|
||
| from crawlee.storage_clients._file_system import FileSystemDatasetClient | ||
|
|
||
| from apify.storage_clients._ppe_dataset_mixin import DatasetClientPpeMixin | ||
|
|
||
| if TYPE_CHECKING: | ||
| from crawlee.configuration import Configuration | ||
|
|
||
|
|
||
| class ApifyFileSystemDatasetClient(FileSystemDatasetClient, DatasetClientPpeMixin): | ||
Mantisus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Apify-specific implementation of the `FileSystemDatasetClient`. | ||
|
|
||
| It extends the functionality of `FileSystemDatasetClient` using `DatasetClientPpeMixin` and updates `push_data` to | ||
| limit and charge for the synthetic `apify-default-dataset-item` event. This is necessary for consistent behavior | ||
| when locally testing the `PAY_PER_EVENT` pricing model. | ||
| """ | ||
|
|
||
| def __init__(self, *args: Any, **kwargs: Any) -> None: | ||
| FileSystemDatasetClient.__init__(self, *args, **kwargs) | ||
| DatasetClientPpeMixin.__init__(self) | ||
|
|
||
| @override | ||
| @classmethod | ||
| async def open( | ||
| cls, | ||
| *, | ||
| id: str | None, | ||
| name: str | None, | ||
| alias: str | None, | ||
| configuration: Configuration, | ||
| ) -> Self: | ||
|
|
||
| dataset_client = await super().open( | ||
| id=id, | ||
| name=name, | ||
| alias=alias, | ||
| configuration=configuration, | ||
| ) | ||
|
|
||
| dataset_client.is_default_dataset = all(v is None for v in (id, name, alias)) | ||
vdusek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return dataset_client | ||
|
|
||
| @override | ||
| async def push_data(self, data: list[dict[str, Any]] | dict[str, Any]) -> None: | ||
Mantisus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| async with self._lock: | ||
| items = data if isinstance(data, list) else [data] | ||
| limit = self._calculate_limit_for_push(len(items)) | ||
|
|
||
| new_item_count = self._metadata.item_count | ||
| for item in items: | ||
Mantisus marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| new_item_count += 1 | ||
| await self._push_item(item, new_item_count) | ||
|
|
||
| # now update metadata under the same lock | ||
| await self._update_metadata( | ||
| update_accessed_at=True, | ||
| update_modified_at=True, | ||
| new_item_count=new_item_count, | ||
| ) | ||
|
|
||
| await self._charge_for_items(limit) | ||
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,23 @@ | ||
| from apify._charging import DEFAULT_DATASET_ITEM_EVENT, charging_manager_ctx | ||
|
|
||
|
|
||
| class DatasetClientPpeMixin: | ||
| """A mixin for dataset clients to add support for PPE pricing model and tracking synthetic events.""" | ||
|
|
||
| def __init__(self) -> None: | ||
| self.is_default_dataset = False | ||
|
|
||
| def _calculate_limit_for_push(self, items_count: int) -> int: | ||
| if self.is_default_dataset and (charging_manager := charging_manager_ctx.get()): | ||
| max_charged_count = charging_manager.calculate_max_event_charge_count_within_limit( | ||
| event_name=DEFAULT_DATASET_ITEM_EVENT | ||
| ) | ||
| return min(max_charged_count, items_count) if max_charged_count is not None else items_count | ||
| return items_count | ||
|
|
||
| async def _charge_for_items(self, count_items: int) -> None: | ||
| if self.is_default_dataset and (charging_manager := charging_manager_ctx.get()): | ||
| await charging_manager.charge( | ||
| event_name=DEFAULT_DATASET_ITEM_EVENT, | ||
| count=count_items, | ||
| ) |
Oops, something went wrong.
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.