From 96c973b70f0d23b97605c3b958bc386db20c74e8 Mon Sep 17 00:00:00 2001 From: tonyrewin Date: Sat, 1 Oct 2022 11:34:15 +0300 Subject: [PATCH] shout-topic-storage, work load fix wip --- resolvers/zine.py | 2 ++ schema.graphql | 2 +- services/zine/shouttopic.py | 38 +++++++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 services/zine/shouttopic.py diff --git a/resolvers/zine.py b/resolvers/zine.py index 09a09110..b77e2204 100644 --- a/resolvers/zine.py +++ b/resolvers/zine.py @@ -119,6 +119,7 @@ async def get_search_results(_, _info, searchtext, offset, limit): @query.field("shoutsByTopics") async def shouts_by_topics(_, _info, slugs, offset, limit): + # TODO: use ShoutTopicsStorage with local_session() as session: shouts = ( session.query(Shout) @@ -154,6 +155,7 @@ async def shouts_by_collection(_, _info, collection, offset, limit): @query.field("shoutsByAuthors") async def shouts_by_authors(_, _info, slugs, offset, limit): + # TODO: use ShoutAuthorsStorage with local_session() as session: shouts = ( session.query(Shout) diff --git a/schema.graphql b/schema.graphql index 6c2d8c51..7f6c1642 100644 --- a/schema.graphql +++ b/schema.graphql @@ -225,7 +225,7 @@ type Query { # shouts getShoutBySlug(slug: String!): Shout! shoutsForFeed(offset: Int!, limit: Int!): [Shout]! # test - shoutsByTopics(slugs: [String]!, offset: Int!, limit: Int!): [Shout]! + shoutsByTopics(slugs: [String]!, offset: Int!, limit: Int!): [Shout]! # TODO: work load fix shoutsByAuthors(slugs: [String]!, offset: Int!, limit: Int!): [Shout]! shoutsByCommunities(slugs: [String]!, offset: Int!, limit: Int!): [Shout]! # topReacted(offset: Int!, limit: Int!): [Shout]! diff --git a/services/zine/shouttopic.py b/services/zine/shouttopic.py new file mode 100644 index 00000000..b98e4242 --- /dev/null +++ b/services/zine/shouttopic.py @@ -0,0 +1,38 @@ +import asyncio + +from base.orm import local_session +from orm.shout import ShoutTopic + + +class ShoutTopicStorage: + topics_by_shout = {} + lock = asyncio.Lock() + period = 30 * 60 # sec + + @staticmethod + async def load(session): + self = ShoutTopicStorage + sas = session.query(ShoutTopic).all() + for sa in sas: + self.topics_by_shout[sa.shout] = self.topics_by_shout.get(sa.shout, []) + self.topics_by_shout[sa.shout].append([sa.user, sa.caption]) + print("[zine.topics] %d shouts preprocessed" % len(self.topics_by_shout)) + + @staticmethod + async def get_topics(shout): + self = ShoutTopicStorage + async with self.lock: + return self.topics_by_shout.get(shout, []) + + @staticmethod + async def worker(): + self = ShoutTopicStorage + while True: + try: + with local_session() as session: + async with self.lock: + await self.load(session) + print("[zine.topics] state updated") + except Exception as err: + print("[zine.topics] errror: %s" % (err)) + await asyncio.sleep(self.period)