CodebaseAi commited on
Commit
825640d
·
1 Parent(s): 59edcea

Detection4

Browse files
Files changed (1) hide show
  1. routes/reports_route.py +32 -43
routes/reports_route.py CHANGED
@@ -38,7 +38,7 @@ def reports_overview():
38
  # --------------------------------------------------------
39
  @reports_bp.route("/trend", methods=["GET"])
40
  def attack_trend():
41
- trend = [{"date": d["date"], "attacks": sum(v for k, v in d.items() if k != "date")} for d in ATTACK_DATA]
42
  return jsonify(trend)
43
 
44
  # --------------------------------------------------------
@@ -80,59 +80,48 @@ def generate_report_pdf():
80
  filename = f"NIDS_Report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.pdf"
81
  return send_file(buffer, as_attachment=True, download_name=filename, mimetype="application/pdf")
82
 
83
- # --------------------------------------------------------
84
  @reports_bp.route("/email", methods=["POST"])
85
  def send_report_email():
86
- data = request.get_json()
87
- recipient = data.get("email")
88
- if not recipient:
89
- return jsonify({"error": "No recipient email provided"}), 400
90
-
91
- # Generate detailed PDF
92
- pdf_buffer = BytesIO()
93
- pdf = FPDF()
94
- pdf.add_page()
95
- pdf.set_font("Helvetica", "B", 16)
96
- pdf.cell(0, 10, "Adaptive AI NIDS - Full System Report", ln=True, align="C")
97
- pdf.ln(8)
98
- pdf.set_font("Helvetica", "", 12)
99
- pdf.cell(0, 10, "Summary of recent network activity:", ln=True)
100
- pdf.ln(5)
101
-
102
- pdf.set_font("Helvetica", "B", 12)
103
- pdf.cell(0, 8, "Attack Distribution:", ln=True)
104
- total = {cls: sum(day.get(cls, 0) for day in ATTACK_DATA) for cls in CLASSES}
105
- pdf.set_font("Helvetica", "", 11)
106
- for cls, val in total.items():
107
- pdf.cell(0, 8, f" - {cls}: {val} attacks", ln=True)
108
-
109
- pdf.ln(6)
110
- pdf.set_font("Helvetica", "B", 12)
111
- pdf.cell(0, 8, "Recent Trend (last 7 days):", ln=True)
112
- pdf.set_font("Helvetica", "", 11)
113
- for d in ATTACK_DATA[-7:]:
114
- pdf.cell(0, 8, f"{d['date']}: {sum(v for k, v in d.items() if k != 'date')} total", ln=True)
115
-
116
- pdf.ln(10)
117
- pdf.set_font("Helvetica", "I", 10)
118
- pdf.multi_cell(0, 8, "This automated report is generated by Adaptive AI NIDS. "
119
- "It summarizes live detections, system diagnostics, and "
120
- "AI-identified attack classes.")
121
-
122
- pdf.output(pdf_buffer)
123
- pdf_buffer.seek(0)
124
-
125
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
  msg = Message(
127
  subject="Adaptive AI NIDS - Full Report",
128
  recipients=[recipient],
129
  body="Attached is your Adaptive AI NIDS summary report with recent attack data.",
 
 
130
  )
131
- msg.attach("Adaptive_NIDS_Report.pdf", "application/pdf", pdf_buffer.read())
 
 
 
 
 
 
132
  mail.send(msg)
133
  return jsonify({"success": True, "message": f"Email sent to {recipient}"})
 
134
  except Exception as e:
135
- return jsonify({"error": str(e)}), 500
 
 
136
 
137
 
138
 
 
38
  # --------------------------------------------------------
39
  @reports_bp.route("/trend", methods=["GET"])
40
  def attack_trend():
41
+ trend = [{"date": d["date"], "value": sum(v for k, v in d.items() if k != "date")} for d in ATTACK_DATA]
42
  return jsonify(trend)
43
 
44
  # --------------------------------------------------------
 
80
  filename = f"NIDS_Report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.pdf"
81
  return send_file(buffer, as_attachment=True, download_name=filename, mimetype="application/pdf")
82
 
 
83
  @reports_bp.route("/email", methods=["POST"])
84
  def send_report_email():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  try:
86
+ data = request.get_json()
87
+ recipient = data.get("email")
88
+ if not recipient:
89
+ return jsonify({"error": "No recipient email provided"}), 400
90
+
91
+ # 1. Generate PDF
92
+ pdf = FPDF()
93
+ pdf.add_page()
94
+ pdf.set_font("Helvetica", "B", 16)
95
+ pdf.cell(0, 10, "Adaptive AI NIDS - Full System Report", ln=True, align="C")
96
+
97
+ # ... (keep your PDF styling code here) ...
98
+
99
+ # 2. Capture PDF content correctly
100
+ # Using getvalue() is safer than reading from a seeked buffer
101
+ pdf_content = pdf.output(dest='S').encode('latin-1')
102
+
103
+ # 3. Construct and Send Email
104
  msg = Message(
105
  subject="Adaptive AI NIDS - Full Report",
106
  recipients=[recipient],
107
  body="Attached is your Adaptive AI NIDS summary report with recent attack data.",
108
+ # Explicitly set sender if not in global config
109
+ sender=getattr(mail, 'username', None)
110
  )
111
+
112
+ msg.attach(
113
+ "Adaptive_NIDS_Report.pdf",
114
+ "application/pdf",
115
+ pdf_content
116
+ )
117
+
118
  mail.send(msg)
119
  return jsonify({"success": True, "message": f"Email sent to {recipient}"})
120
+
121
  except Exception as e:
122
+ # Print the EXACT error to your terminal for debugging
123
+ print(f"❌ MAIL_ERROR: {str(e)}")
124
+ return jsonify({"error": f"Server Error: {str(e)}"}), 500
125
 
126
 
127