Skip to content

Commit

Permalink
Merge pull request #1906 from Scifabric/issue-1905
Browse files Browse the repository at this point in the history
Issue 1905
  • Loading branch information
teleyinex committed Dec 5, 2018
2 parents 4d29882 + 1d47798 commit f1afc65
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 34 deletions.
6 changes: 3 additions & 3 deletions pybossa/leaderboard/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def get_leaderboard(top_users=20, user_id=None, window=0, info=None):
sql = text('''SELECT * from users_rank WHERE rank <= :top_users
ORDER BY rank;''')
if info:
sql = text('''SELECT * from {} WHERE rank <= :top_users
sql = text('''SELECT * from "{}" WHERE rank <= :top_users
ORDER BY rank;'''.format(materialized_view))

results = db.session.execute(sql, dict(top_users=top_users))
Expand All @@ -38,7 +38,7 @@ def get_leaderboard(top_users=20, user_id=None, window=0, info=None):
if user_id:
sql = text('''SELECT * from users_rank where id=:user_id;''')
if info:
sql = text('''SELECT * from {} where
sql = text('''SELECT * from "{}" where
id=:user_id;'''.format(materialized_view))
results = db.session.execute(sql, dict(user_id=user_id))
user = None
Expand All @@ -49,7 +49,7 @@ def get_leaderboard(top_users=20, user_id=None, window=0, info=None):
WHERE rank >= :low AND rank <= :top order by rank;
''')
if info:
sql = text('''SELECT * from {}
sql = text('''SELECT * from "{}"
WHERE rank >= :low AND rank <= :top order by rank;
'''.format(materialized_view))
low = user['rank'] - window
Expand Down
8 changes: 4 additions & 4 deletions pybossa/leaderboard/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def leaderboard(info=None):
return refresh_materialized_view(db, materialized_view)
else:
sql = '''
CREATE MATERIALIZED VIEW {} AS WITH scores AS (
CREATE MATERIALIZED VIEW "{}" AS WITH scores AS (
SELECT "user".*, COUNT(task_run.user_id) AS score
FROM "user" LEFT JOIN task_run
ON task_run.user_id="user".id where
Expand All @@ -42,15 +42,15 @@ def leaderboard(info=None):
'''.format(materialized_view)
if info:
sql = '''
CREATE MATERIALIZED VIEW {} AS WITH scores AS (
CREATE MATERIALIZED VIEW "{}" AS WITH scores AS (
SELECT "user".*, COALESCE(CAST("user".info->>'{}' AS INTEGER), 0) AS score
FROM "user" where "user".restrict=false ORDER BY score DESC) SELECT *, row_number() OVER (ORDER BY score DESC) as rank FROM scores;
'''.format(materialized_view, info)
db.session.execute(sql)
db.session.commit()
sql = '''
CREATE UNIQUE INDEX {}
on {}(id, rank);
CREATE UNIQUE INDEX "{}"
on "{}"(id, rank);
'''.format(materialized_view_idx, materialized_view)
db.session.execute(sql)
db.session.commit()
Expand Down
4 changes: 2 additions & 2 deletions test/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ def delete_materialized_views():
FROM pg_class WHERE relname LIKE '%dashboard%';''')
results = db.session.execute(sql)
for row in results:
sql = 'drop materialized view if exists %s cascade' % row.relname
sql = 'drop materialized view if exists "%s" cascade' % row.relname
db.session.execute(sql)
db.session.commit()
sql = text('''SELECT relname
FROM pg_class WHERE relname LIKE '%users_rank%';''')
results = db.session.execute(sql)
for row in results:
if "_idx" not in row.relname:
sql = 'drop materialized view if exists %s cascade' % row.relname
sql = 'drop materialized view if exists "%s" cascade' % row.relname
db.session.execute(sql)
db.session.commit()

Expand Down
90 changes: 65 additions & 25 deletions test/test_jobs/test_leaderboard_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,24 @@ def test_leaderboard_foo_key(self):
for r in results:
assert r.restrict is False, r

@with_context
def test_leaderboard_foo_dash_key(self):
"""Test JOB leaderboard returns users for foo-dash key."""
users = []
for score in range(1, 11):
users.append(UserFactory.create(info={'foo-dash': score}))
users.append(UserFactory.create(restrict=True, info={'foo-dash': 11}))
leaderboard(info='foo-dash')
top_users = get_leaderboard(info='foo-dash')
assert len(top_users) == 10, len(top_users)
score = 10
for user in top_users:
user['score'] == score, user
score = score - 1

results = db.session.execute('select * from "users_rank_foo-dash"');
for r in results:
assert r.restrict is False, r

@with_context
def test_leaderboard_foo_key_current_user(self):
Expand All @@ -144,6 +162,28 @@ def test_leaderboard_foo_key_current_user(self):
for r in results:
assert r.restrict is False, r

@with_context
def test_leaderboard_foo_dash_key_current_user(self):
"""Test JOB leaderboard returns users for foo-dash key with current user."""
users = []
for score in range(1, 11):
users.append(UserFactory.create(info={'foo-dash': score}))

users.append(UserFactory.create(restrict=True, info={'foo-dash': 11}))

leaderboard(info='foo-dash')
top_users = get_leaderboard(user_id=users[0].id, info='foo-dash')
assert len(top_users) == 11, len(top_users)
score = 10
for user in top_users[0:10]:
user['score'] == score, user
score = score - 1
assert top_users[-1]['name'] == users[0].name
assert top_users[-1]['score'] == users[0].info.get('foo-dash')

results = db.session.execute('select * from "users_rank_foo-dash"');
for r in results:
assert r.restrict is False, r

@with_context
def test_leaderboard_foo_key_current_user_window(self):
Expand Down Expand Up @@ -172,28 +212,28 @@ def test_leaderboard_foo_key_current_user_window(self):
assert r.restrict is False, r


#@with_context
#def test_format_anon_week(self):
# """Test format anon week works."""
# AnonymousTaskRunFactory.create()
# leaderboard()
# res = format_anon_week()
# assert len(res['labels']) == 1
# day = datetime.utcnow().strftime('%Y-%m-%d')
# assert res['labels'][0] == day
# assert len(res['series']) == 1
# assert res['series'][0][0] == 1, res['series'][0][0]

#@with_context
#@patch('pybossa.leaderboard.data.db')
#def test_format_anon_week_empty(self, db_mock):
# """Test format anon week empty works."""
# db_mock.slave_session.execute.return_value = []
# TaskRunFactory.create()
# leaderboard()
# res = format_anon_week()
# assert len(res['labels']) == 1
# day = datetime.utcnow().strftime('%Y-%m-%d')
# assert res['labels'][0] == day
# assert len(res['series']) == 1
# assert res['series'][0][0] == 0, res['series'][0][0]
@with_context
def test_leaderboard_foo_dash_key_current_user_window(self):
"""Test JOB leaderboard returns users for foo-dash key with current user and
window."""
UserFactory.create_batch(10, info={'foo-dash': 0})
UserFactory.create_batch(10, info={'foo-dash': 2})
UserFactory.create_batch(10, info={'foo-dash': 2}, restrict=True)
users = []
for score in range(11, 22):
users.append(UserFactory.create(info={'foo-dash': score}))
myself = UserFactory.create(info={'foo-dash': 1})

leaderboard(info='foo-dash')

top_users = get_leaderboard(user_id=myself.id, info='foo-dash', window=5)

assert len(top_users) == 20 + 5 + 1 + 5, len(top_users)
assert top_users[25]['name'] == myself.name
assert top_users[25]['score'] == myself.info.get('foo-dash')
assert top_users[24]['score'] >= myself.info.get('foo-dash')
assert top_users[26]['score'] <= myself.info.get('foo-dash')

results = db.session.execute('select * from "users_rank_foo-dash"');
for r in results:
assert r.restrict is False, r

0 comments on commit f1afc65

Please sign in to comment.