fix topics

This commit is contained in:
tonyrewin 2022-11-20 10:48:40 +03:00
parent 6d1a916556
commit fe100c8b07
5 changed files with 13 additions and 34 deletions

View File

@ -42,4 +42,4 @@ class Community(Base):
session.add(d) session.add(d)
session.commit() session.commit()
Community.default_community = d Community.default_community = d
print('[orm] created default community with id %s' % d.id) print('[orm] default community id: %s' % d.id)

View File

@ -9,7 +9,6 @@ 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):
@ -17,7 +16,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()),
"viewed": await ViewedStorage.get_topic(slug), # NOTICE: no viewed metric is needed here
"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! # viewed: Int! too expensive
reacted: Int! reacted: Int!
commented: Int commented: Int
rating: Int rating: Int

View File

@ -3,12 +3,14 @@ from services.auth.roles import RoleStorage
from services.auth.users import UserStorage from services.auth.users import UserStorage
from services.zine.topics import TopicStorage from services.zine.topics import TopicStorage
from services.search import SearchService from services.search import SearchService
from services.stat.viewed import ViewedStorage
from base.orm import local_session from base.orm import local_session
async def storages_init(): async def storages_init():
with local_session() as session: with local_session() as session:
print('[main] initialize storages') print('[main] initialize storages')
ViewedStorage.init()
ReactedStorage.init(session) ReactedStorage.init(session)
RoleStorage.init(session) RoleStorage.init(session)
UserStorage.init(session) UserStorage.init(session)

View File

@ -5,8 +5,6 @@ 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.viewed import ViewedEntry from orm.viewed import ViewedEntry
from orm.shout import ShoutTopic
from services.zine.topics import TopicStorage
from ssl import create_default_context from ssl import create_default_context
@ -43,21 +41,23 @@ ssl = create_default_context()
class ViewedStorage: class ViewedStorage:
lock = asyncio.Lock() lock = asyncio.Lock()
by_topics = {}
by_shouts = {} by_shouts = {}
period = 5 * 60 # 5 minutes period = 5 * 60 # 5 minutes
client = None client = None
transport = 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 @staticmethod
async def update_views(session): async def update_views(session):
# TODO: when the struture of payload will be transparent # TODO: when the struture of payload will be transparent
# TODO: perhaps ackee token getting here # TODO: perhaps ackee token getting here
self = ViewedStorage() self = ViewedStorage
async with self.lock: 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) domains = await self.client.execute_async(query_ackee_views)
print("[stat.ackee] loaded domains") print("[stat.ackee] loaded domains")
print(domains) print(domains)
@ -80,23 +80,6 @@ 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 {}
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 @staticmethod
async def increment(shout_slug, amount=1, viewer='anonymous'): async def increment(shout_slug, amount=1, viewer='anonymous'):
self = ViewedStorage self = ViewedStorage
@ -110,11 +93,6 @@ 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
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 @staticmethod
async def worker(): async def worker():
@ -123,7 +101,7 @@ class ViewedStorage:
try: try:
with local_session() as session: with local_session() as session:
await self.update_views(session) await self.update_views(session)
print("[stat.viewed] next renew in %d minutes" % (self.period / 60))
except Exception as err: except Exception as err:
print("[stat.viewed] : %s" % (err)) print("[stat.viewed] %s" % (err))
print("[stat.viewed] renew period: %d minutes" % (self.period / 60))
await asyncio.sleep(self.period) await asyncio.sleep(self.period)