Skip to content

Commit

Permalink
Merge pull request #48 from CCBR/email
Browse files Browse the repository at this point in the history
feat: send the report via email and push to github
  • Loading branch information
kelly-sovacool authored Jan 18, 2024
2 parents a42197c + 4b5d01c commit 1bce3f1
Show file tree
Hide file tree
Showing 5 changed files with 5,946 additions and 335 deletions.
21 changes: 18 additions & 3 deletions bin/render_report_biowulf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,22 @@
# Usage: bash bin/render_report_biowulf.sh
module load singularity
SINGULARITY_CACHEDIR=/data/CCBR_Pipeliner/SIFS
echo "cd /mnt && Rscript bin/render.R" |\
singularity exec -C -B $PWD:/mnt,/data/CCBR_Pipeliner/userdata/spacesavers2/:/mnt/data docker://nciccbr/spacesavers2:0.1.1 bash

today=$(date +'%Y-%m-%d')
cp docs/report.html docs/report_${today}.html
year=$(date +'%Y')
mkdir -p docs/$year
html_filename="docs/${year}/report_${today}.html"
recipient_email="kelly.sovacool@nih.gov"

echo "cd /mnt && \
Rscript bin/render.R && \
cp docs/report.html $html_filename && \
python src/send_email.py \
$html_filename \
$recipient_email \
" |\
singularity exec -C -B $PWD:/mnt,/data/CCBR_Pipeliner/userdata/spacesavers2/:/mnt/data docker://nciccbr/spacesavers2:0.1.1 bash

git add docs
git commit -m 'chore: render report 🤖'
git push
5,539 changes: 5,539 additions & 0 deletions docs/2024/report_2024-01-17.html

Large diffs are not rendered by default.

665 changes: 333 additions & 332 deletions docs/report.html

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions report.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ knit: (function(inputFile, encoding) {
knitr::opts_chunk$set(message = FALSE, warning = FALSE)
```

View this report on the web: `r glue::glue("<https://ccbr.github.io/spacesavers2/{lubridate::year(lubridate::today())}/report_{lubridate::today()}.html>")`

```{r load}
library(bslib)
library(dplyr)
Expand Down
54 changes: 54 additions & 0 deletions src/send_email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env python

""" Email the html report
Usage:
python src/send_email.py <report_html> <recipient_emails>
Example:
python src/send_email.py docs/report.html kelly.sovacool@nih.gov
python src/send_email.py docs/2024/report_2024-01-17.html kelly.sovacool@nih.gov,vishal.koparde@nih.gov
"""

import datetime
from email.message import EmailMessage
import os
import smtplib
import sys


def send_email(
subject="test email from python",
plain_text=None,
html_attach = None,
sender="kelly.sovacool@nih.gov",
recipient="kelly.sovacool@nih.gov"
):
msg = EmailMessage()
msg["Subject"] = subject
msg["From"] = sender
msg["To"] = recipient

if plain_text:
msg.set_content(plain_text)
if html_attach:
with open(html_attach, 'rb') as file:
html_data = file.read()
msg.add_attachment(html_data,
filename = os.path.basename(html_attach),
maintype = 'text', subtype = 'html')

with smtplib.SMTP("localhost") as server:
server.send_message(msg)


if __name__ == "__main__":
html_filename = sys.argv[1] if len(sys.argv) > 1 else ''
recipient_addr = sys.argv[2] if len(sys.argv) > 2 else 'kelly.sovacool@nih.gov'
send_email(
subject=f"🚀 spacesavers2 report",
recipient=recipient_addr,
plain_text = f"Download the attached report or view it at https://ccbr.github.io/spacesavers2/{html_filename.strip('docs/')}\n\nThis is an automated email.",
html_attach = html_filename
)

0 comments on commit 1bce3f1

Please sign in to comment.