codeant-ai-for-open-source[bot] commented on code in PR #44034:
URL: https://github.com/apache/superset/pull/44034#discussion_r3963681584
##########
superset/utils/screenshot_utils.py:
##########
@@ -130,26 +136,77 @@ class ScreenshotCaptureTimeoutError(RuntimeError):
"""Raised when Chromium repeatedly times out while capturing a tile."""
-def is_screenshot_nearly_uniform(screenshot: bytes) -> tuple[bool, float]:
- """Return whether one color occupies nearly all sampled screenshot
pixels."""
+class ScreenshotBlankCaptureError(RuntimeError):
+ """Raised when Chromium repeatedly returns a perceptually blank capture."""
+
+
+@dataclass(frozen=True)
+class ScreenshotBlanknessMetrics:
+ """Metrics used to decide whether a screenshot is perceptually blank."""
+
+ is_blank: bool
+ dominant_pixel_ratio: float
+ near_white_pixel_ratio: float
+ mean_luminance: float
+ luminance_stddev: float
+ entropy: float
+
+
+def get_screenshot_blankness_metrics(screenshot: bytes) ->
ScreenshotBlanknessMetrics:
+ """Measure exact-color and perceptual blankness on a sampled screenshot."""
try:
with Image.open(io.BytesIO(screenshot)) as image:
sample = image.convert("RGB")
sample.thumbnail((256, 256))
- colors = sample.getcolors(maxcolors=256)
- if not colors:
- return False, 0.0
- dominant_pixels = max(count for count, _color in colors)
- dominant_ratio = dominant_pixels / (sample.width * sample.height)
- return (
- dominant_ratio >= TILED_SCREENSHOT_BLANK_DOMINANT_PIXEL_RATIO,
- dominant_ratio,
+ pixel_count = sample.width * sample.height
+ colors = sample.getcolors(maxcolors=pixel_count) or []
+ dominant_pixels = max(
+ (count for count, _color in colors),
+ default=0,
+ )
+ dominant_ratio = dominant_pixels / pixel_count
+
+ grayscale = sample.convert("L")
+ histogram = grayscale.histogram()
+ near_white_ratio = (
+ sum(histogram[SCREENSHOT_BLANK_MIN_LUMINANCE:]) / pixel_count
+ )
+ statistics = ImageStat.Stat(grayscale)
+ mean_luminance = float(statistics.mean[0])
+ luminance_stddev = float(statistics.stddev[0])
+ entropy = -sum(
+ (count / pixel_count) * math.log2(count / pixel_count)
+ for count in histogram
+ if count
+ )
+ exact_uniform = (
+ dominant_ratio >= TILED_SCREENSHOT_BLANK_DOMINANT_PIXEL_RATIO
+ )
+ perceptually_blank = (
+ near_white_ratio >= SCREENSHOT_BLANK_NEAR_WHITE_PIXEL_RATIO
+ and luminance_stddev <= SCREENSHOT_BLANK_MAX_LUMINANCE_STDDEV
+ and entropy <= SCREENSHOT_BLANK_MAX_ENTROPY
+ )
Review Comment:
**Suggestion:** The perceptual rule classifies any image with fewer than
0.5% dark pixels as blank when its remaining pixels are near-white, rejecting
legitimate sparse charts or thin text. [incorrect condition logic]
**Assessment:** ๐ `Major` ยท ๐ `Occurrence: Sometimes`
[](https://docs.codeant.ai/cli/resolve-pr-comments-skill)
[](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=74255db252fd4fe69642faa3622bdba2&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
[](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=74255db252fd4fe69642faa3622bdba2&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
<details>
<summary><b>Prompt for AI Agent ๐ค </b></summary>
```mdx
This is a comment left during a code review.
**Path:** superset/utils/screenshot_utils.py
**Line:** 186:190
**Comment:**
*Incorrect Condition Logic: The perceptual rule classifies any image
with fewer than 0.5% dark pixels as blank when its remaining pixels are
near-white, rejecting legitimate sparse charts or thin text.
Validate the correctness of the flagged issue. If correct, How can I resolve
this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask
user if the user wants to fix the rest of the comments as well. if said yes,
then fetch all the comments validate the correctness and implement a minimal fix
```
</details>
<a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F44034&comment_hash=8d9d4e5a7dc5fe5913856a96e9dd3253243759d562ebd6deeb278d939d9794ec&reaction=like'>๐</a>
| <a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F44034&comment_hash=8d9d4e5a7dc5fe5913856a96e9dd3253243759d562ebd6deeb278d939d9794ec&reaction=dislike'>๐</a>
##########
superset/utils/webdriver.py:
##########
@@ -240,6 +243,105 @@ def _get_screenshot(
else:
return element.screenshot(**timeout_kwargs)
+ @staticmethod
+ def _get_validated_screenshot(
+ page: Page,
+ element: Locator,
+ element_name: str,
+ log_context: str | None,
+ report_execution_context: ReportExecutionContext | None,
+ ) -> bytes:
+ """Capture a standard screenshot and reject blank report output."""
+
+ context_suffix = f" [{log_context}]" if log_context else ""
+ content_expected = element_name == "chart-container" or bool(
+ report_execution_context and
report_execution_context.expected_chart_count
+ )
+ for attempt in range(1, TILED_SCREENSHOT_MAX_CAPTURE_ATTEMPTS + 1):
+ capture_timeout = (
+ report_execution_context.deadline.timeout_seconds(
+ "screenshot_capture",
+ reserve_seconds=(
+ report_execution_context.post_capture_reserve_seconds
+ ),
+ )
+ if report_execution_context
+ else None
+ )
+ capture_started_at = time.monotonic()
+ image = WebDriverPlaywright._get_screenshot(
+ page,
+ element,
+ element_name,
+ timeout_seconds=capture_timeout,
+ )
+ capture_elapsed = time.monotonic() - capture_started_at
+ if report_execution_context is None:
+ return image
+
+ blankness = get_screenshot_blankness_metrics(image)
+ is_blank = content_expected and blankness.is_blank
+ logger.info(
+ "report_capture_validation capture=standard attempt=%s/%s "
+ "capture_elapsed_seconds=%.2f is_blank=%s "
+ "dominant_pixel_ratio=%.5f near_white_pixel_ratio=%.5f "
+ "mean_luminance=%.2f luminance_stddev=%.2f entropy=%.3f%s",
+ attempt,
+ TILED_SCREENSHOT_MAX_CAPTURE_ATTEMPTS,
+ capture_elapsed,
+ is_blank,
+ blankness.dominant_pixel_ratio,
+ blankness.near_white_pixel_ratio,
+ blankness.mean_luminance,
+ blankness.luminance_stddev,
+ blankness.entropy,
+ context_suffix,
+ )
+ if not is_blank:
+ return image
+
+ logger.warning(
+ "report_capture_blank_standard attempt=%s/%s "
+ "capture_elapsed_seconds=%.2f dominant_pixel_ratio=%.5f "
+ "near_white_pixel_ratio=%.5f mean_luminance=%.2f "
+ "luminance_stddev=%.2f entropy=%.3f%s",
+ attempt,
+ TILED_SCREENSHOT_MAX_CAPTURE_ATTEMPTS,
+ capture_elapsed,
+ blankness.dominant_pixel_ratio,
+ blankness.near_white_pixel_ratio,
+ blankness.mean_luminance,
+ blankness.luminance_stddev,
+ blankness.entropy,
+ context_suffix,
+ )
+ if attempt == TILED_SCREENSHOT_MAX_CAPTURE_ATTEMPTS:
+ raise ScreenshotBlankCaptureError(
+ "Chromium returned a blank standard screenshot "
+ f"after {attempt} attempts"
+ )
+ try:
+ page.bring_to_front()
+ page.evaluate(
+ """() => {
+ window.scrollBy(0, 1);
+ window.scrollBy(0, -1);
+ return new Promise(resolve => requestAnimationFrame(
+ () => requestAnimationFrame(resolve)
+ ));
+ }"""
+ )
Review Comment:
**Suggestion:** `page.evaluate()` waits for two animation frames without a
timeout, so a wedged page can block standard screenshot retries beyond the
report deadline. [api mismatch]
**Assessment:** ๐ `Major` ยท ๐ `Occurrence: Sometimes`
[](https://docs.codeant.ai/cli/resolve-pr-comments-skill)
[](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=9865701874cf42949052f5d1ea1de038&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
[](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=9865701874cf42949052f5d1ea1de038&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
<details>
<summary><b>Prompt for AI Agent ๐ค </b></summary>
```mdx
This is a comment left during a code review.
**Path:** superset/utils/webdriver.py
**Line:** 324:333
**Comment:**
*Api Mismatch: `page.evaluate()` waits for two animation frames without
a timeout, so a wedged page can block standard screenshot retries beyond the
report deadline.
Validate the correctness of the flagged issue. If correct, How can I resolve
this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask
user if the user wants to fix the rest of the comments as well. if said yes,
then fetch all the comments validate the correctness and implement a minimal fix
```
</details>
<a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F44034&comment_hash=0d9a84f1723a1505b5901c0c9b3ad31c8e8a9ce4ba1df176d69013feadd2b063&reaction=like'>๐</a>
| <a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F44034&comment_hash=0d9a84f1723a1505b5901c0c9b3ad31c8e8a9ce4ba1df176d69013feadd2b063&reaction=dislike'>๐</a>
--
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]