diff options
Diffstat (limited to 'python/report_ops/utils.py')
| -rw-r--r-- | python/report_ops/utils.py | 91 |
1 files changed, 70 insertions, 21 deletions
diff --git a/python/report_ops/utils.py b/python/report_ops/utils.py index e7cfea75..3bf56462 100644 --- a/python/report_ops/utils.py +++ b/python/report_ops/utils.py @@ -16,6 +16,7 @@ import math import re from zoneinfo import ZoneInfo from .misc import _recipients, _cc_recipients +from tabulate import tabulate logger = logging.getLogger(__name__) @@ -197,28 +198,38 @@ CitcoSubmission._sql_insert = CitcoSubmission._sql_insert.replace( @dataclass class Monitor: date: datetime.date - keys: ClassVar = () - header: ClassVar[str] = None - line_format: ClassVar[str] = None + headers: ClassVar = () + num_format: ClassVar = [] _insert_queue: ClassVar[list] = [] _em: ClassVar = ExchangeMessage() - def __init_subclass__(cls, keys, header, line_format): - cls.keys = keys - cls.header = header + def __init_subclass__(cls, headers, num_format=[]): + cls.headers = headers cls.line_format = line_format @classmethod def stage(cls, d: dict): - cls.insert_queue.append(tuple(d[key] for key in cls.keys)) + cls._insert_queue.append(list(d[key] for key in cls.headers)) @classmethod - def to_email_format(cls): - return "\n".join([cls.line_format.format(line) for line in cls._insert_queue]) + def format(cls): + for line in cls._insert_queue: + for f, i in cls.num_format: + line[i] = f.format[i] + + @classmethod + def to_tabulate(cls): + cls.format() + t = tabulate( + cls._insert_queue, + headers=cls.headers, + tablefmt="unsafehtml", + ) + return t class GFSMonitor( - Monitor, keys=("date", "currency", "amount"), line_format="* {0}: {2:,.2f} {1}" + Monitor, headers=("date", "amount", "currency"), num_format=[("{0:,.2f}", 1)] ): @classmethod def email(cls, fund): @@ -226,8 +237,20 @@ class GFSMonitor( return cls._em.send_email( f"GFS Helper Strategy Issue: {fund}", - "Good morning, \n\nWe noticed some cash in the GFS helper strategy that shouldn't be there:\n\n" - + cls.to_email_format(), + HTMLBody( + f""" +<html> + <head> + <style> + table, th, td {{ border: 1px solid black; border-collapse: collapse;}} + th, td {{ padding: 5px; }} + </style> + </head> + <body> + Good morning,<br><br>Could you please help us with the below transfer breaks:<br><br>{cls.to_tabulate()} + </body> +</html>""" + ), to_recipients=_recipients[fund], cc_recipients=_cc_recipients[fund], ) @@ -235,24 +258,38 @@ class GFSMonitor( class CDXQuoteMonitor( Monitor, - keys=("date", "underlying", "globeop_quote", "serenitas_quote"), - line_format="* ({0}) {1}: Globeop:{3:,.2f} Ours:{4:,.2f}", + headers=("date", "underlying", "globeop_quote", "serenitas_quote"), + num_format=[("{0:,.2f}", 3), ("{0:,.2f}", 4)], ): @classmethod def email(cls, fund): if not cls._insert_queue: return cls._em.send_email( - f"Cleared CDX Quote Issue: {fund}", - "Good morning, \n\nWe've noticed some cleared CDX quotes outside of our tolerance:\n\n" - + cls.to_email_format(), + f"CDX Quote Outside of our Tolerance: {fund}", + HTMLBody( + f""" +<html> + <head> + <style> + table, th, td {{ border: 1px solid black; border-collapse: collapse;}} + th, td {{ padding: 5px; }} + </style> + </head> + <body> + Good morning,<br><br>Could you please help us with the below cleared CDX quotes outside of our tolerance:<br><br>{cls.to_tabulate()} + </body> +</html>""" + ), to_recipients=_recipients[fund], cc_recipients=_cc_recipients[fund], ) class CBMonitor( - Monitor, keys=("date", "currency", "amount"), line_format="* {0}: {2:,.2f} {1}" + Monitor, + headers=("date", "currency", "amount"), + num_format=[("{0:,.2f}", 3)], ): @classmethod def project_cash_balances(cls, account_balances): @@ -267,9 +304,21 @@ class CBMonitor( if not cls._insert_queue: return cls._em.send_email( - f"*ACTION REQUESTED* Projected overdraft for {fund}", - "Good morning, \n\nWe are expecting an overdraft at NT: (Positive Amounts = Positive Balance, Negative Amounts = Negative Balance)\n\n" - + cls.to_email_format(), + f"*ACTION REQUESTED* Projected Overdraft: {fund}", + HTMLBody( + f""" +<html> + <head> + <style> + table, th, td {{ border: 1px solid black; border-collapse: collapse;}} + th, td {{ padding: 5px; }} + </style> + </head> + <body> + Good morning,<br><br>We see a projected overdraft on the below dates. Please move to cover:<br><br>{cls.to_tabulate()} + </body> +</html>""" + ), to_recipients=_recipients[fund], cc_recipients=_cc_recipients[fund], ) |
