aboutsummaryrefslogtreecommitdiffstats
path: root/python/citco_monitor.py
blob: 82bdb3e0611748c776de2e6312092987984ecb90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import datetime
from exchangelib import HTMLBody

from serenitas.analytics.dates import prev_business_day
from serenitas.utils.db import dbconn

from report_ops.utils import check_cleared_cds, Monitor
from report_ops.misc import _recon_recipients, _cc_recipients


class BondFactorMonitor(
    Monitor,
    headers=(
        "date",
        "citco_security_id",
        "security_id",
        "serenitas_factor",
        "admin_factor",
        "difference",
    ),
    num_format=[("{0:,.2f}", 3), ("{0:,.2f}", 4), ("{0:,.2f}", 5)],
):
    @classmethod
    def email(cls, fund):
        if not cls._staging_queue:
            return
        cls._em.send_email(
            f"*ACTION REQUESTED* Incorrect Factors: {fund}",
            HTMLBody(
                f"""
<html>
  <head>
    <style>
      table, th, td {{ border: 1px solid black;  border-collapse: collapse;}}
      th, td {{ padding: 5px; }}
    </style>
  </head>
  <body>
  Hello,<br><br>We see the below factor mismatches. Could you please correct the below?<br><br>{cls.to_tabulate()}
  </body>
</html>"""
            ),
            to_recipients=_recon_recipients[fund],
            cc_recipients=_cc_recipients[fund],
        )


def check_bond_factors(date, fund, conn):
    with conn.cursor() as c:
        c.execute(
            "SELECT citco_security_id, security_id, serenitas_factor, admin_factor, serenitas_factor-admin_factor as difference FROM compare_citco_bonds (%s, %s) WHERE abs(serenitas_factor - admin_factor) > .01;",
            (date, fund),
        )
        for row in c:
            d = row._asdict() | {"date": date}
            BondFactorMonitor.stage(d)
        BondFactorMonitor.email(fund)


if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument(
        "cob",
        nargs="?",
        type=datetime.date.fromisoformat,
        default=prev_business_day(datetime.date.today()),
        help="working date",
    )
    args = parser.parse_args()
    conn = dbconn("dawndb")
    for fund in ("ISOSEL",):
        check_cleared_cds(args.cob, fund, conn)
        check_bond_factors(args.cob, fund, conn)