-
Notifications
You must be signed in to change notification settings - Fork 6
Add JSON output format, structured blocks, and progress bar control #38
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| from cordon.postprocess.formatter import XmlFormatter | ||
| from cordon.postprocess.json_formatter import JsonFormatter | ||
| from cordon.postprocess.merger import IntervalMerger | ||
|
|
||
| __all__ = ["IntervalMerger", "XmlFormatter"] | ||
| __all__ = ["IntervalMerger", "JsonFormatter", "XmlFormatter"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| """JSON output formatter for anomaly blocks.""" | ||
|
|
||
| import json | ||
| from collections.abc import Sequence | ||
|
|
||
| from cordon.core.types import MergedBlock | ||
|
|
||
|
|
||
| class JsonFormatter: | ||
| """Format merged blocks as a JSON document. | ||
|
|
||
| Produces a JSON object with an 'anomalies' array where each entry | ||
| contains start_line, end_line, score, and the original line content. | ||
| """ | ||
|
|
||
| def format_blocks( | ||
| self, | ||
| merged_blocks: Sequence[MergedBlock], | ||
| lines: Sequence[tuple[int, str]], | ||
| ) -> str: | ||
| """Format merged blocks into a JSON string. | ||
|
|
||
| Args: | ||
| merged_blocks: Sequence of merged blocks to format. | ||
| lines: Sequence of (line_number, line_content) tuples. | ||
|
|
||
| Returns: | ||
| JSON string with anomaly blocks. | ||
| """ | ||
| sorted_blocks = sorted(merged_blocks, key=lambda b: b.start_line) | ||
| line_map = dict(lines) | ||
|
|
||
| anomalies = [] | ||
| for block in sorted_blocks: | ||
| content_lines = [] | ||
| for line_num in range(block.start_line, block.end_line + 1): | ||
| content_lines.append(line_map.get(line_num, "")) | ||
|
|
||
| anomalies.append( | ||
| { | ||
| "start_line": block.start_line, | ||
| "end_line": block.end_line, | ||
| "score": round(block.max_score, 4), | ||
| "content": "\n".join(content_lines), | ||
| } | ||
| ) | ||
|
|
||
| return json.dumps({"anomalies": anomalies}, indent=2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| """Tests for JsonFormatter.""" | ||
|
|
||
| import json | ||
|
|
||
| from cordon.core.types import MergedBlock | ||
| from cordon.postprocess.json_formatter import JsonFormatter | ||
|
|
||
|
|
||
| class TestJsonFormatter: | ||
| """Tests for JsonFormatter output.""" | ||
|
|
||
| def test_format_single_block(self) -> None: | ||
| """Test formatting a single anomaly block.""" | ||
| lines = [(1, "line 1"), (2, "line 2"), (3, "line 3")] | ||
| blocks = [MergedBlock(start_line=1, end_line=2, original_windows=(0,), max_score=0.8)] | ||
| formatter = JsonFormatter() | ||
| output = formatter.format_blocks(blocks, lines) | ||
| data = json.loads(output) | ||
| assert len(data["anomalies"]) == 1 | ||
| assert data["anomalies"][0]["start_line"] == 1 | ||
| assert data["anomalies"][0]["end_line"] == 2 | ||
| assert data["anomalies"][0]["score"] == 0.8 | ||
| assert "line 1" in data["anomalies"][0]["content"] | ||
|
|
||
| def test_format_multiple_blocks(self) -> None: | ||
| """Test formatting multiple anomaly blocks.""" | ||
| lines = [(i, f"line {i}") for i in range(1, 11)] | ||
| blocks = [ | ||
| MergedBlock(start_line=1, end_line=2, original_windows=(0,), max_score=0.8), | ||
| MergedBlock(start_line=7, end_line=9, original_windows=(1,), max_score=0.9), | ||
| ] | ||
| formatter = JsonFormatter() | ||
| output = formatter.format_blocks(blocks, lines) | ||
| data = json.loads(output) | ||
| assert len(data["anomalies"]) == 2 | ||
| assert data["anomalies"][0]["score"] == 0.8 | ||
| assert data["anomalies"][1]["score"] == 0.9 | ||
|
|
||
| def test_format_empty_blocks(self) -> None: | ||
| """Test formatting with no blocks produces empty anomalies array.""" | ||
| lines = [(1, "line 1")] | ||
| formatter = JsonFormatter() | ||
| output = formatter.format_blocks([], lines) | ||
| data = json.loads(output) | ||
| assert data["anomalies"] == [] | ||
|
|
||
| def test_no_xml_escaping_in_json(self) -> None: | ||
| """Test that special characters are preserved without XML escaping.""" | ||
| lines = [(1, "x < y && z > 10")] | ||
| blocks = [MergedBlock(start_line=1, end_line=1, original_windows=(0,), max_score=0.5)] | ||
| formatter = JsonFormatter() | ||
| output = formatter.format_blocks(blocks, lines) | ||
| data = json.loads(output) | ||
| assert "x < y && z > 10" in data["anomalies"][0]["content"] | ||
|
|
||
| def test_output_is_valid_json(self) -> None: | ||
| """Test that output is always valid JSON.""" | ||
| lines = [(1, "test")] | ||
| blocks = [MergedBlock(start_line=1, end_line=1, original_windows=(0,), max_score=0.1234)] | ||
| formatter = JsonFormatter() | ||
| output = formatter.format_blocks(blocks, lines) | ||
| json.loads(output) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update
AnalysisResult.outputdocstring to reflect JSON support.outputis documented as XML-only, but this PR introduces JSON output too. Please make the attribute description format-agnostic.Proposed patch
📝 Committable suggestion
🤖 Prompt for AI Agents