fix-prepare

This commit is contained in:
tonyrewin 2022-10-01 14:14:44 +03:00
parent 420d6c1f2d
commit 7ee8003e2d
2 changed files with 15 additions and 36 deletions

View File

@ -110,10 +110,11 @@ async def get_search_results(_, _info, searchtext, offset, limit):
.offset(offset) .offset(offset)
) )
for s in shouts: for s in shouts:
for a in s.authors: shout = s.dict()
a.caption = await ShoutAuthorStorage.get_author_caption(s.slug, a.slug) for a in shout['authors']:
s.stat.relevance = 1 # FIXME: expecting search engine rated relevance a.caption = await ShoutAuthorStorage.get_author_caption(s.slug, a.slug)
s.stat.relevance = 1 # FIXME: expecting search engine rated relevance
return shouts return shouts

View File

@ -6,7 +6,7 @@ from sqlalchemy.orm import selectinload
from base.orm import local_session from base.orm import local_session
from orm.reaction import Reaction, ReactionKind from orm.reaction import Reaction, ReactionKind
from orm.shout import Shout, ShoutAuthor, ShoutTopic from orm.shout import Shout
from services.stat.reacted import ReactedStorage from services.stat.reacted import ReactedStorage
@ -63,6 +63,15 @@ class ShoutsCache:
), ),
) )
async with ShoutsCache.lock: 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 ShoutsCache.recent_published = shouts
print("[zine.cache] %d recently published shouts " % len(shouts)) print("[zine.cache] %d recently published shouts " % len(shouts))
@ -230,20 +239,6 @@ class ShoutsCache:
ShoutsCache.top_commented = shouts ShoutsCache.top_commented = shouts
print("[zine.cache] %d last month top commented shouts " % len(ShoutsCache.top_commented)) 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 @staticmethod
async def get_top_published_before(daysago, offset, limit): async def get_top_published_before(daysago, offset, limit):
shouts_by_rating = [] shouts_by_rating = []
@ -254,20 +249,6 @@ class ShoutsCache:
shouts_by_rating.sort(lambda s: s.stat["rating"], reverse=True) shouts_by_rating.sort(lambda s: s.stat["rating"], reverse=True)
return shouts_by_rating 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 @staticmethod
async def worker(): async def worker():
while True: while True:
@ -280,9 +261,6 @@ class ShoutsCache:
await ShoutsCache.prepare_recent_all() await ShoutsCache.prepare_recent_all()
await ShoutsCache.prepare_recent_reacted() await ShoutsCache.prepare_recent_reacted()
await ShoutsCache.prepare_recent_commented() await ShoutsCache.prepare_recent_commented()
await ShoutsCache.prepare_by_author()
await ShoutsCache.prepare_by_topic()
print("[zine.cache] periodical update") print("[zine.cache] periodical update")
except Exception as err: except Exception as err:
print("[zine.cache] error: %s" % (err)) print("[zine.cache] error: %s" % (err))