need-remigration

This commit is contained in:
tonyrewin 2022-11-21 08:18:50 +03:00
parent 116739f8b5
commit a05ce719a7
3 changed files with 22 additions and 3 deletions

View File

@ -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)

View File

@ -482,7 +482,7 @@ type TopicStat {
shouts: Int!
followers: Int!
authors: Int!
# viewed: Int! too expensive
viewed: Int!
reacted: Int!
commented: Int
rating: Int

View File

@ -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():