villebro commented on code in PR #31757:
URL: https://github.com/apache/superset/pull/31757#discussion_r1937724380


##########
superset/utils/screenshots.py:
##########
@@ -54,6 +56,68 @@
     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().isoformat()
+        self.status = StatusValues.PENDING
+        if image:
+            self.status = StatusValues.UPDATED
+
+    def update_timestamp(self) -> None:
+        self._timestamp = datetime.now().isoformat()
+
+    def pending(self) -> None:
+        self.update_timestamp()
+        self._image = None
+        self.status = StatusValues.PENDING
+
+    def computing(self) -> None:
+        self.update_timestamp()
+        self._image = None
+        self.status = StatusValues.COMPUTING
+
+    def update(self, image: bytes) -> None:
+        self.update_timestamp()
+        self.status = StatusValues.UPDATED
+        self._image = image
+
+    def error(
+        self,
+    ) -> None:
+        self.update_timestamp()
+        self.status = StatusValues.ERROR
+
+    def get_image(self) -> BytesIO | None:
+        if not self._image:
+            return None
+        return BytesIO(self._image)
+
+    def get_timestamp(self) -> str:
+        return self._timestamp
+
+    def get_status(self) -> str:
+        return self.status.value
+
+    def should_trigger_task(self) -> bool:
+        def is_error_cache_ttl_expired() -> bool:
+            error_cache_ttl = app.config["THUMBNAIL_ERROR_CACHE_TTL"]
+            return (
+                datetime.now() - datetime.fromisoformat(self.get_timestamp())
+            ).total_seconds() > error_cache_ttl

Review Comment:
   I'd prefer a separate method, as redefining it each time will incur some 
extra overhead.



-- 
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]

Reply via email to