Skip to content

Commit

Permalink
Improve thanks count calculation
Browse files Browse the repository at this point in the history
Leaderboard and non-unique message thanks totals now de-dupe on the
tuple of user_id, source_user_id, and message_id.

This eliminates an edge case that could inflate thanks numbers.
  • Loading branch information
robwaz committed Oct 18, 2024
1 parent fbee720 commit c50f759
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
12 changes: 6 additions & 6 deletions dojo_plugin/api/v1/discord.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,11 @@ def year_stamp():
except:
return {"success": False, "error": "invalid start format"}, 400

thanks_scores = DiscordUserActivity.query.with_entities(DiscordUserActivity.user_id, db.func.count(DiscordUserActivity.message_id)
).filter(and_(DiscordUserActivity.message_timestamp >= start),
DiscordUserActivity.type == "thanks"
).group_by(DiscordUserActivity.user_id
).order_by(db.func.count(DiscordUserActivity.user_id).desc())[:100]
sq = DiscordUserActivity.query.where(
DiscordUserActivity.type == 'thanks').where(
DiscordUserActivity.message_timestamp >= start).with_entities(
DiscordUserActivity.user_id, DiscordUserActivity.source_user_id, DiscordUserActivity.message_id).distinct().subquery()
thanks_scores = db.session.execute(db.select(sq.c.user_id, db.func.count(sq.c.user_id)).select_from(sq).group_by(sq.c.user_id).order_by(db.func.count(sq.c.user_id).desc())).all()

def get_name(discord_id):
try:
Expand All @@ -189,6 +189,6 @@ def get_name(discord_id):
return response['user']['global_name']

results = [[get_name(discord_id), score] for discord_id, score in thanks_scores]
results = [[name, score] for name, score in results if name is not None][:20]
results = [[name, score] for name, score in results if name is not None][:25]

return {"success": True, "leaderboard": results}, 200
14 changes: 9 additions & 5 deletions dojo_plugin/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -770,15 +770,19 @@ class DiscordUsers(db.Model):
user = db.relationship("Users")

def thanks_count(self, start=None, end=None, unique_messages=False):
query = DiscordUserActivity.query.filter(and_(DiscordUserActivity.user_id == self.discord_id),
distinct_source_user_id = None if unique_messages else DiscordUserActivity.source_user_id

sq = DiscordUserActivity.query.filter(
DiscordUserActivity.type == 'thanks',
DiscordUserActivity.message_timestamp >= start if start else True,
DiscordUserActivity.message_timestamp <= end if end else True,
DiscordUserActivity.type == "thanks")
).with_entities(DiscordUserActivity.user_id, distinct_source_user_id, DiscordUserActivity.message_id
).distinct().subquery()

return db.session.execute(db.select(sq.c.user_id, db.func.count(sq.c.user_id))
.select_from(sq).group_by(sq.c.user_id).order_by(db.func.count(sq.c.user_id).desc())).all()

if unique_messages:
return query.with_entities(db.func.distinct(DiscordUserActivity.message_id)).count()

return query.count()

def meme_count(self, start=None, end=None, weekly=True):
if not weekly:
Expand Down

0 comments on commit c50f759

Please sign in to comment.