minor fix top shouts

This commit is contained in:
knst-kotov 2021-11-04 14:26:50 +03:00
parent 01e71393f7
commit 5cca55d66c

View File

@ -72,7 +72,6 @@ class GitTask:
class ShoutsCache: class ShoutsCache:
limit = 50 limit = 50
period = 60*60 #1 hour period = 60*60 #1 hour
month_ago = datetime.now() - timedelta(days = 30)
lock = asyncio.Lock() lock = asyncio.Lock()
@staticmethod @staticmethod
@ -110,13 +109,11 @@ class ShoutsCache:
@staticmethod @staticmethod
async def prepare_top_month(): async def prepare_top_month():
# FIXME: test filter by month ago month_ago = datetime.now() - timedelta(days = 30)
# where(ShoutRating.ts > month_ago).\
with local_session() as session: with local_session() as session:
stmt = select(Shout, func.sum(ShoutRating.value).label("rating")).\ stmt = select(Shout, func.sum(ShoutRating.value).label("rating")).\
join(ShoutRating).\ join(ShoutRating).\
join(ShoutViewByDay).\ where(Shout.createdAt > month_ago).\
where(ShoutViewByDay.day > ShoutsCache.month_ago).\
group_by(Shout.id).\ group_by(Shout.id).\
order_by(desc("rating")).\ order_by(desc("rating")).\
limit(ShoutsCache.limit) limit(ShoutsCache.limit)
@ -131,28 +128,30 @@ class ShoutsCache:
@staticmethod @staticmethod
async def prepare_top_viewed(): async def prepare_top_viewed():
month_ago = datetime.now() - timedelta(days = 30)
with local_session() as session: with local_session() as session:
stmt = select(Shout, func.sum(ShoutViewByDay.value).label("view")).\ stmt = select(Shout, func.sum(ShoutViewByDay.value).label("views")).\
join(ShoutViewByDay).\ join(ShoutViewByDay).\
where(ShoutViewByDay.day > ShoutsCache.month_ago).\ where(ShoutViewByDay.day > month_ago).\
group_by(Shout.id).\ group_by(Shout.id).\
order_by(desc("view")).\ order_by(desc("views")).\
limit(ShoutsCache.limit) limit(ShoutsCache.limit)
shouts = [] shouts = []
for row in session.execute(stmt): for row in session.execute(stmt):
shout = row.Shout shout = row.Shout
shout.rating = await ShoutRatingStorage.get_rating(shout.id) shout.rating = await ShoutRatingStorage.get_rating(shout.id)
shout.view = row.view shout.views = row.views
shouts.append(shout) shouts.append(shout)
async with ShoutsCache.lock: async with ShoutsCache.lock:
ShoutsCache.top_viewed = shouts ShoutsCache.top_viewed = shouts
@staticmethod @staticmethod
async def prepare_top_authors(): async def prepare_top_authors():
month_ago = datetime.now() - timedelta(days = 30)
with local_session() as session: with local_session() as session:
shout_with_view = select(Shout.id, func.sum(ShoutViewByDay.value).label("view")).\ shout_with_view = select(Shout.id, func.sum(ShoutViewByDay.value).label("view")).\
join(ShoutViewByDay).\ join(ShoutViewByDay).\
where(ShoutViewByDay.day > ShoutsCache.month_ago).\ where(ShoutViewByDay.day > month_ago).\
group_by(Shout.id).\ group_by(Shout.id).\
order_by(desc("view")).cte() order_by(desc("view")).cte()
stmt = select(ShoutAuthor.user, func.sum(shout_with_view.c.view).label("view")).\ stmt = select(ShoutAuthor.user, func.sum(shout_with_view.c.view).label("view")).\