Skip to content

Commit

Permalink
Production v1.4.0 [Email System Update]
Browse files Browse the repository at this point in the history
  • Loading branch information
devonleadman committed Oct 19, 2023
1 parent 2f689b1 commit f912c79
Show file tree
Hide file tree
Showing 17 changed files with 236 additions and 242 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ migrated/*.dump
approved/*.dump
grant/raw/*
grant/processed/*
crt/*.json
removethis.py
2 changes: 1 addition & 1 deletion AlertCypher.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(self, db):
user = os.environ['NEO4J_USERNAME']
password = os.environ['NEO4J_PASSWORD']

watch("neo4j")
#watch("neo4j")

connection = GraphDatabase.driver(uri=server_uri, auth=(user, password)) #encrypted=True, trusted_certificates=TrustAll()
self.session = connection.session(database=db)
Expand Down
1 change: 1 addition & 0 deletions crt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Location where the firebase access key will be stored
114 changes: 44 additions & 70 deletions email/alert.py
Original file line number Diff line number Diff line change
@@ -1,83 +1,57 @@
#import django
#from django.core.mail import send_mail
#from django.template.loader import render_to_string
import os
import boto3
from botocore.exceptions import ClientError
#os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
#django.setup()
'''
def clinical_msg (data): # dict
msg_plain = render_to_string('templates/clinical_notif.txt', data)
msg_html = render_to_string('templates/clinical_notif.html', data)

send_mail(
'Test Email',
msg_plain,
'from-email <[email protected]>',
['RECIPIENT'],
html_message=msg_html
)
def pubmed_msg (data): # dict
msg_plain = render_to_string('templates/pubmed_notif.txt', data)
msg_html = render_to_string('templates/pubmed_notif.html', data)
sender_email = '[email protected]'

send_mail(
'Test Email',
msg_plain,
'from-email <[email protected]>',
['RECIPIENT'],
html_message=msg_html
def setup_email_client():
client = boto3.client(
service_name='ses',
region_name='us-east-1'
)
return client

def grant_msg (data): # dict
msg_plain = render_to_string('templates/grant_notif.txt', data)
msg_html = render_to_string('templates/grant_notif.html', data)
send_mail(
'Test Email',
msg_plain,
'from-email <[email protected]>',
['RECIPIENT'],
html_message=msg_html
)
def send_email(sub,msg,recip,html=None,client=setup_email_client()):
if not client:
return

def test_msg (sub, msg, recip):
send_mail(
f'{sub}',
f'{msg}',
'[email protected]',
[f'{recip}']
response = client.send_email(
Source=sender_email,
Destination={
'ToAddresses': [
f'{recip}',
],
},
Message={
'Subject': {
'Data': f'{sub}',
},
'Body': {
'Text': {
'Data': f'{msg}',
},
'Html': {
'Data': f'{html}'
},
}
}
)
'''

def get_secret():

secret_name = "/smtp/credentials"
region_name = "us-east-1"
def send_raw_email(sub,msg,recip,client=None):
if not client:
return

# Create a Secrets Manager client
session = boto3.session.Session()
client = session.client(
service_name='secretsmanager',
region_name=region_name
service_name='ses',
region_name='us-east-1'
)

response = client.send_raw_email(
Source='[email protected]',
Destinations=[
f'{recip}',
],
RawMessage={
'Data': f'To:{recip}\nFrom:[email protected]\nSubject:{sub}\nMIME-Version: 1.0\nContent-type: Multipart/Mixed; boundary="NextPart"\n\n--NextPart\nContent-Type: text/plain\n\n{msg}\n\n'
},
)

try:
get_secret_value_response = client.get_secret_value(
SecretId=secret_name
)
except ClientError as e:
# For a list of exceptions thrown, see
# https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetSecretValue.html
raise e

# Decrypts secret using the associated KMS key.
secret = get_secret_value_response['SecretString']
print(secret)

# Your code goes here.

get_secret()
#test_msg('test','test','[email protected]')
36 changes: 0 additions & 36 deletions email/send_single_alert_at_date.py

This file was deleted.

122 changes: 91 additions & 31 deletions email/ses_firebase.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,107 @@
import glob
import os
import sys
workspace = os.path.dirname(os.path.abspath(__file__))
sys.path.append(workspace)
sys.path.append(os.getcwd())
import sysvars
from AlertCypher import AlertCypher
from datetime import date
import firebase_admin
from firebase_admin import auth
from firebase_admin import credentials
from firebase_admin import firestore
import alert
def fill_template(type,data):
tabs = {'clinical':'trials', 'grant':'project', 'pubmed':'nonepi-articles'}
txt_db = {'clinical': 'clinical trial', 'grant': 'funded project', 'pubmed': 'publication'}
full_msg = ''
html = ''

full_msg += 'You have {num} new entries for your subscribed rare diseases in the {db_title} database\nOut of that {num},\n\n'.format(num=data['total'], db_title=txt_db[type])

html += """
<html>
<head></head>
<body style='background-color:purple;'>
<div>
<img src="https://rdas.ncats.nih.gov/assets/rdas_final_gradient.png" alt="Rare Disease Alert System" style="display:block;margin:auto;">
</div>
<div style='text-align:center;background-color:white;padding:10px 100px 100px 100px;margin:10px 100px 100px 100px'>
<h1>{name}</h1>
<h1>On {date}, You have {num} new entries for your subscribed rare diseases in the {db_title} database</h1>
<h2>Out of that {num},</h2>
<br>
<p>
""".format(num=data['total'],images_path=sysvars.images_path,db_title=txt_db[type],name=data['name'],date=data['update_date'])

for gard in data['subscriptions']:
if data[gard]['num'] > 0:
full_msg += '{name} [{ID}] - {num} new additions have been added to the database\n'.format(name=data[gard]['name'], num=data[gard]['num'], ID=gard)
html += """
<a href='https://rdas.ncats.nih.gov/disease?id={ID}#{tab}'>{name} [{ID}]</a> - {num} new additions have been added to the database
<br>
""".format(name=data[gard]['name'], num=data[gard]['num'], ID=gard, tab=tabs[type])

html += """
<p>
</div>
<body>
<html>
"""
return (full_msg,html)

def send_mail(type, data):
if data['total'] > 0:

print(data['total'],data['email'])
if data['total'] > 0 and data['email'] == '[email protected]':

data['email'] = '[email protected]' # TEST EMAIL

if type == "clinical":
print('To: {toemail}\nYou have {num} new entries for your subscribed rare diseases in the clinical trial database\nOut of that {num},\n'.format(toemail=data['email'],num=data['total']))
for gard in data['subscriptions']:
if data[gard]['num'] > 0:
print('{name} [{ID}] - {num} new additions have been added to the database'.format(name=data[gard]['name'], num=data[gard]['num'], ID=gard))

#ses.clinical_msg() # pass in data for email as dict
elif type == "pubmed":
print('To: {toemail}\nYou have {num} new entries for your subscribed rare diseases'.format(toemail=data['email'],num=data['update_num']))
#ses.pubmed_msg()
elif type == "grant":
print('To: {toemail}\nYou have {num} new entries for your subscribed rare diseases'.format(toemail=data['email'],num=data['update_num']))
#ses.grant_msg()


def get_stats(db, type, gards):
txt,html = fill_template(type,data)
alert.send_email('RDAS-Alert: Clinical Trial update regarding your subscriptions', txt, data['email'], html=html) #data['email'] in place of email
print('Email Sent...')

if type == "pubmed":
txt,html = fill_template(type,data)
alert.send_email('RDAS-Alert: Publication update regarding your subscriptions', txt, data['email'], html=html)
print('Email Sent...')

if type == "grant":
txt,html = fill_template(type,data)
alert.send_email('RDAS-Alert: Funded Project update regarding your subscriptions', txt, data['email'], html=html)
print('Email Sent...')

def get_stats(type, gards, date=None):
db = AlertCypher(type)
return_data = dict()
now = date.today()
now = now.strftime("%m/%d/%y")
#now = "12/27/22"
convert = {'clinical':['ClinicalTrial','GARD','GARDId'], 'pubmed':['Article','Disease','gard_id'], 'grant':['Project','Disease','gard_id']}

response = db.run('MATCH (x:{node})--(y:{gardnode}) WHERE x.DateCreated = \"{now}\" AND y.{property} IN {list} RETURN COUNT(x)'
.format(node=convert[type][0], gardnode=convert[type][1], property=convert[type][2], list=list(gards.keys()), now=now))
if date:
now = date
else:
now = date.today()
now = now.strftime("%m/%d/%y")
print(f'Searching for nodes created on {now}')

convert = {'clinical':['ClinicalTrial','GARD','GardId'], 'pubmed':['Article','GARD','GardId'], 'grant':['Project','GARD','GardId']}
connect_to_gard = {'clinical':'--(:Condition)--(:Annotation)--','pubmed':'--','grant':'--'}

response = db.run('MATCH (x:{node}){connection}(y:{gardnode}) WHERE x.DateCreatedRDAS = \"{now}\" AND y.{property} IN {list} RETURN COUNT(x)'
.format(node=convert[type][0], gardnode=convert[type][1], property=convert[type][2], list=list(gards.keys()), now=now, connection=connect_to_gard[type]))
return_data['total'] = response.data()[0]['COUNT(x)']

for gard in gards.keys():
response = db.run('MATCH (x:{node})--(y:{gardnode}) WHERE x.DateCreated = \"{now}\" AND y.{property} = \"{gard}\" RETURN COUNT(x)'
.format(node=convert[type][0], gardnode=convert[type][1], property=convert[type][2], gard=gard, now=now))
response = db.run('MATCH (x:{node}){connection}(y:{gardnode}) WHERE x.DateCreatedRDAS = \"{now}\" AND y.{property} = \"{gard}\" RETURN COUNT(x)'
.format(node=convert[type][0], gardnode=convert[type][1], property=convert[type][2], gard=gard, now=now, connection=connect_to_gard[type]))
return_data[gard] = {'name':gards[gard],'num':response.data()[0]['COUNT(x)']}

return_data['update_date'] = now
return return_data

def trigger_email(db, type):
def trigger_email(type,date=None):
convert = {'clinical':'trials', 'pubmed':'articles', 'grant':'grants'}
user_data = dict()
cred = credentials.Certificate('new\\firestore\\key.json')
cred = credentials.Certificate(sysvars.firebase_key_path)
firebase_admin.initialize_app(cred)
firestore_db = firestore.client()

Expand All @@ -55,23 +113,25 @@ def trigger_email(db, type):
else:
print(u'Document Doesnt Exist')


for firestore_user, data in user_data.items():
subscript_gard = dict()
for subscript in data['subscriptions']:
try:
if convert[type] in subscript['alerts']:
subscript_gard[subscript['gardID']] = subscript['diseaseName']
except KeyError:
print('')
pass

users = auth.list_users()
if users:
users = users.iterate_all()
for user in users:
uid = user.uid
if uid == firestore_user:
update_data = get_stats(db, type, subscript_gard)
if uid == firestore_user and len(subscript_gard) > 0:
update_data = get_stats(type, subscript_gard, date)
update_data['email'] = user.email
update_data['name'] = user_data[uid]['displayName']
update_data['subscriptions'] = list(subscript_gard.keys())
send_mail(type, update_data)

trigger_email(sysvars.gnt_db, '04/27/23')
7 changes: 0 additions & 7 deletions email/settings.py

This file was deleted.

22 changes: 0 additions & 22 deletions email/templates/clinical_notif.html

This file was deleted.

3 changes: 0 additions & 3 deletions email/templates/clinical_notif.txt

This file was deleted.

Loading

0 comments on commit f912c79

Please sign in to comment.