uniquified topicstats

This commit is contained in:
tonyrewin 2022-10-04 00:45:26 +03:00
parent 8ba5552943
commit 5af72afe40
2 changed files with 15 additions and 15 deletions

View File

@ -15,9 +15,9 @@ from services.stat.viewed import ViewedStorage
async def get_topic_stat(slug): async def get_topic_stat(slug):
return { return {
"shouts": len(TopicStat.shouts_by_topic.get(slug, [])), "shouts": len(TopicStat.shouts_by_topic.get(slug, {}).keys()),
"authors": len(TopicStat.authors_by_topic.get(slug, [])), "authors": len(TopicStat.authors_by_topic.get(slug, {}).keys()),
"followers": len(TopicStat.followers_by_topic.get(slug, [])), "followers": len(TopicStat.followers_by_topic.get(slug, {}).keys()),
"viewed": await ViewedStorage.get_topic(slug), "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)),

View File

@ -7,9 +7,11 @@ from services.zine.shoutauthor import ShoutAuthorStorage
class TopicStat: class TopicStat:
# by slugs
shouts_by_topic = {} # Shout object stored shouts_by_topic = {} # Shout object stored
authors_by_topic = {} # User authors_by_topic = {} # User
followers_by_topic = {} # User followers_by_topic = {} # User
#
lock = asyncio.Lock() lock = asyncio.Lock()
period = 30 * 60 # sec period = 30 * 60 # sec
@ -22,16 +24,15 @@ class TopicStat:
tpc = shout_topic.topic tpc = shout_topic.topic
# shouts by topics # shouts by topics
shout = session.query(Shout).where(Shout.slug == shout_topic.shout).first() shout = session.query(Shout).where(Shout.slug == shout_topic.shout).first()
self.shouts_by_topic[tpc] = self.shouts_by_topic.get(tpc, []) self.shouts_by_topic[tpc] = self.shouts_by_topic.get(tpc, dict())
if shout not in self.shouts_by_topic[tpc]: self.shouts_by_topic[tpc][shout.slug] = shout
self.shouts_by_topic[tpc].append(shout)
# authors by topics # authors by topics
authors = await ShoutAuthorStorage.get_authors(shout.slug) authors = await ShoutAuthorStorage.get_authors(shout.slug)
self.authors_by_topic[tpc] = self.authors_by_topic.get(tpc, []) self.authors_by_topic[tpc] = self.authors_by_topic.get(tpc, dict())
for a in authors: for a in authors:
if a not in self.authors_by_topic[tpc]: [aslug, acaption] = a
self.authors_by_topic[tpc].append(a) self.authors_by_topic[tpc][aslug] = acaption
print("[stat.topics] shouts indexed by %d topics" % len(self.shouts_by_topic.keys())) print("[stat.topics] shouts indexed by %d topics" % len(self.shouts_by_topic.keys()))
print("[stat.topics] authors indexed by %d topics" % len(self.authors_by_topic.keys())) print("[stat.topics] authors indexed by %d topics" % len(self.authors_by_topic.keys()))
@ -40,17 +41,16 @@ class TopicStat:
followings = session.query(TopicFollower).all() followings = session.query(TopicFollower).all()
for flw in followings: for flw in followings:
topic = flw.topic topic = flw.topic
user = flw.follower userslug = flw.follower
self.followers_by_topic[topic] = self.followers_by_topic.get(topic, []) self.followers_by_topic[topic] = self.followers_by_topic.get(topic, dict())
if user not in self.followers_by_topic[topic]: self.followers_by_topic[topic][userslug] = userslug
self.followers_by_topic[topic].append(user) print("[stat.topics] followers indexed by %d topics" % len(self.followers_by_topic.keys()))
print("[stat.topics] followers sorted")
@staticmethod @staticmethod
async def get_shouts(topic): async def get_shouts(topic):
self = TopicStat self = TopicStat
async with self.lock: async with self.lock:
return self.shouts_by_topic.get(topic, []) return self.shouts_by_topic.get(topic, dict())
@staticmethod @staticmethod
async def worker(): async def worker():