Skip to content

Commit

Permalink
wip: query: support l2 transform
Browse files Browse the repository at this point in the history
Signed-off-by: Jakub Kicinski <[email protected]>
  • Loading branch information
kuba-moo committed Jun 7, 2024
1 parent e54e2fc commit 2c32472
Showing 1 changed file with 37 additions and 10 deletions.
47 changes: 37 additions & 10 deletions contest/backend/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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')

0 comments on commit 2c32472

Please sign in to comment.