From c7d09a5c0a428a79b89495a43d12c9b530adab30 Mon Sep 17 00:00:00 2001 From: Aevann Date: Fri, 15 Sep 2023 17:26:20 +0300 Subject: [PATCH] add average upvotes leaderboards --- files/classes/leaderboard.py | 30 ++++++++++++++++++++++++++---- files/routes/users.py | 3 +++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/files/classes/leaderboard.py b/files/classes/leaderboard.py index fd67ab663..20ae9055f 100644 --- a/files/classes/leaderboard.py +++ b/files/classes/leaderboard.py @@ -56,18 +56,24 @@ class Leaderboard: position = g.db.query(sq.c.id, sq.c.rank).filter(sq.c.id == v.id).limit(1).one()[1] return (leaderboard, position, None) + @classmethod def count_and_label(cls, criteria): return func.count(criteria).label("count") - @classmethod - def rank_filtered_rank_label_by_desc(cls, criteria): - return func.rank().over(order_by=func.count(criteria).desc()).label("rank") - @classmethod def sum_and_label(cls, criteria): return func.sum(criteria).label("sum") + @classmethod + def avg_and_label(cls, criteria1, criteria2): + return (func.sum(criteria1)/func.count(criteria2)).label("avg") + + + @classmethod + def rank_filtered_rank_label_by_desc(cls, criteria): + return func.rank().over(order_by=func.count(criteria).desc()).label("rank") + @classmethod def rank_filtered_rank_label_by_desc_sum(cls, criteria): return func.rank().over(order_by=func.sum(criteria).desc()).label("rank") @@ -76,6 +82,11 @@ class Leaderboard: def rank_filtered_rank_label_by_asc_sum(cls, criteria): return func.rank().over(order_by=func.sum(criteria).asc()).label("rank") + @classmethod + def rank_filtered_rank_label_by_desc_avg(cls, criteria1, criteria2): + return func.rank().over(order_by=-func.sum(criteria1)/func.count(criteria2)).label("rank") + + @classmethod def get_badge_emoji_lb(cls, lb_criteria, v, users, limit, desc): sq = g.db.query(lb_criteria, cls.count_and_label(lb_criteria), cls.rank_filtered_rank_label_by_desc(lb_criteria)) @@ -172,3 +183,14 @@ class Leaderboard: except: pos9 = (len(users9)+1, 0) return (users9_accs, pos9[0], pos9[1]) + + @classmethod + def get_avg_upvotes_lb(cls, lb_criteria, v, users, limit, desc): + sq = g.db.query(lb_criteria.author_id, cls.avg_and_label(lb_criteria.upvotes, lb_criteria.author_id)).filter_by(deleted_utc=0).group_by(lb_criteria.author_id).subquery() + leaderboard = g.db.query(User, sq.c.avg).join(User, User.id == sq.c.author_id).order_by(sq.c.avg.desc()) + + sq = g.db.query(lb_criteria.author_id, cls.avg_and_label(lb_criteria.upvotes, lb_criteria.author_id), cls.rank_filtered_rank_label_by_desc_avg(lb_criteria.upvotes, lb_criteria.author_id)).filter_by(deleted_utc=0).group_by(lb_criteria.author_id).subquery() + position = g.db.query(sq.c.rank, sq.c.avg).join(User, User.id == sq.c.author_id).filter(sq.c.author_id == v.id).limit(1).one_or_none() + if not position: position = (leaderboard.count() + 1, 0) + leaderboard = leaderboard.limit(limit).all() + return (leaderboard, position[0], position[1]) diff --git a/files/routes/users.py b/files/routes/users.py index 91361976b..f4b7c407d 100644 --- a/files/routes/users.py +++ b/files/routes/users.py @@ -513,6 +513,9 @@ def leaderboard_cached(v): leaderboards.append(Leaderboard("Casino winnings (top)", "casino winnings", "casino-winnings-top", "Casino Winnings", None, Leaderboard.get_winnings_lb, CasinoGame.winnings, v, None, None)) leaderboards.append(Leaderboard("Casino winnings (bottom)", "casino winnings", "casino-winnings-bottom", "Casino Winnings", None, Leaderboard.get_winnings_lb, CasinoGame.winnings, v, None, None, 25, False)) + leaderboards.append(Leaderboard("Average upvotes per post", "average upvotes per post", "average-upvotes-per-post", "Average Upvotes", "posts", Leaderboard.get_avg_upvotes_lb, Post, v, None, None)) + leaderboards.append(Leaderboard("Average upvotes per comment", "average upvotes per comment", "average-upvotes-per-comment", "Average Upvotes", "comments", Leaderboard.get_avg_upvotes_lb, Comment, v, None, None)) + return render_template("leaderboard_cached.html", v=v, leaderboards=leaderboards) @app.get("/leaderboard")