top_authors commented out

This commit is contained in:
Tony 2021-12-15 04:23:22 +03:00
parent 4f76f85bed
commit 0503dfaf02
2 changed files with 30 additions and 28 deletions

View File

@ -1,7 +1,7 @@
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, top_month, top_overall, \ from resolvers.zine import create_shout, get_shout_by_slug, top_month, top_overall, \
recent_shouts, top_authors, top_viewed recent_shouts, top_viewed #, top_authors
from resolvers.profile import get_users_by_slugs, get_current_user from resolvers.profile import get_users_by_slugs, get_current_user
from resolvers.topics import topic_subscribe, topic_unsubscribe, topics_by_author, \ from resolvers.topics import topic_subscribe, topic_unsubscribe, topics_by_author, \
topics_by_community, topics_by_slugs topics_by_community, topics_by_slugs

View File

@ -153,27 +153,29 @@ class ShoutsCache:
async with ShoutsCache.lock: async with ShoutsCache.lock:
ShoutsCache.top_viewed = shouts ShoutsCache.top_viewed = shouts
@staticmethod '''
async def prepare_top_authors(): @staticmethod
month_ago = datetime.now() - timedelta(days = 30) async def prepare_top_authors():
with local_session() as session: month_ago = datetime.now() - timedelta(days = 30)
shout_with_view = select(Shout.slug, func.sum(ShoutViewByDay.value).label("view")).\ with local_session() as session:
join(ShoutViewByDay).\ shout_with_view = select(Shout.slug, func.sum(ShoutViewByDay.value).label("view")).\
where(and_(ShoutViewByDay.day > month_ago, Shout.publishedAt != None)).\ join(ShoutViewByDay).\
group_by(Shout.slug).\ where(and_(ShoutViewByDay.day > month_ago, Shout.publishedAt != None)).\
order_by(desc("view")).cte() group_by(Shout.slug).\
stmt = select(ShoutAuthor.user, func.sum(shout_with_view.c.view).label("view")).\ order_by(desc("view")).cte()
join(shout_with_view, ShoutAuthor.shout == shout_with_view.c.slug).\ stmt = select(ShoutAuthor.user, func.sum(shout_with_view.c.view).label("view")).\
group_by(ShoutAuthor.user).\ join(shout_with_view, ShoutAuthor.shout == shout_with_view.c.slug).\
order_by(desc("view")).\ group_by(ShoutAuthor.user).\
limit(ShoutsCache.limit) order_by(desc("view")).\
authors = {} limit(ShoutsCache.limit)
for row in session.execute(stmt): authors = {}
authors[row.user] = row.view for row in session.execute(stmt):
authors_ids = authors.keys() authors[row.user] = row.view
authors = session.query(User).filter(User.id.in_(authors_ids)).all() authors_ids = authors.keys()
async with ShoutsCache.lock: authors = session.query(User).filter(User.id.in_(authors_ids)).all()
ShoutsCache.top_authors = authors async with ShoutsCache.lock:
ShoutsCache.top_authors = authors
'''
@staticmethod @staticmethod
@ -186,7 +188,7 @@ class ShoutsCache:
await ShoutsCache.prepare_top_overall() await ShoutsCache.prepare_top_overall()
await ShoutsCache.prepare_top_viewed() await ShoutsCache.prepare_top_viewed()
await ShoutsCache.prepare_recent_shouts() await ShoutsCache.prepare_recent_shouts()
await ShoutsCache.prepare_top_authors() # await ShoutsCache.prepare_top_authors()
print("shouts cache update finished") print("shouts cache update finished")
except Exception as err: except Exception as err:
print("shouts cache worker error = %s" % (err)) print("shouts cache worker error = %s" % (err))
@ -234,10 +236,10 @@ async def recent_shouts(_, info, limit):
return ShoutsCache.recent_shouts[:limit] return ShoutsCache.recent_shouts[:limit]
@query.field("topAuthors") # @query.field("topAuthors")
async def top_authors(_, info, limit): # async def top_authors(_, info, limit):
async with ShoutsCache.lock: # async with ShoutsCache.lock:
return ShoutsCache.top_authors[:limit] # return ShoutsCache.top_authors[:limit]
@mutation.field("createShout") @mutation.field("createShout")