recent shouts resolve

This commit is contained in:
Untone 2021-10-30 22:15:29 +03:00
parent fb01b2cc78
commit b02f23e82b
3 changed files with 21 additions and 12 deletions

View File

@ -1,6 +1,6 @@
from resolvers.auth import login, sign_out, is_email_free, register, confirm from resolvers.auth import login, sign_out, is_email_free, register, confirm
from resolvers.inbox import create_message, delete_message, update_message, get_messages from resolvers.inbox import create_message, delete_message, update_message, get_messages
from resolvers.zine import create_shout, get_shout_by_slug from resolvers.zine import create_shout, get_shout_by_slug, recent_shouts, top_authors, top_shouts_by_rating, top_shouts_by_view
from resolvers.profile import get_user_by_slug, get_current_user from resolvers.profile import get_user_by_slug, get_current_user
from resolvers.topics import topic_subscribe, topic_unsubscribe, topics_by_author, topics_by_community, topics_by_slugs from resolvers.topics import topic_subscribe, topic_unsubscribe, topics_by_author, topics_by_community, topics_by_slugs
@ -19,6 +19,10 @@ __all__ = [
"get_current_user", "get_current_user",
"get_user_by_slug", "get_user_by_slug",
"get_shout_by_slug", "get_shout_by_slug",
"recent_shouts",
"favorite_shouts",
"top_shouts_by_views",
"top_shouts_by_rating",
"topics_by_slugs", "topics_by_slugs",
"topics_by_community", "topics_by_community",
"topics_by_author", "topics_by_author",

View File

@ -105,7 +105,7 @@ class TopShouts:
).\ ).\
join(ShoutViewByDay).\ join(ShoutViewByDay).\
join(ShoutRating).\ join(ShoutRating).\
where(Shouts.createdAt > month_ago).\ where(Shout.createdAt > month_ago).\
group_by(Shout.id).\ group_by(Shout.id).\
order_by(desc("createdAt")).\ order_by(desc("createdAt")).\
limit(TopShouts.limit) limit(TopShouts.limit)
@ -116,11 +116,11 @@ class TopShouts:
shout.rating = row.rating shout.rating = row.rating
shouts.append(shout) shouts.append(shout)
async with TopShouts.lock: async with TopShouts.lock:
TopShouts.shouts_by_view = shouts TopShouts.recent_shouts = shouts
@staticmethod @staticmethod
async def prepare_favorites_shouts(): async def prepare_favorite_shouts():
with local_session() as session: with local_session() as session:
stmt = select(Shout, func.sum(ShoutRating.value).label("rating")).\ stmt = select(Shout, func.sum(ShoutRating.value).label("rating")).\
join(ShoutRating).\ join(ShoutRating).\
@ -133,7 +133,7 @@ class TopShouts:
shout.rating = row.rating shout.rating = row.rating
shouts.append(shout) shouts.append(shout)
async with TopShouts.lock: async with TopShouts.lock:
TopShouts.favorites_shouts = shouts TopShouts.favorite_shouts = shouts
@staticmethod @staticmethod
async def prepare_shouts_by_view(): async def prepare_shouts_by_view():
@ -182,10 +182,10 @@ class TopShouts:
while True: while True:
try: try:
print("top shouts: update cache") print("top shouts: update cache")
await TopShouts.prepare_favorites_shouts() await TopShouts.prepare_favorite_shouts()
await TopShouts.prepare_recent_shouts()
await TopShouts.prepare_shouts_by_rating() await TopShouts.prepare_shouts_by_rating()
await TopShouts.prepare_shouts_by_view() await TopShouts.prepare_shouts_by_view()
await TopShouts.prepare_recent_shouts()
await TopShouts.prepare_top_authors() await TopShouts.prepare_top_authors()
print("top shouts: update finished") print("top shouts: update finished")
except Exception as err: except Exception as err:
@ -205,10 +205,15 @@ async def top_shouts_by_rating(_, info, limit):
return TopShouts.shouts_by_rating[:limit] return TopShouts.shouts_by_rating[:limit]
@query.field("favoritesShouts") @query.field("favoriteShouts")
async def favorites_shouts(_, info, limit): async def favorite_shouts(_, info, limit):
async with TopShouts.lock: async with TopShouts.lock:
return TopShouts.favorites_shouts[:limit] return TopShouts.favorite_shouts[:limit]
@query.field("recentShouts")
async def recent_shouts(_, info, limit):
async with TopShouts.lock:
return TopShouts.recent_shouts[:limit]
@query.field("topAuthors") @query.field("topAuthors")

View File

@ -100,7 +100,6 @@ type Query {
# shouts # shouts
getShoutBySlug(slug: String!): Shout! getShoutBySlug(slug: String!): Shout!
# NOTE: with .comments: Comments[] # NOTE: with .comments: Comments[]
recentShouts(limit: Int): [Shout]!
shoutsByTopic(topic: String!, limit: Int!): [Shout]! shoutsByTopic(topic: String!, limit: Int!): [Shout]!
shoutsByAuthor(author: String!, limit: Int!): [Shout]! shoutsByAuthor(author: String!, limit: Int!): [Shout]!
shoutsByCommunity(community: String!, limit: Int!): [Shout]! shoutsByCommunity(community: String!, limit: Int!): [Shout]!
@ -108,7 +107,8 @@ type Query {
# mainpage # mainpage
topShoutsByView(limit: Int): [Shout]! topShoutsByView(limit: Int): [Shout]!
topShoutsByRating(limit: Int): [Shout]! topShoutsByRating(limit: Int): [Shout]!
favoritesShouts(limit: Int): [Shout]! favoriteShouts(limit: Int): [Shout]!
recentShouts(limit: Int): [Shout]!
topAuthors(limit: Int): [User]! topAuthors(limit: Int): [User]!
# topics # topics