Skip to content

Commit

Permalink
contest: backend: add trivial query
Browse files Browse the repository at this point in the history
Signed-off-by: Jakub Kicinski <[email protected]>
  • Loading branch information
kuba-moo committed May 18, 2024
1 parent 65ac327 commit 747f810
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 1 deletion.
101 changes: 101 additions & 0 deletions contest/backend/query.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0


from flask import Flask
from flask import request
import couchdb
import os
import datetime


app = Flask("NIPA contest query")

user = os.getenv('DB_USER')
pwd = os.getenv('DB_PWD')
couch = couchdb.Server(f'http://{user}:{pwd}@127.0.0.1:5984')

br_cnt_guesses = {}

def _branches_to_rows(br_cnt, qsize):
rows = couch["results"].find({
"selector": {},
"fields": ["branch"],
"sort": [
{
"branch": "desc"
}
],
"limit": qsize
})

seen_br = set()
cnt = 0
for row in rows:
seen_br.add(row['branch'])
if len(seen_br) > br_cnt:
br_cnt_guesses[br_cnt] = cnt
return cnt
cnt += 1

if cnt < qsize:
return cnt
return _branches_to_rows(br_cnt, qsize * 4)


def branches_to_rows(br_cnt):
return _branches_to_rows(br_cnt, br_cnt_guesses.get(br_cnt, 2000) + 50)


@app.route('/')
def hello():
return '<h1>boo!</h1>'


@app.route('/results')
def results():
global couch

t1 = datetime.datetime.now()

br_cnt = request.args.get('branches')
try:
br_cnt = int(br_cnt)
except:
br_cnt = None
if not br_cnt:
br_cnt = 10

need_rows = branches_to_rows(br_cnt)
rows = couch["results"].find({
"selector": {},
"sort": [
{
"branch": "desc"
}
],
# Overshoot by 20 in case of races
"limit": need_rows + 20
})

t2 = datetime.datetime.now()
print("preq", str(t2-t1))
t1 = t2

return_rows = []
seen_br = set()
for row in rows:
if row["branch"] not in seen_br and len(seen_br) == br_cnt:
break

seen_br.add(row['branch'])
return_rows.append(row)

t2 = datetime.datetime.now()
print("full", str(t2-t1))

return return_rows


if __name__ == "__main__":
app.run(host='0.0.0.0')
2 changes: 1 addition & 1 deletion contest/results-fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def insert_wip(self, remote, run):
else:
data['_id'] = uuid.uuid4().hex
data["remote"] = remote["name"]
when = datetime.datetime.fromisoformat(branch_info[run['branch']]['date'])
when = datetime.datetime.fromisoformat(branch_info['date'])
data["start"] = str(when)
when += datetime.timedelta(hours=2, minutes=58)
data["end"] = str(when)
Expand Down

0 comments on commit 747f810

Please sign in to comment.