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

fix: ScreenshotCachePayload serialization #32156

Merged
merged 2 commits into from
Feb 6, 2025

Conversation

betodealmeida
Copy link
Member

SUMMARY

Change the serialization of ScreenshotCachePayload, since it doesn't work across all cache backends. The new implementation serializes the object into a dictionary of attributes.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

TESTING INSTRUCTIONS

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

Copy link

korbit-ai bot commented Feb 5, 2025

Based on your review schedule, I'll hold off on reviewing this PR until it's marked as ready for review. If you'd like me to take a look now, comment /korbit-review.

Your admin can change your review schedule in the Korbit Console

@github-actions github-actions bot added the api Related to the REST API label Feb 5, 2025
@sadpandajoe
Copy link
Member

@michael-s-molina would be good if we can cherry this in also for part of 5.0rc2

@michael-s-molina michael-s-molina added the v5.0 Label added by the release manager to track PRs to be included in the 5.0 branch label Feb 6, 2025
@betodealmeida betodealmeida marked this pull request as ready for review February 6, 2025 15:51
Copy link

@korbit-ai korbit-ai bot left a comment

Choose a reason for hiding this comment

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

Review by Korbit AI

Korbit automatically attempts to detect when you fix issues in new commits.
Category Issue Fix Detected
Functionality Incorrect Type Cast Before Deserialization ▹ view
Functionality Status Override on Image Presence ▹ view
Performance Unnecessary Object Creation Before Serialization ▹ view
Files scanned
File Path Reviewed
superset/utils/screenshots.py
superset/charts/api.py
superset/dashboards/api.py

Explore our documentation to understand the languages and file types we support and the files we ignore.

Need a new review? Comment /korbit-review on this PR and I'll review your latest changes.

Korbit Guide: Usage and Customization

Interacting with Korbit

  • You can manually ask Korbit to review your PR using the /korbit-review command in a comment at the root of your PR.
  • You can ask Korbit to generate a new PR description using the /korbit-generate-pr-description command in any comment on your PR.
  • Too many Korbit comments? I can resolve all my comment threads if you use the /korbit-resolve command in any comment on your PR.
  • Chat with Korbit on issues we post by tagging @korbit-ai in your reply.
  • Help train Korbit to improve your reviews by giving a 👍 or 👎 on the comments Korbit posts.

Customizing Korbit

  • Check out our docs on how you can make Korbit work best for you and your team.
  • Customize Korbit for your organization through the Korbit Console.

Current Korbit Configuration

General Settings
Setting Value
Review Schedule Automatic excluding drafts
Max Issue Count 10
Automatic PR Descriptions
Issue Categories
Category Enabled
Documentation
Logging
Error Handling
Readability
Design
Performance
Security
Functionality

Feedback and Support

Note

Korbit Pro is free for open source projects 🎉

Looking to add Korbit to your team? Get started with a free 2 week trial here

Comment on lines 211 to 213
elif isinstance(payload, dict):
payload = cast(ScreenshotCachePayload, payload)
payload = ScreenshotCachePayload.from_dict(payload)
Copy link

Choose a reason for hiding this comment

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

Incorrect Type Cast Before Deserialization category Functionality

Tell me more
What is the issue?

The unnecessary cast to ScreenshotCachePayload before calling from_dict can cause runtime errors since the dictionary is being incorrectly typed.

Why this matters

The cast operation is misleading and doesn't actually convert the dictionary to a ScreenshotCachePayload. It could lead to runtime errors if the code tries to use the casted payload before the from_dict conversion.

Suggested change ∙ Feature Preview

Remove the unnecessary cast and directly call from_dict on the dictionary:

elif isinstance(payload, dict):
    payload = ScreenshotCachePayload.from_dict(payload)

Report a problem with this comment

💬 Chat with Korbit by mentioning @korbit-ai.

if image:
self.status = StatusValues.UPDATED
self._timestamp = timestamp or datetime.now().isoformat()
self.status = StatusValues.UPDATED if image else status
Copy link

Choose a reason for hiding this comment

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

Status Override on Image Presence category Functionality

Tell me more
What is the issue?

The initial status logic in ScreenshotCachePayload.init overrides the provided status parameter when an image is present, which may not be the desired behavior in all cases.

Why this matters

This could lead to unexpected state transitions when initializing a payload with both an image and a specific status, as the image presence will always force an UPDATED status.

Suggested change ∙ Feature Preview

Consider whether to respect the provided status parameter regardless of image presence:

self.status = status

Report a problem with this comment

💬 Chat with Korbit by mentioning @korbit-ai.

@@ -623,7 +623,7 @@ def build_response(status_code: int) -> WerkzeugResponse:

if cache_payload.should_trigger_task(force):
logger.info("Triggering screenshot ASYNC")
screenshot_obj.cache.set(cache_key, ScreenshotCachePayload())
screenshot_obj.cache.set(cache_key, ScreenshotCachePayload().to_dict())
Copy link

Choose a reason for hiding this comment

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

Unnecessary Object Creation Before Serialization category Performance

Tell me more
What is the issue?

Creating a ScreenshotCachePayload instance just to immediately convert it to a dictionary introduces unnecessary object creation and serialization overhead.

Why this matters

When this operation is performed frequently, the creation and immediate destruction of temporary objects can impact memory usage and create additional garbage collection pressure.

Suggested change ∙ Feature Preview

Create the dictionary directly instead of creating and converting a temporary object:

screenshot_obj.cache.set(cache_key, {"status": "pending", "timestamp": datetime.now().timestamp()})

Report a problem with this comment

💬 Chat with Korbit by mentioning @korbit-ai.

Copy link
Member Author

Choose a reason for hiding this comment

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

Readability counts. -- Zen of Python

@betodealmeida betodealmeida force-pushed the ScreenshotCachePayload-serialization branch from df2ff6c to ff3bab3 Compare February 6, 2025 16:00
Copy link
Member

@kgabryje kgabryje left a comment

Choose a reason for hiding this comment

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

LGTM

@betodealmeida betodealmeida merged commit e8990f4 into master Feb 6, 2025
40 checks passed
@betodealmeida betodealmeida deleted the ScreenshotCachePayload-serialization branch February 6, 2025 20:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Related to the REST API size/M v5.0 Label added by the release manager to track PRs to be included in the 5.0 branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants