tops-and-recents-wip

This commit is contained in:
tonyrewin 2022-09-19 21:42:38 +03:00
parent a8313d1324
commit a67725a634
2 changed files with 53 additions and 42 deletions

View File

@ -232,11 +232,11 @@ type Query {
topViewed(offset: Int!, limit: Int!): [Shout]! topViewed(offset: Int!, limit: Int!): [Shout]!
# topReacted(offset: Int!, limit: Int!): [Shout]! # topReacted(offset: Int!, limit: Int!): [Shout]!
topAuthors(offset: Int!, limit: Int!): [Author]! topAuthors(offset: Int!, limit: Int!): [Author]!
topMonth(offset: Int!, limit: Int!): [Shout]! topMonth(offset: Int!, limit: Int!): [Shout]! # TODO: rename topRatedMonth
topOverall(offset: Int!, limit: Int!): [Shout]! topOverall(offset: Int!, limit: Int!): [Shout]! # TODO: rename topRated
topCommented(offset: Int!, limit: Int!): [Shout]! topCommented(offset: Int!, limit: Int!): [Shout]!
recentPublished(offset: Int!, limit: Int!): [Shout]! # homepage recentPublished(offset: Int!, limit: Int!): [Shout]! # homepage
recentReacted(offset: Int!, limit: Int!): [Shout]! recentReacted(offset: Int!, limit: Int!): [Shout]! # TODO: use in design!
recentCommented(offset: Int!, limit: Int!): [Shout]! recentCommented(offset: Int!, limit: Int!): [Shout]!
recentAll(offset: Int!, limit: Int!): [Shout]! recentAll(offset: Int!, limit: Int!): [Shout]!

View File

@ -1,7 +1,7 @@
import asyncio import asyncio
from datetime import datetime, timedelta from datetime import datetime, timedelta
from sqlalchemy import and_, desc, func, select from sqlalchemy import desc, func, select
from sqlalchemy.orm import selectinload from sqlalchemy.orm import selectinload
from base.orm import local_session from base.orm import local_session
@ -57,9 +57,7 @@ class ShoutsCache:
selectinload(Shout.topics) selectinload(Shout.topics)
) )
.where(bool(Shout.publishedAt)) .where(bool(Shout.publishedAt))
.filter(not bool(Shout.deletedAt)) .order_by(desc("publishedAt"))
.group_by(Shout.slug)
.order_by(desc(Shout.publishedAt))
.limit(ShoutsCache.limit) .limit(ShoutsCache.limit)
), ),
) )
@ -78,15 +76,13 @@ class ShoutsCache:
selectinload(Shout.authors), selectinload(Shout.authors),
selectinload(Shout.topics) selectinload(Shout.topics)
) )
.filter(not bool(Shout.deletedAt)) .order_by(desc("createdAt"))
.group_by(Shout.slug)
.order_by(desc(Shout.createdAt))
.limit(ShoutsCache.limit) .limit(ShoutsCache.limit)
), ),
) )
async with ShoutsCache.lock: async with ShoutsCache.lock:
ShoutsCache.recent_all = shouts ShoutsCache.recent_all = shouts[0:ShoutsCache.limit]
print("[zine.cache] %d recently created shouts " % len(shouts)) print("[zine.cache] %d recently created shouts " % len(ShoutsCache.recent_all))
@staticmethod @staticmethod
async def prepare_recent_reacted(): async def prepare_recent_reacted():
@ -98,15 +94,17 @@ class ShoutsCache:
shouts = await prepare_shouts( shouts = await prepare_shouts(
session, session,
( (
select(Shout) select(Shout, Reaction.createdAt.label('reactedAt'))
.options( .options(
selectinload(Shout.authors), selectinload(Shout.authors),
selectinload(Shout.topics), selectinload(Shout.topics),
selectinload(Shout.reactions),
) )
.where(Shout.slug.in_(list(reacted_slugs))) .join(Reaction)
.filter(not bool(Shout.deletedAt)) .where(Shout.slug.in_(reacted_slugs))
.group_by(Shout.slug) .filter(bool(Shout.publishedAt))
.order_by(Shout.publishedAt) .group_by(Shout.slug, "reactedAt")
.order_by(desc("reactedAt"))
.limit(ShoutsCache.limit) .limit(ShoutsCache.limit)
) )
) )
@ -125,15 +123,17 @@ class ShoutsCache:
shouts = await prepare_shouts( shouts = await prepare_shouts(
session, session,
( (
select(Shout) select(Shout, Reaction.createdAt.label('reactedAt'))
.options( .options(
selectinload(Shout.authors), selectinload(Shout.authors),
selectinload(Shout.topics), selectinload(Shout.topics),
selectinload(Shout.reactions),
) )
.where(Shout.slug.in_(list(commented_slugs))) .join(Reaction, Reaction.shout == Shout.slug)
.filter(not bool(Shout.deletedAt)) .where(Shout.slug.in_(commented_slugs))
.group_by(Shout.slug) .filter(bool(Shout.publishedAt))
.order_by(Shout.publishedAt) .group_by(Shout.slug, "reactedAt")
.order_by(desc("reactedAt"))
.limit(ShoutsCache.limit) .limit(ShoutsCache.limit)
) )
) )
@ -144,26 +144,24 @@ class ShoutsCache:
@staticmethod @staticmethod
async def prepare_top_overall(): async def prepare_top_overall():
with local_session() as session: with local_session() as session:
# with reacted times counter
shouts = await prepare_shouts( shouts = await prepare_shouts(
session, session,
( (
select(Shout, func.count(Reaction.id).label("reacted")) select(Shout)
.options( .options(
selectinload(Shout.authors), selectinload(Shout.authors),
selectinload(Shout.topics), selectinload(Shout.topics),
selectinload(Shout.reactions), selectinload(Shout.reactions),
) )
.join(Reaction) .join(Reaction)
.where(and_(bool(Shout.publishedAt), bool(Reaction.deletedAt))) .where(bool(Shout.publishedAt))
.group_by(Shout.slug) .group_by(Shout.slug)
.order_by(desc("reacted"))
.limit(ShoutsCache.limit) .limit(ShoutsCache.limit)
), ),
) )
shouts.sort(key=lambda s: s.stat["rating"], reverse=True) shouts.sort(key=lambda s: s.stat["rating"], reverse=True)
async with ShoutsCache.lock: async with ShoutsCache.lock:
print("[zine.cache] %d top shouts " % len(shouts)) print("[zine.cache] %d top rated published " % len(shouts))
ShoutsCache.top_overall = shouts ShoutsCache.top_overall = shouts
@staticmethod @staticmethod
@ -173,19 +171,22 @@ class ShoutsCache:
shouts = await prepare_shouts( shouts = await prepare_shouts(
session, session,
( (
select(Shout, func.count(Reaction.id).label("reacted")) select(Shout)
.options(selectinload(Shout.authors), selectinload(Shout.topics)) .options(
selectinload(Shout.authors),
selectinload(Shout.topics),
selectinload(Shout.reactions),
)
.join(Reaction) .join(Reaction)
.where(and_(Shout.createdAt > month_ago, bool(Reaction.deletedAt))) .where(Shout.publishedAt > month_ago)
.group_by(Shout.slug) .group_by(Shout.slug)
.order_by(desc("reacted"))
.limit(ShoutsCache.limit) .limit(ShoutsCache.limit)
), ),
) )
shouts.sort(key=lambda s: s.stat["rating"], reverse=True) shouts.sort(key=lambda s: s.stat["rating"], reverse=True)
async with ShoutsCache.lock: async with ShoutsCache.lock:
print("[zine.cache] %d top month shouts " % len(shouts))
ShoutsCache.top_month = shouts ShoutsCache.top_month = shouts
print("[zine.cache] %d top month published " % len(ShoutsCache.top_month))
@staticmethod @staticmethod
async def prepare_top_commented(): async def prepare_top_commented():
@ -194,10 +195,14 @@ class ShoutsCache:
shouts = await prepare_shouts( shouts = await prepare_shouts(
session, session,
( (
select(Shout, Reaction) select(Shout, func.sum(int(bool(Reaction.body))).label("commented"))
.options(selectinload(Shout.authors), selectinload(Shout.topics)) .options(
selectinload(Shout.authors),
selectinload(Shout.topics),
selectinload(Shout.reactions)
)
.join(Reaction) .join(Reaction)
.where(and_(Shout.createdAt > month_ago, bool(Reaction.deletedAt))) .filter(Shout.createdAt > month_ago)
.group_by(Shout.slug) .group_by(Shout.slug)
.order_by(desc("commented")) .order_by(desc("commented"))
.limit(ShoutsCache.limit) .limit(ShoutsCache.limit)
@ -205,8 +210,8 @@ class ShoutsCache:
) )
shouts.sort(key=lambda s: s.stat["commented"], reverse=True) shouts.sort(key=lambda s: s.stat["commented"], reverse=True)
async with ShoutsCache.lock: async with ShoutsCache.lock:
print("[zine.cache] %d top commented shouts " % len(shouts)) ShoutsCache.top_commented = shouts
ShoutsCache.top_viewed = shouts print("[zine.cache] %d last month top commented shouts " % len(ShoutsCache.top_commented))
@staticmethod @staticmethod
async def prepare_top_viewed(): async def prepare_top_viewed():
@ -216,17 +221,22 @@ class ShoutsCache:
session, session,
( (
select(Shout, func.sum(ViewedByDay.value).label("viewed")) select(Shout, func.sum(ViewedByDay.value).label("viewed"))
.options(selectinload(Shout.authors), selectinload(Shout.topics)) .options(
.join(ViewedByDay) selectinload(Shout.authors),
.where(and_(Shout.createdAt > month_ago, bool(Reaction.deletedAt))) selectinload(Shout.topics),
selectinload(Shout.reactions)
)
.join(Reaction)
.join(ViewedByDay, ViewedByDay.shout == Shout.slug)
.filter(Shout.createdAt > month_ago)
.group_by(Shout.slug) .group_by(Shout.slug)
.order_by(desc("viewed")) .order_by(desc("viewed"))
.limit(ShoutsCache.limit) .limit(ShoutsCache.limit)
), )
) )
shouts.sort(key=lambda s: s.stat["viewed"], reverse=True) shouts.sort(key=lambda s: s.stat["viewed"], reverse=True)
async with ShoutsCache.lock: async with ShoutsCache.lock:
print("[zine.cache] %d top viewed shouts " % len(shouts)) print("[zine.cache] %d last month top viewed shouts " % len(shouts))
ShoutsCache.top_viewed = shouts ShoutsCache.top_viewed = shouts
@staticmethod @staticmethod
@ -264,6 +274,7 @@ class ShoutsCache:
await ShoutsCache.prepare_top_month() await ShoutsCache.prepare_top_month()
await ShoutsCache.prepare_top_overall() await ShoutsCache.prepare_top_overall()
await ShoutsCache.prepare_top_viewed() await ShoutsCache.prepare_top_viewed()
await ShoutsCache.prepare_top_commented()
await ShoutsCache.prepare_recent_published() await ShoutsCache.prepare_recent_published()
await ShoutsCache.prepare_recent_all() await ShoutsCache.prepare_recent_all()