|
8 | 8 | from pathlib import Path |
9 | 9 | from datetime import datetime |
10 | 10 | from colorama import Fore, Style |
| 11 | +from .compute_metrics import analyze_source_code, compare_code_metrics |
| 12 | + |
| 13 | + |
| 14 | +def compute_code_metrics(final_state: dict) -> dict: |
| 15 | + """Compute metrics for original and refactored code.""" |
| 16 | + try: |
| 17 | + metrics_data = {} |
| 18 | + |
| 19 | + # Analyze original code |
| 20 | + if final_state.get('code'): |
| 21 | + original_metrics = analyze_source_code(final_state['code'], "Original.java") |
| 22 | + # Remove functions list to keep it simple |
| 23 | + original_metrics_simplified = {k: v for k, v in original_metrics.items() if k != 'functions'} |
| 24 | + metrics_data['original_metrics'] = original_metrics_simplified |
| 25 | + |
| 26 | + # Analyze refactored code |
| 27 | + if final_state.get('refactored_code'): |
| 28 | + refactored_metrics = analyze_source_code(final_state['refactored_code'], "Refactored.java") |
| 29 | + # Remove functions list to keep it simple |
| 30 | + refactored_metrics_simplified = {k: v for k, v in refactored_metrics.items() if k != 'functions'} |
| 31 | + metrics_data['refactored_metrics'] = refactored_metrics_simplified |
| 32 | + |
| 33 | + # Compare if both exist |
| 34 | + if final_state.get('code'): |
| 35 | + comparison = compare_code_metrics( |
| 36 | + original_metrics, # Pass the full metrics objects |
| 37 | + refactored_metrics # Pass the full metrics objects |
| 38 | + ) |
| 39 | + # Only include the improvements, not the full original/refactored data |
| 40 | + metrics_data['improvements'] = comparison['improvements'] |
| 41 | + |
| 42 | + return metrics_data |
| 43 | + except Exception as e: |
| 44 | + print(Fore.YELLOW + f"Warning: Could not compute metrics: {e}" + Style.RESET_ALL) |
| 45 | + return {} |
11 | 46 |
|
12 | 47 |
|
13 | 48 | def save_intermediate_results(file_path: str, final_state: dict, settings, results_dir: str = "../processing_results") -> bool: |
14 | 49 | """Save intermediate results from the agentic workflow for analysis in markdown format.""" |
15 | 50 | try: |
| 51 | + # Compute code metrics |
| 52 | + metrics_data = compute_code_metrics(final_state) |
| 53 | + |
16 | 54 | if file_path != 'java_code_snippet' and not None: |
17 | 55 | # Create results directory if it doesn't exist |
18 | 56 | results_path = Path(results_dir) |
@@ -170,25 +208,79 @@ def save_intermediate_results(file_path: str, final_state: dict, settings, resul |
170 | 208 |
|
171 | 209 | markdown_content += '</div>\n\n' |
172 | 210 |
|
173 | | - # Add code comparison summary |
174 | | - if original_code and refactored_code: |
175 | | - original_lines = len(original_code.splitlines()) |
176 | | - refactored_lines = len(refactored_code.splitlines()) |
177 | | - line_change = refactored_lines - original_lines |
178 | | - line_change_str = f"+{line_change}" if line_change > 0 else str(line_change) |
179 | | - |
180 | | - markdown_content += f"**Code Metrics:**\n" |
181 | | - markdown_content += f"- Original Loc: {original_lines}\n" |
182 | | - markdown_content += f"- Refactored Loc: {refactored_lines}\n" |
183 | | - markdown_content += f"- LoC change: {line_change_str}\n\n" |
184 | 211 | else: |
185 | 212 | markdown_content += "No code available for comparison.\n\n" |
186 | 213 |
|
| 214 | + # Add code metrics section |
| 215 | + if metrics_data: |
| 216 | + markdown_content += "---\n\n## Code Metrics\n\n" |
| 217 | + |
| 218 | + # Display Original and Refactored metrics side by side |
| 219 | + if 'original_metrics' in metrics_data and 'refactored_metrics' in metrics_data: |
| 220 | + orig = metrics_data['original_metrics'] |
| 221 | + refac = metrics_data['refactored_metrics'] |
| 222 | + |
| 223 | + markdown_content += "### Original vs Refactored Code \n\n" |
| 224 | + markdown_content += "| Metric | Original | Refactored |\n" |
| 225 | + markdown_content += "|--------|----------|------------|\n" |
| 226 | + markdown_content += f"| **Source Lines of Code (SLOC)** | {orig.get('file_sloc_nloc', 'N/A')} | {refac.get('file_sloc_nloc', 'N/A')} |\n" |
| 227 | + markdown_content += f"| **Total Functions** | {orig.get('total_functions', 'N/A')} | {refac.get('total_functions', 'N/A')} |\n" |
| 228 | + markdown_content += f"| **Average Cyclomatic Complexity** | {orig.get('avg_cc', 'N/A')} | {refac.get('avg_cc', 'N/A')} |\n" |
| 229 | + markdown_content += f"| **Max Cyclomatic Complexity** | {orig.get('max_cc', 'N/A')} | {refac.get('max_cc', 'N/A')} |\n" |
| 230 | + markdown_content += f"| **Max Nesting Depth** | {orig.get('max_nd_in_file', 'N/A')} | {refac.get('max_nd_in_file', 'N/A')} |\n\n" |
| 231 | + |
| 232 | + elif 'original_metrics' in metrics_data: |
| 233 | + orig = metrics_data['original_metrics'] |
| 234 | + markdown_content += "### Original Code Metrics\n\n" |
| 235 | + markdown_content += f"- **Source Lines of Code (SLOC):** {orig.get('file_sloc_nloc', 'N/A')}\n" |
| 236 | + markdown_content += f"- **Total Functions:** {orig.get('total_functions', 'N/A')}\n" |
| 237 | + markdown_content += f"- **Average Cyclomatic Complexity:** {orig.get('avg_cc', 'N/A')}\n" |
| 238 | + markdown_content += f"- **Max Cyclomatic Complexity:** {orig.get('max_cc', 'N/A')}\n" |
| 239 | + markdown_content += f"- **Max Nesting Depth:** {orig.get('max_nd_in_file', 'N/A')}\n\n" |
| 240 | + |
| 241 | + # Display Improvements |
| 242 | + if 'improvements' in metrics_data: |
| 243 | + imp = metrics_data['improvements'] |
| 244 | + markdown_content += "### Comparison\n\n" |
| 245 | + |
| 246 | + sloc_change = imp.get('sloc_reduction', 0) |
| 247 | + func_change = imp.get('function_count_change', 0) |
| 248 | + avg_cc_imp = imp.get('avg_cc_improvement', 0) |
| 249 | + max_cc_red = imp.get('max_cc_reduction', 0) |
| 250 | + nest_red = imp.get('max_nesting_reduction', 0) |
| 251 | + |
| 252 | + markdown_content += f"- **Lines of Code Change:** {sloc_change:+d} lines\n" |
| 253 | + markdown_content += f"- **Function Count Change:** {func_change:+d} functions\n" |
| 254 | + markdown_content += f"- **Average Complexity Improvement:** {avg_cc_imp:+.2f}\n" |
| 255 | + markdown_content += f"- **Max Complexity Reduction:** {max_cc_red:+d}\n" |
| 256 | + markdown_content += f"- **Max Nesting Reduction:** {nest_red:+d}\n\n" |
| 257 | + |
187 | 258 | markdown_content += f"---\n\n*Generated by AntiPattern Remediator Tool using {settings.LLM_MODEL}*\n" |
188 | 259 | # Save to markdown file |
189 | 260 | with open(results_file_path, 'w', encoding='utf-8') as f: |
190 | 261 | f.write(markdown_content) |
191 | 262 |
|
| 263 | + # Save metrics to JSON file if available |
| 264 | + if metrics_data: |
| 265 | + if file_path != 'java_code_snippet': |
| 266 | + json_filename = f"{safe_filename}_metrics.json" |
| 267 | + json_file_path = results_path / json_filename |
| 268 | + else: |
| 269 | + json_filename = "java_code_snippet_metrics.json" |
| 270 | + json_file_path = json_filename |
| 271 | + |
| 272 | + # Include file path and timestamp in metrics JSON |
| 273 | + metrics_with_metadata = { |
| 274 | + "file_path": file_path, |
| 275 | + "timestamp": timestamp, |
| 276 | + "metrics": metrics_data |
| 277 | + } |
| 278 | + |
| 279 | + with open(json_file_path, 'w', encoding='utf-8') as f: |
| 280 | + json.dump(metrics_with_metadata, f, indent=2) |
| 281 | + |
| 282 | + print(Fore.CYAN + f"Code metrics saved: {json_file_path}" + Style.RESET_ALL) |
| 283 | + |
192 | 284 | print(Fore.CYAN + f"Intermediate results saved: {results_file_path}" + Style.RESET_ALL) |
193 | 285 | return True |
194 | 286 |
|
|
0 commit comments