-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jakub Kicinski <[email protected]>
- Loading branch information
Showing
2 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters