aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--tranches_insight/tranches_insight.py49
1 files changed, 38 insertions, 11 deletions
diff --git a/tranches_insight/tranches_insight.py b/tranches_insight/tranches_insight.py
index 13928c7c..1c6b2991 100644
--- a/tranches_insight/tranches_insight.py
+++ b/tranches_insight/tranches_insight.py
@@ -1,23 +1,50 @@
-from flask import Flask, render_template, jsonify, request
+from flask import Flask, render_template, jsonify, request, g
import os, csv, yaml
+import datetime
+import psycopg2
+
app = Flask(__name__)
basedir = "/home/share/CorpCDOs"
+
+def get_attach_from_name(index_type, series):
+ if index_type.lower() == "ig":
+ if series == 9:
+ attach = [0, 3, 7, 10, 15, 30, 100]
+ else:
+ attach = [0, 3, 7, 15, 100]
+ elif index_type.lower() == "hy":
+ attach = [0, 15, 25, 35, 100]
+ return attach
+
+def get_db():
+ db = getattr(g, '_database', None)
+ if db is None:
+ db = g._database = psycopg2.connect(database="serenitasdb",
+ user="serenitas_user",
+ password="Serenitas1",
+ host="debian")
+ return db
+
+@app.teardown_appcontext
+def close_connection(exception):
+ db = getattr(g, '_database', None)
+ if db is not None:
+ db.close()
+
@app.route("/_data")
def get_corr():
index = request.args.get("i")
- series = request.args.get("s")
+ series = request.args.get("s", 0, int)
tenor = request.args.get("t")
data = []
- with open(os.path.join(basedir, "Tranche_data", "Runs",
- "{0}{1}.{2}.csv".format(index, series, tenor))) as fh:
- csvreader = csv.DictReader(fh)
- for line in csvreader:
- date = line["date"].replace("-","/")
- corrkeys = [k for k in line.keys() if k.endswith("Corr")]
- corrkeys = sorted(corrkeys, key=lambda x: int(x.split(" ")[0]))
- data.append([date] + [line[k] for k in corrkeys])
- return jsonify(labels=["Date"] + corrkeys, data=data)
+ db = get_db()
+ attach = get_attach_from_name(index, series)
+ with db.cursor() as c:
+ c.execute("SELECT date, skew from risk_numbers WHERE index=%s and series=%s" \
+ "and tenor=%s ORDER BY date", (index.upper(), series, tenor))
+ data = [["%s/%s/%s" % (date.year, date.month, date.day)] + skew[:-1] for date, skew in c]
+ return jsonify(labels=["Date"] + ["{0} Corr".format(a) for a in attach[1:-1]], data=data)
@app.route("/")
def main():