aboutsummaryrefslogtreecommitdiffstats
path: root/python/ack_checker.py
blob: a516ff9d2d97f362546a1410059cbc78a52d345e (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
import codecs
import datetime
from csv import reader
from io import BytesIO
from remote import FtpClient
from utils.db import dbconn

dawndb = dbconn("dawndb")
ftp = FtpClient.from_creds("globeop")
ftp.client.cwd("outgoing")
date = datetime.date.today()
files = [f for f in ftp.client.nlst() if f.startswith(f"Serenitas.ALL.{date:%Y%m%d}")]
for f in files:
    buf = BytesIO()
    ftp.client.retrbinary("RETR " + f, buf.write)
    buf.seek(0)
    csv = reader(codecs.iterdecode(buf, "utf-8"))
    for serenitas_id, action, dealtype, result, globeop_id, _, _ in csv:
        if action == "NEW" and result == "Loaded":
            globeop_id = int(globeop_id)
            serenitas_id = int(serenitas_id[5:])
            if dealtype == "CreditDefaultSwapDeal":
                with dawndb.cursor() as c:
                    c.execute(
                        "SELECT orig_attach FROM cds WHERE dealid=%s", (serenitas_id,)
                    )
                    (attach,) = c.fetchone()
                    if attach is None:
                        continue

                with dawndb.cursor() as c:
                    c.execute(
                        "INSERT INTO id_mapping VALUES(%s, %s, %s, %s)",
                        (date, "CDS", serenitas_id, globeop_id),
                    )
            if dealtype == "SwaptionDeal":
                with dawndb.cursor() as c:
                    c.execute(
                        "UPDATE swaptions SET globeop_id=%s WHERE dealid=%s",
                        (globeop_id, serenitas_id),
                    )
        dawndb.commit()