stats refactored

This commit is contained in:
2022-09-19 16:50:43 +03:00
parent dffdff2869
commit 4536370c79
24 changed files with 355 additions and 410 deletions

View File

@@ -6,16 +6,30 @@ from auth.authenticate import login_required
from base.orm import local_session
from base.resolvers import mutation, query
from orm.topic import Topic, TopicFollower
from services.stat.topicstat import TopicStat
from services.zine.shoutscache import ShoutsCache
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):
return {
"shouts": len(TopicStat.shouts_by_topic.get(slug, [])),
"authors": len(TopicStat.authors_by_topic.get(slug, [])),
"followers": len(TopicStat.followers_by_topic.get(slug, [])),
"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)
}
@query.field("topicsAll")
async def topics_all(_, _info):
topics = await TopicStorage.get_topics_all()
for topic in topics:
topic.stat = await TopicStat.get_stat(topic.slug)
topic.stat = await get_topic_stat(topic.slug)
return topics
@@ -23,18 +37,19 @@ async def topics_all(_, _info):
async def topics_by_community(_, info, community):
topics = await TopicStorage.get_topics_by_community(community)
for topic in topics:
topic.stat = await TopicStat.get_stat(topic.slug)
topic.stat = await get_topic_stat(topic.slug)
return topics
@query.field("topicsByAuthor")
async def topics_by_author(_, _info, author):
topics = ShoutsCache.by_author.get(author)
shouts = ShoutsCache.by_author.get(author)
author_topics = set()
for tpc in topics:
tpc = await TopicStorage.topics[tpc.slug]
tpc.stat = await TopicStat.get_stat(tpc.slug)
author_topics.add(tpc)
for s in shouts:
for tpc in s.topics:
tpc = await TopicStorage.topics[tpc.slug]
tpc.stat = await get_topic_stat(tpc.slug)
author_topics.add(tpc)
return list(author_topics)
@@ -91,11 +106,8 @@ async def topics_random(_, info, amount=12):
topics = await TopicStorage.get_topics_all()
normalized_topics = []
for topic in topics:
topic_stat = await TopicStat.get_stat(topic.slug)
# FIXME: expects topicstat fix
# #if topic_stat["shouts"] > 2:
# normalized_topics.append(topic)
topic.stat = topic_stat
normalized_topics.append(topic)
topic.stat = await get_topic_stat(topic.slug)
if topic.stat["shouts"] > 2:
normalized_topics.append(topic)
sample_length = min(len(normalized_topics), amount)
return random.sample(normalized_topics, sample_length)