villebro commented on code in PR #31757:
URL: https://github.com/apache/superset/pull/31757#discussion_r1937624416
##########
superset/utils/screenshots.py:
##########
@@ -54,6 +56,57 @@
from flask_caching import Cache
+class StatusValues(Enum):
+ PENDING = "Pending"
+ COMPUTING = "Computing"
+ UPDATED = "Updated"
+ ERROR = "Error"
+
+
+class ScreenshotCachePayload:
+ def __init__(self, image: bytes | None = None):
+ self._image = image
+ self._timestamp = datetime.now().strftime("%Y/%m/%d-%H:%M:%S")
Review Comment:
Hmm, I'd prefer not to use a custom format here. Let's just use `isoformat()`
```suggestion
self._timestamp = datetime.now().isoformat()
```
##########
superset/utils/screenshots.py:
##########
@@ -54,6 +56,57 @@
from flask_caching import Cache
+class StatusValues(Enum):
+ PENDING = "Pending"
+ COMPUTING = "Computing"
+ UPDATED = "Updated"
+ ERROR = "Error"
+
+
+class ScreenshotCachePayload:
+ def __init__(self, image: bytes | None = None):
+ self._image = image
+ self._timestamp = datetime.now().strftime("%Y/%m/%d-%H:%M:%S")
+ self.status = StatusValues.PENDING
+ if image:
+ self.status = StatusValues.UPDATED
+
+ def update_timestamp(self) -> None:
+ self._timestamp = datetime.now().strftime("%Y/%m/%d-%H:%M:%S")
Review Comment:
same
```suggestion
self._timestamp = datetime.now().isoformat()
```
##########
superset/charts/api.py:
##########
@@ -712,44 +745,41 @@ def thumbnail(self, pk: int, digest: str, **kwargs: Any)
-> WerkzeugResponse:
return self.response_404()
current_user = get_current_user()
- url = get_url_path("Superset.slice", slice_id=chart.id)
- if kwargs["rison"].get("force", False):
- logger.info(
- "Triggering thumbnail compute (chart id: %s) ASYNC",
str(chart.id)
- )
- cache_chart_thumbnail.delay(
- current_user=current_user,
- chart_id=chart.id,
- force=True,
+ if chart.digest != digest:
+ self.incr_stats("redirect", self.thumbnail.__name__)
+ return redirect(
+ url_for(
+ f"{self.__class__.__name__}.thumbnail", pk=pk,
digest=chart.digest
+ )
)
- return self.response(202, message="OK Async")
- # fetch the chart screenshot using the current user and cache if set
- screenshot = ChartScreenshot(url, chart.digest).get_from_cache(
- cache=thumbnail_cache
+ url = get_url_path("Superset.slice", slice_id=chart.id)
+ screenshot_obj = ChartScreenshot(url, chart.digest)
+ cache_key = screenshot_obj.get_cache_key()
+ cache_payload = (
+ screenshot_obj.get_from_cache_key(cache_key) or
ScreenshotCachePayload()
)
- # If not screenshot then send request to compute thumb to celery
- if not screenshot:
+
+ if cache_payload.status in [StatusValues.PENDING, StatusValues.ERROR]:
Review Comment:
Don't we want to check the error TTL here?
##########
superset/charts/api.py:
##########
@@ -564,8 +569,14 @@ def cache_screenshot(self, pk: int, **kwargs: Any) ->
WerkzeugResponse:
schema:
$ref: '#/components/schemas/screenshot_query_schema'
responses:
+ 200:
+ description: Chart async result
+ content:
+ application/json:
+ schema:
+ $ref:
"#/components/schemas/ChartCacheScreenshotResponseSchema"
202:
- description: Chart async result
+ description: Chart async task created
Review Comment:
Sorry about the back and forth here, but this seems more appropriate
```suggestion
description: Chart screenshot task created
```
##########
superset/dashboards/api.py:
##########
@@ -1296,13 +1205,120 @@ def screenshot(self, pk: int, digest: str) ->
WerkzeugResponse:
)
if download_format == "png":
return Response(
- FileWrapper(img),
+ FileWrapper(image),
mimetype="image/png",
direct_passthrough=True,
)
-
return self.response_404()
+ @expose("/<pk>/thumbnail/<digest>/", methods=("GET",))
+ @validate_feature_flags(["THUMBNAILS"])
+ @protect()
+ @safe
+ @rison(thumbnail_query_schema)
+ @event_logger.log_this_with_context(
+ action=lambda self, *args, **kwargs:
f"{self.__class__.__name__}.thumbnail",
+ log_to_statsd=False,
+ )
+ def thumbnail(self, pk: int, digest: str, **kwargs: Any) ->
WerkzeugResponse:
+ """Compute async or get already computed dashboard thumbnail from
cache.
+ ---
+ get:
+ summary: Get dashboard's thumbnail
+ description: >-
+ Computes async or get already computed dashboard thumbnail from
cache.
+ parameters:
+ - in: path
+ schema:
+ type: integer
+ name: pk
+ - in: path
+ name: digest
+ description: A hex digest that makes this dashboard unique
+ schema:
+ type: string
+ responses:
+ 200:
+ description: Dashboard thumbnail image
+ content:
+ image/*:
+ schema:
+ type: string
+ format: binary
+ 202:
+ description: Thumbnail does not exist on cache, fired async to
compute
+ content:
+ application/json:
+ schema:
+ type: object
+ properties:
+ message:
+ type: string
+ 302:
+ description: Redirects to the current digest
+ 401:
+ $ref: '#/components/responses/401'
+ 404:
+ $ref: '#/components/responses/404'
+ 422:
+ $ref: '#/components/responses/422'
+ 500:
+ $ref: '#/components/responses/500'
+ """
+ dashboard = cast(Dashboard, self.datamodel.get(pk, self._base_filters))
+ if not dashboard:
+ return self.response_404()
+
+ current_user = get_current_user()
+ dashboard_url = get_url_path(
+ "Superset.dashboard", dashboard_id_or_slug=dashboard.id
+ )
+ if dashboard.digest != digest:
+ self.incr_stats("redirect", self.thumbnail.__name__)
+ return redirect(
+ url_for(
+ f"{self.__class__.__name__}.thumbnail",
+ pk=pk,
+ digest=dashboard.digest,
+ )
+ )
+ screenshot_obj = DashboardScreenshot(dashboard_url, dashboard.digest)
+ cache_key = screenshot_obj.get_cache_key()
+ cache_payload = (
+ screenshot_obj.get_from_cache_key(cache_key) or
ScreenshotCachePayload()
+ )
+ image_url = get_url_path(
+ "DashboardRestApi.thumbnail", pk=dashboard.id, digest=cache_key
+ )
+
+ if cache_payload.status in [StatusValues.PENDING, StatusValues.ERROR]:
Review Comment:
here, too
##########
superset/charts/api.py:
##########
@@ -591,25 +603,45 @@ def cache_screenshot(self, pk: int, **kwargs: Any) ->
WerkzeugResponse:
chart_url = get_url_path("Superset.slice", slice_id=chart.id)
screenshot_obj = ChartScreenshot(chart_url, chart.digest)
- cache_key = screenshot_obj.cache_key(window_size, thumb_size)
+ cache_key = screenshot_obj.get_cache_key(window_size, thumb_size)
+ cache_payload = (
+ screenshot_obj.get_from_cache_key(cache_key) or
ScreenshotCachePayload()
+ )
image_url = get_url_path(
"ChartRestApi.screenshot", pk=chart.id, digest=cache_key
)
- def trigger_celery() -> WerkzeugResponse:
+ def build_response(status_code: int) -> WerkzeugResponse:
+ return self.response(
+ status_code,
+ cache_key=cache_key,
+ chart_url=chart_url,
+ image_url=image_url,
+ task_updated_at=cache_payload.get_timestamp(),
+ task_status=cache_payload.get_status(),
+ )
+
+ error_cache_ttl = config["THUMBNAIL_ERROR_CACHE_TTL"]
+ error_cache_expired = (
+ datetime.now()
+ - datetime.strptime(cache_payload.get_timestamp(),
"%Y/%m/%d-%H:%M:%S")
Review Comment:
same
```suggestion
- datetime.fromisoformat(cache_payload.get_timestamp())
```
##########
superset/dashboards/api.py:
##########
@@ -1197,13 +1094,29 @@ def cache_dashboard_screenshot(self, pk: int, **kwargs:
Any) -> WerkzeugResponse
dashboard_url = get_url_path("Superset.dashboard_permalink",
key=permalink_key)
screenshot_obj = DashboardScreenshot(dashboard_url, dashboard.digest)
- cache_key = screenshot_obj.cache_key(window_size, thumb_size,
dashboard_state)
+ cache_key = screenshot_obj.get_cache_key(
+ window_size, thumb_size, dashboard_state
+ )
image_url = get_url_path(
"DashboardRestApi.screenshot", pk=dashboard.id, digest=cache_key
)
+ cache_payload = (
+ screenshot_obj.get_from_cache_key(cache_key) or
ScreenshotCachePayload()
+ )
+
+ def build_response(status_code: int) -> WerkzeugResponse:
+ return self.response(
+ status_code,
+ cache_key=cache_key,
+ dashboard_url=dashboard_url,
+ image_url=image_url,
+ task_updated_at=cache_payload.get_timestamp(),
+ task_status=cache_payload.get_status(),
+ )
- def trigger_celery() -> WerkzeugResponse:
+ if cache_payload.status in [StatusValues.PENDING, StatusValues.ERROR]
or force:
Review Comment:
same here about checking error TTL
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]