From 04aaaab609ac6bf92ffb9df505a4c677d2d658c2 Mon Sep 17 00:00:00 2001 From: tonyrewin Date: Wed, 17 Aug 2022 10:59:17 +0300 Subject: [PATCH] counters fixes --- orm/reaction.py | 7 +++---- orm/shout.py | 9 +++++---- queries/shout-by-slug.sql | 4 ---- resolvers/zine.py | 39 ++++++++++++++++++++++----------------- schema.graphql | 2 ++ 5 files changed, 32 insertions(+), 29 deletions(-) delete mode 100644 queries/shout-by-slug.sql diff --git a/orm/reaction.py b/orm/reaction.py index ba62abc7..b9f7cc57 100644 --- a/orm/reaction.py +++ b/orm/reaction.py @@ -22,10 +22,9 @@ class Reaction(Base): @property async def stat(self): - rrr = await ReactedStorage.get_reaction(self.id) - print(rrr[0]) return { "viewed": await ViewedStorage.get_reaction(self.id), - "reacted": len(rrr), - "rating": await ReactedStorage.get_reaction_rating(self.id) + "reacted": len(await ReactedStorage.get_reaction(self.id)), + "rating": await ReactedStorage.get_reaction_rating(self.id), + "commented": len(await ReactedStorage.get_reaction_comments(self.id)) } diff --git a/orm/shout.py b/orm/shout.py index 77f74071..b07922dd 100644 --- a/orm/shout.py +++ b/orm/shout.py @@ -63,9 +63,10 @@ class Shout(Base): @property async def stat(self): - rrr = await ReactedStorage.get_shout(self.slug) return { - "viewed": await ViewedStorage.get_shout(self.slug), - "reacted": len(rrr), - "rating": await ReactedStorage.get_rating(self.slug) + "viewed": await ViewedStorage.get_shout(self.slug), + "reacted": len(await ReactedStorage.get_shout(self.slug)), + "commented": len(await ReactedStorage.get_comments(self.slug)), + "rating": await ReactedStorage.get_rating(self.slug), + "bookmarked": len(await ReactedStorage.get_bookmarked(self.slug)) } diff --git a/queries/shout-by-slug.sql b/queries/shout-by-slug.sql deleted file mode 100644 index 4c274abe..00000000 --- a/queries/shout-by-slug.sql +++ /dev/null @@ -1,4 +0,0 @@ -SELECT s.*, a.*, sa.* FROM shout s -JOIN shout_author sa ON s.slug = sa.shout -JOIN user a ON a.slug = sa.user -WHERE sa.slug = a.slug AND a.slug = %s; \ No newline at end of file diff --git a/resolvers/zine.py b/resolvers/zine.py index 7471fde8..3cb772f8 100644 --- a/resolvers/zine.py +++ b/resolvers/zine.py @@ -123,25 +123,30 @@ async def shouts_by_authors(_, info, slugs, page, size): a.caption = await ShoutAuthorStorage.get_author_caption(s.slug, a.slug) return shouts +SINGLE_COMMUNITY = True + @query.field("shoutsByCommunities") async def shouts_by_communities(_, info, slugs, page, size): - page = page - 1 - with local_session() as session: - #TODO fix postgres high load - shouts = session.query(Shout).distinct().\ - join(ShoutTopic).\ - where(and_(Shout.publishedAt != None,\ - ShoutTopic.topic.in_(\ - select(Topic.slug).where(Topic.community.in_(slugs))\ - ))).\ - order_by(desc(Shout.publishedAt)).\ - limit(size).\ - offset(page * size) - - for s in shouts: - for a in s.authors: - a.caption = await ShoutAuthorStorage.get_author_caption(s.slug, a.slug) - return shouts + if SINGLE_COMMUNITY: + return recent_published(_, info, page, size) + else: + page = page - 1 + with local_session() as session: + #TODO fix postgres high load + shouts = session.query(Shout).distinct().\ + join(ShoutTopic).\ + where(and_(Shout.publishedAt != None,\ + ShoutTopic.topic.in_(\ + select(Topic.slug).where(Topic.community.in_(slugs))\ + ))).\ + order_by(desc(Shout.publishedAt)).\ + limit(size).\ + offset(page * size) + + for s in shouts: + for a in s.authors: + a.caption = await ShoutAuthorStorage.get_author_caption(s.slug, a.slug) + return shouts @mutation.field("follow") @login_required diff --git a/schema.graphql b/schema.graphql index c55468fc..dd3a1121 100644 --- a/schema.graphql +++ b/schema.graphql @@ -412,6 +412,7 @@ type Stat { viewed: Int reacted: Int rating: Int + commented: Int bookmarked: Int } @@ -440,6 +441,7 @@ type TopicStat { authors: Int! viewed: Int! reacted: Int! + commented: Int rating: Int }