diff --git a/orm/community.py b/orm/community.py index 63f104c6..d789d0e3 100644 --- a/orm/community.py +++ b/orm/community.py @@ -42,4 +42,4 @@ class Community(Base): session.add(d) session.commit() Community.default_community = d - print('[orm] created default community with id %s' % d.id) + print('[orm] default community id: %s' % d.id) diff --git a/resolvers/topics.py b/resolvers/topics.py index 399953a7..cd908325 100644 --- a/resolvers/topics.py +++ b/resolvers/topics.py @@ -9,7 +9,6 @@ from orm.topic import Topic, TopicFollower from services.zine.topics import TopicStorage from services.stat.reacted import ReactedStorage from services.stat.topicstat import TopicStat -from services.stat.viewed import ViewedStorage async def get_topic_stat(slug): @@ -17,7 +16,7 @@ async def get_topic_stat(slug): "shouts": len(TopicStat.shouts_by_topic.get(slug, {}).keys()), "authors": len(TopicStat.authors_by_topic.get(slug, {}).keys()), "followers": len(TopicStat.followers_by_topic.get(slug, {}).keys()), - "viewed": await ViewedStorage.get_topic(slug), + # NOTICE: no viewed metric is needed here "reacted": len(await ReactedStorage.get_topic(slug)), "commented": len(await ReactedStorage.get_topic_comments(slug)), "rating": await ReactedStorage.get_topic_rating(slug) diff --git a/schema.graphql b/schema.graphql index 1f89ecdc..bddb936e 100644 --- a/schema.graphql +++ b/schema.graphql @@ -482,7 +482,7 @@ type TopicStat { shouts: Int! followers: Int! authors: Int! - viewed: Int! + # viewed: Int! too expensive reacted: Int! commented: Int rating: Int diff --git a/services/main.py b/services/main.py index bcd886f0..ad7cbde5 100644 --- a/services/main.py +++ b/services/main.py @@ -3,12 +3,14 @@ from services.auth.roles import RoleStorage from services.auth.users import UserStorage from services.zine.topics import TopicStorage from services.search import SearchService +from services.stat.viewed import ViewedStorage from base.orm import local_session async def storages_init(): with local_session() as session: print('[main] initialize storages') + ViewedStorage.init() ReactedStorage.init(session) RoleStorage.init(session) UserStorage.init(session) diff --git a/services/stat/viewed.py b/services/stat/viewed.py index 7b11f377..72fa8902 100644 --- a/services/stat/viewed.py +++ b/services/stat/viewed.py @@ -5,8 +5,6 @@ from gql.transport.aiohttp import AIOHTTPTransport from base.orm import local_session from sqlalchemy import func, select from orm.viewed import ViewedEntry -from orm.shout import ShoutTopic -from services.zine.topics import TopicStorage from ssl import create_default_context @@ -43,21 +41,23 @@ ssl = create_default_context() class ViewedStorage: lock = asyncio.Lock() - by_topics = {} by_shouts = {} period = 5 * 60 # 5 minutes client = None transport = None + @staticmethod + def init(): + ViewedStorage.transport = AIOHTTPTransport(url="https://ackee.discours.io/", ssl=ssl) + ViewedStorage.client = Client(transport=ViewedStorage.transport, fetch_schema_from_transport=True) + @staticmethod async def update_views(session): # TODO: when the struture of payload will be transparent # TODO: perhaps ackee token getting here - self = ViewedStorage() + self = ViewedStorage async with self.lock: - self.transport = AIOHTTPTransport(url="https://ackee.discours.io/", ssl=ssl) - self.client = Client(transport=self.transport, fetch_schema_from_transport=True) domains = await self.client.execute_async(query_ackee_views) print("[stat.ackee] loaded domains") print(domains) @@ -80,23 +80,6 @@ class ViewedStorage: else: return r - @staticmethod - async def get_topic(topic_slug): - self = ViewedStorage - topic_views = 0 - async with self.lock: - topic_views_by_shouts = self.by_topics.get(topic_slug) or {} - if len(topic_views_by_shouts.keys()) == 0: - with local_session() as session: - shoutslugs = session.query(ShoutTopic.shout).where(ShoutTopic.topic == topic_slug).all() - self.by_topics[topic_slug] = {} - for slug in shoutslugs: - self.by_topics[topic_slug][slug] = await self.get_shout(slug) - topic_views_by_shouts = self.by_topics.get(topic_slug) or {} - for shout in topic_views_by_shouts: - topic_views += shout - return topic_views - @staticmethod async def increment(shout_slug, amount=1, viewer='anonymous'): self = ViewedStorage @@ -110,11 +93,6 @@ class ViewedStorage: session.add(viewed) session.commit() self.by_shouts[shout_slug] = self.by_shouts.get(shout_slug, 0) + amount - shout_topics = await TopicStorage.get_topics_by_slugs([shout_slug, ]) - for t in shout_topics: - self.by_topics[t] = self.by_topics.get(t) or {} - self.by_topics[t][shout_slug] = self.by_topics[t].get(shout_slug) or 0 - self.by_topics[t][shout_slug] += amount @staticmethod async def worker(): @@ -123,7 +101,7 @@ class ViewedStorage: try: with local_session() as session: await self.update_views(session) + print("[stat.viewed] next renew in %d minutes" % (self.period / 60)) except Exception as err: - print("[stat.viewed] : %s" % (err)) - print("[stat.viewed] renew period: %d minutes" % (self.period / 60)) + print("[stat.viewed] %s" % (err)) await asyncio.sleep(self.period)