From 5b34cab6bc7f4da17b138f6e062c149a2f049e91 Mon Sep 17 00:00:00 2001 From: Untone Date: Fri, 23 Feb 2024 14:40:38 +0300 Subject: [PATCH] add-columns-stat --- resolvers/stat.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/resolvers/stat.py b/resolvers/stat.py index fab00be4..c4db4967 100644 --- a/resolvers/stat.py +++ b/resolvers/stat.py @@ -13,11 +13,11 @@ def add_topic_stat_columns(q): aliased_topic = aliased(Topic) q = ( q.outerjoin(ShoutTopic, aliased_topic.id == ShoutTopic.topic) - .with_entities(func.count(distinct(ShoutTopic.shout)).label('shouts_stat')) + .add_columns(func.count(distinct(ShoutTopic.shout)).label('shouts_stat')) .outerjoin(aliased_shout_authors, ShoutTopic.shout == aliased_shout_authors.shout) - .with_entities(func.count(distinct(aliased_shout_authors.author)).label('authors_stat')) + .add_columns(func.count(distinct(aliased_shout_authors.author)).label('authors_stat')) .outerjoin(aliased_topic_followers, aliased_topic.id == aliased_topic_followers.topic) - .with_entities(func.count(distinct(aliased_topic_followers.follower)).label('followers_stat')) + .add_columns(func.count(distinct(aliased_topic_followers.follower)).label('followers_stat')) ) q = q.group_by(aliased_topic.id) @@ -27,15 +27,15 @@ def add_topic_stat_columns(q): def add_author_stat_columns(q): aliased_author_authors = aliased(AuthorFollower, name='af_authors') - aliased_author_followers = aliased(AuthorFollower, name='af_followers') + aliased_author_followers = aliased(AuthorFollower, name='af_followers') # Добавлен второй псевдоним aliased_author = aliased(Author) q = ( q.outerjoin(ShoutAuthor, aliased_author.id == ShoutAuthor.author) - .with_entities(func.count(distinct(ShoutAuthor.shout)).label('shouts_stat')) + .add_columns(func.count(distinct(ShoutAuthor.shout)).label('shouts_stat')) .outerjoin(aliased_author_authors, AuthorFollower.follower == aliased_author.id) - .with_entities(func.count(distinct(aliased_author_authors.author)).label('authors_stat')) - .outerjoin(aliased_author_followers, AuthorFollower.author == aliased_author.id) - .with_entities(func.count(distinct(aliased_author_followers.follower)).label('followers_stat')) + .add_columns(func.count(distinct(aliased_author_authors.author)).label('authors_stat')) + .outerjoin(aliased_author_followers, AuthorFollower.author == aliased_author.id) # Используется второй псевдоним + .add_columns(func.count(distinct(aliased_author_followers.follower)).label('followers_stat')) # Используется второй псевдоним ) q = q.group_by(aliased_author.id) @@ -46,13 +46,13 @@ def add_author_stat_columns(q): def execute_with_ministat(q): records = [] with local_session() as session: - for [r, shouts_stat, authors_stat, followers_stat] in session.execute(q): - r.stat = { + for [entity, shouts_stat, authors_stat, followers_stat] in session.execute(q): + entity.stat = { 'shouts': shouts_stat, 'authors': authors_stat, 'followers': followers_stat } - records.append(r) + records.append(entity) return records