Spaces:
Sleeping
Sleeping
| import pandas as pd | |
| from reportlab.lib.pagesizes import letter | |
| from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph, Spacer | |
| from reportlab.lib import colors | |
| from reportlab.lib.styles import getSampleStyleSheet | |
| def generate_csv_report(flagged_list: list, path: str): | |
| rows = [] | |
| for item in flagged_list: | |
| txn = item.get("transaction", {}) | |
| rows.append({ | |
| "transaction_id": txn.get("transaction_id"), | |
| "timestamp": txn.get("timestamp"), | |
| "amount": txn.get("amount"), | |
| "description": txn.get("description"), | |
| "risk_factor": item.get("risk_factor"), | |
| "risk_score": item.get("risk_score") | |
| }) | |
| df = pd.DataFrame(rows) | |
| df.to_csv(path, index=False) | |
| return path | |
| def generate_pdf_report(flagged_list: list, path: str): | |
| doc = SimpleDocTemplate(path, pagesize=letter) | |
| styles = getSampleStyleSheet() | |
| elements = [] | |
| elements.append(Paragraph("Fraud Detection Report", styles["Title"])) | |
| elements.append(Spacer(1, 12)) | |
| data = [["Transaction ID", "Timestamp", "Amount", "Risk Factor", "Risk Score"]] | |
| for item in flagged_list: | |
| txn = item.get("transaction", {}) | |
| data.append([ | |
| str(txn.get("transaction_id", "")), | |
| str(txn.get("timestamp", "")), | |
| f"{txn.get('amount', '')}", | |
| item.get("risk_factor", ""), | |
| f"{item.get('risk_score', 0):.2f}" | |
| ]) | |
| table = Table(data, repeatRows=1) | |
| table.setStyle(TableStyle([ | |
| ('BACKGROUND', (0,0), (-1,0), colors.grey), | |
| ('TEXTCOLOR',(0,0),(-1,0),colors.whitesmoke), | |
| ('GRID', (0,0), (-1,-1), 0.5, colors.black), | |
| ('ALIGN',(2,1),(2,-1),'RIGHT') | |
| ])) | |
| elements.append(table) | |
| doc.build(elements) | |
| return path | |