This commit is contained in:
2025-08-01 05:17:01 +03:00
parent 8c363a6615
commit 21d65e134f
4 changed files with 22 additions and 23 deletions

View File

@@ -358,13 +358,13 @@ class CommunityStats:
@property @property
def shouts(self) -> int: def shouts(self) -> int:
return self.community.session.query(func.count(Shout.id)).where(Shout.community == self.community.id).scalar() return self.community.session.query(func.count(Shout.id)).filter(Shout.community == self.community.id).scalar()
@property @property
def followers(self) -> int: def followers(self) -> int:
return ( return (
self.community.session.query(func.count(CommunityFollower.follower)) self.community.session.query(func.count(CommunityFollower.follower))
.where(CommunityFollower.community == self.community.id) .filter(CommunityFollower.community == self.community.id)
.scalar() .scalar()
) )
@@ -374,7 +374,7 @@ class CommunityStats:
return ( return (
self.community.session.query(func.count(distinct(Author.id))) self.community.session.query(func.count(distinct(Author.id)))
.join(Shout) .join(Shout)
.where( .filter(
Shout.community == self.community.id, Shout.community == self.community.id,
Shout.featured_at.is_not(None), Shout.featured_at.is_not(None),
Author.id.in_(Shout.authors), Author.id.in_(Shout.authors),

View File

@@ -63,7 +63,7 @@ def add_reaction_stat_columns(q: Select) -> Select:
).add_columns( ).add_columns(
# Count unique comments # Count unique comments
func.coalesce( func.coalesce(
func.count(aliased_reaction.id).where(aliased_reaction.kind == ReactionKind.COMMENT.value), 0 func.count(case((aliased_reaction.kind == ReactionKind.COMMENT.value, aliased_reaction.id), else_=None)), 0
).label("comments_stat"), ).label("comments_stat"),
# Calculate rating as the difference between likes and dislikes # Calculate rating as the difference between likes and dislikes
func.sum( func.sum(

View File

@@ -157,23 +157,22 @@ def query_with_stat(info: GraphQLResolveInfo) -> Select:
stats_subquery = ( stats_subquery = (
select( select(
Reaction.shout, Reaction.shout,
func.count(func.distinct(Reaction.id)) func.count(case((Reaction.kind == ReactionKind.COMMENT.value, Reaction.id), else_=None)).label(
.where(Reaction.kind == ReactionKind.COMMENT.value) "comments_count"
.label("comments_count"), ),
func.sum( func.sum(
case( case(
(Reaction.kind == ReactionKind.LIKE.value, 1), (Reaction.kind == ReactionKind.LIKE.value, 1),
(Reaction.kind == ReactionKind.DISLIKE.value, -1), (Reaction.kind == ReactionKind.DISLIKE.value, -1),
else_=0, else_=0,
) )
) ).label("rating"),
.where(Reaction.reply_to.is_(None)) func.max(case((Reaction.kind == ReactionKind.COMMENT.value, Reaction.created_at), else_=None)).label(
.label("rating"), "last_commented_at"
func.max(Reaction.created_at) ),
.where(Reaction.kind == ReactionKind.COMMENT.value)
.label("last_commented_at"),
) )
.where(Reaction.deleted_at.is_(None)) .where(Reaction.deleted_at.is_(None))
.where(Reaction.reply_to.is_(None))
.group_by(Reaction.shout) .group_by(Reaction.shout)
.subquery() .subquery()
) )

View File

@@ -147,7 +147,7 @@ def get_topic_followers_stat(topic_id: int) -> int:
:return: Количество уникальных подписчиков темы. :return: Количество уникальных подписчиков темы.
""" """
aliased_followers = aliased(TopicFollower) aliased_followers = aliased(TopicFollower)
q = select(func.count(distinct(aliased_followers.follower))).where(aliased_followers.topic == topic_id) q = select(func.count(distinct(aliased_followers.follower))).filter(aliased_followers.topic == topic_id)
with local_session() as session: with local_session() as session:
result = session.execute(q).scalar() result = session.execute(q).scalar()
return int(result) if result else 0 return int(result) if result else 0
@@ -240,7 +240,7 @@ def get_author_followers_stat(author_id: int) -> int:
""" """
Получает количество подписчиков для указанного автора Получает количество подписчиков для указанного автора
""" """
q = select(func.count(AuthorFollower.follower)).where(AuthorFollower.author == author_id) q = select(func.count(AuthorFollower.follower)).filter(AuthorFollower.author == author_id)
with local_session() as session: with local_session() as session:
result = session.execute(q).scalar() result = session.execute(q).scalar()
@@ -388,19 +388,19 @@ def get_followers_count(entity_type: str, entity_id: int) -> int:
with local_session() as session: with local_session() as session:
if entity_type == "topic": if entity_type == "topic":
result = ( result = (
session.query(func.count(TopicFollower.follower)).where(TopicFollower.topic == entity_id).scalar() session.query(func.count(TopicFollower.follower)).filter(TopicFollower.topic == entity_id).scalar()
) )
elif entity_type == "author": elif entity_type == "author":
# Count followers of this author # Count followers of this author
result = ( result = (
session.query(func.count(AuthorFollower.follower)) session.query(func.count(AuthorFollower.follower))
.where(AuthorFollower.author == entity_id) .filter(AuthorFollower.author == entity_id)
.scalar() .scalar()
) )
elif entity_type == "community": elif entity_type == "community":
result = ( result = (
session.query(func.count(CommunityFollower.follower)) session.query(func.count(CommunityFollower.follower))
.where(CommunityFollower.community == entity_id) .filter(CommunityFollower.community == entity_id)
.scalar() .scalar()
) )
else: else:
@@ -419,12 +419,12 @@ def get_following_count(entity_type: str, entity_id: int) -> int:
if entity_type == "author": if entity_type == "author":
# Count what this author follows # Count what this author follows
topic_follows = ( topic_follows = (
session.query(func.count(TopicFollower.topic)).where(TopicFollower.follower == entity_id).scalar() session.query(func.count(TopicFollower.topic)).filter(TopicFollower.follower == entity_id).scalar()
or 0 or 0
) )
community_follows = ( community_follows = (
session.query(func.count(CommunityFollower.community)) session.query(func.count(CommunityFollower.community))
.where(CommunityFollower.follower == entity_id) .filter(CommunityFollower.follower == entity_id)
.scalar() .scalar()
or 0 or 0
) )
@@ -441,7 +441,7 @@ def get_shouts_count(
"""Получает количество публикаций""" """Получает количество публикаций"""
try: try:
with local_session() as session: with local_session() as session:
query = session.query(func.count(Shout.id)).where(Shout.published_at.isnot(None)) query = session.query(func.count(Shout.id)).filter(Shout.published_at.isnot(None))
if author_id: if author_id:
query = query.where(Shout.created_by == author_id) query = query.where(Shout.created_by == author_id)
@@ -466,12 +466,12 @@ def get_authors_count(community_id: Optional[int] = None) -> int:
# Count authors in specific community # Count authors in specific community
result = ( result = (
session.query(func.count(distinct(CommunityFollower.follower))) session.query(func.count(distinct(CommunityFollower.follower)))
.where(CommunityFollower.community == community_id) .filter(CommunityFollower.community == community_id)
.scalar() .scalar()
) )
else: else:
# Count all authors # Count all authors
result = session.query(func.count(Author.id)).where(Author.deleted_at.is_(None)).scalar() result = session.query(func.count(Author.id)).filter(Author.deleted_at.is_(None)).scalar()
return int(result) if result else 0 return int(result) if result else 0
except Exception as e: except Exception as e: