groupby-fix
All checks were successful
Deploy to core / deploy (push) Successful in 1m23s

This commit is contained in:
Untone 2024-02-23 00:03:12 +03:00
parent 54f7dd9c1f
commit b0e2551e9b
4 changed files with 9 additions and 10 deletions

View File

@ -98,7 +98,7 @@ def count_author_shouts_rating(session, author_id) -> int:
async def load_author_with_stats(q):
q = add_author_stat_columns(q, author_model=Author)
q = add_author_stat_columns(q)
result = await get_authors_from_query(q)
@ -176,7 +176,7 @@ async def get_author_id(_, _info, user: str):
@query.field('load_authors_by')
async def load_authors_by(_, _info, by, limit, offset):
q = select(Author)
q = add_author_stat_columns(q, author_model=Author)
q = add_author_stat_columns(q)
if by.get('slug'):
q = q.filter(Author.slug.ilike(f"%{by['slug']}%"))
elif by.get('name'):
@ -273,7 +273,7 @@ async def create_author(user_id: str, slug: str, name: str = ''):
@query.field('get_author_followers')
async def get_author_followers(_, _info, slug) -> List[Author]:
q = select(Author)
q = add_author_stat_columns(q, author_model=Author)
q = add_author_stat_columns(q)
aliased_author = aliased(Author)
q = (

View File

@ -110,7 +110,7 @@ def query_follows(user_id: str):
.filter(TopicFollower.topic == Topic.id)
)
authors_query = add_author_stat_columns(authors_query, author_model=aliased_author)
authors_query = add_author_stat_columns(authors_query)
topics_query = add_topic_stat_columns(topics_query)
authors = [
{

View File

@ -8,8 +8,7 @@ from services.db import local_session
# from services.viewed import ViewedStorage
def add_author_stat_columns(q, author_model=None):
aliased_author = author_model or aliased(Author)
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')
@ -17,15 +16,15 @@ def add_author_stat_columns(q, author_model=None):
authors_table = aliased(AuthorFollower)
q = q.outerjoin(
authors_table, authors_table.follower == aliased_author.id
authors_table, authors_table.follower == Author.id
).add_columns(func.count(distinct(authors_table.author)).label('authors_stat'))
followers_table = aliased(AuthorFollower)
q = q.outerjoin(followers_table, followers_table.author == aliased_author.id).add_columns(
q = q.outerjoin(followers_table, followers_table.author == Author.id).add_columns(
func.count(distinct(followers_table.follower)).label('followers_stat')
)
q = q.group_by(aliased_author.id)
q = q.group_by(Author.id)
return q

View File

@ -77,7 +77,7 @@ async def update_follows_for_user(
async def handle_author_follower_change(connection, author_id, follower_id, is_insert):
q = select(Author).filter(Author.id == author_id)
q = add_author_stat_columns(q, author_model=Author)
q = add_author_stat_columns(q)
async with connection.begin() as conn:
[author, shouts_stat, followers_stat, followings_stat] = await conn.execute(
q