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 a3c6187
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
93 changes: 93 additions & 0 deletions contest/backend/query.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/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')


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

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

if row_cnt < qsize:
return row_cnt
return branches_to_rows(br_cnt, qsize * 2)


@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
})

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(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 a3c6187

Please sign in to comment.