top-month, top-overall

This commit is contained in:
Untone 2021-10-31 17:50:55 +03:00
parent c353e8d650
commit a327100eb7
2 changed files with 35 additions and 12 deletions

View File

@ -1,6 +1,6 @@
from resolvers.auth import login, sign_out, is_email_free, register, confirm from resolvers.auth import login, sign_out, is_email_free, register, confirm
from resolvers.inbox import create_message, delete_message, update_message, get_messages from resolvers.inbox import create_message, delete_message, update_message, get_messages
from resolvers.zine import create_shout, get_shout_by_slug, favorite_shouts, \ from resolvers.zine import create_shout, get_shout_by_slug, top_month, top_overall, \
recent_shouts, top_authors, top_shouts_by_rating, top_shouts_by_view recent_shouts, top_authors, top_shouts_by_rating, top_shouts_by_view
from resolvers.profile import get_user_by_slug, get_current_user from resolvers.profile import get_user_by_slug, get_current_user
from resolvers.topics import topic_subscribe, topic_unsubscribe, topics_by_author, \ from resolvers.topics import topic_subscribe, topic_unsubscribe, topics_by_author, \
@ -22,7 +22,8 @@ __all__ = [
"get_user_by_slug", "get_user_by_slug",
"get_shout_by_slug", "get_shout_by_slug",
"recent_shouts", "recent_shouts",
"favorite_shouts", "top_month",
"top_overall",
"top_shouts_by_views", "top_shouts_by_views",
"top_shouts_by_rating", "top_shouts_by_rating",
"topics_by_slugs", "topics_by_slugs",

View File

@ -72,12 +72,11 @@ class GitTask:
class TopShouts: class TopShouts:
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
async def prepare_shouts_by_rating(): async def prepare_shouts_by_rating():
month_ago = datetime.now() - timedelta(days = 30)
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).\
@ -111,7 +110,7 @@ class TopShouts:
@staticmethod @staticmethod
async def prepare_favorite_shouts(): async def prepare_top_overall():
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).\
@ -125,11 +124,29 @@ class TopShouts:
shout.views = await ShoutViewStorage.get_view(shout.id) shout.views = await ShoutViewStorage.get_view(shout.id)
shouts.append(shout) shouts.append(shout)
async with TopShouts.lock: async with TopShouts.lock:
TopShouts.favorite_shouts = shouts TopShouts.top_overall = shouts
@staticmethod
async def prepare_top_month():
# FIXME: filter by month ago
with local_session() as session:
stmt = select(Shout, func.sum(ShoutRating.value).label("rating")).\
join(ShoutRating).\
join(ShoutViewByDay).where(ShoutViewByDay.day > month_ago).\
group_by(Shout.id).\
order_by(desc("rating")).\
limit(TopShouts.limit)
shouts = []
for row in session.execute(stmt):
shout = row.Shout
shout.rating = row.rating
shout.views = await ShoutViewStorage.get_view(shout.id)
shouts.append(shout)
async with TopShouts.lock:
TopShouts.top_month = shouts
@staticmethod @staticmethod
async def prepare_shouts_by_view(): async def prepare_shouts_by_view():
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("view")).\
join(ShoutViewByDay).\ join(ShoutViewByDay).\
@ -148,7 +165,6 @@ class TopShouts:
@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).\
@ -175,7 +191,8 @@ class TopShouts:
while True: while True:
try: try:
print("shouts cache updating...") print("shouts cache updating...")
await TopShouts.prepare_favorite_shouts() await TopShouts.prepare_top_month()
await TopShouts.prepare_top_overall()
await TopShouts.prepare_recent_shouts() await TopShouts.prepare_recent_shouts()
await TopShouts.prepare_shouts_by_rating() await TopShouts.prepare_shouts_by_rating()
await TopShouts.prepare_shouts_by_view() await TopShouts.prepare_shouts_by_view()
@ -198,10 +215,15 @@ async def top_shouts_by_rating(_, info, limit):
return TopShouts.shouts_by_rating[:limit] return TopShouts.shouts_by_rating[:limit]
@query.field("favorites") @query.field("topMonth")
async def favorite_shouts(_, info, limit): async def top_month(_, info, limit):
async with TopShouts.lock: async with TopShouts.lock:
return TopShouts.favorite_shouts[:limit] return TopShouts.top_month[:limit]
@query.field("topOverall")
async def top_overall(_, info, limit):
async with TopShouts.lock:
return TopShouts.top_overall[:limit]
@query.field("recents") @query.field("recents")
async def recent_shouts(_, info, limit): async def recent_shouts(_, info, limit):