From 4ca02a7243ae38fb7494c5ad9e0e1fdcbf60b784 Mon Sep 17 00:00:00 2001 From: Terrtia Date: Fri, 10 Jul 2020 15:54:14 +0200 Subject: [PATCH] chg: [Trackers] email notifications: add tracker description in email subject --- bin/NotificationHelper.py | 9 ++++----- bin/RegexTracker.py | 4 +++- bin/TermTrackerMod.py | 5 ++++- bin/lib/Tracker.py | 26 ++++++++++++++++++++++++++ 4 files changed, 37 insertions(+), 7 deletions(-) create mode 100755 bin/lib/Tracker.py diff --git a/bin/NotificationHelper.py b/bin/NotificationHelper.py index b9c90104..5e08f7e4 100755 --- a/bin/NotificationHelper.py +++ b/bin/NotificationHelper.py @@ -23,7 +23,7 @@ config_loader = ConfigLoader.ConfigLoader() publisher.port = 6380 publisher.channel = "Script" -def sendEmailNotification(recipient, alert_name, content): +def sendEmailNotification(recipient, mail_subject, mail_body): sender = config_loader.get_config_str("Notifications", "sender") sender_user = config_loader.get_config_str("Notifications", "sender_user") @@ -60,14 +60,13 @@ def sendEmailNotification(recipient, alert_name, content): mime_msg = MIMEMultipart() mime_msg['From'] = sender mime_msg['To'] = recipient - mime_msg['Subject'] = "AIL Framework " + alert_name + " Alert" + mime_msg['Subject'] = mail_subject - body = content - mime_msg.attach(MIMEText(body, 'plain')) + mime_msg.attach(MIMEText(mail_body, 'plain')) smtp_server.sendmail(sender, recipient, mime_msg.as_string()) smtp_server.quit() - print('Send notification ' + alert_name + ' to '+recipient) + print('Send notification: ' + mail_subject + ' to '+recipient) except Exception as err: traceback.print_tb(err.__traceback__) diff --git a/bin/RegexTracker.py b/bin/RegexTracker.py index d1bdc26e..270df81f 100755 --- a/bin/RegexTracker.py +++ b/bin/RegexTracker.py @@ -20,6 +20,7 @@ from packages import Item from packages import Term sys.path.append(os.path.join(os.environ['AIL_BIN'], 'lib')) +import Tracker import regex_helper full_item_url = "/showsavedpaste/?paste=" @@ -42,9 +43,10 @@ def new_term_found(term, term_type, item_id, item_date): mail_to_notify = Term.get_term_mails(term_uuid) if mail_to_notify: + mail_subject = Tracker.get_email_subject(tracker_uuid) mail_body = mail_body_template.format(term, item_id, full_item_url, item_id) for mail in mail_to_notify: - NotificationHelper.sendEmailNotification(mail, 'Term Tracker', mail_body) + NotificationHelper.sendEmailNotification(mail, mail_subject, mail_body) if __name__ == "__main__": publisher.port = 6380 diff --git a/bin/TermTrackerMod.py b/bin/TermTrackerMod.py index ce4dcf20..931b5979 100755 --- a/bin/TermTrackerMod.py +++ b/bin/TermTrackerMod.py @@ -18,6 +18,8 @@ import NotificationHelper from packages import Item from packages import Term +from lib import Tracker + full_item_url = "/showsavedpaste/?paste=" mail_body_template = "AIL Framework,\nNew occurrence for term tracked term: {}\nitem id: {}\nurl: {}{}" @@ -48,9 +50,10 @@ def new_term_found(term, term_type, item_id, item_date): mail_to_notify = Term.get_term_mails(term_uuid) if mail_to_notify: + mail_subject = Tracker.get_email_subject(tracker_uuid) mail_body = mail_body_template.format(term, item_id, full_item_url, item_id) for mail in mail_to_notify: - NotificationHelper.sendEmailNotification(mail, 'Term Tracker', mail_body) + NotificationHelper.sendEmailNotification(mail, mail_subject, mail_body) if __name__ == "__main__": diff --git a/bin/lib/Tracker.py b/bin/lib/Tracker.py new file mode 100755 index 00000000..edf59198 --- /dev/null +++ b/bin/lib/Tracker.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 +# -*-coding:UTF-8 -* + +import os +import sys +import time +import redis + +sys.path.append(os.path.join(os.environ['AIL_BIN'], 'lib/')) +import ConfigLoader + +import item_basic + +config_loader = ConfigLoader.ConfigLoader() +r_serv_tracker = config_loader.get_redis_conn("ARDB_Tracker") +config_loader = None + +get_tracker_description(tracker_uuid): + return r_serv_tracker.hget('tracker:{}'.format(term_uuid), 'description') + +def get_email_subject(tracker_uuid): + tracker_description = get_tracker_description(tracker_uuid) + if not tracker_description: + return "AIL framework: Tracker Alert" + else: + return 'AIL framework: {}'.format(tracker_description)