add average upvotes leaderboards

pull/199/head
Aevann 2023-09-15 17:26:20 +03:00
parent 8f4f0b8cb3
commit c7d09a5c0a
2 changed files with 29 additions and 4 deletions

View File

@ -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])

View File

@ -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")