From 7ee8003e2d23cb97ad7589dfb54809411e99359a Mon Sep 17 00:00:00 2001 From: tonyrewin Date: Sat, 1 Oct 2022 14:14:44 +0300 Subject: [PATCH] fix-prepare --- resolvers/zine.py | 9 ++++---- services/zine/shoutscache.py | 42 +++++++++--------------------------- 2 files changed, 15 insertions(+), 36 deletions(-) diff --git a/resolvers/zine.py b/resolvers/zine.py index 38b5077c..06d986ff 100644 --- a/resolvers/zine.py +++ b/resolvers/zine.py @@ -110,10 +110,11 @@ async def get_search_results(_, _info, searchtext, offset, limit): .offset(offset) ) - for s in shouts: - for a in s.authors: - a.caption = await ShoutAuthorStorage.get_author_caption(s.slug, a.slug) - s.stat.relevance = 1 # FIXME: expecting search engine rated relevance + for s in shouts: + shout = s.dict() + for a in shout['authors']: + a.caption = await ShoutAuthorStorage.get_author_caption(s.slug, a.slug) + s.stat.relevance = 1 # FIXME: expecting search engine rated relevance return shouts diff --git a/services/zine/shoutscache.py b/services/zine/shoutscache.py index 90789ed3..c39823b1 100644 --- a/services/zine/shoutscache.py +++ b/services/zine/shoutscache.py @@ -6,7 +6,7 @@ from sqlalchemy.orm import selectinload from base.orm import local_session from orm.reaction import Reaction, ReactionKind -from orm.shout import Shout, ShoutAuthor, ShoutTopic +from orm.shout import Shout from services.stat.reacted import ReactedStorage @@ -63,6 +63,15 @@ class ShoutsCache: ), ) async with ShoutsCache.lock: + for s in shouts: + for a in s.authors: + ShoutsCache.by_author[a.slug] = ShoutsCache.by_author.get(a.slug, []) + ShoutsCache.by_author[a.slug].append(s) + for t in s.topics: + ShoutsCache.by_topic[t.slug] = ShoutsCache.by_topic.get(t.slug, []) + ShoutsCache.by_topic[t.slug].append(s) + print("[zine.cache] indexed by %d topics " % len(ShoutsCache.by_topic.keys())) + print("[zine.cache] indexed by %d authors " % len(ShoutsCache.by_author.keys())) ShoutsCache.recent_published = shouts print("[zine.cache] %d recently published shouts " % len(shouts)) @@ -230,20 +239,6 @@ class ShoutsCache: ShoutsCache.top_commented = shouts print("[zine.cache] %d last month top commented shouts " % len(ShoutsCache.top_commented)) - @staticmethod - async def prepare_by_author(): - shouts_by_author = {} - with local_session() as session: - for a in session.query(ShoutAuthor).all(): - shout = session.query(Shout).where(Shout.slug == a.shout).first() - shout.stat = await get_shout_stat(shout.slug) - shouts_by_author[a.user] = shouts_by_author.get(a.user, []) - if shout not in shouts_by_author[a.user]: - shouts_by_author[a.user].append(shout) - async with ShoutsCache.lock: - print("[zine.cache] indexed by %d authors " % len(shouts_by_author.keys())) - ShoutsCache.by_author = shouts_by_author - @staticmethod async def get_top_published_before(daysago, offset, limit): shouts_by_rating = [] @@ -254,20 +249,6 @@ class ShoutsCache: shouts_by_rating.sort(lambda s: s.stat["rating"], reverse=True) return shouts_by_rating - @staticmethod - async def prepare_by_topic(): - shouts_by_topic = {} - with local_session() as session: - for a in session.query(ShoutTopic).all(): - shout = session.query(Shout).where(Shout.slug == a.shout).first() - shout.stat = await get_shout_stat(shout.slug) - shouts_by_topic[a.topic] = shouts_by_topic.get(a.topic, []) - if shout not in shouts_by_topic[a.topic]: - shouts_by_topic[a.topic].append(shout) - async with ShoutsCache.lock: - print("[zine.cache] indexed by %d topics " % len(shouts_by_topic.keys())) - ShoutsCache.by_topic = shouts_by_topic - @staticmethod async def worker(): while True: @@ -280,9 +261,6 @@ class ShoutsCache: await ShoutsCache.prepare_recent_all() await ShoutsCache.prepare_recent_reacted() await ShoutsCache.prepare_recent_commented() - - await ShoutsCache.prepare_by_author() - await ShoutsCache.prepare_by_topic() print("[zine.cache] periodical update") except Exception as err: print("[zine.cache] error: %s" % (err))