Skip to content

Commit

Permalink
site: api: Support excluding duplicates from logs results
Browse files Browse the repository at this point in the history
This allows excluding duplicates from logs results, which is almost always
what you want unless you are looking for a particular log (or are
interested in the duplicates themselves).

Signed-off-by: Sean Anderson <[email protected]>
  • Loading branch information
Forty-Bot committed Feb 19, 2024
1 parent 927f7a2 commit de79028
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
3 changes: 3 additions & 0 deletions test/site_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,6 @@ def paged(limit=10):

for log in get(updated_since=updated_pivot)[0]:
assert log['updated'] > updated_pivot

for log in get(include_dupes='no')[0]:
assert log['duplicate_of'] is None
6 changes: 4 additions & 2 deletions trends/site/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ def next_page(rows):

@api.route('/logs')
def logs():
if resp := logs_last_modified():
dupes = flask.request.args.get('include_dupes', 'yes', str) == 'yes'
if resp := logs_last_modified(dupes):
return resp

view = flask.request.args.get('view', 'basic', str)
logs = [dict(log) for log in get_logs(view)]
logs = [dict(log) for log in get_logs(view, dupes)]
return flask.jsonify(logs=logs, next_page=next_page(logs))

@api.route('/maps')
Expand Down
9 changes: 7 additions & 2 deletions trends/site/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
from .util import get_db, get_filter_params, get_filter_clauses, get_order, get_pagination, \
last_modified

def logs_last_modified():
def logs_last_modified(dupes):
filters = get_filter_params()
filter_clauses = get_filter_clauses(filters, 'title', 'formatd', 'mapid', 'time', 'logid',
'updated', 'league')
if not dupes:
filter_clauses += "\nAND duplicate_of ISNULL"

db = get_db()
cur = db.cursor()
Expand All @@ -34,11 +36,14 @@ def logs_last_modified():
cur.execute(query, filters)
return last_modified(cur.fetchone()[0])

def get_logs(view):
def get_logs(view, dupes):
limit, offset = get_pagination()
filters = get_filter_params()
filter_clauses = get_filter_clauses(filters, 'title', 'format', 'map', 'time', 'logid',
'updated', league='log.league')
if not dupes:
filter_clauses += "\nAND duplicate_of ISNULL"

order, order_clause = get_order({
'logid': "logid",
'duration': "duration",
Expand Down
4 changes: 2 additions & 2 deletions trends/site/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ def leaderboard():

@root.route('/logs')
def logs():
if resp := logs_last_modified():
if resp := logs_last_modified(True):
return resp
return flask.render_template("logs.html", logs=get_logs('basic').fetchall())
return flask.render_template("logs.html", logs=get_logs('basic', True).fetchall())

@root.route('/log')
def log_form():
Expand Down
8 changes: 8 additions & 0 deletions trends/site/templates/api.html
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,14 @@ <h4>Parameters</h4>
</dl>
</p>
</li>
<li>
{{ pre("include_dupes") }} (default {{ pre("yes") }})
<p>
Include duplicate logs in the output. If {{ pre("yes") }}, then
{{ pre("duplicate_of") }} may be non-{{ pre("null") }}. If {{ pre("no") }},
then such logs will be not be included with the results.
</p>
</li>
</ul>
</p>
<h4>Response</h4>
Expand Down

0 comments on commit de79028

Please sign in to comment.