Skip to content
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

refactor: remove pydantic from execution_store #4877

Merged
merged 1 commit into from
Oct 3, 2024

Conversation

ritch
Copy link
Contributor

@ritch ritch commented Oct 2, 2024

Removes pydantic from execution store implementation. Uses dataclasses instead.

Summary by CodeRabbit

  • New Features

    • Enhanced document creation and update handling with improved field management.
    • Introduced a new serialization method for KeyDocument to convert instances into MongoDB-compatible dictionaries.
  • Bug Fixes

    • Corrected method parameters for better consistency in document handling.
  • Refactor

    • Transitioned KeyDocument from a Pydantic model to a simpler dataclass structure.
    • Removed unused import from the ExecutionStore class, streamlining the code.

Copy link
Contributor

coderabbitai bot commented Oct 2, 2024

Walkthrough

The pull request introduces several modifications across three files related to the handling of store documents and key documents. In execution_store.py, the ExecutionStoreRepo class has updated methods for creating and setting keys, emphasizing the use of a MongoDB-compatible dictionary format. The KeyDocument class in models.py transitions from a Pydantic model to a dataclass, adding a new serialization method. Lastly, an unused import is removed from store.py, maintaining the existing functionality of the ExecutionStore class.

Changes

File Path Change Summary
fiftyone/factory/repos/execution_store.py - Modified create_store: changed permissions to value, and used to_mongo_dict() for document conversion.
- Updated set_key: added expires_at field and used to_mongo_dict() for updates.
fiftyone/operators/store/models.py - Changed KeyDocument from Pydantic to a dataclass, added _id field, and set created_at default with datetime.now.
- Introduced to_mongo_dict method for serialization.
fiftyone/operators/store/store.py - Removed unused import of RepositoryFactory.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant ExecutionStoreRepo
    participant MongoDB

    Client->>ExecutionStoreRepo: create_store(store_name, value)
    ExecutionStoreRepo->>MongoDB: insert(to_mongo_dict())
    
    Client->>ExecutionStoreRepo: set_key(store_name, key, value, ttl)
    ExecutionStoreRepo->>MongoDB: update($set: to_mongo_dict(exclude_keys))
Loading

🐰 In the meadow, changes bloom,
New structures rise, dispelling gloom.
From Pydantic to dataclass bright,
KeyDocument shines, a new delight!
With Mongo's call, we craft anew,
In the garden of code, we hop and chew! 🌼


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@@ -24,10 +24,8 @@ def __init__(self, collection: Collection):

def create_store(self, store_name, permissions=None) -> StoreDocument:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: Going to remove permissions ins a follow up commit

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 9f9182b and 1a4a41d.

📒 Files selected for processing (3)
  • fiftyone/factory/repos/execution_store.py (2 hunks)
  • fiftyone/operators/store/models.py (2 hunks)
  • fiftyone/operators/store/store.py (0 hunks)
💤 Files with no reviewable changes (1)
  • fiftyone/operators/store/store.py
🔇 Additional comments (3)
fiftyone/factory/repos/execution_store.py (3)

27-28: Initialization of StoreDocument and MongoDB insertion updated correctly

The StoreDocument is now initialized with store_name=store_name, value=permissions, and the insertion uses store_doc.to_mongo_dict(). This change aligns with the refactoring from Pydantic models to dataclasses and ensures proper serialization for MongoDB insertion.


41-45: Including expires_at in KeyDocument initialization

Adding expires_at=expiration to the KeyDocument initialization ensures that key expiration is correctly handled. This modification supports the proper management of TTL (time-to-live) for keys in the execution store.


50-55: Constructing $set update fields with to_mongo_dict()

Using key_doc.to_mongo_dict() and excluding specific keys to build the $set operation is appropriate. This approach ensures that only the intended fields are updated in the MongoDB document, aligning with the transition away from Pydantic.

Comment on lines +6 to 18
@dataclass
class KeyDocument:
"""Model representing a key in the store."""

store_name: str
key: str
value: Any
created_at: datetime.datetime = Field(
created_at: datetime.datetime = field(
default_factory=datetime.datetime.now
)
_id: Optional[Any] = None
updated_at: Optional[datetime.datetime] = None
expires_at: Optional[datetime.datetime] = None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider implementing data validation for KeyDocument fields

Transitioning from Pydantic's BaseModel to dataclasses removes the automatic data validation that Pydantic provides. To maintain data integrity, especially when instances of KeyDocument are created from external sources, consider adding validation methods or using libraries like pydantic.dataclasses to incorporate validation into your dataclasses.

Comment on lines +13 to 15
created_at: datetime.datetime = field(
default_factory=datetime.datetime.now
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Use timezone-aware timestamps for created_at

The created_at field uses datetime.datetime.now, which returns a naive datetime object without timezone information. This can lead to inconsistencies across different systems. Consider using timezone-aware datetime objects to ensure consistent timestamping.

You can modify the default_factory to include timezone information:

- created_at: datetime.datetime = field(
-     default_factory=datetime.datetime.now
- )
+ created_at: datetime.datetime = field(
+     default_factory=lambda: datetime.datetime.now(datetime.timezone.utc)
+ )
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
created_at: datetime.datetime = field(
default_factory=datetime.datetime.now
)
created_at: datetime.datetime = field(
default_factory=lambda: datetime.datetime.now(datetime.timezone.utc)
)

@ritch ritch merged commit 0a7acb4 into feat/execution-store Oct 3, 2024
9 of 13 checks passed
@ritch ritch deleted the remove-pydantic-from-execution-store branch October 3, 2024 02:02
@coderabbitai coderabbitai bot mentioned this pull request Oct 3, 2024
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant