From ebe034a527dccc02aca55a9bfa3d09261ecaee51 Mon Sep 17 00:00:00 2001 From: Untone Date: Sat, 2 Dec 2023 23:38:28 +0300 Subject: [PATCH] stat-fix-5 --- resolvers/author.py | 28 +++++++++++----------------- resolvers/reaction.py | 29 ++++++++++------------------- resolvers/reader.py | 15 ++------------- resolvers/topic.py | 2 -- 4 files changed, 23 insertions(+), 51 deletions(-) diff --git a/resolvers/author.py b/resolvers/author.py index f99e41ee..1bd255db 100644 --- a/resolvers/author.py +++ b/resolvers/author.py @@ -18,7 +18,6 @@ from resolvers.reaction import reacted_shouts_updates as followed_reactions def add_author_stat_columns(q): - shout_author_aliased = aliased(ShoutAuthor) q = q.outerjoin(shout_author_aliased).add_columns( func.count(distinct(shout_author_aliased.shout)).label("shouts_stat") @@ -36,22 +35,17 @@ def add_author_stat_columns(q): rating_aliased = aliased(Reaction) # q = q.add_columns(literal(0).label("rating_stat")) - q = ( - q.outerjoin(rating_aliased, rating_aliased.shout == shout_author_aliased.shout) - .add_columns( - func.coalesce(func.sum(case( - (and_( - rating_aliased.kind == ReactionKind.LIKE.value, - rating_aliased.reply_to.is_(None) - ), 1), - (and_( - rating_aliased.kind == ReactionKind.DISLIKE.value, - rating_aliased.reply_to.is_(None) - ), -1), - else_=0 - )), 0) - .label("rating_stat") - ) + q = q.outerjoin(rating_aliased, rating_aliased.shout == shout_author_aliased.shout).add_columns( + func.coalesce( + func.sum( + case( + (and_(rating_aliased.kind == ReactionKind.LIKE.value, rating_aliased.reply_to.is_(None)), 1), + (and_(rating_aliased.kind == ReactionKind.DISLIKE.value, rating_aliased.reply_to.is_(None)), -1), + else_=0, + ) + ), + 0, + ).label("rating_stat") ) q = q.add_columns(literal(0).label("commented_stat")) diff --git a/resolvers/reaction.py b/resolvers/reaction.py index 53ec8c81..5c8e28e1 100644 --- a/resolvers/reaction.py +++ b/resolvers/reaction.py @@ -15,12 +15,9 @@ from orm.author import Author def add_reaction_stat_columns(q): aliased_reaction = aliased(Reaction) - q = ( - q.outerjoin(aliased_reaction, Reaction.id == aliased_reaction.reply_to).add_columns( - func.sum(aliased_reaction.id) - .label("reacted_stat"), - func.sum(case((aliased_reaction.kind == ReactionKind.COMMENT.value, 1), else_=0)) - .label("commented_stat"), + q = q.outerjoin(aliased_reaction, Reaction.id == aliased_reaction.reply_to).add_columns( + func.sum(aliased_reaction.id).label("reacted_stat"), + func.sum(case((aliased_reaction.kind == ReactionKind.COMMENT.value, 1), else_=0)).label("commented_stat"), func.sum( case( (aliased_reaction.kind == ReactionKind.AGREE.value, 1), @@ -33,9 +30,8 @@ def add_reaction_stat_columns(q): (aliased_reaction.kind == ReactionKind.DISLIKE.value, -1), else_=0, ) - ) - .label("rating_stat"), - )) + ).label("rating_stat"), + ) return q, aliased_reaction @@ -187,7 +183,9 @@ async def create_reaction(_, info, reaction): return {"error": "You can't vote twice"} opposite_reaction_kind = ( - ReactionKind.DISLIKE.value if reaction["kind"] == ReactionKind.LIKE.value else ReactionKind.LIKE.value + ReactionKind.DISLIKE.value + if reaction["kind"] == ReactionKind.LIKE.value + else ReactionKind.LIKE.value ) opposite_reaction = ( session.query(Reaction) @@ -405,11 +403,7 @@ async def load_reactions_by(_, info, by, limit=50, offset=0): ] in result_rows: reaction.created_by = author reaction.shout = shout - reaction.stat = { - "rating": rating_stat, - "commented": commented_stat, - "reacted": reacted_stat - } + reaction.stat = {"rating": rating_stat, "commented": commented_stat, "reacted": reacted_stat} reactions.append(reaction) # sort if by stat is present @@ -429,10 +423,7 @@ def reacted_shouts_updates(follower_id: int, limit=50, offset=0) -> List[Shout]: .join(Reaction) .filter(Reaction.created_by == follower_id) .filter(Reaction.created_at > author.last_seen) - .options( - joinedload(Reaction.created_by), - joinedload(Reaction.shout) - ) + .options(joinedload(Reaction.created_by), joinedload(Reaction.shout)) .limit(limit) .offset(offset) .all() diff --git a/resolvers/reader.py b/resolvers/reader.py index 7cb193d4..e25730c1 100644 --- a/resolvers/reader.py +++ b/resolvers/reader.py @@ -17,12 +17,7 @@ def add_stat_columns(q): aliased_reaction = aliased(Reaction) q = q.outerjoin(aliased_reaction).add_columns( func.sum(aliased_reaction.id).label("reacted_stat"), - func.sum( - case( - (aliased_reaction.kind == ReactionKind.COMMENT.value, 1), - else_=0 - ) - ).label("commented_stat"), + func.sum(case((aliased_reaction.kind == ReactionKind.COMMENT.value, 1), else_=0)).label("commented_stat"), func.sum( case( (aliased_reaction.kind == ReactionKind.AGREE.value, 1), @@ -47,7 +42,6 @@ def add_stat_columns(q): return q - def apply_filters(q, filters, author_id=None): # LoadShoutsFilters handling if filters.get("reacted") and author_id: @@ -175,12 +169,7 @@ async def load_shouts_by(_, info, options): shouts = [] with local_session() as session: - for [ - shout, - reacted_stat, - commented_stat, - rating_stat, - ] in session.execute(q).unique(): + for [shout, reacted_stat, commented_stat, rating_stat, _last_comment] in session.execute(q).unique(): shout.stat = { "viewed": ViewedStorage.get_shout(shout.slug), "reacted": reacted_stat, diff --git a/resolvers/topic.py b/resolvers/topic.py index 4391db60..8adc40a4 100644 --- a/resolvers/topic.py +++ b/resolvers/topic.py @@ -24,10 +24,8 @@ def add_topic_stat_columns(q): q = ( q.outerjoin(ShoutTopic, Topic.id == ShoutTopic.topic) .add_columns(func.count(distinct(ShoutTopic.shout)).label("shouts_stat")) - .outerjoin(aliased_shout_author, ShoutTopic.shout == aliased_shout_author.shout) .add_columns(func.count(distinct(aliased_shout_author.author)).label("authors_stat")) - .outerjoin(aliased_topic_follower) .add_columns(func.count(distinct(aliased_topic_follower.follower)).label("followers_stat")) )