Merge pull request #79 from vexxhost/improve-result-report

Improve result report
diff --git a/staffeln/common/email.py b/staffeln/common/email.py
index 693feb5..7fa07cb 100644
--- a/staffeln/common/email.py
+++ b/staffeln/common/email.py
@@ -1,35 +1,36 @@
-# Email notification package

-# This should be upgraded by integrating with mail server to send batch

+""" Email module with SMTP"""

+

 import smtplib

-from email.mime.multipart import MIMEMultipart

+from email.header import Header

 from email.mime.text import MIMEText

 

-__DRY_RUN__ = False

+from oslo_log import log

+

+LOG = log.getLogger(__name__)

 

 

-def send(

-    src_email,

-    src_pwd,

-    dest_email,

-    subject,

-    content,

-    smtp_server_domain,

-    smtp_server_port,

-):

-    message = MIMEMultipart("alternative")

-    message["Subject"] = subject

-    # This part is commented as it won't be able to parce the items in list.

-    # message["From"] = src_email

-    # message["To"] = dest_email

-    part = MIMEText(content, "html")

-    message.attach(part)

-    if __DRY_RUN__:

-        print(part)

-        return

-    s = smtplib.SMTP(host=smtp_server_domain, port=smtp_server_port)

-    # s.ehlo()

-    # s.starttls()

-    # we can comment this auth func when use the trusted ip without authentication against the smtp server

-    # s.login(src_email, src_pwd)

-    s.sendmail(src_email, dest_email, message.as_string())

-    s.close()

+def send(smtp_profile):

+    """Email send with SMTP"""

+    message = MIMEText(smtp_profile["content"], "html", "utf-8")

+    message["From"] = Header(smtp_profile["src_name"], "utf-8")

+    message["To"] = Header(smtp_profile["dest_email"], "utf-8")

+    message["Subject"] = Header(smtp_profile["subject"], "utf-8")

+    try:

+        smtp_obj = smtplib.SMTP(

+            smtp_profile["smtp_server_domain"], smtp_profile["smtp_server_port"]

+        )

+        smtp_obj.connect(

+            smtp_profile["smtp_server_domain"], smtp_profile["smtp_server_port"]

+        )

+        smtp_obj.ehlo()

+        smtp_obj.starttls()

+        smtp_obj.ehlo()

+        # SMTP Login

+        smtp_obj.login(smtp_profile["src_email"], smtp_profile["src_pwd"])

+        smtp_obj.sendmail(

+            smtp_profile["src_email"], smtp_profile["dest_email"], message.as_string()

+        )

+        # Email Sent

+    except smtplib.SMTPException as error:

+        LOG.info(f"again this name is {str(error)}")

+        raise

diff --git a/staffeln/conductor/manager.py b/staffeln/conductor/manager.py
index 62530c0..6869caa 100755
--- a/staffeln/conductor/manager.py
+++ b/staffeln/conductor/manager.py
@@ -129,15 +129,19 @@
         threshold_strtime = datetime.now() - timedelta(minutes=report_period_mins)

         filters = {

             "created_at__lt": threshold_strtime.astimezone(),

-            "backup_status": constants.BACKUP_COMPLETED,

         }

-        success_tasks = self.controller.get_queues(filters=filters)

-        filters["backup_status"] = constants.BACKUP_FAILED

-        failed_tasks = self.controller.get_queues(filters=filters)

-        if success_tasks or failed_tasks:

-            self.controller.publish_backup_result()

-            # Purge backup queue tasks

-            self.controller.purge_backups()

+        old_tasks = self.controller.get_queues(filters=filters)

+        if old_tasks:

+            filters = {

+                "backup_status": constants.BACKUP_COMPLETED,

+            }

+            success_tasks = self.controller.get_queues(filters=filters)

+            filters["backup_status"] = constants.BACKUP_FAILED

+            failed_tasks = self.controller.get_queues(filters=filters)

+            if success_tasks or failed_tasks:

+                self.controller.publish_backup_result()

+                # Purge backup queue tasks

+                self.controller.purge_backups()

 

     def backup_engine(self, backup_service_period):

         LOG.info("Backup manager started %s" % str(time.time()))

diff --git a/staffeln/conductor/result.py b/staffeln/conductor/result.py
index df47a19..af7eb5f 100644
--- a/staffeln/conductor/result.py
+++ b/staffeln/conductor/result.py
@@ -23,19 +23,25 @@
         self.project_list.add((project_id, project_name))

 

     def send_result_email(self):

-        subject = "Backup result"

+        subject = "Staffeln Backup result"

         try:

             if len(CONF.notification.receiver) == 0:

+                LOG.info(

+                    "Directly record report in log as no receiver "

+                    "email provided. Report: %s" % self.content

+                )

                 return

-            email.send(

-                src_email=CONF.notification.sender_email,

-                src_pwd=CONF.notification.sender_pwd,

-                dest_email=CONF.notification.receiver,

-                subject=subject,

-                content=self.content,

-                smtp_server_domain=CONF.notification.smtp_server_domain,

-                smtp_server_port=CONF.notification.smtp_server_port,

-            )

+            smtp_profile = {

+                "src_email": CONF.notification.sender_email,

+                "src_name": "Staffeln",

+                "src_pwd": CONF.notification.sender_pwd,

+                "dest_email": CONF.notification.receiver,

+                "subject": subject,

+                "content": self.content,

+                "smtp_server_domain": CONF.notification.smtp_server_domain,

+                "smtp_server_port": CONF.notification.smtp_server_port,

+            }

+            email.send(smtp_profile)

             LOG.info(_("Backup result email sent"))

         except Exception as e:

             LOG.error(

@@ -74,13 +80,14 @@
             quota = backup_mgt.get_backup_quota(project_id)

 

             html += (

-                "<h3>Project: ${PROJECT}</h3><br>"

-                "<h3>Quota Usage</h3><br>"

-                "<h4>Limit: ${QUOTA_LIMIT}, In Use: ${QUOTA_IN_USE}, Reserved: ${QUOTA_RESERVED}</h4><br>"

-                "<h3>Success List</h3><br>"

-                "<h4>${SUCCESS_VOLUME_LIST}</h4><br>"

-                "<h3>Failed List</h3><br>"

-                "<h4>${FAILED_VOLUME_LIST}</h4><br>"

+                "<h3>Project: ${PROJECT}</h3><h3>Quota Usage</h3>"

+                "<FONT COLOR=${QUOTA_COLLOR}><h4>Limit: ${QUOTA_LIMIT}, In Use: "

+                "${QUOTA_IN_USE}, Reserved: ${QUOTA_RESERVED}, Total "

+                "rate: ${QUOTA_USAGE}</h4></FONT>"

+                "<h3>Success List</h3>"

+                "<FONT COLOR=GREEN><h4>${SUCCESS_VOLUME_LIST}</h4></FONT><br>"

+                "<h3>Failed List</h3>"

+                "<FONT COLOR=RED><h4>${FAILED_VOLUME_LIST}</h4></FONT><br>"

             )

 

             if project_id in project_success:

@@ -106,7 +113,15 @@
                 )

             else:

                 failed_volumes = "<br>"

-

+            quota_usage = (quota["in_use"] + quota["reserved"]) / quota["limit"]

+            if quota_usage > 0.8:

+                quota_color = "RED"

+            elif quota_usage > 0.5:

+                quota_color = "YALLOW"

+            else:

+                quota_color = "GREEN"

+            html = html.replace("${QUOTA_USAGE}", quota_usage)

+            html = html.replace("${QUOTA_COLLOR}", quota_color)

             html = html.replace("${QUOTA_LIMIT}", str(quota["limit"]))

             html = html.replace("${QUOTA_IN_USE}", str(quota["in_use"]))

             html = html.replace("${QUOTA_RESERVED}", str(quota["reserved"]))