diff --git a/email/ses_firebase.py b/email/ses_firebase.py deleted file mode 100644 index 44445c4..0000000 --- a/email/ses_firebase.py +++ /dev/null @@ -1,164 +0,0 @@ -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,datetime -import pandas as pd -from dateutil.relativedelta import relativedelta -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 += """ - - - - -
-

Rare Disease Alert System

-
-
-

{name}

-

Within the last week, {num} new entries for your subscribed rare diseases have been added to the {db_title} database

- - - - - - - """.format(num=data['total'],images_path=sysvars.images_path,db_title=txt_db[type],name=data['name']) - - for gard in data['subscriptions']: - if data[gard]['num'] > 0: - full_msg += '{name} [{gardId}] - {num} new additions have been added to the database\n'.format(name=data[gard]['name'], num=data[gard]['num'], gardId=gard) - html += """ - - - - - - """.format(name=data[gard]['name'], num=data[gard]['num'], gardId=gard, tab=tabs[type]) - - html += """ -
NameGARD IDNodes Modified
{name}{gardId}{num}
-

Results gathered within the time period of {date_start}-{date_end}

-
-
-
- Rare Disease Alert System - - - """.format(date_start=data['update_date_start'],date_end=data['update_date_end']) - - print(html) - return (full_msg,html) - -def send_mail(type, data): - print(f"[{data['total']}, {data['email']}]") - if data['total'] > 0 and data['email'] == 'timothy.sheils@ncats.nih.gov' or data['email'] == 'zhuqianzq@gmail.com': #TEST - - data['email'] = 'devon.leadman@nih.gov' # TEST EMAIL - - if type == "clinical": - 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_start=datetime.today().strftime('%m/%d/%y'), date_end=datetime.today().strftime('%m/%d/%y')): - db = AlertCypher(type) - return_data = dict() - - date_start_string = date_start - date_end_string = date_end - date_start_obj = datetime.strptime(date_start, '%m/%d/%y') - date_end_obj = datetime.strptime(date_end, '%m/%d/%y') - - date_list = pd.date_range(date_start_obj, date_end_obj, freq='D').strftime('%m/%d/%y').to_list() - - print(f'Searching for nodes created between {date_start_string} and {date_end_string}') - - convert = {'clinical':['ClinicalTrial','GARD','GardId'], 'pubmed':['Article','GARD','GardId'], 'grant':['Project','GARD','GardId']} - connect_to_gard = {'clinical':'--(:Condition)--(:Annotation)--','pubmed':'--','grant':'--'} - - query = 'MATCH (x:{node}){connection}(y:{gardnode}) WHERE x.DateCreatedRDAS IN {date_list} 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()), date_list=date_list, connection=connect_to_gard[type]) - - response = db.run(query) - return_data['total'] = response.data()[0]['COUNT(x)'] - - for gard in gards.keys(): - response = db.run('MATCH (x:{node}){connection}(y:{gardnode}) WHERE x.DateCreatedRDAS IN {date_list} AND y.{property} = \"{gard}\" RETURN COUNT(x)'.format(node=convert[type][0], gardnode=convert[type][1], property=convert[type][2], gard=gard, date_list=date_list, connection=connect_to_gard[type])) - return_data[gard] = {'name':gards[gard],'num':response.data()[0]['COUNT(x)']} - - return_data['update_date_end'] = date_end_string - return_data['update_date_start'] = date_start_string - - return return_data - -def trigger_email(type,date_start=None,date_end=None): - convert = {'clinical':'trials', 'pubmed':'articles', 'grant':'grants'} - user_data = dict() - cred = credentials.Certificate(sysvars.firebase_key_path) - firebase_admin.initialize_app(cred) - firestore_db = firestore.client() - - firestore_docs = firestore_db.collection(u'users').stream() - - for doc in firestore_docs: - if doc.exists: - user_data[doc.id] = doc.to_dict() - else: - print('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 and len(subscript_gard) > 0: - if not date_start and not date_end: - update_data = get_stats(type, subscript_gard) - elif date_start and date_end: - update_data = get_stats(type, subscript_gard, date_start=date_start, date_end=date_end) - elif date_start: - update_data = get_stats(type, subscript_gard, date_start=date_start) - elif date_end: - update_data = get_stats(type, subscript_gard, date_end=date_end) - - 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.pm_db, date_start='12/07/22') #TEST diff --git a/emails/alert.py b/emails/alert.py index 3b4f509..865dafa 100644 --- a/emails/alert.py +++ b/emails/alert.py @@ -1,4 +1,7 @@ import boto3 +import sys +sys.path.append('/home/aom2/RDAS') +sys.path.append('/home/aom2/RDAS/emails') from botocore.exceptions import ClientError sender_email = 'ncatsrdas@mail.nih.gov' @@ -10,31 +13,54 @@ def setup_email_client(): ) return client -def send_email(sub,msg,recip,html=None,client=setup_email_client()): - if not client: - return - - response = client.send_email( - Source=sender_email, - Destination={ - 'ToAddresses': [ - f'{recip}', - ], - }, - Message={ +def send_email(subject, html, recipient, client=setup_email_client()): + print("Sending emails to:", recipient) + # sender_email = client # Replace with your email + # Set up the email + message={ 'Subject': { - 'Data': f'{sub}', + 'Data': f'{subject}', }, 'Body': { - 'Text': { - 'Data': f'{msg}', - }, + 'Html': { 'Data': f'{html}' }, } } + + + # Send the email + response = client.send_email( + Source=sender_email, + Destination={'ToAddresses': [recipient]}, + Message=message ) + print("Email sent successfully.") + + +# def send_email(sub,html,recip,client=setup_email_client()): +# if not client: +# return + +# response = client.send_email( +# Source=sender_email, +# Destination={ +# 'ToAddresses': [ +# f'{recip}', +# ], +# }, +# Message={ +# 'Subject': { +# 'Data': f'{sub}', +# }, +# 'Body': { +# 'Html': { +# 'Data': f'{html}' +# }, +# } +# } +# ) def send_raw_email(sub,msg,recip,client=None): if not client: diff --git a/emails/email_test.py b/emails/email_test.py deleted file mode 100644 index bc1ae73..0000000 --- a/emails/email_test.py +++ /dev/null @@ -1,50 +0,0 @@ - -import sys - -sys.path.append('/home/aom2/RDAS') -sys.path.append('/home/aom2/RDAS/emails') -import sysvars -import smtplib -from email.mime.text import MIMEText -from email.mime.multipart import MIMEMultipart -from jinja2 import Environment, FileSystemLoader -import os - - -def send_email(subject, html, recipient): - print("sending emails to::", recipient) - sender = "" # Replace with your email - password = "" # Replace with your email password - - # Set up the email - msg = MIMEMultipart('alternative') - msg['From'] = "" - # msg['To'] = "" - msg['Subject'] = subject - - # Attach both plain text and HTML parts - # part1 = MIMEText(text, 'plain') - part2 = MIMEText(html, 'html') - # msg.attach(part1) - msg.attach(part2) - - # Send the email - server = smtplib.SMTP('', 587) # Replace with SMTP server and port - server.starttls() - server.login(sender, password) - text = msg.as_string() - server.sendmail(sender, recipient, msg.as_string()) - server.quit() - - -# def render_template(filename, data={}): -# # template_dir = "path/to/templates" -# template_dir = os.getcwd() -# env = Environment(loader=FileSystemLoader(template_dir)) -# template_path = filename # Relative to the template_dir -# template = env.get_template(template_path) -# return template.render(data=data) - -# the fill_template method was remove, becaus the generation of message was integrated into the html template. - - diff --git a/emails/ses_firebase.py b/emails/ses_firebase.py index f699a4d..a1e396c 100644 --- a/emails/ses_firebase.py +++ b/emails/ses_firebase.py @@ -1,12 +1,16 @@ import glob import os import sys +import json workspace = os.path.dirname(os.path.abspath(__file__)) sys.path.append(workspace) -sys.path.append(os.getcwd()) +# sys.path.append(os.getcwd()) +sys.path.append('/home/aom2/RDAS') +sys.path.append('/home/aom2/RDAS/emails') import sysvars from AlertCypher import AlertCypher from datetime import date,datetime +from jinja2 import Environment, FileSystemLoader import pandas as pd from dateutil.relativedelta import relativedelta import firebase_admin @@ -14,88 +18,37 @@ 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 += """ - - - - -
-

Rare Disease Alert System

-
-
-

{name}

-

Within the last week, {num} new entries for your subscribed rare diseases have been added to the {db_title} database

- - - - - - - """.format(num=data['total'],images_path=sysvars.images_path,db_title=txt_db[type],name=data['name']) - - for gard in data['subscriptions']: - if data[gard]['num'] > 0: - full_msg += '{name} [{gardId}] - {num} new additions have been added to the database\n'.format(name=data[gard]['name'], num=data[gard]['num'], gardId=gard) - html += """ - - - - - - """.format(name=data[gard]['name'], num=data[gard]['num'], gardId=gard, tab=tabs[type]) - - html += """ -
NameGARD IDNodes Modified
{name}{gardId}{num}
-

Results gathered within the time period of {date_start}-{date_end}

-
-
-
- Rare Disease Alert System - - - """.format(date_start=data['update_date_start'],date_end=data['update_date_end']) - - print(html) - return (full_msg,html) +import email_test -def send_mail(type, data): - - print('type::',type) - print('data::',data) +prefix = sysvars.db_prefix # you can set the db_prefix in sysvars.py - # print(f"[{data['total']}, {data['email']}]") - # if data['total'] > 0 and data['email'] == 'timothy.sheils@ncats.nih.gov' or data['email'] == 'zhuqianzq@gmail.com': - # # data['email'] = 'devon.leadman@nih.gov' # TEST EMAIL - # data['email'] = 'minghui.ao@nih.gov' # TEST EMAIL - # if type == "clinical": - # 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...]') +def render_template(filename, data={}): + env = Environment(loader=FileSystemLoader(f'{sysvars.base_path}emails/')) + template = env.get_template(filename) + rendered_content = template.render(data=data) + return rendered_content - # 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 send_mail(type, data): + # Define the tabs dictionary and txt_db + tabs = {prefix +'clinical': 'trials', prefix +'grant': 'project', prefix +'pubmed': 'nonepi-articles'} + txt_db = {prefix +'clinical': 'clinical trial', prefix +'grant': 'funded project', prefix +'pubmed': 'publication'} + + # Add tabs and type to the data dictionary + data['tabs'] = tabs + data["db_title"]=str(txt_db[type]) + + if data['total'] > 0 and data['email'] == '' or data['email'] == '':# for testing + + data['email'] = '' # TEST EMAIL + html_content = render_template('email_template1.html', data=data) + alert.send_email(f'RDAS-Alert: {str(txt_db[type])} update regarding your subscriptions', html_content, data['email'])# change to your alert.py sending email method.you may need to adjust your method abit to read in these parameters. + print("finish sending enail") def get_stats(type, gards, date_start=datetime.today().strftime('%m/%d/%y'), date_end=datetime.today().strftime('%m/%d/%y')): db = AlertCypher(type) return_data = dict() - date_start_string = date_start date_end_string = date_end date_start_obj = datetime.strptime(date_start, '%m/%d/%y') @@ -103,67 +56,78 @@ def get_stats(type, gards, date_start=datetime.today().strftime('%m/%d/%y'), dat date_list = pd.date_range(date_start_obj, date_end_obj, freq='D').strftime('%m/%d/%y').to_list() - print(f'Searching for nodes created between {date_start_string} and {date_end_string}') - - convert = {'clinical':['ClinicalTrial','GARD','GardId'], 'pubmed':['Article','GARD','GardId'], 'grant':['Project','GARD','GardId']} - connect_to_gard = {'clinical':'--(:Condition)--(:Annotation)--','pubmed':'--','grant':'--'} + convert = {prefix+'clinical':['ClinicalTrial','GARD','GardId'], prefix+'pubmed':['Article','GARD','GardId'], prefix+'grant':['Project','GARD','GardId']} + connect_to_gard = {prefix+'clinical':'--(:Condition)--(:Annotation)--',prefix+'pubmed':'--',prefix+'grant':'--'} query = 'MATCH (x:{node}){connection}(y:{gardnode}) WHERE x.DateCreatedRDAS IN {date_list} 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()), date_list=date_list, connection=connect_to_gard[type]) response = db.run(query) - return_data['total'] = response.data()[0]['COUNT(x)'] - + result = response.single() + return_data['total'] = result['COUNT(x)'] + for gard in gards.keys(): - response = db.run('MATCH (x:{node}){connection}(y:{gardnode}) WHERE x.DateCreatedRDAS IN {date_list} AND y.{property} = \"{gard}\" RETURN COUNT(x)'.format(node=convert[type][0], gardnode=convert[type][1], property=convert[type][2], gard=gard, date_list=date_list, connection=connect_to_gard[type])) - return_data[gard] = {'name':gards[gard],'num':response.data()[0]['COUNT(x)']} + query_1='MATCH (x:{node}){connection}(y:{gardnode}) WHERE x.DateCreatedRDAS IN {date_list} AND y.{property} = \"{gard}\" RETURN COUNT(x)'.format(node=convert[type][0], gardnode=convert[type][1], property=convert[type][2], gard=gard, date_list=date_list, connection=connect_to_gard[type]) + response = db.run(query_1) + result = response.single() + return_data[gard] = {'name':gards[gard],'num':result['COUNT(x)']} return_data['update_date_end'] = date_end_string return_data['update_date_start'] = date_start_string return return_data -def trigger_email(type,date_start=None,date_end=None): - convert = {'clinical':'trials', 'pubmed':'articles', 'grant':'grants'} - user_data = dict() - cred = credentials.Certificate(sysvars.firebase_key_path) - firebase_admin.initialize_app(cred) - firestore_db = firestore.client() +# the trigger_email function was rewrite to avoid the three nested for loops. +def trigger_email(firestore_db,type,date_start=None,date_end=None): + convert = {prefix+'clinical':'trials', prefix+'pubmed':'articles', prefix+'grant':'grants'} + user_data = dict() firestore_docs = firestore_db.collection(u'users').stream() - + + # get user subscription data here to avoid 3 nested for loops for doc in firestore_docs: if doc.exists: user_data[doc.id] = doc.to_dict() else: print('Document Doesnt Exist') + users = auth.list_users() + user_info={} + if users: + users = users.iterate_all() + for user in users: + uid = user.uid + user_info[user.uid]=user + for firestore_user, data in user_data.items(): subscript_gard = dict() for subscript in data['subscriptions']: - try: - if convert[type] in subscript['alerts']: + if convert[type] in subscript['alerts']: + if 'diseaseName' not in subscript: + subscript_gard[subscript['gardID']] = "" + else: 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 and len(subscript_gard) > 0: - if not date_start and not date_end: - update_data = get_stats(type, subscript_gard) - elif date_start and date_end: - update_data = get_stats(type, subscript_gard, date_start=date_start, date_end=date_end) - elif date_start: - update_data = get_stats(type, subscript_gard, date_start=date_start) - elif date_end: - update_data = get_stats(type, subscript_gard, date_end=date_end) - - 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.pm_db, date_start='12/07/22') #TEST + # get user emails + user=user_info.get(firestore_user,None) + if user: + uid=user.uid + # print("uid == firestore_user::",uid == firestore_user,len(subscript_gard)) + if uid == firestore_user and len(subscript_gard) > 0: + + if not date_start and not date_end: + update_data = get_stats(type, subscript_gard) + elif date_start and date_end: + update_data = get_stats(type, subscript_gard, date_start=date_start, date_end=date_end) + elif date_start: + update_data = get_stats(type, subscript_gard, date_start=date_start) + elif date_end: + update_data = get_stats(type, subscript_gard, date_end=date_end) + + update_data['email'] = user.email + update_data['name'] = user_data[uid].get('displayName',"") + update_data['subscriptions'] = list(subscript_gard.keys()) + # print("update_data::",update_data) + if update_data["total"]>0: # only send email to user if there is any updates + send_mail(type, update_data) + +# trigger_email(sysvars.ct_db, date_start='12/07/22') #TEST. you can put this to the start_dev. so when there are any db upates, it will trigger emails diff --git a/emails/test_ses_firebase.py b/emails/test_ses_firebase.py deleted file mode 100644 index ecfd572..0000000 --- a/emails/test_ses_firebase.py +++ /dev/null @@ -1,190 +0,0 @@ -import glob -import os -import sys -import json -workspace = os.path.dirname(os.path.abspath(__file__)) -sys.path.append(workspace) -# sys.path.append(os.getcwd()) -sys.path.append('/home/aom2/RDAS') -sys.path.append('/home/aom2/RDAS/emails') -import sysvars -from AlertCypher import AlertCypher -from datetime import date,datetime -from jinja2 import Environment, FileSystemLoader -import pandas as pd -from dateutil.relativedelta import relativedelta -import firebase_admin -from firebase_admin import auth -from firebase_admin import credentials -from firebase_admin import firestore -import alert -import email_test - - -prefix = sysvars.db_prefix # you can set the db_prefix in sysvars.py - - -def render_template(filename, data={}): - env = Environment(loader=FileSystemLoader(f'{sysvars.base_path}emails/')) - template = env.get_template(filename) - rendered_content = template.render(data=data) - return rendered_content - -def send_mail(type, data): - # Define the tabs dictionary and txt_db - tabs = {prefix +'clinical': 'trials', prefix +'grant': 'project', prefix +'pubmed': 'nonepi-articles'} - txt_db = {prefix +'clinical': 'clinical trial', prefix +'grant': 'funded project', prefix +'pubmed': 'publication'} - - # Add tabs and type to the data dictionary - data['tabs'] = tabs - data["db_title"]=str(txt_db[type]) - - if data['total'] > 0 and data['email'] == 'timothy.sheils@ncats.nih.gov' or data['email'] == 'zhuqianzq@gmail.com':# for testing - - data['email'] = 'minghui.ao@nih.gov' # TEST EMAIL - html_content = render_template('email_template1.html', data=data) - email_test.send_email(f'RDAS-Alert: {str(txt_db[type])} update regarding your subscriptions', html_content, data['email'])# change to your alert.py sending email method.you may need to adjust your method abit to read in these parameters. - print("finish sending enail") - -def get_stats(type, gards, date_start=datetime.today().strftime('%m/%d/%y'), date_end=datetime.today().strftime('%m/%d/%y')): - db = AlertCypher(type) - return_data = dict() - date_start_string = date_start - date_end_string = date_end - date_start_obj = datetime.strptime(date_start, '%m/%d/%y') - date_end_obj = datetime.strptime(date_end, '%m/%d/%y') - - date_list = pd.date_range(date_start_obj, date_end_obj, freq='D').strftime('%m/%d/%y').to_list() - - convert = {prefix+'clinical':['ClinicalTrial','GARD','GardId'], prefix+'pubmed':['Article','GARD','GardId'], prefix+'grant':['Project','GARD','GardId']} - connect_to_gard = {prefix+'clinical':'--(:Condition)--(:Annotation)--',prefix+'pubmed':'--',prefix+'grant':'--'} - - query = 'MATCH (x:{node}){connection}(y:{gardnode}) WHERE x.DateCreatedRDAS IN {date_list} 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()), date_list=date_list, connection=connect_to_gard[type]) - - response = db.run(query) - result = response.single() - return_data['total'] = result['COUNT(x)'] - - for gard in gards.keys(): - query_1='MATCH (x:{node}){connection}(y:{gardnode}) WHERE x.DateCreatedRDAS IN {date_list} AND y.{property} = \"{gard}\" RETURN COUNT(x)'.format(node=convert[type][0], gardnode=convert[type][1], property=convert[type][2], gard=gard, date_list=date_list, connection=connect_to_gard[type]) - response = db.run(query_1) - result = response.single() - return_data[gard] = {'name':gards[gard],'num':result['COUNT(x)']} - - return_data['update_date_end'] = date_end_string - return_data['update_date_start'] = date_start_string - - return return_data - - -# def trigger_email(type,date_start=None,date_end=None): -# convert = {prefix+'clinical':'trials', prefix+'pubmed':'articles', prefix+'grant':'grants'} -# user_data = dict() -# cred = credentials.Certificate(sysvars.firebase_key_path) -# firebase_admin.initialize_app(cred) -# firestore_db = firestore.client() -# firestore_docs = firestore_db.collection(u'users').stream() - -# # get user subscription data -# for doc in firestore_docs: -# if doc.exists: -# user_data[doc.id] = doc.to_dict() -# else: -# print('Document Doesnt Exist') - -# for firestore_user, data in user_data.items(): -# subscript_gard = dict() -# for subscript in data['subscriptions']: - -# if convert[type] in subscript['alerts']: -# print("user_data::", data) -# print("subscript::",subscript,"\n") - -# if 'diseaseName' not in subscript: - -# subscript_gard[subscript['gardID']] = subscript['gardID'] -# else: -# subscript_gard[subscript['gardID']] = subscript['diseaseName'] - -# # get user emails -# users = auth.list_users() - -# if users: -# users = users.iterate_all() -# for user in users: -# uid = user.uid - -# if uid == firestore_user and len(subscript_gard) > 0: - -# if not date_start and not date_end: -# update_data = get_stats(type, subscript_gard) -# elif date_start and date_end: -# update_data = get_stats(type, subscript_gard, date_start=date_start, date_end=date_end) -# elif date_start: -# update_data = get_stats(type, subscript_gard, date_start=date_start) -# elif date_end: -# update_data = get_stats(type, subscript_gard, date_end=date_end) - -# update_data['email'] = user.email -# update_data['name'] = user_data[uid].get('displayName',"") -# update_data['subscriptions'] = list(subscript_gard.keys()) -# print("update_data::",update_data) -# if update_data["total"]>0: # only send email to user if there is any updates -# send_mail(type, update_data) - - - -# the trigger_email function was rewrite to avoid the three nested for loops. -def trigger_email(firestore_db,type,date_start=None,date_end=None): - convert = {prefix+'clinical':'trials', prefix+'pubmed':'articles', prefix+'grant':'grants'} - user_data = dict() - firestore_docs = firestore_db.collection(u'users').stream() - - # get user subscription data here to avoid 3 nested for loops - for doc in firestore_docs: - if doc.exists: - user_data[doc.id] = doc.to_dict() - else: - print('Document Doesnt Exist') - - users = auth.list_users() - user_info={} - if users: - users = users.iterate_all() - for user in users: - uid = user.uid - user_info[user.uid]=user - - for firestore_user, data in user_data.items(): - subscript_gard = dict() - for subscript in data['subscriptions']: - if convert[type] in subscript['alerts']: - if 'diseaseName' not in subscript: - subscript_gard[subscript['gardID']] = "" - else: - subscript_gard[subscript['gardID']] = subscript['diseaseName'] - - # get user emails - user=user_info.get(firestore_user,None) - if user: - uid=user.uid - # print("uid == firestore_user::",uid == firestore_user,len(subscript_gard)) - if uid == firestore_user and len(subscript_gard) > 0: - - if not date_start and not date_end: - update_data = get_stats(type, subscript_gard) - elif date_start and date_end: - update_data = get_stats(type, subscript_gard, date_start=date_start, date_end=date_end) - elif date_start: - update_data = get_stats(type, subscript_gard, date_start=date_start) - elif date_end: - update_data = get_stats(type, subscript_gard, date_end=date_end) - - update_data['email'] = user.email - update_data['name'] = user_data[uid].get('displayName',"") - update_data['subscriptions'] = list(subscript_gard.keys()) - # print("update_data::",update_data) - if update_data["total"]>0: # only send email to user if there is any updates - send_mail(type, update_data) - -# trigger_email(sysvars.ct_db, date_start='12/07/22') #TEST. you can put this to the start_dev. so when there are any db upates, it will trigger emails diff --git a/start_dev.py b/start_dev.py index 7463601..ce46f6b 100644 --- a/start_dev.py +++ b/start_dev.py @@ -14,6 +14,9 @@ from datetime import date,datetime from AlertCypher import AlertCypher from gard.methods import get_node_counts +sys.path.append('/home/aom2/RDAS') +sys.path.append('/home/aom2/RDAS/emails') +import ses_firebase import firebase_admin from firebase_admin import auth from firebase_admin import credentials @@ -63,6 +66,10 @@ def check_update(db_type): else: return [False,last_update] +cred = credentials.Certificate(sysvars.firebase_key_path) +firebase_admin.initialize_app(cred) +firestore_db = firestore.client() + while True: # Initialize a dictionary to track update status for each database current_updates = {k:False for k,v in sysvars.db_abbrevs.items()} @@ -78,7 +85,7 @@ def check_update(db_type): if v == True: full_db_name = sysvars.db_abbrevs[k] print(f'{full_db_name} Update Initiated') - + p = Popen(['python3', 'driver_manual.py', '-db', f'{k}', '-m', 'update'], encoding='utf8') p.wait() @@ -110,6 +117,9 @@ def check_update(db_type): p.wait() print(f'Update of {full_db_name} Database Complete...') + + ses_firebase.trigger_email(firestore_db,sysvars.ct_db, date_start='12/07/22') + sleep(3600) diff --git a/sysvars.py b/sysvars.py index cbabdec..316fdd5 100644 --- a/sysvars.py +++ b/sysvars.py @@ -20,7 +20,8 @@ # Conversions dump_dirs = ['clinical','pubmed','grant','gard'] -db_abbrevs = {'ct':'clinical', 'pm':'pubmed', 'gnt':'grant'} +db_abbrevs = {'ct':db_prefix+'clinical', 'pm':db_prefix+'pubmed', 'gnt':db_prefix+'grant'} +db_abbrevs2 = {db_prefix+'clinical':'ct', db_prefix+'pubmed':'pm', db_prefix+'grant':'gnt'} # Paths to database creation and update source files ct_files_path = '{base_path}clinical/src/'.format(base_path=base_path) @@ -29,10 +30,12 @@ gard_files_path = '{base_path}gard/src/'.format(base_path=base_path) # Database names being used on the current server -ct_db = 'clinical' -pm_db = 'pubmed' -gnt_db = 'grant' -gard_db = 'gard' + +ct_db = db_prefix+'clinical' +pm_db = db_prefix+'pubmed' +gnt_db = db_prefix+'grant' +gard_db = db_prefix+'gard' +convert = {ct_db:'trials', pm_db:'articles', gnt_db:'grants'} # Server URLS and addresses # Original epiapi_url is https://rdas.ncats.nih.gov/api/epi/ epiapi_url = "https://rdas.ncats.nih.gov/api/epi/"