Skip to content

Show sample for each Level type on ROBOT report#134

Open
anitacaron wants to merge 5 commits intomasterfrom
anitacaron/issue133
Open

Show sample for each Level type on ROBOT report#134
anitacaron wants to merge 5 commits intomasterfrom
anitacaron/issue133

Conversation

@anitacaron
Copy link
Copy Markdown
Collaborator

@anitacaron anitacaron commented Oct 10, 2024

Fixes #133

Preview using CDAO as example

Screenshot 2024-10-10 at 18 03 55

Summary by CodeRabbit

  • Bug Fixes

    • Improved error handling with structured logging for clearer issue reporting.
  • New Features

    • Controlled sampling of report rows: ensures all ERROR entries are retained, samples other levels by rule/subject, and applies truncation when limits are exceeded.
    • HTML output now reflects the filtered/sampled report data.
  • Chores

    • Disabled an automated report-truncation make target.

@anitacaron anitacaron self-assigned this Oct 10, 2024
@anitacaron
Copy link
Copy Markdown
Collaborator Author

@coderabbitai full review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Oct 14, 2024

✅ Actions performed

Full review triggered.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Oct 14, 2024

📝 Walkthrough

Walkthrough

The PR updates util/create_report_html.py to add stratified sampling of TSV report rows (ensuring all "ERROR" rows and sampling others by Level/Rule/Subject up to a line limit), switches file mode to read-write, replaces prints with structured logging, improves exception handling, and writes filtered data back. The Makefile target recipe is commented out.

Changes

Cohort / File(s) Summary
Report processing script
util/create_report_html.py
Added stratified sampling: count ERROR rows, include all ERRORs, sample remaining rows by Level,Rule Name,Subject up to --limitlines; fall back to truncation if columns missing or ERRORs exceed limit. Switched report file open mode to r+. Replaced prints with logging and improved exception handling. HTML rendering now uses the filtered dataframe. Areas to review: sampling correctness, group/sample logic, file overwrite behavior, and logging messages.
Build/CI helper
Makefile
Commented out the recipe for the truncate_reports_for_github target (target left defined but recipe disabled). Review for any CI/developer workflow impacts.

Assessment against linked issues

Objective Addressed Explanation
Include all ERROR, increase rows to 1000, and strip out owlclassexpression hashcodes (#133) All ERRORs are included; limitlines controls total rows (e.g., 1000). Hashcode stripping not touched.
Stratify the cut-off by shrinking large groups where necessary (#133) Implements stratified sampling by Level, Rule Name, and Subject.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Out of Scope Changes check ❓ Inconclusive The Makefile change disabling truncate_reports_for_github is potentially out-of-scope or requires clarification, as it may conflict with the goal of enforcing a line limit. All other changes in util/create_report_html.py directly support the stratified sampling objective. Clarify whether disabling the truncate_reports_for_github make target is intentional and how it relates to the stratified sampling implementation for compliance with issue #133.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: implementing stratified sampling in the ROBOT report to show samples for each Level type, which aligns with the primary objective of the PR.
Linked Issues check ✅ Passed The PR partially addresses linked issue #133 by implementing stratified sampling to include all ERROR entries and sample other levels, and increasing support for larger datasets. However, it does not address removing owlclassexpression hashcodes or increasing the row limit to 1000.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch anitacaron/issue133

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
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: 5

🧹 Outside diff range and nitpick comments (1)
Makefile (1)

Line range hint 1-27: Overall impact on Makefile is minimal and aligned with objectives.

The removal of the truncate_reports_for_github target is the only change in this Makefile. This modification doesn't affect the execution of other targets or introduce any new dependencies. The overall structure for building and managing the dashboard remains intact.

Consider updating any documentation that might reference the removed truncate_reports_for_github target to reflect this change. This will help maintain consistency between the code and its documentation.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between ea99dd2 and 32c9dbb.

📒 Files selected for processing (3)
  • Makefile (1 hunks)
  • util/create_report_html.py (1 hunks)
  • util/dashboard_config.py (1 hunks)
🧰 Additional context used
🔇 Additional comments (1)
util/dashboard_config.py (1)

78-79: Verify the impact of disabling GitHub post-processing

The lines for post-processing files for GitHub have been commented out. This change may affect how the dashboard files are handled after their generation.

To ensure this change doesn't negatively impact the workflow, please run the following script:

This script will help identify any potential issues arising from the removal of this functionality. Please review the results to ensure that no critical GitHub-related processing is being overlooked.

Consider the following points:

  1. If this change is intentional, update any documentation that might reference the GitHub post-processing step.
  2. Ensure that any downstream processes or scripts that may have depended on this post-processing step are updated accordingly.
  3. If this is a temporary change for debugging or testing purposes, consider using a feature flag instead of commenting out the code to make it easier to re-enable in the future.
✅ Verification successful

Verified: Disabling GitHub post-processing has no adverse effects

The commented-out lines for post-processing files for GitHub in util/dashboard_config.py do not impact the current functionality:

  • The truncate_reports_for_github make target does not exist in the Makefile, indicating that this functionality is not in use.
  • Other GitHub-related references found are confined to comments and documentation, posing no risk to the workflow.
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for any references to the truncate_reports_for_github make target

# Test 1: Search for references to the make target in Python files
echo "Searching for references to truncate_reports_for_github in Python files:"
rg --type python "truncate_reports_for_github"

# Test 2: Check if the make target still exists in the Makefile
echo "Checking if the truncate_reports_for_github target exists in the Makefile:"
rg --type make "^truncate_reports_for_github:"

# Test 3: Look for any GitHub-related processing in other parts of the codebase
echo "Searching for other GitHub-related processing:"
rg --type python "github"

Length of output: 600


Script:

#!/bin/bash
# Description: Re-verify references to truncate_reports_for_github and GitHub-related processing

# Test 1: Search for references to the make target in Python files
echo "Searching for references to truncate_reports_for_github in Python files:"
rg "truncate_reports_for_github" -- '*.py'

# Test 2: Check if the truncate_reports_for_github target exists in the Makefile
echo "Checking if the truncate_reports_for_github target exists in the Makefile:"
rg "^truncate_reports_for_github:" Makefile

# Test 3: Look for any GitHub-related processing in other parts of the codebase
echo "Searching for other GitHub-related processing in Python files:"
rg "github" -- '*.py'

Length of output: 649


Script:

#!/bin/bash
# Description: Re-verify references to truncate_reports_for_github and GitHub-related processing

# Test 1: Search for references to the make target in Python files
echo "Searching for references to truncate_reports_for_github in Python files:"
rg "truncate_reports_for_github" --glob "*.py"

# Test 2: Check if the truncate_reports_for_github target exists in the Makefile
echo "Checking if the truncate_reports_for_github target exists in the Makefile:"
rg "^truncate_reports_for_github:" Makefile

# Test 3: Look for any GitHub-related processing in Python files
echo "Searching for other GitHub-related processing in Python files:"
rg "github" --glob "*.py"

Length of output: 8422


Script:

#!/bin/bash
# Description: Re-verify references to truncate_reports_for_github and GitHub-related processing

# Test 1: Search for references to the make target in Python files
echo "Searching for references to truncate_reports_for_github in Python files:"
rg "truncate_reports_for_github" --glob "*.py"

# Test 2: Check if the truncate_reports_for_github target exists in the Makefile
echo "Checking if the truncate_reports_for_github target exists in the Makefile:"
rg "^truncate_reports_for_github:" Makefile

# Test 3: Look for any GitHub-related processing in Python files
echo "Searching for other GitHub-related processing in Python files:"
rg "github" --glob "*.py"

Length of output: 8422

Comment thread Makefile
Comment thread util/create_report_html.py Outdated
Comment thread util/create_report_html.py Outdated
Comment thread util/create_report_html.py
Comment thread util/create_report_html.py Outdated
Copy link
Copy Markdown
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: 4

♻️ Duplicate comments (2)
util/create_report_html.py (2)

65-68: Good implementation of column validation.

This properly addresses the previous review comment about ensuring required columns exist.


84-89: Good implementation of structured exception handling.

This properly addresses the previous review comment about using logging instead of print statements.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 32c9dbb and 634ede8.

📒 Files selected for processing (1)
  • util/create_report_html.py (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: QC
🔇 Additional comments (1)
util/create_report_html.py (1)

5-5: LGTM! Good addition of structured logging.

The logging configuration provides better error tracking with timestamps and log levels.

Also applies to: 13-14

parser = argparse.ArgumentParser(description='Create a report HTML page')
parser.add_argument('report',
type=argparse.FileType('r'),
type=argparse.FileType('r+'),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Consider using a separate output file instead of overwriting the input.

Opening the report file in 'r+' mode and overwriting it at line 82 is risky. If an error occurs during processing, the original data could be lost.

Consider adding a separate output parameter for the filtered report:

 parser.add_argument('report',
-                    type=argparse.FileType('r+'),
+                    type=argparse.FileType('r'),
                     help='TSV report to convert to HTML')
+parser.add_argument('--output-report',
+                    type=argparse.FileType('w'),
+                    help='Output filtered report TSV file (optional)')

Then modify line 82:

-if len(report_filtered) > args.limitlines:
-    report_filtered.to_csv(args.report, sep="\t", index=False)
+if args.output_report and len(report) > args.limitlines:
+    report_filtered.to_csv(args.output_report, sep="\t", index=False)

Also applies to: 82-82

🤖 Prompt for AI Agents
In util/create_report_html.py at line 21, the argument parser currently opens
the input report file in 'r+' mode, which risks overwriting the original file if
an error occurs. To fix this, add a new command-line argument for a separate
output file to write the filtered report. Then, at line 82, change the code to
write to this new output file instead of overwriting the input file. This
ensures the original input remains intact in case of processing errors.

Comment on lines +81 to +82
if len(report_filtered) > args.limitlines:
report_filtered.to_csv(args.report, sep="\t", index=False)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Remove or fix the illogical condition.

The condition len(report_filtered) > args.limitlines doesn't make sense after filtering. The filtered report should already be within the limit.

Either remove this block or change the logic to save the filtered report when the original report exceeds the limit:

-if len(report_filtered) > args.limitlines:
-    report_filtered.to_csv(args.report, sep="\t", index=False)
+# Save filtered report if original exceeded limit
+if len(report) > args.limitlines and args.output_report:
+    report_filtered.to_csv(args.output_report, sep="\t", index=False)

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In util/create_report_html.py around lines 81 to 82, the condition checking if
the filtered report length exceeds the limit is illogical since filtering should
already enforce the limit. Fix this by changing the condition to check if the
original report length exceeds args.limitlines before saving the filtered
report, or remove the condition entirely to always save the filtered report.

Comment on lines +71 to +75
report_filtered = report.groupby(by=["Level","Rule Name","Subject"])[
["Level", "Rule Name", "Subject", "Property", "Value"]
].apply(
lambda x: x.sample(calculate_sample_size(x, rest))
).reset_index(drop=True)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Make the sampling more robust against edge cases.

The current implementation could fail if groups are empty or if sampling parameters are invalid.

Consider adding error handling:

 # Get a sample of each Level type
-report_filtered = report.groupby(by=["Level","Rule Name","Subject"])[
-    ["Level", "Rule Name", "Subject", "Property", "Value"]
-].apply(
-    lambda x: x.sample(calculate_sample_size(x, rest))
-).reset_index(drop=True)
+def safe_sample(group, sample_size_func):
+    try:
+        n = sample_size_func(group)
+        if n > 0 and len(group) > 0:
+            return group.sample(min(n, len(group)))
+        return group
+    except Exception as e:
+        logging.warning(f"Sampling failed for group: {e}")
+        return group
+
+report_filtered = report.groupby(by=["Level","Rule Name","Subject"])[
+    ["Level", "Rule Name", "Subject", "Property", "Value"]
+].apply(
+    lambda x: safe_sample(x, calculate_sample_size)
+).reset_index(drop=True)

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In util/create_report_html.py around lines 71 to 75, the current groupby
sampling can fail if groups are empty or sampling parameters are invalid. To fix
this, add error handling inside the lambda function to check if the group is
empty or if the sample size is valid before calling sample. If invalid, return
the group as is or an empty DataFrame to avoid exceptions during sampling.

Comment on lines +56 to +64
rest = args.limitlines - error_count_level["ERROR"]

# Calculate the sample number for each level based on group size
def calculate_sample_size(group, rest):
if group["Level"].iloc[0] == "ERROR":
return group.shape[0]

return min(group.shape[0], rest)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix the sampling logic to properly distribute remaining lines.

The current logic incorrectly uses the full rest value for each non-ERROR group, which could exceed the limit when multiple groups exist.

The sampling should distribute the remaining lines proportionally among non-ERROR levels:

-error_count_error = error_count_level.get("ERROR", 0)
-if error_count_error < args.limitlines:
-    rest = args.limitlines - error_count_level["ERROR"]
-
-    # Calculate the sample number for each level based on group size
-    def calculate_sample_size(group, rest):
-        if group["Level"].iloc[0] == "ERROR":
-            return group.shape[0]
-
-        return min(group.shape[0], rest)
+error_count_error = error_count_level.get("ERROR", 0)
+if error_count_error < args.limitlines:
+    rest = args.limitlines - error_count_error
+    
+    # Calculate proportional sample sizes for non-ERROR levels
+    non_error_counts = {level: count for level, count in error_count_level.items() if level != "ERROR"}
+    total_non_error = sum(non_error_counts.values())
+    
+    level_sample_sizes = {"ERROR": error_count_error}
+    if total_non_error > 0:
+        for level, count in non_error_counts.items():
+            # Proportionally distribute remaining lines
+            level_sample_sizes[level] = int(rest * count / total_non_error)
+    
+    def calculate_sample_size(group):
+        level = group["Level"].iloc[0]
+        group_key = (level, group["Rule Name"].iloc[0], group["Subject"].iloc[0])
+        # Distribute level sample size among groups of that level
+        level_sample = level_sample_sizes.get(level, 0)
+        # This is a simplified approach - you may want to further distribute among groups
+        return min(group.shape[0], max(1, level_sample))

Also, line 56 still uses direct dictionary access instead of .get():

-rest = args.limitlines - error_count_level["ERROR"]
+rest = args.limitlines - error_count_error

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In util/create_report_html.py around lines 56 to 64, fix the sampling logic by
replacing the direct dictionary access on line 56 with .get() to avoid KeyError,
and update the calculate_sample_size function to distribute the remaining lines
proportionally among non-ERROR groups instead of using the full rest value for
each group. Compute the total size of non-ERROR groups, then allocate sample
sizes based on each group's proportion of that total, ensuring the sum does not
exceed the limit.

Copy link
Copy Markdown
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.

🧹 Nitpick comments (1)
Makefile (1)

21-27: Replace the retired recipe with a single pointer comment.

Lines 21-27 leave obsolete Make logic in-place as commented code. Since truncation now happens in util/create_report_html.py, this block is more likely to confuse than help.

♻️ Suggested cleanup
-# Truncate potentially huge robot reports
-# truncate_reports_for_github:
-# 	$(eval REPORTS := $(wildcard dashboard/*/robot_report.tsv))
-# 	for REP in $(REPORTS); do \
-# 		touch $$REP; \
-# 		cat $$REP | head -$(REPORT_LENGTH_LIMIT) > $$REP.tmp; \
-# 		mv $$REP.tmp $$REP; \
-# 	done
+# Report sampling/truncation is handled in util/create_report_html.py.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@Makefile` around lines 21 - 27, The commented-out Make recipe
truncate_reports_for_github (and related variables like REPORTS and
REPORT_LENGTH_LIMIT) is obsolete and should be removed to avoid confusion;
replace the entire commented block with a single pointer comment that directs
readers to util/create_report_html.py where truncation now happens, e.g.
"Truncation moved to util/create_report_html.py — see that script for current
behavior." Locate the commented recipe by the recipe name
truncate_reports_for_github in the Makefile and update that section accordingly.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@Makefile`:
- Around line 21-27: The commented-out Make recipe truncate_reports_for_github
(and related variables like REPORTS and REPORT_LENGTH_LIMIT) is obsolete and
should be removed to avoid confusion; replace the entire commented block with a
single pointer comment that directs readers to util/create_report_html.py where
truncation now happens, e.g. "Truncation moved to util/create_report_html.py —
see that script for current behavior." Locate the commented recipe by the recipe
name truncate_reports_for_github in the Makefile and update that section
accordingly.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: dfc08311-dd99-4c98-9c67-08e30ef19686

📥 Commits

Reviewing files that changed from the base of the PR and between 634ede8 and bb8be87.

📒 Files selected for processing (1)
  • Makefile

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.

[ROBOT report] Show a sample of each rule type

1 participant