limit-offset-patch
This commit is contained in:
@@ -8,11 +8,10 @@ from orm.shout import Shout, ShoutAuthor, ShoutTopic
|
||||
from services.stat.viewed import ViewedByDay
|
||||
|
||||
|
||||
def shoutify(session, stmt):
|
||||
async def prepare_shouts(session, stmt):
|
||||
shouts = []
|
||||
for s in list(map(lambda r: r.Shout, session.execute(stmt))):
|
||||
if not bool(s.stat):
|
||||
raise Exception
|
||||
s.stats = await s.stat
|
||||
shouts.append(s)
|
||||
return shouts
|
||||
|
||||
@@ -35,7 +34,7 @@ class ShoutsCache:
|
||||
@staticmethod
|
||||
async def prepare_recent_published():
|
||||
with local_session() as session:
|
||||
shouts = shoutify(session, (
|
||||
shouts = await prepare_shouts(session, (
|
||||
select(Shout)
|
||||
.options(selectinload(Shout.authors), selectinload(Shout.topics))
|
||||
.where(bool(Shout.publishedAt))
|
||||
@@ -49,7 +48,7 @@ class ShoutsCache:
|
||||
@staticmethod
|
||||
async def prepare_recent_all():
|
||||
with local_session() as session:
|
||||
shouts = shoutify(session, (
|
||||
shouts = await prepare_shouts(session, (
|
||||
select(Shout)
|
||||
.options(selectinload(Shout.authors), selectinload(Shout.topics))
|
||||
.order_by(desc("createdAt"))
|
||||
@@ -62,7 +61,7 @@ class ShoutsCache:
|
||||
@staticmethod
|
||||
async def prepare_recent_reacted():
|
||||
with local_session() as session:
|
||||
shouts = shoutify(session, (
|
||||
shouts = await prepare_shouts(session, (
|
||||
select(Shout, func.max(Reaction.createdAt).label("reactionCreatedAt"))
|
||||
.options(
|
||||
selectinload(Shout.authors),
|
||||
@@ -82,7 +81,7 @@ class ShoutsCache:
|
||||
async def prepare_top_overall():
|
||||
with local_session() as session:
|
||||
# with reacted times counter
|
||||
shouts = shoutify(session, (
|
||||
shouts = await prepare_shouts(session, (
|
||||
select(Shout, func.count(Reaction.id).label("reacted"))
|
||||
.options(
|
||||
selectinload(Shout.authors),
|
||||
@@ -95,7 +94,7 @@ class ShoutsCache:
|
||||
.order_by(desc("reacted"))
|
||||
.limit(ShoutsCache.limit)
|
||||
))
|
||||
shouts.sort(key=lambda s: s.stat['rating'], reverse=True)
|
||||
shouts.sort(key=lambda s: s.stats['rating'], reverse=True)
|
||||
async with ShoutsCache.lock:
|
||||
print("[zine.cache] %d top shouts " % len(shouts))
|
||||
ShoutsCache.top_overall = shouts
|
||||
@@ -104,7 +103,7 @@ class ShoutsCache:
|
||||
async def prepare_top_month():
|
||||
month_ago = datetime.now() - timedelta(days=30)
|
||||
with local_session() as session:
|
||||
shouts = shoutify(session, (
|
||||
shouts = await prepare_shouts(session, (
|
||||
select(Shout, func.count(Reaction.id).label("reacted"))
|
||||
.options(selectinload(Shout.authors), selectinload(Shout.topics))
|
||||
.join(Reaction)
|
||||
@@ -113,7 +112,7 @@ class ShoutsCache:
|
||||
.order_by(desc("reacted"))
|
||||
.limit(ShoutsCache.limit)
|
||||
))
|
||||
shouts.sort(key=lambda s: s.stat['rating'], reverse=True)
|
||||
shouts.sort(key=lambda s: s.stats['rating'], reverse=True)
|
||||
async with ShoutsCache.lock:
|
||||
print("[zine.cache] %d top month shouts " % len(shouts))
|
||||
ShoutsCache.top_month = shouts
|
||||
@@ -122,7 +121,7 @@ class ShoutsCache:
|
||||
async def prepare_top_viewed():
|
||||
month_ago = datetime.now() - timedelta(days=30)
|
||||
with local_session() as session:
|
||||
shouts = shoutify(session, (
|
||||
shouts = await prepare_shouts(session, (
|
||||
select(Shout, func.sum(ViewedByDay.value).label("viewed"))
|
||||
.options(selectinload(Shout.authors), selectinload(Shout.topics))
|
||||
.join(ViewedByDay)
|
||||
@@ -131,7 +130,7 @@ class ShoutsCache:
|
||||
.order_by(desc("viewed"))
|
||||
.limit(ShoutsCache.limit)
|
||||
))
|
||||
shouts.sort(key=lambda s: s.stat['viewed'], reverse=True)
|
||||
shouts.sort(key=lambda s: s.stats['viewed'], reverse=True)
|
||||
async with ShoutsCache.lock:
|
||||
print("[zine.cache] %d top viewed shouts " % len(shouts))
|
||||
ShoutsCache.top_viewed = shouts
|
||||
|
Reference in New Issue
Block a user