diff --git a/main.py b/main.py index 8cf9a788..bf695d55 100644 --- a/main.py +++ b/main.py @@ -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() diff --git a/orm/shout.py b/orm/shout.py index 6ee56656..2a754765 100644 --- a/orm/shout.py +++ b/orm/shout.py @@ -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: diff --git a/resolvers/topics.py b/resolvers/topics.py index f551a1b0..0eaaa678 100644 --- a/resolvers/topics.py +++ b/resolvers/topics.py @@ -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): diff --git a/schema.graphql b/schema.graphql index 6d369201..68abf97e 100644 --- a/schema.graphql +++ b/schema.graphql @@ -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 {