add authors and subscriptions in Topic stat

This commit is contained in:
knst-kotov 2021-12-15 12:17:16 +03:00
parent 01a974e974
commit a98d5f6ee6
4 changed files with 28 additions and 17 deletions

View File

@ -32,8 +32,8 @@ async def start_up():
git_task = asyncio.create_task(GitTask.git_task_worker())
shouts_cache_task = asyncio.create_task(ShoutsCache.worker())
view_storage_task = asyncio.create_task(ShoutViewStorage.worker())
topic_stat_task = asyncio.create_task(TopicStat.worker())
shout_author_task = asyncio.create_task(ShoutAuthorStorage.worker())
topic_stat_task = asyncio.create_task(TopicStat.worker())
async def shutdown():
await redis.disconnect()

View File

@ -3,7 +3,7 @@ from datetime import datetime, timedelta
from sqlalchemy import Table, Column, Integer, String, ForeignKey, DateTime, Boolean
from sqlalchemy.orm import relationship
from sqlalchemy.orm.attributes import flag_modified
from orm import Permission, User, Topic
from orm import Permission, User, Topic, TopicSubscription
from orm.comment import Comment
from orm.base import Base, local_session
@ -148,6 +148,8 @@ class ShoutViewStorage:
class TopicStat:
shouts_by_topic = {}
authors_by_topic = {}
subs_by_topic = {}
lock = asyncio.Lock()
period = 30*60 #sec
@ -164,6 +166,21 @@ class TopicStat:
else:
self.shouts_by_topic[topic] = [shout]
authors = await ShoutAuthorStorage.get_authors(shout)
if topic in self.authors_by_topic:
self.authors_by_topic[topic].update(authors)
else:
self.authors_by_topic[topic] = set(authors)
subs = session.query(TopicSubscription)
for sub in subs:
topic = sub.topic
user = sub.user
if topic in self.subs_by_topic:
self.subs_by_topic[topic].append(user)
else:
self.subs_by_topic[topic] = [user]
async def get_shouts(topic):
self = TopicStat
async with self.lock:
@ -174,7 +191,13 @@ class TopicStat:
self = TopicStat
async with self.lock:
shouts = self.shouts_by_topic.get(topic, [])
stat = { "shouts" : len(shouts) }
subs = self.subs_by_topic.get(topic, [])
authors = self.authors_by_topic.get(topic, set())
stat = {
"shouts" : len(shouts),
"authors" : len(authors),
"subscriptions" : len(subs)
}
views = 0
for shout in shouts:

View File

@ -32,17 +32,6 @@ async def topics_by_author(_, info, author):
slugs.update([topic.slug for topic in shout.topics])
return await TopicStorage.get_topics(slugs)
@query.field("getTopicAuthors")
async def topics_by_author(_, info, slug, count, page):
shouts = await TopicStat.get_shouts(slug)
authors = set()
for shout in shouts:
authors.update(await ShoutAuthorStorage.get_authors(shout))
authors = list(authors)
authors.sort() #TODO sort by username
authors = authors[count * page : count * (page + 1) ]
return [await UserStorage.get_user(author) for author in authors]
@mutation.field("createTopic")
@login_required
async def create_topic(_, info, input):

View File

@ -170,7 +170,6 @@ type Query {
topicsBySlugs(slugs: [String]): [Topic]!
topicsByCommunity(community: String!): [Topic]!
topicsByAuthor(author: String!): [Topic]!
getTopicAuthors(slug: String!, count: Int!, page: Int!): [User]!
# getOnlineUsers: [User!]!
@ -335,8 +334,8 @@ type Community {
type TopicStat {
shouts: Int!
views: Int!
subscriptions: Int
authors: Int
subscriptions: Int!
authors: Int!
}
type Topic {