From a05ce719a7a067569268f1b4ec29426d265f874a Mon Sep 17 00:00:00 2001 From: tonyrewin Date: Mon, 21 Nov 2022 08:18:50 +0300 Subject: [PATCH] need-remigration --- resolvers/topics.py | 3 ++- schema.graphql | 2 +- services/stat/viewed.py | 20 +++++++++++++++++++- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/resolvers/topics.py b/resolvers/topics.py index cd908325..399953a7 100644 --- a/resolvers/topics.py +++ b/resolvers/topics.py @@ -9,6 +9,7 @@ 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): @@ -16,7 +17,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()), - # NOTICE: no viewed metric is needed here + "viewed": await ViewedStorage.get_topic(slug), "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 bddb936e..1f89ecdc 100644 --- a/schema.graphql +++ b/schema.graphql @@ -482,7 +482,7 @@ type TopicStat { shouts: Int! followers: Int! authors: Int! - # viewed: Int! too expensive + viewed: Int! reacted: Int! commented: Int rating: Int diff --git a/services/stat/viewed.py b/services/stat/viewed.py index 72fa8902..fc984f5b 100644 --- a/services/stat/viewed.py +++ b/services/stat/viewed.py @@ -4,6 +4,7 @@ from gql import Client, gql from gql.transport.aiohttp import AIOHTTPTransport from base.orm import local_session from sqlalchemy import func, select +from orm.shout import ShoutTopic from orm.viewed import ViewedEntry from ssl import create_default_context @@ -42,6 +43,7 @@ ssl = create_default_context() class ViewedStorage: lock = asyncio.Lock() by_shouts = {} + by_topics = {} period = 5 * 60 # 5 minutes client = None transport = None @@ -68,7 +70,7 @@ class ViewedStorage: self = ViewedStorage async with self.lock: r = self.by_shouts.get(shout_slug) - if r: + if not r: with local_session() as session: shout_views = 0 shout_views_q = select(func.sum(ViewedEntry.amount)).where( @@ -80,6 +82,16 @@ 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 {} + 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 @@ -93,6 +105,12 @@ class ViewedStorage: session.add(viewed) session.commit() self.by_shouts[shout_slug] = self.by_shouts.get(shout_slug, 0) + amount + topics = session.query(ShoutTopic).where(ShoutTopic.shout == shout_slug).all() + for t in topics: + tpc = t.topic + if not self.by_topics.get(tpc): + self.by_topics[tpc] = {} + self.by_topics[tpc][shout_slug] = self.by_shouts[shout_slug] @staticmethod async def worker():