Skip to content

Commit

Permalink
mkdocs for influxdb and smtp
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorwalton committed Jul 14, 2023
1 parent b9fc29f commit 478a0e3
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 2 deletions.
8 changes: 6 additions & 2 deletions backend/app/routes/smtp.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ def put_credentials() -> jsonify:
Endpoint to store credentials into `smtp_credentials` table.
Returns:
jsonify: A JSON response containing if the credentials were stored successfully.
Tuple[jsonify, int]: A Tuple where the first element is a JSON response
indicating if the credentials were stored successfully and the second element
is the HTTP status code.
"""
if not request.is_json:
return jsonify({"message": "Missing JSON in request", "success": False}), 400
Expand Down Expand Up @@ -52,7 +54,9 @@ def send_report() -> jsonify:
Endpoint to send a report via email.
Returns:
jsonify: A JSON response containing if the report was sent successfully.
Tuple[jsonify, int]: A Tuple where the first element is a JSON response
indicating if the report was sent successfully and the second element
is the HTTP status code.
"""
logger.info("Received request to send a report via email")
if not request.is_json:
Expand Down
Empty file.
Empty file.
49 changes: 49 additions & 0 deletions backend/app/services/smtp/create_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,33 @@


def fetch_alert_data(service, fetch_func):
"""
Fetches alert data using the provided function.
Args:
service: An instance of the service to use for fetching data.
fetch_func (function): The function to use to fetch the data.
Returns:
Returns the result of the fetch function.
"""
alerts = fetch_func()
logger.info(alerts)
return alerts


def create_bar_chart(alerts: dict, title: str, output_filename: str) -> None:
"""
Creates a horizontal bar chart of alerts and saves it to a file.
Args:
alerts (dict): A dictionary containing alert data.
title (str): The title for the chart.
output_filename (str): The filename to save the chart to.
Returns:
None
"""
entities = [alert["hostname"] for alert in alerts["alerts_by_host"]]
num_alerts = [alert["number_of_alerts"] for alert in alerts["alerts_by_host"]]

Expand All @@ -35,6 +56,17 @@ def create_bar_chart(alerts: dict, title: str, output_filename: str) -> None:


def create_pie_chart(alerts: dict, title: str, output_filename: str) -> None:
"""
Creates a pie chart of alerts and saves it to a file.
Args:
alerts (dict): A dictionary containing alert data.
title (str): The title for the chart.
output_filename (str): The filename to save the chart to.
Returns:
None
"""
entities = [alert["rule"] for alert in alerts["alerts_by_rule"]]
num_alerts = [alert["number_of_alerts"] for alert in alerts["alerts_by_rule"]]

Expand All @@ -46,6 +78,17 @@ def create_pie_chart(alerts: dict, title: str, output_filename: str) -> None:


def create_pdf(title: str, image_filenames: List[str], pdf_filename: str) -> None:
"""
Creates a PDF containing images.
Args:
title (str): The title for the PDF.
image_filenames (List[str]): A list of image filenames to include in the PDF.
pdf_filename (str): The filename to save the PDF to.
Returns:
None
"""
c = canvas.Canvas(pdf_filename, pagesize=letter)
width, height = letter
c.setFont("Helvetica", 24)
Expand All @@ -58,6 +101,12 @@ def create_pdf(title: str, image_filenames: List[str], pdf_filename: str) -> Non


def create_alerts_report_pdf() -> None:
"""
Creates a PDF report of alerts including a bar chart and a pie chart.
Returns:
None
"""
service = AlertsService()

alerts_by_host = fetch_alert_data(service, service.collect_alerts_by_host)
Expand Down
45 changes: 45 additions & 0 deletions backend/app/services/smtp/send_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,44 @@


class EmailReportSender:
"""
Class for sending an email report with PDF attachments.
"""

def __init__(self, to_email: str):
"""
Constructor for the EmailReportSender class.
Args:
to_email (str): The email address to send the report to.
"""
self.to_email = to_email

def _get_credentials(self) -> dict:
"""
Fetches the email credentials.
Returns:
dict: A dictionary containing the email credentials. If no credentials are found,
the dictionary contains an "error" key.
"""
try:
return UniversalEmailCredentials.read_all()["emails_configured"][0]
except IndexError:
return {"error": "No email credentials found"}

def create_email_message(self, subject: str, body: str) -> MIMEMultipart:
"""
Creates an email message with the provided subject and body.
Args:
subject (str): The subject of the email.
body (str): The body of the email.
Returns:
MIMEMultipart: An email message object. If an error occurs while fetching credentials,
the return value is a dictionary containing an "error" key.
"""
msg = MIMEMultipart()
credentials = self._get_credentials()
if "error" in credentials:
Expand All @@ -32,6 +60,16 @@ def create_email_message(self, subject: str, body: str) -> MIMEMultipart:
return msg

def attach_pdfs(self, msg: MIMEMultipart, filenames: List[str]) -> MIMEMultipart:
"""
Attaches PDF files to an email message.
Args:
msg (MIMEMultipart): The email message to attach the PDFs to.
filenames (List[str]): A list of filenames of the PDFs to attach.
Returns:
MIMEMultipart: The email message with the attached PDFs.
"""
for filename in filenames:
with open(filename, "rb") as attachment_file:
part = MIMEBase("application", "octet-stream")
Expand All @@ -42,6 +80,13 @@ def attach_pdfs(self, msg: MIMEMultipart, filenames: List[str]) -> MIMEMultipart
return msg

def send_email_with_pdf(self):
"""
Sends an email with a PDF report.
Returns:
dict: A dictionary containing a "message" key describing the result of the operation
and a "success" key indicating whether the operation was successful.
"""
# Generate the PDF report
create_alerts_report_pdf()

Expand Down
22 changes: 22 additions & 0 deletions backend/docs/influxdb.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## InfluxDB Overview

### <span style="color:blue">Alerts Model</span>

::: app.models.influxdb_alerts
<br>

### <span style="color:green">Checks Routes</span>

### <span style="color:green">Alerts Routes</span>

::: app.routes.influxdb
<br>

### <span style="color:red">Checks Services</span>


::: app.services.InfluxDB.checks

### <span style="color:red">Alerts Services</span>

::: app.services.InfluxDB.alerts
26 changes: 26 additions & 0 deletions backend/docs/smtp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
## SMTP Overview

### <span style="color:blue">EmailCredentials Model</span>

::: app.models.smtp
<br>

### <span style="color:green">Checks Routes</span>

### <span style="color:green">Alerts Routes</span>

::: app.routes.smtp
<br>

### <span style="color:red">Create Report Services</span>

::: app.services.smtp.create_report

### <span style="color:red">Send Report Services</span>


::: app.services.smtp.send_report

### <span style="color:red">Universal Services</span>

::: app.services.smtp.universal
2 changes: 2 additions & 0 deletions backend/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ nav:
- Velociraptor: velociraptor.md
- Shuffle: shuffle.md
- Sublime: sublime.md
- InfluxDB: influxdb.md
- SMTP: smtp.md

markdown_extensions:
- pymdownx.highlight:
Expand Down

0 comments on commit 478a0e3

Please sign in to comment.