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.zine.topics import TopicStorage
from services.stat.reacted import ReactedStorage from services.stat.reacted import ReactedStorage
from services.stat.topicstat import TopicStat from services.stat.topicstat import TopicStat
from services.stat.viewed import ViewedStorage
async def get_topic_stat(slug): 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()), "shouts": len(TopicStat.shouts_by_topic.get(slug, {}).keys()),
"authors": len(TopicStat.authors_by_topic.get(slug, {}).keys()), "authors": len(TopicStat.authors_by_topic.get(slug, {}).keys()),
"followers": len(TopicStat.followers_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)), "reacted": len(await ReactedStorage.get_topic(slug)),
"commented": len(await ReactedStorage.get_topic_comments(slug)), "commented": len(await ReactedStorage.get_topic_comments(slug)),
"rating": await ReactedStorage.get_topic_rating(slug) "rating": await ReactedStorage.get_topic_rating(slug)

View File

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

View File

@ -4,6 +4,7 @@ from gql import Client, gql
from gql.transport.aiohttp import AIOHTTPTransport from gql.transport.aiohttp import AIOHTTPTransport
from base.orm import local_session from base.orm import local_session
from sqlalchemy import func, select from sqlalchemy import func, select
from orm.shout import ShoutTopic
from orm.viewed import ViewedEntry from orm.viewed import ViewedEntry
from ssl import create_default_context from ssl import create_default_context
@ -42,6 +43,7 @@ ssl = create_default_context()
class ViewedStorage: class ViewedStorage:
lock = asyncio.Lock() lock = asyncio.Lock()
by_shouts = {} by_shouts = {}
by_topics = {}
period = 5 * 60 # 5 minutes period = 5 * 60 # 5 minutes
client = None client = None
transport = None transport = None
@ -68,7 +70,7 @@ class ViewedStorage:
self = ViewedStorage self = ViewedStorage
async with self.lock: async with self.lock:
r = self.by_shouts.get(shout_slug) r = self.by_shouts.get(shout_slug)
if r: if not r:
with local_session() as session: with local_session() as session:
shout_views = 0 shout_views = 0
shout_views_q = select(func.sum(ViewedEntry.amount)).where( shout_views_q = select(func.sum(ViewedEntry.amount)).where(
@ -80,6 +82,16 @@ class ViewedStorage:
else: else:
return r 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 @staticmethod
async def increment(shout_slug, amount=1, viewer='anonymous'): async def increment(shout_slug, amount=1, viewer='anonymous'):
self = ViewedStorage self = ViewedStorage
@ -93,6 +105,12 @@ class ViewedStorage:
session.add(viewed) session.add(viewed)
session.commit() session.commit()
self.by_shouts[shout_slug] = self.by_shouts.get(shout_slug, 0) + amount 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 @staticmethod
async def worker(): async def worker():