stats-follows
All checks were successful
Deploy to core / deploy (push) Successful in 3m20s

This commit is contained in:
Untone 2024-02-21 19:48:33 +03:00
parent 1eac614e35
commit 784f790b83
3 changed files with 77 additions and 41 deletions

View File

@ -264,7 +264,11 @@ async def get_author_follows(
) -> List[Author]:
with local_session() as session:
if not user and (author_id or slug):
user = session.query(Author.user).where(or_(Author.id == author_id, Author.slug == slug)).first()
user = (
session.query(Author.user)
.where(or_(Author.id == author_id, Author.slug == slug))
.first()
)
if user:
follows = await get_follows_by_user_id(user)
return follows

View File

@ -98,7 +98,13 @@ def query_follows(user_id: str):
if isinstance(author, Author):
author_id = author.id
authors_query = (
select(column('name'), column('id'), column('slug'), column('pic'), column('bio'))
select(
column('name'),
column('id'),
column('slug'),
column('pic'),
column('bio'),
)
.select_from(Author)
.join(AuthorFollower, AuthorFollower.follower == author_id)
.filter(AuthorFollower.author == Author.id)
@ -114,29 +120,45 @@ def query_follows(user_id: str):
topics_query = add_topic_stat_columns(topics_query)
# Convert query results to lists of dictionaries
authors = [{
'id': author.id,
'name': author.name,
'slug': author.slug,
'pic': author.pic,
'bio': author.bio,
'stat': {
'shouts': shouts_stat,
'followers': followers_stat,
'followings': followings_stat,
}
} for [author, shouts_stat, followers_stat, followings_stat] in session.execute(authors_query)]
topics = [{
'id': topic.id,
'title': topic.title,
'slug': topic.slug,
'body': topic.body,
'stat': {
'shouts': shouts_stat,
'authors': authors_stat,
'followers': followers_stat,
}
} for [topic, shouts_stat, authors_stat, followers_stat] in session.execute(topics_query)]
authors = [
{
'id': author.id,
'name': author.name,
'slug': author.slug,
'pic': author.pic,
'bio': author.bio,
'stat': {
'shouts': shouts_stat,
'followers': followers_stat,
'followings': followings_stat,
},
}
for [
author,
shouts_stat,
followers_stat,
followings_stat,
] in session.execute(authors_query)
]
topics = [
{
'id': topic.id,
'title': topic.title,
'slug': topic.slug,
'body': topic.body,
'stat': {
'shouts': shouts_stat,
'authors': authors_stat,
'followers': followers_stat,
},
}
for [
topic,
shouts_stat,
authors_stat,
followers_stat,
] in session.execute(topics_query)
]
# shouts_query = (
# session.query(Shout)
# .join(ShoutReactionsFollower, ShoutReactionsFollower.follower == author_id)

View File

@ -103,14 +103,18 @@ async def handle_author_follower_change(connection, author_id, follower_id, is_i
).first()
if follower and author:
await update_follows_for_user(
connection, follower.user, 'author', {
"id": author.id,
"name": author.name,
"slug": author.slug,
"pic": author.pic,
"bio": author.bio,
"stat": author.stat
}, is_insert
connection,
follower.user,
'author',
{
'id': author.id,
'name': author.name,
'slug': author.slug,
'pic': author.pic,
'bio': author.bio,
'stat': author.stat,
},
is_insert,
)
@ -132,17 +136,23 @@ async def handle_topic_follower_change(connection, topic_id, follower_id, is_ins
).first()
if follower and topic:
await update_follows_for_user(
connection, follower.user, 'topic', {
"id": topic.id,
"title": topic.title,
"slug": topic.slug,
"body": topic.body,
"stat": topic.stat
}, is_insert
connection,
follower.user,
'topic',
{
'id': topic.id,
'title': topic.title,
'slug': topic.slug,
'body': topic.body,
'stat': topic.stat,
},
is_insert,
)
BATCH_SIZE = 33
class FollowsCached:
lock = asyncio.Lock()
@ -175,7 +185,7 @@ class FollowsCached:
'slug': author.slug,
'pic': author.pic,
'bio': author.bio,
'stat': author.stat
'stat': author.stat,
}
),
)