From c292d7da559cbdd0926a79fb20f52adb9fb0f2e4 Mon Sep 17 00:00:00 2001 From: tonyrewin Date: Tue, 20 Sep 2022 16:32:04 +0300 Subject: [PATCH] topPublished --- resolvers/zine.py | 6 ++++++ schema.graphql | 7 ++++--- services/zine/shoutscache.py | 9 +++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/resolvers/zine.py b/resolvers/zine.py index fb51b63f..0ea191d7 100644 --- a/resolvers/zine.py +++ b/resolvers/zine.py @@ -29,6 +29,12 @@ async def top_month(_, _info, offset, limit): return ShoutsCache.top_month[offset : offset + limit] +@query.field("topPublished") +async def top_published(_, _info, daysago, offset, limit): + async with ShoutsCache.lock: + return ShoutsCache.get_top_published_before(daysago, offset, limit) + + @query.field("topCommented") async def top_commented(_, _info, offset, limit): async with ShoutsCache.lock: diff --git a/schema.graphql b/schema.graphql index 24c84530..290de67d 100644 --- a/schema.graphql +++ b/schema.graphql @@ -230,9 +230,10 @@ type Query { shoutsByCommunities(slugs: [String]!, offset: Int!, limit: Int!): [Shout]! myCandidates(offset: Int!, limit: Int!): [Shout]! # test # topReacted(offset: Int!, limit: Int!): [Shout]! - topAuthors(offset: Int!, limit: Int!): [Author]! - topMonth(offset: Int!, limit: Int!): [Shout]! # TODO: rename topRatedMonth - topOverall(offset: Int!, limit: Int!): [Shout]! # TODO: rename topRated + topAuthors(offset: Int!, limit: Int!): [Author]! # by User.rating + topPublished(daysago: Int!, offset: Int!, limit: Int!): [Shout]! + topMonth(offset: Int!, limit: Int!): [Shout]! # TODO: implement topPublishedAfter(day, offset, limit) + topOverall(offset: Int!, limit: Int!): [Shout]! topCommented(offset: Int!, limit: Int!): [Shout]! recentPublished(offset: Int!, limit: Int!): [Shout]! # homepage recentReacted(offset: Int!, limit: Int!): [Shout]! # TODO: use in design! diff --git a/services/zine/shoutscache.py b/services/zine/shoutscache.py index bf5bad11..5594a9df 100644 --- a/services/zine/shoutscache.py +++ b/services/zine/shoutscache.py @@ -248,6 +248,15 @@ class ShoutsCache: print("[zine.cache] indexed by %d authors " % len(shouts_by_author.keys())) ShoutsCache.by_author = shouts_by_author + @staticmethod + async def get_top_published_before(daysago, offset, limit): + shouts_by_rating = [] + before = datetime.now() - timedelta(days=daysago) + for s in ShoutsCache.recent_published: + if s.publishedAt >= before: + shouts_by_rating.append(s) + return shouts_by_rating + @staticmethod async def prepare_by_topic(): shouts_by_topic = {}