From 2c32472d752a678b3c6cd673456dfe78cbda0988 Mon Sep 17 00:00:00 2001 From: Jakub Kicinski Date: Fri, 7 Jun 2024 13:57:05 -0700 Subject: [PATCH] wip: query: support l2 transform Signed-off-by: Jakub Kicinski --- contest/backend/query.py | 47 +++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/contest/backend/query.py b/contest/backend/query.py index c0203f2..e36a2da 100644 --- a/contest/backend/query.py +++ b/contest/backend/query.py @@ -96,6 +96,22 @@ def branches_to_rows2(br_cnt): return cnt +def result_as_l2(raw): + row = json.loads(raw) + flat = [] + + for l1 in row["results"]: + if "results" not in l1: + flat.append(l1) + else: + for case in l1["results"]: + data = l1.copy() + data |= case + data["test"] = l1["test"] + '.' + case["test"] + row["results"] = flat + return json.dumps(row) + + @app.route('/results2') def results2(): global psql @@ -115,6 +131,7 @@ def results2(): t1 = datetime.datetime.now() + format = request.args.get('format') br_cnt = request.args.get('branches') try: br_cnt = int(br_cnt) @@ -123,20 +140,30 @@ def results2(): if not br_cnt: br_cnt = 10 - raw = request.args.get('raw') - need_rows = branches_to_rows2(br_cnt) + t2 = datetime.datetime.now() - with psql.cursor() as cur: - cur.execute(f"SELECT json_normal FROM results ORDER BY branch DESC LIMIT {need_rows}") - if raw: + + if not format or format == "normal": + with psql.cursor() as cur: + cur.execute(f"SELECT json_normal FROM results ORDER BY branch DESC LIMIT {need_rows}") rows = "[" + ",".join([r[0] for r in cur.fetchall()]) + "]" - else: - rows = [json.loads(r[0]) for r in cur.fetchall()] + elif format == "l2": + with psql.cursor() as cur: + cur.execute(f"SELECT json_normal, json_full FROM results ORDER BY branch DESC LIMIT {need_rows}") + rows = "[" + for r in cur.fetchall(): + if rows[-1] != '[': + rows += ',' + if r[1] and len(r[1]) > 50: + rows += result_as_l2(r[1]) + else: + rows += r[0] + rows += ']' + else: + rows = "[]" t3 = datetime.datetime.now() print(f"Query for {br_cnt} branches, {need_rows} records took: {str(t3-t1)} ({str(t2-t1)}+{str(t3-t2)})") - if raw: - return Response(rows, mimetype='application/json') - return rows + return Response(rows, mimetype='application/json')