File size: 1,807 Bytes
3595d5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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